Interrupt - modified version

Fork of digitalInInterrupt_sample by William Marsh

Committer:
novinfard
Date:
Thu Feb 01 16:42:49 2018 +0000
Revision:
4:f2c84efa572d
Parent:
3:05b6a1431a6b
Finalise Project

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WilliamMarshQMUL 0:a66a8cb0012c 1 #include "mbed.h"
WilliamMarshQMUL 0:a66a8cb0012c 2
WilliamMarshQMUL 1:13e0c1956b54 3 // Labs 2: Example program for using an interrupt (or callback)
WilliamMarshQMUL 1:13e0c1956b54 4 // -----------------------------------------------------------
novinfard 4:f2c84efa572d 5 // A callback function (corresponding to an ISR) is called when a button
WilliamMarshQMUL 1:13e0c1956b54 6 // is pressed
WilliamMarshQMUL 1:13e0c1956b54 7 // The callback uses a shared variable to signal another thread
WilliamMarshQMUL 0:a66a8cb0012c 8
novinfard 4:f2c84efa572d 9 InterruptIn button_blue(PTD0);
novinfard 4:f2c84efa572d 10 DigitalOut led_blue(LED_BLUE);
WilliamMarshQMUL 0:a66a8cb0012c 11
novinfard 4:f2c84efa572d 12 InterruptIn button_red(PTD4);
novinfard 4:f2c84efa572d 13 DigitalOut led_red(LED_RED);
novinfard 4:f2c84efa572d 14
novinfard 4:f2c84efa572d 15 volatile int pressEvent_blue = 0;
novinfard 4:f2c84efa572d 16 volatile int pressEvent_red = 0;
novinfard 4:f2c84efa572d 17
WilliamMarshQMUL 0:a66a8cb0012c 18
WilliamMarshQMUL 3:05b6a1431a6b 19 // This function is invoked when then interrupt occurs
WilliamMarshQMUL 3:05b6a1431a6b 20 // Signal that the button has been pressed
novinfard 4:f2c84efa572d 21 // Note: bounce may occur
novinfard 4:f2c84efa572d 22 void buttonCallback_blue()
novinfard 4:f2c84efa572d 23 {
novinfard 4:f2c84efa572d 24 pressEvent_blue = 1 ;
novinfard 4:f2c84efa572d 25 }
novinfard 4:f2c84efa572d 26 void buttonCallback_red()
novinfard 4:f2c84efa572d 27 {
novinfard 4:f2c84efa572d 28 pressEvent_red = 1 ;
WilliamMarshQMUL 0:a66a8cb0012c 29 }
WilliamMarshQMUL 0:a66a8cb0012c 30
WilliamMarshQMUL 3:05b6a1431a6b 31 /* ---- Main function (default thread) ----
WilliamMarshQMUL 3:05b6a1431a6b 32 Note that if this thread completes, nothing else works
WilliamMarshQMUL 3:05b6a1431a6b 33 */
novinfard 4:f2c84efa572d 34 int main()
novinfard 4:f2c84efa572d 35 {
novinfard 4:f2c84efa572d 36 led_blue = 0;
novinfard 4:f2c84efa572d 37 led_red = 0;
novinfard 4:f2c84efa572d 38
novinfard 4:f2c84efa572d 39 button_blue.mode(PullUp); // Ensure button i/p has pull up
novinfard 4:f2c84efa572d 40 button_blue.fall(&buttonCallback_blue) ; // Attach function to falling edge
novinfard 4:f2c84efa572d 41
novinfard 4:f2c84efa572d 42 button_red.mode(PullUp);
novinfard 4:f2c84efa572d 43 button_red.fall(&buttonCallback_red);
novinfard 4:f2c84efa572d 44
novinfard 4:f2c84efa572d 45 // active mode of each LED
novinfard 4:f2c84efa572d 46 int active_blue = 1;
novinfard 4:f2c84efa572d 47 int active_red = 1;
novinfard 4:f2c84efa572d 48
novinfard 4:f2c84efa572d 49 // cycle counter of each LED
novinfard 4:f2c84efa572d 50 int cycle_blue = 1;
novinfard 4:f2c84efa572d 51 int cycle_red = 1;
WilliamMarshQMUL 3:05b6a1431a6b 52
WilliamMarshQMUL 0:a66a8cb0012c 53 while(true) {
novinfard 4:f2c84efa572d 54 // blue led
novinfard 4:f2c84efa572d 55 if (pressEvent_blue) {
novinfard 4:f2c84efa572d 56 active_blue = !active_blue;
novinfard 4:f2c84efa572d 57 pressEvent_blue = 0 ; // Clear the event variable
novinfard 4:f2c84efa572d 58 cycle_blue = 1;
novinfard 4:f2c84efa572d 59 }
novinfard 4:f2c84efa572d 60 if(active_blue) {
novinfard 4:f2c84efa572d 61 if(cycle_blue == 5) {
novinfard 4:f2c84efa572d 62 led_blue = !led_blue ;
novinfard 4:f2c84efa572d 63 cycle_blue = 1;
novinfard 4:f2c84efa572d 64 } else {
novinfard 4:f2c84efa572d 65 cycle_blue = cycle_blue+1;
novinfard 4:f2c84efa572d 66 }
WilliamMarshQMUL 0:a66a8cb0012c 67 }
novinfard 4:f2c84efa572d 68
novinfard 4:f2c84efa572d 69 // red led
novinfard 4:f2c84efa572d 70 if (pressEvent_red) {
novinfard 4:f2c84efa572d 71 active_red = !active_red;
novinfard 4:f2c84efa572d 72 pressEvent_red = 0 ; // Clear the event variable
novinfard 4:f2c84efa572d 73 cycle_red = 1;
novinfard 4:f2c84efa572d 74 }
novinfard 4:f2c84efa572d 75 if(active_red) {
novinfard 4:f2c84efa572d 76 if(cycle_red == 5) {
novinfard 4:f2c84efa572d 77 led_red = !led_red ;
novinfard 4:f2c84efa572d 78 cycle_red = 1;
novinfard 4:f2c84efa572d 79 } else {
novinfard 4:f2c84efa572d 80 cycle_red = cycle_red+1;
novinfard 4:f2c84efa572d 81 }
novinfard 4:f2c84efa572d 82 }
novinfard 4:f2c84efa572d 83
WilliamMarshQMUL 0:a66a8cb0012c 84 Thread::wait(100) ;
WilliamMarshQMUL 0:a66a8cb0012c 85 }
WilliamMarshQMUL 0:a66a8cb0012c 86 }