VT3_Goran Herek

Dependencies:   mbed

Committer:
Goran002
Date:
Thu Nov 17 17:58:29 2016 +0000
Revision:
0:918d74953a0f
VT3_Goran Herek

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Goran002 0:918d74953a0f 1 // main program code for Exercise 7-6
Goran002 0:918d74953a0f 2 #include "mbed.h"
Goran002 0:918d74953a0f 3 Serial pc(USBTX, USBRX); // comms to host PC
Goran002 0:918d74953a0f 4 BusOut Seg1(p5,p6,p7,p8,p9,p10,p11,p12); // A,B,C,D,E,F,G,DP
Goran002 0:918d74953a0f 5 BusOut Seg2(p13,p14,p15,p16,p17,p18,p19,p20); // A,B,C,D,E,F,G,DP
Goran002 0:918d74953a0f 6 void SegInit(void); // function prototype
Goran002 0:918d74953a0f 7 void HostInit(void); // function prototype
Goran002 0:918d74953a0f 8 char GetKeyInput(void); // function prototype
Goran002 0:918d74953a0f 9 char SegConvert(char SegValue); // function prototype
Goran002 0:918d74953a0f 10 char data1, data2; // variable declarations
Goran002 0:918d74953a0f 11 int main() // main program
Goran002 0:918d74953a0f 12 {
Goran002 0:918d74953a0f 13 SegInit(); // call function to initialise the 7-seg displays
Goran002 0:918d74953a0f 14 HostInit(); // call function to initialise the host terminal
Goran002 0:918d74953a0f 15 while (1) { // infinite loop
Goran002 0:918d74953a0f 16 data2 = GetKeyInput(); // call function to get 1st key press
Goran002 0:918d74953a0f 17 Seg2=SegConvert(data2); // call function to convert and output
Goran002 0:918d74953a0f 18 data1 = GetKeyInput(); // call function to get 2nd key press
Goran002 0:918d74953a0f 19 Seg1=SegConvert(data1); // call function to convert and output
Goran002 0:918d74953a0f 20 pc.printf(" "); // display spaces between 2 digit numbers
Goran002 0:918d74953a0f 21 }
Goran002 0:918d74953a0f 22 }
Goran002 0:918d74953a0f 23 void SegInit(void)
Goran002 0:918d74953a0f 24 {
Goran002 0:918d74953a0f 25 Seg1=SegConvert(0); // initialise to zero
Goran002 0:918d74953a0f 26 Seg2=SegConvert(0); // initialise to zero
Goran002 0:918d74953a0f 27 }
Goran002 0:918d74953a0f 28 void HostInit(void)
Goran002 0:918d74953a0f 29 {
Goran002 0:918d74953a0f 30 pc.printf("\n\rType two digit numbers to be displayed on the 7-seg display\n\r");
Goran002 0:918d74953a0f 31 }
Goran002 0:918d74953a0f 32 char GetKeyInput(void)
Goran002 0:918d74953a0f 33 {
Goran002 0:918d74953a0f 34 char c = pc.getc(); // get keyboard data (note num. ascii range 0x30-0x39)
Goran002 0:918d74953a0f 35 pc.printf("%c",c); // print ascii value to host PC terminal
Goran002 0:918d74953a0f 36 return (c&0x0F); // return value as non-ascii (bitmask c with value 0x0F)
Goran002 0:918d74953a0f 37 }
Goran002 0:918d74953a0f 38 char SegConvert(char SegValue) // function 'SegConvert'
Goran002 0:918d74953a0f 39 {
Goran002 0:918d74953a0f 40 char SegByte=0x00;
Goran002 0:918d74953a0f 41 switch (SegValue) { //DPGFEDCBA
Goran002 0:918d74953a0f 42 case 0 :
Goran002 0:918d74953a0f 43 SegByte = 0x3F;
Goran002 0:918d74953a0f 44 break; // 00111111 binary
Goran002 0:918d74953a0f 45 case 1 :
Goran002 0:918d74953a0f 46 SegByte = 0x06;
Goran002 0:918d74953a0f 47 break; // 00000110 binary
Goran002 0:918d74953a0f 48 case 2 :
Goran002 0:918d74953a0f 49 SegByte = 0x5B;
Goran002 0:918d74953a0f 50 break; // 01011011 binary
Goran002 0:918d74953a0f 51 case 3 :
Goran002 0:918d74953a0f 52 SegByte = 0x4F;
Goran002 0:918d74953a0f 53 break; // 01001111 binary
Goran002 0:918d74953a0f 54 case 4 :
Goran002 0:918d74953a0f 55 SegByte = 0x66;
Goran002 0:918d74953a0f 56 break; // 01100110 binary
Goran002 0:918d74953a0f 57 case 5 :
Goran002 0:918d74953a0f 58 SegByte = 0x6D;
Goran002 0:918d74953a0f 59 break; // 01101101 binary
Goran002 0:918d74953a0f 60 case 6 :
Goran002 0:918d74953a0f 61 SegByte = 0x7D;
Goran002 0:918d74953a0f 62 break; // 01111101 binary
Goran002 0:918d74953a0f 63 case 7 :
Goran002 0:918d74953a0f 64 SegByte = 0x07;
Goran002 0:918d74953a0f 65 break; // 00000111 binary
Goran002 0:918d74953a0f 66 case 8 :
Goran002 0:918d74953a0f 67 SegByte = 0x7F;
Goran002 0:918d74953a0f 68 break; // 01111111 binary
Goran002 0:918d74953a0f 69 case 9 :
Goran002 0:918d74953a0f 70 SegByte = 0x6F;
Goran002 0:918d74953a0f 71 break; // 01101111 binary
Goran002 0:918d74953a0f 72 }
Goran002 0:918d74953a0f 73 return SegByte;
Goran002 0:918d74953a0f 74 }