Lab version

Dependencies:   mbed

Committer:
noutram
Date:
Thu Sep 24 12:32:36 2015 +0000
Revision:
0:8ee1b72f9a9b
Initial version 24-09-2015

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:8ee1b72f9a9b 1 #include "mbed.h"
noutram 0:8ee1b72f9a9b 2
noutram 0:8ee1b72f9a9b 3
noutram 0:8ee1b72f9a9b 4 //Global objects
noutram 0:8ee1b72f9a9b 5 DigitalOut myled(LED1);
noutram 0:8ee1b72f9a9b 6 Serial pc(USBTX, USBRX);
noutram 0:8ee1b72f9a9b 7 Ticker t;
noutram 0:8ee1b72f9a9b 8
noutram 0:8ee1b72f9a9b 9 //Integer state of the LED
noutram 0:8ee1b72f9a9b 10 //static makes it visible only within main.cpp
noutram 0:8ee1b72f9a9b 11 static int state = 0;
noutram 0:8ee1b72f9a9b 12
noutram 0:8ee1b72f9a9b 13 //Function prototype
noutram 0:8ee1b72f9a9b 14 void doISR();
noutram 0:8ee1b72f9a9b 15
noutram 0:8ee1b72f9a9b 16 int main() {
noutram 0:8ee1b72f9a9b 17
noutram 0:8ee1b72f9a9b 18 //Initialise
noutram 0:8ee1b72f9a9b 19 pc.baud(115200);
noutram 0:8ee1b72f9a9b 20 t.attach(&doISR, 2);
noutram 0:8ee1b72f9a9b 21 myled = 0;
noutram 0:8ee1b72f9a9b 22
noutram 0:8ee1b72f9a9b 23 //Main loop
noutram 0:8ee1b72f9a9b 24 while(1) {
noutram 0:8ee1b72f9a9b 25
noutram 0:8ee1b72f9a9b 26 //Go to sleep and wait for an ISR to wake
noutram 0:8ee1b72f9a9b 27 sleep(); //At is says
noutram 0:8ee1b72f9a9b 28
noutram 0:8ee1b72f9a9b 29 //At this point, the ISR has run
noutram 0:8ee1b72f9a9b 30
noutram 0:8ee1b72f9a9b 31 //Update LED
noutram 0:8ee1b72f9a9b 32 myled = state;
noutram 0:8ee1b72f9a9b 33
noutram 0:8ee1b72f9a9b 34 //Echo to terminal
noutram 0:8ee1b72f9a9b 35 if (state == 0) {
noutram 0:8ee1b72f9a9b 36 pc.printf("LED OFF\n");
noutram 0:8ee1b72f9a9b 37 } else {
noutram 0:8ee1b72f9a9b 38 pc.printf("LED ON\n");
noutram 0:8ee1b72f9a9b 39 }
noutram 0:8ee1b72f9a9b 40 }
noutram 0:8ee1b72f9a9b 41 }
noutram 0:8ee1b72f9a9b 42
noutram 0:8ee1b72f9a9b 43 //Note - the ISR is short, and does NOT call functions
noutram 0:8ee1b72f9a9b 44 //such as printf (it's actually unsafe to do so)
noutram 0:8ee1b72f9a9b 45 //Always check a function is "reentrant" before calling it
noutram 0:8ee1b72f9a9b 46 void doISR() {
noutram 0:8ee1b72f9a9b 47 //Toggle 0 to 1, or 1 to 0
noutram 0:8ee1b72f9a9b 48 state ^= 1;
noutram 0:8ee1b72f9a9b 49 }