serial with 7 segment

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 Serial pc(USBTX, USBRX);
00003 BusOut Seg1(PC_9,PC_8,PB_8,PC_6,PB_9,PC_5,PA_5,PA_12);// A,B,C,D,E,F,G,DP
00004 BusOut Seg2(PA_8,PB_1,PB_10,PB_15,PB_4,PB_14,PB_5,PB_13);// A,B,C,D,E,F,G,DP
00005 char SegConvert(char SegValue); // functionprototype
00006 
00007 void SegInit(void); // function prototype
00008 void HostInit(void); // function prototype
00009 char GetKeyInput(void); // function prototype
00010 char data1, data2; // variable declarations
00011 
00012 int main() 
00013 { // main program
00014     SegInit(); // call function to initialise the 7-seg displays
00015     HostInit(); // call function to initialise the host terminal
00016         while (1) 
00017         { // infinite loop
00018             data2 = GetKeyInput(); // call function to get 1st key press
00019             Seg1=SegConvert(data2); // call function to convert and output
00020             data1 = GetKeyInput(); // call function to get 2nd key press
00021             Seg2=SegConvert(data1); // call function to convert and output
00022             pc.printf(" "); // display spaces between 2 digit numbers
00023         }
00024 }
00025 
00026 char SegConvert(char SegValue) { // function 'SegConvert'
00027 char SegByte=0x00;
00028 switch (SegValue) { //DPGFEDCBA
00029 case 0 : SegByte = 0x3F;break; // 00111111 binary
00030 case 1 : SegByte = 0x06;break; // 00000110 binary
00031 case 2 : SegByte = 0x5B;break; // 01011011 binary
00032 case 3 : SegByte = 0x4F;break; // 01001111 binary
00033 case 4 : SegByte = 0x66;break; // 01100110 binary
00034 case 5 : SegByte = 0x6D;break; // 01101101 binary
00035 case 6 : SegByte = 0x7D;break; // 01111101 binary
00036 case 7 : SegByte = 0x07;break; // 00000111 binary
00037 case 8 : SegByte = 0x7F;break; // 01111111 binary
00038 case 9 : SegByte = 0x6F;break; // 01101111 binary
00039    }
00040     return SegByte;
00041 }
00042 
00043 void SegInit(void) {
00044 Seg1=SegConvert(0); // initialise to zero
00045 Seg2=SegConvert(0); // initialise to zero
00046 }
00047 
00048 void HostInit(void) {
00049 pc.printf("\n\rType two digit numbers to be displayed on the 7-seg display\n\r");
00050 }
00051 
00052 char GetKeyInput(void) {
00053 char c = pc.getc(); // get keyboard data (note numerical ascii range 0x30-0x39)
00054 pc.printf("%c",c); // print ascii value to host PC terminal
00055 return (c&0x0F); // return value as non-ascii (bitmask c with value 0x0F)
00056 }