University of Plymouth - Stages 1, 2 and 3
/
Task343Solution
Task 3.4.3 Solution
Fork of Task343Solution by
main.cpp@2:1e53c842f790, 2019-09-18 (annotated)
- Committer:
- noutram
- Date:
- Wed Sep 18 12:14:02 2019 +0000
- Revision:
- 2:1e53c842f790
- Parent:
- 0:807cc79e50f0
2019
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:807cc79e50f0 | 1 | #include "mbed.h" |
noutram | 0:807cc79e50f0 | 2 | |
noutram | 2:1e53c842f790 | 3 | #ifdef TARGET_NUCLEO_F429ZI |
noutram | 2:1e53c842f790 | 4 | //#define ONBOARD |
noutram | 2:1e53c842f790 | 5 | #endif |
noutram | 2:1e53c842f790 | 6 | |
noutram | 2:1e53c842f790 | 7 | #ifdef ONBOARD |
noutram | 2:1e53c842f790 | 8 | BusOut binaryOutput(LED1, LED2, LED3); |
noutram | 2:1e53c842f790 | 9 | DigitalIn SW1(USER_BUTTON); |
noutram | 2:1e53c842f790 | 10 | #else |
noutram | 0:807cc79e50f0 | 11 | //Global objects |
noutram | 0:807cc79e50f0 | 12 | BusOut binaryOutput(D5, D6, D7); |
noutram | 0:807cc79e50f0 | 13 | DigitalIn SW1(D4); |
noutram | 2:1e53c842f790 | 14 | #endif |
noutram | 0:807cc79e50f0 | 15 | |
noutram | 0:807cc79e50f0 | 16 | //Function prototypes |
noutram | 0:807cc79e50f0 | 17 | void waitForButtonPress(); |
noutram | 0:807cc79e50f0 | 18 | |
noutram | 0:807cc79e50f0 | 19 | //Main function |
noutram | 0:807cc79e50f0 | 20 | int main() { |
noutram | 0:807cc79e50f0 | 21 | |
noutram | 0:807cc79e50f0 | 22 | //Create a variable to hold the bit pattern |
noutram | 0:807cc79e50f0 | 23 | unsigned int u; |
noutram | 0:807cc79e50f0 | 24 | |
noutram | 0:807cc79e50f0 | 25 | //Flash LED's to indicate the code is running |
noutram | 0:807cc79e50f0 | 26 | binaryOutput = 7; |
noutram | 0:807cc79e50f0 | 27 | wait(0.5); |
noutram | 0:807cc79e50f0 | 28 | binaryOutput = 0; |
noutram | 0:807cc79e50f0 | 29 | |
noutram | 0:807cc79e50f0 | 30 | while(1) { |
noutram | 0:807cc79e50f0 | 31 | |
noutram | 0:807cc79e50f0 | 32 | u = 0; //Set initial value 0 |
noutram | 0:807cc79e50f0 | 33 | binaryOutput = u; //Show binary on LED's |
noutram | 0:807cc79e50f0 | 34 | waitForButtonPress(); //Call function |
noutram | 0:807cc79e50f0 | 35 | |
noutram | 0:807cc79e50f0 | 36 | //Use & to toggle bit 0 |
noutram | 0:807cc79e50f0 | 37 | u = u ^ 1; //XOR with binary 001 |
noutram | 0:807cc79e50f0 | 38 | binaryOutput = u; |
noutram | 0:807cc79e50f0 | 39 | waitForButtonPress(); |
noutram | 0:807cc79e50f0 | 40 | |
noutram | 0:807cc79e50f0 | 41 | //Toggle bit 1 |
noutram | 0:807cc79e50f0 | 42 | u = u ^ 2; //XOR with 010 |
noutram | 0:807cc79e50f0 | 43 | binaryOutput = u; |
noutram | 0:807cc79e50f0 | 44 | waitForButtonPress(); |
noutram | 0:807cc79e50f0 | 45 | |
noutram | 0:807cc79e50f0 | 46 | |
noutram | 0:807cc79e50f0 | 47 | //Toggle bit 2 |
noutram | 0:807cc79e50f0 | 48 | u = u ^ 4; //XOR with 100 |
noutram | 0:807cc79e50f0 | 49 | binaryOutput = u; |
noutram | 0:807cc79e50f0 | 50 | waitForButtonPress(); |
noutram | 0:807cc79e50f0 | 51 | |
noutram | 0:807cc79e50f0 | 52 | //Toggle bits 0 and 2 |
noutram | 0:807cc79e50f0 | 53 | u = u ^ 5; |
noutram | 0:807cc79e50f0 | 54 | binaryOutput = u; |
noutram | 0:807cc79e50f0 | 55 | waitForButtonPress(); |
noutram | 0:807cc79e50f0 | 56 | |
noutram | 0:807cc79e50f0 | 57 | //Toggle all bits |
noutram | 0:807cc79e50f0 | 58 | u = u ^ 7; |
noutram | 0:807cc79e50f0 | 59 | binaryOutput = u; |
noutram | 0:807cc79e50f0 | 60 | waitForButtonPress(); |
noutram | 0:807cc79e50f0 | 61 | |
noutram | 0:807cc79e50f0 | 62 | //Toggle all bits |
noutram | 0:807cc79e50f0 | 63 | u = u ^ 7; |
noutram | 0:807cc79e50f0 | 64 | binaryOutput = u; |
noutram | 0:807cc79e50f0 | 65 | waitForButtonPress(); |
noutram | 0:807cc79e50f0 | 66 | |
noutram | 0:807cc79e50f0 | 67 | } //end while(1) |
noutram | 0:807cc79e50f0 | 68 | } //end main |
noutram | 0:807cc79e50f0 | 69 | |
noutram | 0:807cc79e50f0 | 70 | |
noutram | 0:807cc79e50f0 | 71 | void waitForButtonPress() { |
noutram | 0:807cc79e50f0 | 72 | while (SW1 == 0); |
noutram | 0:807cc79e50f0 | 73 | wait(0.25); |
noutram | 0:807cc79e50f0 | 74 | while (SW1 == 1); |
noutram | 0:807cc79e50f0 | 75 | } |