#include "mbed.h" //Incluse the mbed header file which contains the prototypes of the libraries used here (DigitalOut, Ticker) as well as various syntax definitions. void segout(); //Function prototype - Must have either whole function or function void attimeout(); //prototype declared above main function to avoid errors. DigitalOut MUX(P1_25); //Make the pin that controls which seven segment display is ON into an output pin. //Create an instance for each segment piece to be a different output pin. DigitalOut LEDA(P1_23); //Seg A as output. DigitalOut LEDB(P1_28); //Seg B as output. DigitalOut LEDC(P0_16); //Seg C as output. DigitalOut LEDD(P1_31); //Seg D as output. DigitalOut LEDE(P1_13); //Seg E as output. DigitalOut LEDF(P1_16); //Seg F as output. DigitalOut LEDG(P1_19); //Seg G as output. DigitalOut LEDDP(P0_23); //Seg Decimal Point as output. Ticker timeout; //Create an instance of class Ticker called timeout. //Declare global variables int num = 0; //Declare the counter number as 0. int tens; //Declare variable to hold the tens digit. int units; //Declare variable to hold the units digit. //Array of variables which correspond to the bit output required by the seven seg to display a number. //i.e. a call to this array with a digit 0-9 will return the 7 bit data that will show that number on the segments. int segNums[10] = {63, 6, 91, 79, 102, 109, 125, 7, 127, 103}; int main() { timeout.attach(&attimeout, 1); //Call attach function in timeout instance to call attimeout() every 1 second. LEDDP = 1; //Turn off the Decimal Points (Active Low). while(1) { //Continuous loop for the rest of the program. segout(); //Call segout() function to output a character to the one of the seven segs. (It will alternate between segs on each call). } } void attimeout() //Timer interrupt routine. { if (num<99) { //If the counter number "num" is less than 99 then increment it. num++; } else { num = 0; //Reset the counter number "num" when it is greater than or equal to 99. } int temp = num; //Put num into a temp variable so that it can be changed without affecting num. tens = 0; //Reset tens so that it can count from 0 again after the last round. while (temp >= 10) { //When temp is greater than 10 there are tens to display. temp = temp - 10; //Remove ten from temp tens++; //and add one on to tens. } //Check again whether temp is greater than ten. Repeat until all the tens have been but into the tens variable. units = temp; //When all the tens have been removed from temp, all that should be left are the units. Put these into the units variable. } void segout() //Function to output digits to seven segment displays. { int bits; //Declare bits which is used to contain the bits that relate to the segment outputs. int out; //Declare out which is used to hold the current digit (tens or units) and then as the segNums index. LEDA = 1; //Turn off all segments before the output seven seg is changed. LEDB = 1; //Otherwise the digits will be displayed on both seven segs causing blurring. LEDC = 1; // LEDD = 1; // LEDE = 1; // LEDF = 1; // LEDG = 1; // MUX = !MUX; //Once all segs are off, change seven seg digit. switch (MUX) { //Switch case used to place correct digit into output for the seven seg digit selected (by MUX). case 0: //If tens seven seg is selected by MUX. out = tens; //Set output to tens. break; //break to end switch-case. case 1: //If units seven seg is selected by MUX. out = units; //Set output to units. break; //break to end switch-case. default: //If MUX does not equal 1 or 0 (error, should never happen). out = 0; //Show 0 on display. break; //break to end switch-case. } bits = ~segNums[out]; //Call the array segNums with the number that wants to be displayed. Set bits to equal the bitwise inverted (~) number. //The Bitwise Inverter (~) is used because the LEDs in the seven seg are active low. //The next section uses Bitwise AND (&) to filter out the value of each bit using "bits" and a bitmask. LEDA = bits & 1; //Set LEDA to the value of the first bit of bits. LEDB = bits & 2; //Set LEDB to the value of the second bit of bits. LEDC = bits & 4; //Set LEDC to the value of the third bit of bits. LEDD = bits & 8; //Set LEDD to the value of the fourth bit of bits. LEDE = bits & 16; //Set LEDE to the value of the fifth bit of bits. LEDF = bits & 32; //Set LEDF to the value of the sixth bit of bits. LEDG = bits & 64; //Set LEDG to the value of the seventh bit of bits. }