
Task 4.3.2 Solution
Fork of Task342Solution by
main.cpp@2:717364fdf071, 2019-09-18 (annotated)
- Committer:
- noutram
- Date:
- Wed Sep 18 12:09:27 2019 +0000
- Revision:
- 2:717364fdf071
- Parent:
- 0:cdb59b84bd58
2019
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:cdb59b84bd58 | 1 | #include "mbed.h" |
noutram | 0:cdb59b84bd58 | 2 | |
noutram | 2:717364fdf071 | 3 | #ifdef TARGET_NUCLEO_F429ZI |
noutram | 2:717364fdf071 | 4 | //#define ONBOARD |
noutram | 2:717364fdf071 | 5 | #endif |
noutram | 2:717364fdf071 | 6 | |
noutram | 2:717364fdf071 | 7 | #ifdef ONBOARD |
noutram | 2:717364fdf071 | 8 | BusOut binaryOutput(LED1, LED2, LED3); |
noutram | 2:717364fdf071 | 9 | DigitalIn SW1(USER_BUTTON); |
noutram | 2:717364fdf071 | 10 | #else |
noutram | 0:cdb59b84bd58 | 11 | //Global objects |
noutram | 0:cdb59b84bd58 | 12 | BusOut binaryOutput(D5, D6, D7); |
noutram | 0:cdb59b84bd58 | 13 | DigitalIn SW1(D4); |
noutram | 2:717364fdf071 | 14 | #endif |
noutram | 0:cdb59b84bd58 | 15 | |
noutram | 0:cdb59b84bd58 | 16 | //Function prototypes |
noutram | 0:cdb59b84bd58 | 17 | void waitForButtonPress(); |
noutram | 0:cdb59b84bd58 | 18 | |
noutram | 0:cdb59b84bd58 | 19 | //Main function |
noutram | 0:cdb59b84bd58 | 20 | int main() { |
noutram | 0:cdb59b84bd58 | 21 | |
noutram | 0:cdb59b84bd58 | 22 | //Create a variable to hold the bit pattern |
noutram | 0:cdb59b84bd58 | 23 | unsigned int u; |
noutram | 0:cdb59b84bd58 | 24 | |
noutram | 0:cdb59b84bd58 | 25 | //Flash LED's to indicate the code is running |
noutram | 0:cdb59b84bd58 | 26 | binaryOutput = 7; |
noutram | 0:cdb59b84bd58 | 27 | wait(0.5); |
noutram | 0:cdb59b84bd58 | 28 | binaryOutput = 0; |
noutram | 0:cdb59b84bd58 | 29 | |
noutram | 0:cdb59b84bd58 | 30 | while(1) { |
noutram | 0:cdb59b84bd58 | 31 | |
noutram | 0:cdb59b84bd58 | 32 | u = 7; //Set initial value 111 |
noutram | 0:cdb59b84bd58 | 33 | binaryOutput = u; //Show binary on LED's |
noutram | 0:cdb59b84bd58 | 34 | waitForButtonPress(); //Call function |
noutram | 0:cdb59b84bd58 | 35 | |
noutram | 0:cdb59b84bd58 | 36 | //Use & to reset bit 1 |
noutram | 0:cdb59b84bd58 | 37 | u = u & 5; //AND with binary 101 |
noutram | 0:cdb59b84bd58 | 38 | binaryOutput = u; |
noutram | 0:cdb59b84bd58 | 39 | waitForButtonPress(); |
noutram | 0:cdb59b84bd58 | 40 | |
noutram | 0:cdb59b84bd58 | 41 | //Modify u with & to reset bit 2 to a 0 |
noutram | 0:cdb59b84bd58 | 42 | u = u & 3; //AND with 011 |
noutram | 0:cdb59b84bd58 | 43 | binaryOutput = u; |
noutram | 0:cdb59b84bd58 | 44 | waitForButtonPress(); |
noutram | 0:cdb59b84bd58 | 45 | |
noutram | 0:cdb59b84bd58 | 46 | |
noutram | 0:cdb59b84bd58 | 47 | //Modify u with & to reset bit 0 to a 0 |
noutram | 0:cdb59b84bd58 | 48 | u = u & 6; //AND with 110 |
noutram | 0:cdb59b84bd58 | 49 | binaryOutput = u; |
noutram | 0:cdb59b84bd58 | 50 | waitForButtonPress(); |
noutram | 0:cdb59b84bd58 | 51 | |
noutram | 0:cdb59b84bd58 | 52 | } //end while(1) |
noutram | 0:cdb59b84bd58 | 53 | } //end main |
noutram | 0:cdb59b84bd58 | 54 | |
noutram | 0:cdb59b84bd58 | 55 | |
noutram | 0:cdb59b84bd58 | 56 | void waitForButtonPress() { |
noutram | 0:cdb59b84bd58 | 57 | while (SW1 == 0); |
noutram | 0:cdb59b84bd58 | 58 | wait(0.25); |
noutram | 0:cdb59b84bd58 | 59 | while (SW1 == 1); |
noutram | 0:cdb59b84bd58 | 60 | } |