VT3_Goran Herek

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

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