Nicholas Outram
/
Task341
Task 3.4.1
main.cpp@1:bcafc833577d, 2017-07-13 (annotated)
- Committer:
- noutram
- Date:
- Thu Jul 13 14:50:03 2017 +0000
- Revision:
- 1:bcafc833577d
- Parent:
- 0:08f1d029d089
updated for mbed-os 5.5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:08f1d029d089 | 1 | #include "mbed.h" |
noutram | 0:08f1d029d089 | 2 | |
noutram | 0:08f1d029d089 | 3 | //Global objects |
noutram | 0:08f1d029d089 | 4 | BusOut binaryOutput(D5, D6, D7); |
noutram | 0:08f1d029d089 | 5 | DigitalIn SW1(D4); |
noutram | 0:08f1d029d089 | 6 | |
noutram | 0:08f1d029d089 | 7 | //Function prototypes |
noutram | 0:08f1d029d089 | 8 | void waitForButtonPress(); |
noutram | 0:08f1d029d089 | 9 | |
noutram | 0:08f1d029d089 | 10 | //Main function |
noutram | 0:08f1d029d089 | 11 | int main() { |
noutram | 0:08f1d029d089 | 12 | |
noutram | 0:08f1d029d089 | 13 | //Create a variable to hold the bit pattern |
noutram | 0:08f1d029d089 | 14 | unsigned int u; |
noutram | 0:08f1d029d089 | 15 | |
noutram | 0:08f1d029d089 | 16 | //Flash LED's to indicate the code is running |
noutram | 0:08f1d029d089 | 17 | binaryOutput = 7; |
noutram | 0:08f1d029d089 | 18 | wait(0.5); |
noutram | 0:08f1d029d089 | 19 | binaryOutput = 0; |
noutram | 0:08f1d029d089 | 20 | |
noutram | 0:08f1d029d089 | 21 | //Main Loop |
noutram | 0:08f1d029d089 | 22 | while(1) { |
noutram | 0:08f1d029d089 | 23 | |
noutram | 0:08f1d029d089 | 24 | u = 0; //Set initial value |
noutram | 0:08f1d029d089 | 25 | binaryOutput = u; //Show binary on LED's |
noutram | 0:08f1d029d089 | 26 | waitForButtonPress(); //Call function |
noutram | 0:08f1d029d089 | 27 | |
noutram | 0:08f1d029d089 | 28 | //Here is the first - use | to set bit 1 |
noutram | 0:08f1d029d089 | 29 | u = u | 2; //OR with binary 010 |
noutram | 0:08f1d029d089 | 30 | binaryOutput = u; |
noutram | 0:08f1d029d089 | 31 | waitForButtonPress(); |
noutram | 0:08f1d029d089 | 32 | |
noutram | 0:08f1d029d089 | 33 | //Modify u with the | to set bit 2 |
noutram | 0:08f1d029d089 | 34 | //WRITE CODE HERE |
noutram | 0:08f1d029d089 | 35 | |
noutram | 0:08f1d029d089 | 36 | binaryOutput = u; |
noutram | 0:08f1d029d089 | 37 | waitForButtonPress(); |
noutram | 0:08f1d029d089 | 38 | |
noutram | 0:08f1d029d089 | 39 | |
noutram | 0:08f1d029d089 | 40 | //Modify u with the | to set bit 0 |
noutram | 0:08f1d029d089 | 41 | //WRITE CODE HERE |
noutram | 0:08f1d029d089 | 42 | |
noutram | 0:08f1d029d089 | 43 | binaryOutput = u; |
noutram | 0:08f1d029d089 | 44 | waitForButtonPress(); |
noutram | 0:08f1d029d089 | 45 | |
noutram | 0:08f1d029d089 | 46 | } //end while(1) |
noutram | 0:08f1d029d089 | 47 | } //end main |
noutram | 0:08f1d029d089 | 48 | |
noutram | 0:08f1d029d089 | 49 | //This is known as a C function. |
noutram | 0:08f1d029d089 | 50 | //This saves a lot of code repetition |
noutram | 0:08f1d029d089 | 51 | void waitForButtonPress() { |
noutram | 0:08f1d029d089 | 52 | while (SW1 == 0); |
noutram | 0:08f1d029d089 | 53 | wait(0.25); |
noutram | 0:08f1d029d089 | 54 | while (SW1 == 1); |
noutram | 0:08f1d029d089 | 55 | } |