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.
Diff: main.cpp
- Revision:
- 107:e7084caca83a
- Parent:
- 105:ed03c03b353e
- Child:
- 108:61d009929518
diff -r d323dd088ba2 -r e7084caca83a main.cpp --- a/main.cpp Fri Nov 22 16:00:04 2019 +0000 +++ b/main.cpp Fri Oct 01 18:16:36 2021 +0000 @@ -1,23 +1,49 @@ -/* mbed Microcontroller Library - * Copyright (c) 2019 ARM Limited - * SPDX-License-Identifier: Apache-2.0 - */ - +/* +Project: 21_BitTests_v5 +File: main.cpp + +Explores literal integers and bitwise operations. + +Last modified 9/30/21 by C. S. Tritt (v. 1.0) +*/ #include "mbed.h" -#include "platform/mbed_thread.h" +// Pulse durations. +const int SPULSE = 500; +const int LPULSE = 1000; -// Blinking rate in milliseconds -#define BLINKING_RATE_MS 500 - - +// Construct a transmit only serial connection over our USB. +Serial pc(USBTX, NC, 9600); // Serial channel to PC. +// Construct a 8-bit BusOut bargraph display. +BusOut barGraph(D2, D3, D4, D5, D6, D7, D8, D9); // The display. + int main() { - // Initialise the digital pin LED1 as an output - DigitalOut led(LED1); - - while (true) { - led = !led; - thread_sleep_for(BLINKING_RATE_MS); + while (true) { // Main loop. + // Set some values. + unsigned char myIntA = 0b10101010; // Binary. 170_10. + unsigned char myIntB = 0x3E; // Hex. 62_10. + unsigned char myIntC = 077; // Octal, 63_10. + + // Do a bitwise operation. + unsigned char myIntD = myIntA | myIntB; + + // Send output to PC and bargraph. + pc.printf("myIntA = %d (base 10).\n", myIntA); + barGraph = myIntA; + ThisThread::sleep_for(SPULSE); + + pc.printf("myIntB = %X (Hexadecimal).\n", myIntB); + barGraph = myIntB; + ThisThread::sleep_for(SPULSE); + + pc.printf("myIntC = %o (Octal).\n", myIntC); + barGraph = myIntC; + ThisThread::sleep_for(SPULSE); + + pc.printf("myIntD = %X (Hexadecimal).\n", myIntD); + barGraph = myIntD; + ThisThread::sleep_for(LPULSE); + pc.printf("Repeating...\n\n"); } -} +} \ No newline at end of file