by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
main.cpp@0:b738c2b5c143, 2012-10-15 (annotated)
- Committer:
- robt
- Date:
- Mon Oct 15 21:22:04 2012 +0000
- Revision:
- 0:b738c2b5c143
by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
robt | 0:b738c2b5c143 | 1 | /* Program Example 6.1: seven-segment display counter |
robt | 0:b738c2b5c143 | 2 | */ |
robt | 0:b738c2b5c143 | 3 | #include "mbed.h" |
robt | 0:b738c2b5c143 | 4 | |
robt | 0:b738c2b5c143 | 5 | BusOut Seg1(p5,p6,p7,p8,p9,p10,p11,p12); // A,B,C,D,E,F,G,DP |
robt | 0:b738c2b5c143 | 6 | char SegConvert(char SegValue); // function prototype |
robt | 0:b738c2b5c143 | 7 | char A=0; // declare variables A and B |
robt | 0:b738c2b5c143 | 8 | char B; |
robt | 0:b738c2b5c143 | 9 | |
robt | 0:b738c2b5c143 | 10 | int main() { // main program |
robt | 0:b738c2b5c143 | 11 | while (1) { // infinite loop |
robt | 0:b738c2b5c143 | 12 | B=SegConvert(A); // Call function to return B |
robt | 0:b738c2b5c143 | 13 | Seg1=B; // Output B |
robt | 0:b738c2b5c143 | 14 | A++; // increment A |
robt | 0:b738c2b5c143 | 15 | if (A>0x09){ // if A > 9 reset to zero |
robt | 0:b738c2b5c143 | 16 | A=0; |
robt | 0:b738c2b5c143 | 17 | } |
robt | 0:b738c2b5c143 | 18 | wait(0.5); // delay 500 milliseconds |
robt | 0:b738c2b5c143 | 19 | } |
robt | 0:b738c2b5c143 | 20 | } |
robt | 0:b738c2b5c143 | 21 | |
robt | 0:b738c2b5c143 | 22 | char SegConvert(char SegValue) { // function 'SegConvert' |
robt | 0:b738c2b5c143 | 23 | char SegByte=0x00; |
robt | 0:b738c2b5c143 | 24 | switch (SegValue) { //DP G F E D C B A |
robt | 0:b738c2b5c143 | 25 | case 0 : SegByte = 0x3F;break; // 0 0 1 1 1 1 1 1 binary |
robt | 0:b738c2b5c143 | 26 | case 1 : SegByte = 0x06;break; // 0 0 0 0 0 1 1 0 binary |
robt | 0:b738c2b5c143 | 27 | case 2 : SegByte = 0x5B;break; // 0 1 0 1 1 0 1 1 binary |
robt | 0:b738c2b5c143 | 28 | case 3 : SegByte = 0x4F;break; // 0 1 0 0 1 1 1 1 binary |
robt | 0:b738c2b5c143 | 29 | case 4 : SegByte = 0x66;break; // 0 1 1 0 0 1 1 0 binary |
robt | 0:b738c2b5c143 | 30 | case 5 : SegByte = 0x6D;break; // 0 1 1 0 1 1 0 1 binary |
robt | 0:b738c2b5c143 | 31 | case 6 : SegByte = 0x7D;break; // 0 1 1 1 1 1 0 1 binary |
robt | 0:b738c2b5c143 | 32 | case 7 : SegByte = 0x07;break; // 0 0 0 0 0 1 1 1 binary |
robt | 0:b738c2b5c143 | 33 | case 8 : SegByte = 0x7F;break; // 0 1 1 1 1 1 1 1 binary |
robt | 0:b738c2b5c143 | 34 | case 9 : SegByte = 0x6F;break; // 0 1 1 0 1 1 1 1 binary |
robt | 0:b738c2b5c143 | 35 | } |
robt | 0:b738c2b5c143 | 36 | return SegByte; |
robt | 0:b738c2b5c143 | 37 | } |
robt | 0:b738c2b5c143 | 38 |