Stage-1 Students SoCEM / Mbed 2 deprecated Task341Solution

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 //Global objects
00004 BusOut binaryOutput(D5, D6, D7);
00005 DigitalIn SW1(D4);
00006 
00007 //Function prototypes
00008 void waitForButtonPress();
00009 
00010 //Main function
00011 int main() {
00012 
00013     //Create a variable to hold the bit pattern
00014     unsigned int u;
00015 
00016     //Flash LED's to indicate the code is running
00017     binaryOutput = 7;
00018     wait(0.5);
00019     binaryOutput = 0;
00020 
00021     while(1) {
00022         
00023         u = 0;             //Set initial value
00024         binaryOutput = u;  //Show binary on LED's
00025         waitForButtonPress(); //Call function      
00026   
00027         //Here is the first - use | to set bit 1 
00028         u = u | 2;      //OR with binary 010
00029         binaryOutput = u;   
00030         waitForButtonPress();   
00031                                
00032         //Modify u with the | to set bit 2 to a 1
00033         u = u | 4;
00034         binaryOutput = u;   
00035         waitForButtonPress();                          
00036 
00037 
00038         //Modify u with the | to set bit 0 to a 1
00039         u = u | 1;
00040         binaryOutput = u;   
00041         waitForButtonPress();   
00042         
00043     } //end while(1)
00044 } //end main
00045 
00046 
00047 void waitForButtonPress() {
00048     while (SW1 == 0);
00049     wait(0.25);
00050     while (SW1 == 1);
00051 }