Task 3.4.1
Fork of Task341 by
main.cpp@2:bea5a01bd4d8, 2019-09-18 (annotated)
- Committer:
- noutram
- Date:
- Wed Sep 18 12:01:28 2019 +0000
- Revision:
- 2:bea5a01bd4d8
- Parent:
- 0:08f1d029d089
2019
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 | 2:bea5a01bd4d8 | 3 | #ifdef TARGET_NUCLEO_F429ZI |
| noutram | 2:bea5a01bd4d8 | 4 | //#define ONBOARD |
| noutram | 2:bea5a01bd4d8 | 5 | #endif |
| noutram | 2:bea5a01bd4d8 | 6 | |
| noutram | 2:bea5a01bd4d8 | 7 | #ifdef ONBOARD |
| noutram | 2:bea5a01bd4d8 | 8 | BusOut binaryOutput(LED1, LED2, LED3); |
| noutram | 2:bea5a01bd4d8 | 9 | DigitalIn SW1(USER_BUTTON); |
| noutram | 2:bea5a01bd4d8 | 10 | #else |
| noutram | 2:bea5a01bd4d8 | 11 | //lsb first |
| noutram | 0:08f1d029d089 | 12 | //Global objects |
| noutram | 0:08f1d029d089 | 13 | BusOut binaryOutput(D5, D6, D7); |
| noutram | 0:08f1d029d089 | 14 | DigitalIn SW1(D4); |
| noutram | 2:bea5a01bd4d8 | 15 | #endif |
| noutram | 2:bea5a01bd4d8 | 16 | |
| noutram | 0:08f1d029d089 | 17 | |
| noutram | 0:08f1d029d089 | 18 | //Function prototypes |
| noutram | 0:08f1d029d089 | 19 | void waitForButtonPress(); |
| noutram | 0:08f1d029d089 | 20 | |
| noutram | 0:08f1d029d089 | 21 | //Main function |
| noutram | 0:08f1d029d089 | 22 | int main() { |
| noutram | 0:08f1d029d089 | 23 | |
| noutram | 0:08f1d029d089 | 24 | //Create a variable to hold the bit pattern |
| noutram | 0:08f1d029d089 | 25 | unsigned int u; |
| noutram | 0:08f1d029d089 | 26 | |
| noutram | 0:08f1d029d089 | 27 | //Flash LED's to indicate the code is running |
| noutram | 0:08f1d029d089 | 28 | binaryOutput = 7; |
| noutram | 0:08f1d029d089 | 29 | wait(0.5); |
| noutram | 0:08f1d029d089 | 30 | binaryOutput = 0; |
| noutram | 0:08f1d029d089 | 31 | |
| noutram | 0:08f1d029d089 | 32 | //Main Loop |
| noutram | 0:08f1d029d089 | 33 | while(1) { |
| noutram | 0:08f1d029d089 | 34 | |
| noutram | 0:08f1d029d089 | 35 | u = 0; //Set initial value |
| noutram | 0:08f1d029d089 | 36 | binaryOutput = u; //Show binary on LED's |
| noutram | 0:08f1d029d089 | 37 | waitForButtonPress(); //Call function |
| noutram | 0:08f1d029d089 | 38 | |
| noutram | 0:08f1d029d089 | 39 | //Here is the first - use | to set bit 1 |
| noutram | 0:08f1d029d089 | 40 | u = u | 2; //OR with binary 010 |
| noutram | 0:08f1d029d089 | 41 | binaryOutput = u; |
| noutram | 0:08f1d029d089 | 42 | waitForButtonPress(); |
| noutram | 0:08f1d029d089 | 43 | |
| noutram | 0:08f1d029d089 | 44 | //Modify u with the | to set bit 2 |
| noutram | 0:08f1d029d089 | 45 | //WRITE CODE HERE |
| noutram | 0:08f1d029d089 | 46 | |
| noutram | 0:08f1d029d089 | 47 | binaryOutput = u; |
| noutram | 0:08f1d029d089 | 48 | waitForButtonPress(); |
| noutram | 0:08f1d029d089 | 49 | |
| noutram | 0:08f1d029d089 | 50 | |
| noutram | 0:08f1d029d089 | 51 | //Modify u with the | to set bit 0 |
| noutram | 0:08f1d029d089 | 52 | //WRITE CODE HERE |
| noutram | 0:08f1d029d089 | 53 | |
| noutram | 0:08f1d029d089 | 54 | binaryOutput = u; |
| noutram | 0:08f1d029d089 | 55 | waitForButtonPress(); |
| noutram | 0:08f1d029d089 | 56 | |
| noutram | 0:08f1d029d089 | 57 | } //end while(1) |
| noutram | 0:08f1d029d089 | 58 | } //end main |
| noutram | 0:08f1d029d089 | 59 | |
| noutram | 0:08f1d029d089 | 60 | //This is known as a C function. |
| noutram | 0:08f1d029d089 | 61 | //This saves a lot of code repetition |
| noutram | 0:08f1d029d089 | 62 | void waitForButtonPress() { |
| noutram | 0:08f1d029d089 | 63 | while (SW1 == 0); |
| noutram | 0:08f1d029d089 | 64 | wait(0.25); |
| noutram | 0:08f1d029d089 | 65 | while (SW1 == 1); |
| noutram | 0:08f1d029d089 | 66 | } |
