University of Plymouth - Stages 1, 2 and 3
/
Task343Solution
Task 3.4.3 Solution
Fork of Task343Solution by
main.cpp@0:807cc79e50f0, 2015-09-24 (annotated)
- Committer:
- noutram
- Date:
- Thu Sep 24 12:27:10 2015 +0000
- Revision:
- 0:807cc79e50f0
- Child:
- 2:1e53c842f790
Initial version 24-09-2015
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 | 0:807cc79e50f0 | 3 | //Global objects |
noutram | 0:807cc79e50f0 | 4 | BusOut binaryOutput(D5, D6, D7); |
noutram | 0:807cc79e50f0 | 5 | DigitalIn SW1(D4); |
noutram | 0:807cc79e50f0 | 6 | |
noutram | 0:807cc79e50f0 | 7 | //Function prototypes |
noutram | 0:807cc79e50f0 | 8 | void waitForButtonPress(); |
noutram | 0:807cc79e50f0 | 9 | |
noutram | 0:807cc79e50f0 | 10 | //Main function |
noutram | 0:807cc79e50f0 | 11 | int main() { |
noutram | 0:807cc79e50f0 | 12 | |
noutram | 0:807cc79e50f0 | 13 | //Create a variable to hold the bit pattern |
noutram | 0:807cc79e50f0 | 14 | unsigned int u; |
noutram | 0:807cc79e50f0 | 15 | |
noutram | 0:807cc79e50f0 | 16 | //Flash LED's to indicate the code is running |
noutram | 0:807cc79e50f0 | 17 | binaryOutput = 7; |
noutram | 0:807cc79e50f0 | 18 | wait(0.5); |
noutram | 0:807cc79e50f0 | 19 | binaryOutput = 0; |
noutram | 0:807cc79e50f0 | 20 | |
noutram | 0:807cc79e50f0 | 21 | while(1) { |
noutram | 0:807cc79e50f0 | 22 | |
noutram | 0:807cc79e50f0 | 23 | u = 0; //Set initial value 0 |
noutram | 0:807cc79e50f0 | 24 | binaryOutput = u; //Show binary on LED's |
noutram | 0:807cc79e50f0 | 25 | waitForButtonPress(); //Call function |
noutram | 0:807cc79e50f0 | 26 | |
noutram | 0:807cc79e50f0 | 27 | //Use & to toggle bit 0 |
noutram | 0:807cc79e50f0 | 28 | u = u ^ 1; //XOR with binary 001 |
noutram | 0:807cc79e50f0 | 29 | binaryOutput = u; |
noutram | 0:807cc79e50f0 | 30 | waitForButtonPress(); |
noutram | 0:807cc79e50f0 | 31 | |
noutram | 0:807cc79e50f0 | 32 | //Toggle bit 1 |
noutram | 0:807cc79e50f0 | 33 | u = u ^ 2; //XOR with 010 |
noutram | 0:807cc79e50f0 | 34 | binaryOutput = u; |
noutram | 0:807cc79e50f0 | 35 | waitForButtonPress(); |
noutram | 0:807cc79e50f0 | 36 | |
noutram | 0:807cc79e50f0 | 37 | |
noutram | 0:807cc79e50f0 | 38 | //Toggle bit 2 |
noutram | 0:807cc79e50f0 | 39 | u = u ^ 4; //XOR with 100 |
noutram | 0:807cc79e50f0 | 40 | binaryOutput = u; |
noutram | 0:807cc79e50f0 | 41 | waitForButtonPress(); |
noutram | 0:807cc79e50f0 | 42 | |
noutram | 0:807cc79e50f0 | 43 | //Toggle bits 0 and 2 |
noutram | 0:807cc79e50f0 | 44 | u = u ^ 5; |
noutram | 0:807cc79e50f0 | 45 | binaryOutput = u; |
noutram | 0:807cc79e50f0 | 46 | waitForButtonPress(); |
noutram | 0:807cc79e50f0 | 47 | |
noutram | 0:807cc79e50f0 | 48 | //Toggle all bits |
noutram | 0:807cc79e50f0 | 49 | u = u ^ 7; |
noutram | 0:807cc79e50f0 | 50 | binaryOutput = u; |
noutram | 0:807cc79e50f0 | 51 | waitForButtonPress(); |
noutram | 0:807cc79e50f0 | 52 | |
noutram | 0:807cc79e50f0 | 53 | //Toggle all bits |
noutram | 0:807cc79e50f0 | 54 | u = u ^ 7; |
noutram | 0:807cc79e50f0 | 55 | binaryOutput = u; |
noutram | 0:807cc79e50f0 | 56 | waitForButtonPress(); |
noutram | 0:807cc79e50f0 | 57 | |
noutram | 0:807cc79e50f0 | 58 | } //end while(1) |
noutram | 0:807cc79e50f0 | 59 | } //end main |
noutram | 0:807cc79e50f0 | 60 | |
noutram | 0:807cc79e50f0 | 61 | |
noutram | 0:807cc79e50f0 | 62 | void waitForButtonPress() { |
noutram | 0:807cc79e50f0 | 63 | while (SW1 == 0); |
noutram | 0:807cc79e50f0 | 64 | wait(0.25); |
noutram | 0:807cc79e50f0 | 65 | while (SW1 == 1); |
noutram | 0:807cc79e50f0 | 66 | } |