This program is intended for use exploring bitwise operator. It also demonstrates the use of the 8-bit unsigned char type and using printf to display integer values in various number bases.
main.cpp@107:e7084caca83a, 2021-10-01 (annotated)
- Committer:
- CSTritt
- Date:
- Fri Oct 01 18:16:36 2021 +0000
- Revision:
- 107:e7084caca83a
- Parent:
- 105:ed03c03b353e
- Child:
- 108:61d009929518
Initial version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CSTritt | 107:e7084caca83a | 1 | /* |
CSTritt | 107:e7084caca83a | 2 | Project: 21_BitTests_v5 |
CSTritt | 107:e7084caca83a | 3 | File: main.cpp |
CSTritt | 107:e7084caca83a | 4 | |
CSTritt | 107:e7084caca83a | 5 | Explores literal integers and bitwise operations. |
CSTritt | 107:e7084caca83a | 6 | |
CSTritt | 107:e7084caca83a | 7 | Last modified 9/30/21 by C. S. Tritt (v. 1.0) |
CSTritt | 107:e7084caca83a | 8 | */ |
Jonathan Austin |
0:2757d7abb7d9 | 9 | #include "mbed.h" |
Jonathan Austin |
0:2757d7abb7d9 | 10 | |
CSTritt | 107:e7084caca83a | 11 | // Pulse durations. |
CSTritt | 107:e7084caca83a | 12 | const int SPULSE = 500; |
CSTritt | 107:e7084caca83a | 13 | const int LPULSE = 1000; |
Jonathan Austin |
0:2757d7abb7d9 | 14 | |
CSTritt | 107:e7084caca83a | 15 | // Construct a transmit only serial connection over our USB. |
CSTritt | 107:e7084caca83a | 16 | Serial pc(USBTX, NC, 9600); // Serial channel to PC. |
CSTritt | 107:e7084caca83a | 17 | // Construct a 8-bit BusOut bargraph display. |
CSTritt | 107:e7084caca83a | 18 | BusOut barGraph(D2, D3, D4, D5, D6, D7, D8, D9); // The display. |
CSTritt | 107:e7084caca83a | 19 | |
mbed_official | 82:abf1b1785bd7 | 20 | int main() |
mbed_official | 82:abf1b1785bd7 | 21 | { |
CSTritt | 107:e7084caca83a | 22 | while (true) { // Main loop. |
CSTritt | 107:e7084caca83a | 23 | // Set some values. |
CSTritt | 107:e7084caca83a | 24 | unsigned char myIntA = 0b10101010; // Binary. 170_10. |
CSTritt | 107:e7084caca83a | 25 | unsigned char myIntB = 0x3E; // Hex. 62_10. |
CSTritt | 107:e7084caca83a | 26 | unsigned char myIntC = 077; // Octal, 63_10. |
CSTritt | 107:e7084caca83a | 27 | |
CSTritt | 107:e7084caca83a | 28 | // Do a bitwise operation. |
CSTritt | 107:e7084caca83a | 29 | unsigned char myIntD = myIntA | myIntB; |
CSTritt | 107:e7084caca83a | 30 | |
CSTritt | 107:e7084caca83a | 31 | // Send output to PC and bargraph. |
CSTritt | 107:e7084caca83a | 32 | pc.printf("myIntA = %d (base 10).\n", myIntA); |
CSTritt | 107:e7084caca83a | 33 | barGraph = myIntA; |
CSTritt | 107:e7084caca83a | 34 | ThisThread::sleep_for(SPULSE); |
CSTritt | 107:e7084caca83a | 35 | |
CSTritt | 107:e7084caca83a | 36 | pc.printf("myIntB = %X (Hexadecimal).\n", myIntB); |
CSTritt | 107:e7084caca83a | 37 | barGraph = myIntB; |
CSTritt | 107:e7084caca83a | 38 | ThisThread::sleep_for(SPULSE); |
CSTritt | 107:e7084caca83a | 39 | |
CSTritt | 107:e7084caca83a | 40 | pc.printf("myIntC = %o (Octal).\n", myIntC); |
CSTritt | 107:e7084caca83a | 41 | barGraph = myIntC; |
CSTritt | 107:e7084caca83a | 42 | ThisThread::sleep_for(SPULSE); |
CSTritt | 107:e7084caca83a | 43 | |
CSTritt | 107:e7084caca83a | 44 | pc.printf("myIntD = %X (Hexadecimal).\n", myIntD); |
CSTritt | 107:e7084caca83a | 45 | barGraph = myIntD; |
CSTritt | 107:e7084caca83a | 46 | ThisThread::sleep_for(LPULSE); |
CSTritt | 107:e7084caca83a | 47 | pc.printf("Repeating...\n\n"); |
Jonathan Austin |
0:2757d7abb7d9 | 48 | } |
CSTritt | 107:e7084caca83a | 49 | } |