Task 3.4.1 Solution
Dependencies: mbed
main.cpp@0:9de80b65dfc5, 2015-09-24 (annotated)
- Committer:
- noutram
- Date:
- Thu Sep 24 12:26:30 2015 +0000
- Revision:
- 0:9de80b65dfc5
Initial version 24-09-2015
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:9de80b65dfc5 | 1 | #include "mbed.h" |
noutram | 0:9de80b65dfc5 | 2 | |
noutram | 0:9de80b65dfc5 | 3 | //Global objects |
noutram | 0:9de80b65dfc5 | 4 | BusOut binaryOutput(D5, D6, D7); |
noutram | 0:9de80b65dfc5 | 5 | DigitalIn SW1(D4); |
noutram | 0:9de80b65dfc5 | 6 | |
noutram | 0:9de80b65dfc5 | 7 | //Function prototypes |
noutram | 0:9de80b65dfc5 | 8 | void waitForButtonPress(); |
noutram | 0:9de80b65dfc5 | 9 | |
noutram | 0:9de80b65dfc5 | 10 | //Main function |
noutram | 0:9de80b65dfc5 | 11 | int main() { |
noutram | 0:9de80b65dfc5 | 12 | |
noutram | 0:9de80b65dfc5 | 13 | //Create a variable to hold the bit pattern |
noutram | 0:9de80b65dfc5 | 14 | unsigned int u; |
noutram | 0:9de80b65dfc5 | 15 | |
noutram | 0:9de80b65dfc5 | 16 | //Flash LED's to indicate the code is running |
noutram | 0:9de80b65dfc5 | 17 | binaryOutput = 7; |
noutram | 0:9de80b65dfc5 | 18 | wait(0.5); |
noutram | 0:9de80b65dfc5 | 19 | binaryOutput = 0; |
noutram | 0:9de80b65dfc5 | 20 | |
noutram | 0:9de80b65dfc5 | 21 | while(1) { |
noutram | 0:9de80b65dfc5 | 22 | |
noutram | 0:9de80b65dfc5 | 23 | u = 0; //Set initial value |
noutram | 0:9de80b65dfc5 | 24 | binaryOutput = u; //Show binary on LED's |
noutram | 0:9de80b65dfc5 | 25 | waitForButtonPress(); //Call function |
noutram | 0:9de80b65dfc5 | 26 | |
noutram | 0:9de80b65dfc5 | 27 | //Here is the first - use | to set bit 1 |
noutram | 0:9de80b65dfc5 | 28 | u = u | 2; //OR with binary 010 |
noutram | 0:9de80b65dfc5 | 29 | binaryOutput = u; |
noutram | 0:9de80b65dfc5 | 30 | waitForButtonPress(); |
noutram | 0:9de80b65dfc5 | 31 | |
noutram | 0:9de80b65dfc5 | 32 | //Modify u with the | to set bit 2 to a 1 |
noutram | 0:9de80b65dfc5 | 33 | u = u | 4; |
noutram | 0:9de80b65dfc5 | 34 | binaryOutput = u; |
noutram | 0:9de80b65dfc5 | 35 | waitForButtonPress(); |
noutram | 0:9de80b65dfc5 | 36 | |
noutram | 0:9de80b65dfc5 | 37 | |
noutram | 0:9de80b65dfc5 | 38 | //Modify u with the | to set bit 0 to a 1 |
noutram | 0:9de80b65dfc5 | 39 | u = u | 1; |
noutram | 0:9de80b65dfc5 | 40 | binaryOutput = u; |
noutram | 0:9de80b65dfc5 | 41 | waitForButtonPress(); |
noutram | 0:9de80b65dfc5 | 42 | |
noutram | 0:9de80b65dfc5 | 43 | } //end while(1) |
noutram | 0:9de80b65dfc5 | 44 | } //end main |
noutram | 0:9de80b65dfc5 | 45 | |
noutram | 0:9de80b65dfc5 | 46 | |
noutram | 0:9de80b65dfc5 | 47 | void waitForButtonPress() { |
noutram | 0:9de80b65dfc5 | 48 | while (SW1 == 0); |
noutram | 0:9de80b65dfc5 | 49 | wait(0.25); |
noutram | 0:9de80b65dfc5 | 50 | while (SW1 == 1); |
noutram | 0:9de80b65dfc5 | 51 | } |