Solution

Dependencies:   mbed

Fork of Task352Solution by Stage-1 Students SoCEM

Committer:
noutram
Date:
Tue Oct 02 07:37:08 2018 +0000
Revision:
1:facc2bd1d423
Parent:
0:e2f876895db4
Added solution to tasks

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:e2f876895db4 1 #include "mbed.h"
noutram 0:e2f876895db4 2
noutram 0:e2f876895db4 3 //Global objects
noutram 0:e2f876895db4 4 BusOut binaryOutput(D5, D6, D7); //Outputs as an integer
noutram 0:e2f876895db4 5 //BusIn binaryInput(D3, D4); //Inputs as an integer
noutram 0:e2f876895db4 6 DigitalIn SW1(D3);
noutram 0:e2f876895db4 7 DigitalIn SW2(D4);
noutram 0:e2f876895db4 8
noutram 0:e2f876895db4 9 //Main function
noutram 0:e2f876895db4 10 int main() {
noutram 0:e2f876895db4 11
noutram 0:e2f876895db4 12 //Create a variable to hold the bit pattern
noutram 0:e2f876895db4 13 unsigned int u = 7;
noutram 0:e2f876895db4 14
noutram 0:e2f876895db4 15 while(1) {
noutram 0:e2f876895db4 16
noutram 0:e2f876895db4 17 binaryOutput = u; //Write to LEDs
noutram 0:e2f876895db4 18
noutram 0:e2f876895db4 19 //TOGGLE all 3 bits in u
noutram 0:e2f876895db4 20 u = u ^ 7;
noutram 0:e2f876895db4 21
noutram 0:e2f876895db4 22 //Calculate the delay
noutram 0:e2f876895db4 23 int binaryInput = SW1 + (SW2 << 1);
noutram 0:e2f876895db4 24 double delay = (double)(binaryInput+1);
noutram 0:e2f876895db4 25 wait(delay * 0.25); //Wait
noutram 0:e2f876895db4 26
noutram 0:e2f876895db4 27 } //end while(1)
noutram 0:e2f876895db4 28 } //end main