Task 4.3.2 Solution

Committer:
noutram
Date:
Thu Jul 13 14:50:37 2017 +0000
Revision:
1:43cdf29e4ec6
Parent:
0:cdb59b84bd58
updated for mbed-os 5.5

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:cdb59b84bd58 1 #include "mbed.h"
noutram 0:cdb59b84bd58 2
noutram 0:cdb59b84bd58 3 //Global objects
noutram 0:cdb59b84bd58 4 BusOut binaryOutput(D5, D6, D7);
noutram 0:cdb59b84bd58 5 DigitalIn SW1(D4);
noutram 0:cdb59b84bd58 6
noutram 0:cdb59b84bd58 7 //Function prototypes
noutram 0:cdb59b84bd58 8 void waitForButtonPress();
noutram 0:cdb59b84bd58 9
noutram 0:cdb59b84bd58 10 //Main function
noutram 0:cdb59b84bd58 11 int main() {
noutram 0:cdb59b84bd58 12
noutram 0:cdb59b84bd58 13 //Create a variable to hold the bit pattern
noutram 0:cdb59b84bd58 14 unsigned int u;
noutram 0:cdb59b84bd58 15
noutram 0:cdb59b84bd58 16 //Flash LED's to indicate the code is running
noutram 0:cdb59b84bd58 17 binaryOutput = 7;
noutram 0:cdb59b84bd58 18 wait(0.5);
noutram 0:cdb59b84bd58 19 binaryOutput = 0;
noutram 0:cdb59b84bd58 20
noutram 0:cdb59b84bd58 21 while(1) {
noutram 0:cdb59b84bd58 22
noutram 0:cdb59b84bd58 23 u = 7; //Set initial value 111
noutram 0:cdb59b84bd58 24 binaryOutput = u; //Show binary on LED's
noutram 0:cdb59b84bd58 25 waitForButtonPress(); //Call function
noutram 0:cdb59b84bd58 26
noutram 0:cdb59b84bd58 27 //Use & to reset bit 1
noutram 0:cdb59b84bd58 28 u = u & 5; //AND with binary 101
noutram 0:cdb59b84bd58 29 binaryOutput = u;
noutram 0:cdb59b84bd58 30 waitForButtonPress();
noutram 0:cdb59b84bd58 31
noutram 0:cdb59b84bd58 32 //Modify u with & to reset bit 2 to a 0
noutram 0:cdb59b84bd58 33 u = u & 3; //AND with 011
noutram 0:cdb59b84bd58 34 binaryOutput = u;
noutram 0:cdb59b84bd58 35 waitForButtonPress();
noutram 0:cdb59b84bd58 36
noutram 0:cdb59b84bd58 37
noutram 0:cdb59b84bd58 38 //Modify u with & to reset bit 0 to a 0
noutram 0:cdb59b84bd58 39 u = u & 6; //AND with 110
noutram 0:cdb59b84bd58 40 binaryOutput = u;
noutram 0:cdb59b84bd58 41 waitForButtonPress();
noutram 0:cdb59b84bd58 42
noutram 0:cdb59b84bd58 43 } //end while(1)
noutram 0:cdb59b84bd58 44 } //end main
noutram 0:cdb59b84bd58 45
noutram 0:cdb59b84bd58 46
noutram 0:cdb59b84bd58 47 void waitForButtonPress() {
noutram 0:cdb59b84bd58 48 while (SW1 == 0);
noutram 0:cdb59b84bd58 49 wait(0.25);
noutram 0:cdb59b84bd58 50 while (SW1 == 1);
noutram 0:cdb59b84bd58 51 }