Stage-1 Students SoCEM / Mbed 2 deprecated Task325

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 // This uses INTERRUPTS to detect a falling edge of the switch input
00004 // However, pressing and releasing the switch can result in spurious falling edges
00005 // which trigger the routine 
00006 
00007 //Declare functions
00008 void sw1FallingEdge();
00009 
00010 //Global Objects
00011 DigitalOut  red_led(D7);
00012 DigitalOut  green_led(D5);
00013 InterruptIn   sw1(D4);
00014 
00015 //Interrupt service routine for a rising edge (press)
00016 void sw1FallingEdge() {
00017     red_led = !red_led;     //Toggle the LED
00018 }
00019 
00020 //Main - only has to initialise and sleep
00021 int main() {
00022     //Initial logging message
00023     puts("START");
00024     red_led   = 0;
00025     green_led = 1;
00026     
00027     //Configure interrupts, wait for first rising edge
00028     sw1.fall(&sw1FallingEdge);
00029     
00030     //Main Polling Loop
00031     while (true) {
00032         
00033         //Put CPU back to sleep
00034         sleep();
00035         
00036         //You can ONLY reach this point if an ISR wakes the CPU
00037         puts("ISR just woke the MPU");
00038         
00039     } //end while
00040 
00041 }