VT3_Mario_Sekulić

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 // main program code for Exercise 7-6
00003 #include "mbed.h"
00004 Serial pc(USBTX, USBRX); // comms to host PC
00005 BusOut Seg1(p5,p6,p7,p8,p9,p10,p11,p12); // A,B,C,D,E,F,G,DP
00006 BusOut Seg2(p13,p14,p15,p16,p17,p18,p19,p20); // A,B,C,D,E,F,G,DP
00007 void SegInit(void); // function prototype
00008 void HostInit(void); // function prototype
00009 char GetKeyInput(void); // function prototype
00010 char SegConvert(char SegValue); // function prototype
00011 char data1, data2; // variable declarations
00012 int main()   // main program
00013 {
00014     SegInit(); // call function to initialise the 7-seg displays
00015     HostInit(); // call function to initialise the host terminal
00016     while (1) { // infinite loop
00017         data2 = GetKeyInput(); // call function to get 1st key press
00018         Seg2=SegConvert(data2); // call function to convert and output
00019         data1 = GetKeyInput(); // call function to get 2nd key press
00020         Seg1=SegConvert(data1); // call function to convert and output
00021         pc.printf(" "); // display spaces between 2 digit numbers
00022     }
00023 }
00024 // functions for Exercise 7-6
00025 void SegInit(void)
00026 {
00027     Seg1=SegConvert(0); // initialise to zero
00028     Seg2=SegConvert(0); // initialise to zero
00029 }
00030 void HostInit(void)
00031 {
00032     pc.printf("\n\rType two digit numbers to be displayed on the 7-seg display\n\r");
00033 }
00034 char GetKeyInput(void)
00035 {
00036     char c = pc.getc(); // get keyboard data (note num. ascii range 0x30-0x39)
00037     pc.printf("%c",c); // print ascii value to host PC terminal
00038     return (c&0x0F); // return value as non-ascii (bitmask c with value 0x0F)
00039 }
00040 char SegConvert(char SegValue)   // function 'SegConvert'
00041 {
00042     char SegByte=0x00;
00043     switch (SegValue) { //DPGFEDCBA
00044         case 0 :
00045             SegByte = 0x3F;
00046             break; // 00111111 binary
00047         case 1 :
00048             SegByte = 0x06;
00049             break; // 00000110 binary
00050         case 2 :
00051             SegByte = 0x5B;
00052             break; // 01011011 binary
00053         case 3 :
00054             SegByte = 0x4F;
00055             break; // 01001111 binary
00056         case 4 :
00057             SegByte = 0x66;
00058             break; // 01100110 binary
00059         case 5 :
00060             SegByte = 0x6D;
00061             break; // 01101101 binary
00062         case 6 :
00063             SegByte = 0x7D;
00064             break; // 01111101 binary
00065         case 7 :
00066             SegByte = 0x07;
00067             break; // 00000111 binary
00068         case 8 :
00069             SegByte = 0x7F;
00070             break; // 01111111 binary
00071         case 9 :
00072             SegByte = 0x6F;
00073             break; // 01101111 binary
00074     }
00075     return SegByte;
00076 }