ddad
Dependencies: mbed
Fork of class_7segment_serial by
main.cpp@1:65d57cc8cb22, 2017-06-06 (annotated)
- Committer:
- mijimy
- Date:
- Tue Jun 06 09:26:52 2017 +0000
- Revision:
- 1:65d57cc8cb22
- Parent:
- 0:8cf7ed60c892
- Child:
- 2:120a14d6f055
rwer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mijimy | 0:8cf7ed60c892 | 1 | #include "mbed.h" |
mijimy | 0:8cf7ed60c892 | 2 | Serial pc(USBTX, USBRX); |
mijimy | 0:8cf7ed60c892 | 3 | BusOut Seg1(PC_9,PC_8,PB_8,PC_6,PB_9,PC_5,PA_5,PA_12);// A,B,C,D,E,F,G,DP |
mijimy | 0:8cf7ed60c892 | 4 | BusOut Seg2(PA_8,PB_1,PB_10,PB_15,PB_4,PB_14,PB_5,PB_13);// A,B,C,D,E,F,G,DP |
mijimy | 0:8cf7ed60c892 | 5 | char SegConvert(char SegValue); // functionprototype |
mijimy | 0:8cf7ed60c892 | 6 | |
mijimy | 0:8cf7ed60c892 | 7 | void SegInit(void); // function prototype |
mijimy | 0:8cf7ed60c892 | 8 | void HostInit(void); // function prototype |
mijimy | 0:8cf7ed60c892 | 9 | char GetKeyInput(void); // function prototype |
mijimy | 0:8cf7ed60c892 | 10 | char data1, data2; // variable declarations |
mijimy | 0:8cf7ed60c892 | 11 | |
mijimy | 0:8cf7ed60c892 | 12 | int main() |
mijimy | 0:8cf7ed60c892 | 13 | { // main program |
mijimy | 0:8cf7ed60c892 | 14 | SegInit(); // call function to initialise the 7-seg displays |
mijimy | 0:8cf7ed60c892 | 15 | HostInit(); // call function to initialise the host terminal |
mijimy | 0:8cf7ed60c892 | 16 | while (1) |
mijimy | 0:8cf7ed60c892 | 17 | { // infinite loop |
mijimy | 0:8cf7ed60c892 | 18 | data2 = GetKeyInput(); // call function to get 1st key press |
mijimy | 1:65d57cc8cb22 | 19 | Seg1=SegConvert(data2); // call function to convert and output |
mijimy | 0:8cf7ed60c892 | 20 | data1 = GetKeyInput(); // call function to get 2nd key press |
mijimy | 1:65d57cc8cb22 | 21 | Seg2=SegConvert(data1); // call function to convert and output |
mijimy | 0:8cf7ed60c892 | 22 | pc.printf(" "); // display spaces between 2 digit numbers |
mijimy | 0:8cf7ed60c892 | 23 | } |
mijimy | 0:8cf7ed60c892 | 24 | } |
mijimy | 0:8cf7ed60c892 | 25 | |
mijimy | 0:8cf7ed60c892 | 26 | char SegConvert(char SegValue) { // function 'SegConvert' |
mijimy | 0:8cf7ed60c892 | 27 | char SegByte=0x00; |
mijimy | 0:8cf7ed60c892 | 28 | switch (SegValue) { //DPGFEDCBA |
mijimy | 0:8cf7ed60c892 | 29 | case 0 : SegByte = 0x3F;break; // 00111111 binary |
mijimy | 0:8cf7ed60c892 | 30 | case 1 : SegByte = 0x06;break; // 00000110 binary |
mijimy | 0:8cf7ed60c892 | 31 | case 2 : SegByte = 0x5B;break; // 01011011 binary |
mijimy | 0:8cf7ed60c892 | 32 | case 3 : SegByte = 0x4F;break; // 01001111 binary |
mijimy | 0:8cf7ed60c892 | 33 | case 4 : SegByte = 0x66;break; // 01100110 binary |
mijimy | 0:8cf7ed60c892 | 34 | case 5 : SegByte = 0x6D;break; // 01101101 binary |
mijimy | 0:8cf7ed60c892 | 35 | case 6 : SegByte = 0x7D;break; // 01111101 binary |
mijimy | 0:8cf7ed60c892 | 36 | case 7 : SegByte = 0x07;break; // 00000111 binary |
mijimy | 0:8cf7ed60c892 | 37 | case 8 : SegByte = 0x7F;break; // 01111111 binary |
mijimy | 0:8cf7ed60c892 | 38 | case 9 : SegByte = 0x6F;break; // 01101111 binary |
mijimy | 0:8cf7ed60c892 | 39 | } |
mijimy | 0:8cf7ed60c892 | 40 | return SegByte; |
mijimy | 0:8cf7ed60c892 | 41 | } |
mijimy | 0:8cf7ed60c892 | 42 | |
mijimy | 0:8cf7ed60c892 | 43 | void SegInit(void) { |
mijimy | 0:8cf7ed60c892 | 44 | Seg1=SegConvert(0); // initialise to zero |
mijimy | 0:8cf7ed60c892 | 45 | Seg2=SegConvert(0); // initialise to zero |
mijimy | 0:8cf7ed60c892 | 46 | } |
mijimy | 0:8cf7ed60c892 | 47 | |
mijimy | 0:8cf7ed60c892 | 48 | void HostInit(void) { |
mijimy | 0:8cf7ed60c892 | 49 | pc.printf("\n\rType two digit numbers to be displayed on the 7-seg display\n\r"); |
mijimy | 0:8cf7ed60c892 | 50 | } |
mijimy | 0:8cf7ed60c892 | 51 | |
mijimy | 0:8cf7ed60c892 | 52 | char GetKeyInput(void) { |
mijimy | 0:8cf7ed60c892 | 53 | char c = pc.getc(); // get keyboard data (note numerical ascii range 0x30-0x39) |
mijimy | 0:8cf7ed60c892 | 54 | pc.printf("%c",c); // print ascii value to host PC terminal |
mijimy | 0:8cf7ed60c892 | 55 | return (c&0x0F); // return value as non-ascii (bitmask c with value 0x0F) |
mijimy | 0:8cf7ed60c892 | 56 | } |