First version. Demo of PWM setup and speaker operation. Fine grain control of the PWM parameters is an advantage Mbed has over the Arduino environment.
main.cpp@109:86a37f0a397f, 2021-10-05 (annotated)
- Committer:
- CSTritt
- Date:
- Tue Oct 05 05:00:06 2021 +0000
- Revision:
- 109:86a37f0a397f
- Parent:
- 108:eee3167b25b4
- Child:
- 110:e80444a6fe3a
Correct error in pow function use.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CSTritt | 107:61b9c99a4e27 | 1 | /* |
CSTritt | 109:86a37f0a397f | 2 | Project: Binary Counter Improved (while loop) Version |
CSTritt | 108:eee3167b25b4 | 3 | File: main.cpp |
CSTritt | 108:eee3167b25b4 | 4 | |
CSTritt | 109:86a37f0a397f | 5 | Displays 8-bit binary count on bar graph display. This version uses a while |
CSTritt | 109:86a37f0a397f | 6 | loop which provides a much better starting point for an Up/Down counter. |
CSTritt | 109:86a37f0a397f | 7 | |
CSTritt | 109:86a37f0a397f | 8 | Error in pow function use corrected in v. 1.0. |
CSTritt | 108:eee3167b25b4 | 9 | |
CSTritt | 109:86a37f0a397f | 10 | Note: I tried to use count for the loop counter but this resulted in an |
CSTritt | 109:86a37f0a397f | 11 | ambiguous name error. |
CSTritt | 109:86a37f0a397f | 12 | |
CSTritt | 108:eee3167b25b4 | 13 | Written by: Dr. C. S. Tritt |
CSTritt | 109:86a37f0a397f | 14 | Created: 9/27/21 (v. 1.0) |
CSTritt | 107:61b9c99a4e27 | 15 | */ |
Jonathan Austin |
0:2757d7abb7d9 | 16 | #include "mbed.h" |
CSTritt | 108:eee3167b25b4 | 17 | |
CSTritt | 109:86a37f0a397f | 18 | DigitalIn uB(USER_BUTTON); // Button is active low (up = 1, down = 0). |
CSTritt | 109:86a37f0a397f | 19 | const int bits = 4; // Number of bits to use. |
CSTritt | 109:86a37f0a397f | 20 | const int c_max = pow(2, bits) - 1; // Maximum count. pow is type double. |
CSTritt | 109:86a37f0a397f | 21 | const int sleepTime = 100; // Sleep time in milliseconds. |
CSTritt | 109:86a37f0a397f | 22 | int myCount = 0; // Count to be displayed. Loop counter. |
CSTritt | 109:86a37f0a397f | 23 | BusOut barGraph(D2, D3, D4, D5, D6, D7, D8, D9); // The display. |
CSTritt | 108:eee3167b25b4 | 24 | |
CSTritt | 108:eee3167b25b4 | 25 | int main() { |
CSTritt | 108:eee3167b25b4 | 26 | // Test the wiring. |
CSTritt | 108:eee3167b25b4 | 27 | ThisThread::sleep_for(400); // For 0.4 seconds. |
CSTritt | 108:eee3167b25b4 | 28 | barGraph = 0b01010101; // Odd bars on (binary). |
CSTritt | 108:eee3167b25b4 | 29 | ThisThread::sleep_for(400); // Test even bars for 0.4 seconds. |
CSTritt | 108:eee3167b25b4 | 30 | barGraph = 0b10101010; // Even bars on (binary). |
CSTritt | 108:eee3167b25b4 | 31 | ThisThread::sleep_for(400); // Test even bars for 0.4 seconds. |
CSTritt | 108:eee3167b25b4 | 32 | barGraph = 0xFF; // All bars on. Hex. |
CSTritt | 109:86a37f0a397f | 33 | ThisThread::sleep_for(400); // For 0.4 seconds. |
CSTritt | 109:86a37f0a397f | 34 | |
CSTritt | 109:86a37f0a397f | 35 | barGraph = myCount; // Initialize the display to myCount. |
CSTritt | 109:86a37f0a397f | 36 | // Enter main loop. |
CSTritt | 108:eee3167b25b4 | 37 | while(true) { |
CSTritt | 109:86a37f0a397f | 38 | while (myCount >= 0 && myCount <= c_max) { |
CSTritt | 109:86a37f0a397f | 39 | barGraph = myCount; // Set display to myCount. |
CSTritt | 109:86a37f0a397f | 40 | ThisThread::sleep_for(sleepTime); // Display value for 0.1 seconds. |
CSTritt | 109:86a37f0a397f | 41 | myCount++; |
CSTritt | 108:eee3167b25b4 | 42 | } |
CSTritt | 109:86a37f0a397f | 43 | myCount = 0; |
CSTritt | 108:eee3167b25b4 | 44 | } |
CSTritt | 108:eee3167b25b4 | 45 | } |