#include "mbed.h" //Include the mbed header file which contains the prototypes of the libraries used here (DigitalOut) as well as various syntax definitions. #include "USBSerial.h" //Include USBSerial library which allows for comms with PC. void segout(); //Prototype declared above main function to avoid errors. USBSerial pc; //Create an instance of USBSerial called pc. Allows use of USBSerial library. 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. int num, temp; //Declare variables for output number value (num) and a temporary value (temp) for holding the decimal value of input characters. //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[11] = {63, 6, 91, 79, 102, 109, 125, 7, 127, 103, 256}; int main() { MUX = 1; //MUX is set to 1 to select the right display for the duration of the program. LEDA = LEDB = LEDC = LEDD = LEDE = LEDF = LEDG = LEDDP= 1; //All segments set to 1 to turn off all segments initially. char buf[3]; //Declare buffer to hold incoming data. Set to 3 as this is the longest length of string that is used. //Any extra characters will be ignored. while(1) { //Start while loop for start sequence. Repeat until user input is received. pc.printf("Press any key and then enter to begin.\r\n"); //Start message is repeatedly put to the screen so that it is there even if the terminal is not opened right away. if (pc.available()) { //When there is a character available. pc.scanf("%s", buf); //Read in string and place in buf array. break; //When start string has been read in the start sequence can be broken out of. } } //Print instructions for the program to the PC terminal. pc.printf("\r\nEnter a number to display and press enter.\r\nTyping a number will display that number.\r\nTyping . will toggle the Decimal point.\r\nTyping OFF will turn all LEDs off.\r\n\r\n"); while(1) { pc.scanf("%s", buf); //Hold program and wait until the user inputs a string into the terminal. temp = buf[0] - 48; //Put the first character of the input buffer into temp and convert it to decimal by removing 48. //(Only works for chars 0-9) if (temp < 10 && temp >= 0) { //If the first character of the input string is a digit between 0 and 9. num = temp; //Make equal to the input digit so the digit will be put to the seven seg display. pc.printf("%d\r\n", num); //Print the number back to the PC terminal to show that it has been received and displayed on seven seg. segout(); //Call segout() to update the seven seg display with the new num. } else if (temp == -2) { //If the first character is a decimal point (46 in ASCII, -2 after 48 has been removed). LEDDP = !LEDDP; //Toggle the LEDDP to toggle the decimal point. pc.printf("Decimal Point\r\n"); //Print Decimal Point back to the PC terminal to show that it has been received and displayed on seven seg. } else if (buf[0] == 'O' && buf[1] == 'F' && buf[2] == 'F') { //If the first three characters in the incoming string are OFF. num = 10; //Make num = 10 which when used in the segNums array will turn all segments off (make all = 1). LEDDP = 1; //Turn off decimal point. pc.printf("OFF\r\n"); //Print OFF back to the PC terminal to show that it has been received and displayed on seven seg. segout(); //Call segout() to update the seven seg display with the new num. } } } void segout() //Function to output digits to seven segment display. { int bits = ~segNums[num]; //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. }