
Modified, multi-file version of my orginal 7-segment test program.
main.cpp@111:47b0267b8dd4, 2021-11-09 (annotated)
- Committer:
- CSTritt
- Date:
- Tue Nov 09 20:33:18 2021 +0000
- Revision:
- 111:47b0267b8dd4
- Parent:
- 110:e80444a6fe3a
- Child:
- 112:2af5a1ac47d5
This is the initial version of my 7-segement hardware test and software demo project.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CSTritt | 107:61b9c99a4e27 | 1 | /* |
CSTritt | 111:47b0267b8dd4 | 2 | Project: 21_7segmentTest_v5 (Lab Wk 10) |
CSTritt | 108:eee3167b25b4 | 3 | File: main.cpp |
CSTritt | 108:eee3167b25b4 | 4 | |
CSTritt | 111:47b0267b8dd4 | 5 | This test program confirms hardware operation and demonstrates use of the |
CSTritt | 111:47b0267b8dd4 | 6 | 7-segement display. It will also be used as the starting point for the |
CSTritt | 111:47b0267b8dd4 | 7 | Week 10 lab practical. |
CSTritt | 111:47b0267b8dd4 | 8 | |
CSTritt | 111:47b0267b8dd4 | 9 | Display "value" from 0 to F_16 to DP (16_10) to all off (17_10) to all on |
CSTritt | 111:47b0267b8dd4 | 10 | (17_10) on 7-segment display. |
CSTritt | 108:eee3167b25b4 | 11 | |
CSTritt | 111:47b0267b8dd4 | 12 | Note: This display function takes an integer argument to be display and a |
CSTritt | 111:47b0267b8dd4 | 13 | BusOut object (passed by reference) on which to display it as arguments. |
CSTritt | 111:47b0267b8dd4 | 14 | This differs from the Week 8 Lab display function. |
CSTritt | 111:47b0267b8dd4 | 15 | |
CSTritt | 108:eee3167b25b4 | 16 | Written by: Dr. C. S. Tritt |
CSTritt | 111:47b0267b8dd4 | 17 | Created: 11/9/21 (v. 1.0) |
CSTritt | 107:61b9c99a4e27 | 18 | */ |
Jonathan Austin |
0:2757d7abb7d9 | 19 | #include "mbed.h" |
CSTritt | 108:eee3167b25b4 | 20 | |
CSTritt | 111:47b0267b8dd4 | 21 | void display(int disVal, BusOut &disBus); |
CSTritt | 111:47b0267b8dd4 | 22 | |
CSTritt | 111:47b0267b8dd4 | 23 | Serial pc(USBTX, NC, 9600); // Use for debugging. |
CSTritt | 111:47b0267b8dd4 | 24 | |
CSTritt | 111:47b0267b8dd4 | 25 | const int SLP_TIME = 1000; // Update once per second. |
CSTritt | 108:eee3167b25b4 | 26 | |
CSTritt | 111:47b0267b8dd4 | 27 | int main(void) |
CSTritt | 111:47b0267b8dd4 | 28 | { |
CSTritt | 111:47b0267b8dd4 | 29 | // Note that bit sequence is the oposite of what I initially thought they |
CSTritt | 111:47b0267b8dd4 | 30 | // would be. That is, pins are listed in low order to high order sequence. |
CSTritt | 111:47b0267b8dd4 | 31 | BusOut disBus(D9, D8, D7, D6, D5, D4, D3, D2); // The display. |
CSTritt | 111:47b0267b8dd4 | 32 | int myCount = 0; // Loop count. Rolls over at 18. |
CSTritt | 111:47b0267b8dd4 | 33 | |
CSTritt | 108:eee3167b25b4 | 34 | // Test the wiring. |
CSTritt | 111:47b0267b8dd4 | 35 | display(18, disBus); // All on (binary). |
CSTritt | 111:47b0267b8dd4 | 36 | ThisThread::sleep_for(2000); // Test even bars for 0.4 seconds. |
CSTritt | 111:47b0267b8dd4 | 37 | display(17, disBus); // All off (binary). |
CSTritt | 111:47b0267b8dd4 | 38 | ThisThread::sleep_for(2000); // Test even bars for 0.4 seconds. |
CSTritt | 111:47b0267b8dd4 | 39 | |
CSTritt | 111:47b0267b8dd4 | 40 | // Enter main loop. |
CSTritt | 111:47b0267b8dd4 | 41 | while(true) { |
CSTritt | 111:47b0267b8dd4 | 42 | display(myCount, disBus); // Display myCount (count). |
CSTritt | 111:47b0267b8dd4 | 43 | pc.printf("In main myCount = %d.\n", myCount); // Send count to PC. |
CSTritt | 111:47b0267b8dd4 | 44 | myCount++; // Increment the count. |
CSTritt | 111:47b0267b8dd4 | 45 | if (myCount > 18) myCount = 0; // Rollover! |
CSTritt | 111:47b0267b8dd4 | 46 | ThisThread::sleep_for(SLP_TIME); // Display value for 1 second. |
CSTritt | 111:47b0267b8dd4 | 47 | } |
CSTritt | 111:47b0267b8dd4 | 48 | } |
CSTritt | 109:86a37f0a397f | 49 | |
CSTritt | 111:47b0267b8dd4 | 50 | void display(int disVal, BusOut &disBus){ |
CSTritt | 111:47b0267b8dd4 | 51 | // 7-segment, active low look-up-table. Displays Hex 0 to F, dp, all off |
CSTritt | 111:47b0267b8dd4 | 52 | // and all on. |
CSTritt | 111:47b0267b8dd4 | 53 | const int lutAL[] = { |
CSTritt | 111:47b0267b8dd4 | 54 | 0b00000011, // 0 = 0 |
CSTritt | 111:47b0267b8dd4 | 55 | 0b10011111, // 1 = 1 |
CSTritt | 111:47b0267b8dd4 | 56 | 0b00100101, // 2 = 2 |
CSTritt | 111:47b0267b8dd4 | 57 | 0b00001101, // 3 = 3 |
CSTritt | 111:47b0267b8dd4 | 58 | 0b10011001, // 4 = 4 |
CSTritt | 111:47b0267b8dd4 | 59 | 0b01001001, // 5 = 5 |
CSTritt | 111:47b0267b8dd4 | 60 | 0b01000001, // 6 = 6 |
CSTritt | 111:47b0267b8dd4 | 61 | 0b00011111, // 7 = 7 |
CSTritt | 111:47b0267b8dd4 | 62 | 0b00000001, // 8 = 8 |
CSTritt | 111:47b0267b8dd4 | 63 | 0b00001001, // 9 = 9 |
CSTritt | 111:47b0267b8dd4 | 64 | 0b00010001, // 10 = a |
CSTritt | 111:47b0267b8dd4 | 65 | 0b11000001, // 11 = b |
CSTritt | 111:47b0267b8dd4 | 66 | 0b11100101, // 12 = c |
CSTritt | 111:47b0267b8dd4 | 67 | 0b10000101, // 13 = d |
CSTritt | 111:47b0267b8dd4 | 68 | 0b01100001, // 14 = e |
CSTritt | 111:47b0267b8dd4 | 69 | 0b01110001, // 15 = f |
CSTritt | 111:47b0267b8dd4 | 70 | 0b11111110, // 16 = dp |
CSTritt | 111:47b0267b8dd4 | 71 | 0b11111111, // 17 = All off |
CSTritt | 111:47b0267b8dd4 | 72 | 0b00000000 // 18 = All on |
CSTritt | 111:47b0267b8dd4 | 73 | //abcdefg_dp – 7-segement output codes 0 to 18. |
CSTritt | 111:47b0267b8dd4 | 74 | }; |
CSTritt | 111:47b0267b8dd4 | 75 | disBus = lutAL[disVal]; |
CSTritt | 111:47b0267b8dd4 | 76 | pc.printf("In display, disVal = %d\n", disVal); |
CSTritt | 108:eee3167b25b4 | 77 | } |