Rob Toulson / Mbed 2 deprecated PE_06-03_KeyPressDisplay

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Program Example 6.3: Host keypress to 7-seg display
00002                                                               */
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 
00008 void SegInit(void);                          // function prototype
00009 void HostInit(void);                         // function prototype
00010 char GetKeyInput(void);                      // function prototype
00011 char SegConvert(char SegValue);              // function prototype
00012 char data1, data2;                           // variable declarations
00013 
00014 int main() {         // main program
00015   SegInit();         // call function to initialise the 7-seg displays 
00016   HostInit();        // call function to initialise the host terminal              
00017   while (1) {                 // infinite loop
00018     data2 = GetKeyInput();    // call function to get 1st key press
00019     Seg2=SegConvert(data2);   // call function to convert and output
00020     data1 = GetKeyInput();    // call function to get 2nd key press
00021     Seg1=SegConvert(data1);   // call function to convert and output
00022     pc.printf("  ");          // display spaces between numbers
00023   }
00024 }
00025 // functions
00026 void SegInit(void) {
00027   Seg1=SegConvert(0);        // initialise to zero
00028   Seg2=SegConvert(0);        // initialise to zero
00029 }
00030 
00031 void HostInit(void) {
00032   pc.printf("\n\rType two digit numbers to be displayed\n\r");
00033 }
00034 
00035 char GetKeyInput(void) {
00036   char c = pc.getc();      // get keyboard data (ascii 0x30-0x39)
00037   pc.printf("%c",c);       // print ascii value to host PC terminal
00038   return (c&0x0F);         // apply bit mask to convert to decimal, and return 
00039 }
00040 
00041 char SegConvert(char SegValue) {        // function 'SegConvert'
00042   char SegByte=0x00;
00043   switch (SegValue) {                   //DP G F E D C B A
00044     case 0 : SegByte = 0x3F;break;      // 0 0 1 1 1 1 1 1 binary
00045     case 1 : SegByte = 0x06;break;      // 0 0 0 0 0 1 1 0 binary
00046     case 2 : SegByte = 0x5B;break;      // 0 1 0 1 1 0 1 1 binary
00047     case 3 : SegByte = 0x4F;break;      // 0 1 0 0 1 1 1 1 binary
00048     case 4 : SegByte = 0x66;break;      // 0 1 1 0 0 1 1 0 binary
00049     case 5 : SegByte = 0x6D;break;      // 0 1 1 0 1 1 0 1 binary
00050     case 6 : SegByte = 0x7D;break;      // 0 1 1 1 1 1 0 1 binary
00051     case 7 : SegByte = 0x07;break;      // 0 0 0 0 0 1 1 1 binary
00052     case 8 : SegByte = 0x7F;break;      // 0 1 1 1 1 1 1 1 binary
00053     case 9 : SegByte = 0x6F;break;      // 0 1 1 0 1 1 1 1 binary
00054   }
00055     return SegByte;
00056 }