Task 3.4.1 Solution

Fork of Task341Solution by Nicholas Outram

Committer:
noutram
Date:
Wed Sep 18 12:04:05 2019 +0000
Revision:
2:3d313447620d
Parent:
0:9de80b65dfc5
2019

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:9de80b65dfc5 1 #include "mbed.h"
noutram 0:9de80b65dfc5 2
noutram 2:3d313447620d 3 #ifdef TARGET_NUCLEO_F429ZI
noutram 2:3d313447620d 4 //#define ONBOARD
noutram 2:3d313447620d 5 #endif
noutram 2:3d313447620d 6
noutram 2:3d313447620d 7 #ifdef ONBOARD
noutram 2:3d313447620d 8 BusOut binaryOutput(LED1, LED2, LED3);
noutram 2:3d313447620d 9 DigitalIn SW1(USER_BUTTON);
noutram 2:3d313447620d 10 #else
noutram 0:9de80b65dfc5 11 //Global objects
noutram 0:9de80b65dfc5 12 BusOut binaryOutput(D5, D6, D7);
noutram 0:9de80b65dfc5 13 DigitalIn SW1(D4);
noutram 2:3d313447620d 14 #endif
noutram 0:9de80b65dfc5 15
noutram 0:9de80b65dfc5 16 //Function prototypes
noutram 0:9de80b65dfc5 17 void waitForButtonPress();
noutram 0:9de80b65dfc5 18
noutram 0:9de80b65dfc5 19 //Main function
noutram 0:9de80b65dfc5 20 int main() {
noutram 0:9de80b65dfc5 21
noutram 0:9de80b65dfc5 22 //Create a variable to hold the bit pattern
noutram 0:9de80b65dfc5 23 unsigned int u;
noutram 0:9de80b65dfc5 24
noutram 0:9de80b65dfc5 25 //Flash LED's to indicate the code is running
noutram 0:9de80b65dfc5 26 binaryOutput = 7;
noutram 0:9de80b65dfc5 27 wait(0.5);
noutram 0:9de80b65dfc5 28 binaryOutput = 0;
noutram 0:9de80b65dfc5 29
noutram 0:9de80b65dfc5 30 while(1) {
noutram 0:9de80b65dfc5 31
noutram 0:9de80b65dfc5 32 u = 0; //Set initial value
noutram 0:9de80b65dfc5 33 binaryOutput = u; //Show binary on LED's
noutram 0:9de80b65dfc5 34 waitForButtonPress(); //Call function
noutram 0:9de80b65dfc5 35
noutram 0:9de80b65dfc5 36 //Here is the first - use | to set bit 1
noutram 0:9de80b65dfc5 37 u = u | 2; //OR with binary 010
noutram 0:9de80b65dfc5 38 binaryOutput = u;
noutram 0:9de80b65dfc5 39 waitForButtonPress();
noutram 0:9de80b65dfc5 40
noutram 0:9de80b65dfc5 41 //Modify u with the | to set bit 2 to a 1
noutram 0:9de80b65dfc5 42 u = u | 4;
noutram 0:9de80b65dfc5 43 binaryOutput = u;
noutram 0:9de80b65dfc5 44 waitForButtonPress();
noutram 0:9de80b65dfc5 45
noutram 0:9de80b65dfc5 46
noutram 0:9de80b65dfc5 47 //Modify u with the | to set bit 0 to a 1
noutram 0:9de80b65dfc5 48 u = u | 1;
noutram 0:9de80b65dfc5 49 binaryOutput = u;
noutram 0:9de80b65dfc5 50 waitForButtonPress();
noutram 0:9de80b65dfc5 51
noutram 0:9de80b65dfc5 52 } //end while(1)
noutram 0:9de80b65dfc5 53 } //end main
noutram 0:9de80b65dfc5 54
noutram 0:9de80b65dfc5 55
noutram 0:9de80b65dfc5 56 void waitForButtonPress() {
noutram 0:9de80b65dfc5 57 while (SW1 == 0);
noutram 0:9de80b65dfc5 58 wait(0.25);
noutram 0:9de80b65dfc5 59 while (SW1 == 1);
noutram 0:9de80b65dfc5 60 }