Nicholas Outram / Mbed OS Task343Solution
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 0
00024         binaryOutput = u;  //Show binary on LED's
00025         waitForButtonPress(); //Call function      
00026   
00027         //Use & to toggle bit 0 
00028         u = u ^ 1;          //XOR with binary 001
00029         binaryOutput = u;   
00030         waitForButtonPress();   
00031                                
00032         //Toggle bit 1
00033         u = u ^ 2;  //XOR with 010
00034         binaryOutput = u;   
00035         waitForButtonPress();                          
00036 
00037 
00038         //Toggle bit 2
00039         u = u ^ 4;  //XOR with 100
00040         binaryOutput = u;   
00041         waitForButtonPress();   
00042         
00043         //Toggle bits 0 and 2
00044         u = u ^ 5;
00045         binaryOutput = u;   
00046         waitForButtonPress();  
00047         
00048         //Toggle all bits
00049         u = u ^ 7;
00050         binaryOutput = u;   
00051         waitForButtonPress();
00052         
00053         //Toggle all bits
00054         u = u ^ 7;
00055         binaryOutput = u;   
00056         waitForButtonPress();               
00057         
00058     } //end while(1)
00059 } //end main
00060 
00061 
00062 void waitForButtonPress() {
00063     while (SW1 == 0);
00064     wait(0.25);
00065     while (SW1 == 1);
00066 }