by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"

Dependencies:   mbed

Committer:
robt
Date:
Mon Oct 15 21:23:00 2012 +0000
Revision:
0:f2b51b7dab8f
by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"

Who changed what in which revision?

UserRevisionLine numberNew contents of line
robt 0:f2b51b7dab8f 1 /* Program Example 6.3: Host keypress to 7-seg display
robt 0:f2b51b7dab8f 2 */
robt 0:f2b51b7dab8f 3 #include "mbed.h"
robt 0:f2b51b7dab8f 4 Serial pc(USBTX, USBRX); // comms to host PC
robt 0:f2b51b7dab8f 5 BusOut Seg1(p5,p6,p7,p8,p9,p10,p11,p12); // A,B,C,D,E,F,G,DP
robt 0:f2b51b7dab8f 6 BusOut Seg2(p13,p14,p15,p16,p17,p18,p19,p20); // A,B,C,D,E,F,G,DP
robt 0:f2b51b7dab8f 7
robt 0:f2b51b7dab8f 8 void SegInit(void); // function prototype
robt 0:f2b51b7dab8f 9 void HostInit(void); // function prototype
robt 0:f2b51b7dab8f 10 char GetKeyInput(void); // function prototype
robt 0:f2b51b7dab8f 11 char SegConvert(char SegValue); // function prototype
robt 0:f2b51b7dab8f 12 char data1, data2; // variable declarations
robt 0:f2b51b7dab8f 13
robt 0:f2b51b7dab8f 14 int main() { // main program
robt 0:f2b51b7dab8f 15 SegInit(); // call function to initialise the 7-seg displays
robt 0:f2b51b7dab8f 16 HostInit(); // call function to initialise the host terminal
robt 0:f2b51b7dab8f 17 while (1) { // infinite loop
robt 0:f2b51b7dab8f 18 data2 = GetKeyInput(); // call function to get 1st key press
robt 0:f2b51b7dab8f 19 Seg2=SegConvert(data2); // call function to convert and output
robt 0:f2b51b7dab8f 20 data1 = GetKeyInput(); // call function to get 2nd key press
robt 0:f2b51b7dab8f 21 Seg1=SegConvert(data1); // call function to convert and output
robt 0:f2b51b7dab8f 22 pc.printf(" "); // display spaces between numbers
robt 0:f2b51b7dab8f 23 }
robt 0:f2b51b7dab8f 24 }
robt 0:f2b51b7dab8f 25 // functions
robt 0:f2b51b7dab8f 26 void SegInit(void) {
robt 0:f2b51b7dab8f 27 Seg1=SegConvert(0); // initialise to zero
robt 0:f2b51b7dab8f 28 Seg2=SegConvert(0); // initialise to zero
robt 0:f2b51b7dab8f 29 }
robt 0:f2b51b7dab8f 30
robt 0:f2b51b7dab8f 31 void HostInit(void) {
robt 0:f2b51b7dab8f 32 pc.printf("\n\rType two digit numbers to be displayed\n\r");
robt 0:f2b51b7dab8f 33 }
robt 0:f2b51b7dab8f 34
robt 0:f2b51b7dab8f 35 char GetKeyInput(void) {
robt 0:f2b51b7dab8f 36 char c = pc.getc(); // get keyboard data (ascii 0x30-0x39)
robt 0:f2b51b7dab8f 37 pc.printf("%c",c); // print ascii value to host PC terminal
robt 0:f2b51b7dab8f 38 return (c&0x0F); // apply bit mask to convert to decimal, and return
robt 0:f2b51b7dab8f 39 }
robt 0:f2b51b7dab8f 40
robt 0:f2b51b7dab8f 41 char SegConvert(char SegValue) { // function 'SegConvert'
robt 0:f2b51b7dab8f 42 char SegByte=0x00;
robt 0:f2b51b7dab8f 43 switch (SegValue) { //DP G F E D C B A
robt 0:f2b51b7dab8f 44 case 0 : SegByte = 0x3F;break; // 0 0 1 1 1 1 1 1 binary
robt 0:f2b51b7dab8f 45 case 1 : SegByte = 0x06;break; // 0 0 0 0 0 1 1 0 binary
robt 0:f2b51b7dab8f 46 case 2 : SegByte = 0x5B;break; // 0 1 0 1 1 0 1 1 binary
robt 0:f2b51b7dab8f 47 case 3 : SegByte = 0x4F;break; // 0 1 0 0 1 1 1 1 binary
robt 0:f2b51b7dab8f 48 case 4 : SegByte = 0x66;break; // 0 1 1 0 0 1 1 0 binary
robt 0:f2b51b7dab8f 49 case 5 : SegByte = 0x6D;break; // 0 1 1 0 1 1 0 1 binary
robt 0:f2b51b7dab8f 50 case 6 : SegByte = 0x7D;break; // 0 1 1 1 1 1 0 1 binary
robt 0:f2b51b7dab8f 51 case 7 : SegByte = 0x07;break; // 0 0 0 0 0 1 1 1 binary
robt 0:f2b51b7dab8f 52 case 8 : SegByte = 0x7F;break; // 0 1 1 1 1 1 1 1 binary
robt 0:f2b51b7dab8f 53 case 9 : SegByte = 0x6F;break; // 0 1 1 0 1 1 1 1 binary
robt 0:f2b51b7dab8f 54 }
robt 0:f2b51b7dab8f 55 return SegByte;
robt 0:f2b51b7dab8f 56 }