interrupt for lab2

Fork of digitalInInterrupt_sample by William Marsh

Committer:
toh2018
Date:
Thu Feb 01 17:20:49 2018 +0000
Revision:
4:795b1612b41d
Parent:
3:05b6a1431a6b
Interrupt_part_for_lab2

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 // -----------------------------------------------------------
WilliamMarshQMUL 1:13e0c1956b54 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
WilliamMarshQMUL 1:13e0c1956b54 9 InterruptIn button(PTD0);
toh2018 4:795b1612b41d 10 InterruptIn s_button(PTD2);
toh2018 4:795b1612b41d 11 DigitalOut led(LED_BLUE);
toh2018 4:795b1612b41d 12 DigitalOut s_led(LED_RED);
toh2018 4:795b1612b41d 13 volatile int time1;
toh2018 4:795b1612b41d 14 volatile int time2;
WilliamMarshQMUL 0:a66a8cb0012c 15
WilliamMarshQMUL 1:13e0c1956b54 16 volatile int pressEvent = 0 ;
toh2018 4:795b1612b41d 17 volatile int s_pressEvent = 0 ;
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
WilliamMarshQMUL 1:13e0c1956b54 21 // Note: bounce may occur
WilliamMarshQMUL 1:13e0c1956b54 22 void buttonCallback(){
toh2018 4:795b1612b41d 23 pressEvent = 1 ;
toh2018 4:795b1612b41d 24 }
toh2018 4:795b1612b41d 25
toh2018 4:795b1612b41d 26 void s_buttonCallback(){
toh2018 4:795b1612b41d 27 s_pressEvent = 1 ;
toh2018 4:795b1612b41d 28
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 */
WilliamMarshQMUL 3:05b6a1431a6b 34 int main() {
toh2018 4:795b1612b41d 35 //S_led=1;
WilliamMarshQMUL 3:05b6a1431a6b 36 button.mode(PullUp); // Ensure button i/p has pull up
WilliamMarshQMUL 3:05b6a1431a6b 37 button.fall(&buttonCallback) ; // Attach function to falling edge
toh2018 4:795b1612b41d 38
toh2018 4:795b1612b41d 39 //second button
toh2018 4:795b1612b41d 40
toh2018 4:795b1612b41d 41 s_button.mode(PullUp);
toh2018 4:795b1612b41d 42 s_button.fall(&s_buttonCallback) ;
toh2018 4:795b1612b41d 43 //s_led=1;
toh2018 4:795b1612b41d 44
WilliamMarshQMUL 0:a66a8cb0012c 45 while(true) {
WilliamMarshQMUL 3:05b6a1431a6b 46 // Toggle the LED every time the button is pressed
toh2018 4:795b1612b41d 47
toh2018 4:795b1612b41d 48 // control button 1 (blue)
toh2018 4:795b1612b41d 49
toh2018 4:795b1612b41d 50 if(time1==0){
toh2018 4:795b1612b41d 51
toh2018 4:795b1612b41d 52 led=!led;
toh2018 4:795b1612b41d 53 }
toh2018 4:795b1612b41d 54
toh2018 4:795b1612b41d 55 if(time2==0){
toh2018 4:795b1612b41d 56 s_led=!s_led;
toh2018 4:795b1612b41d 57
toh2018 4:795b1612b41d 58 }
toh2018 4:795b1612b41d 59
toh2018 4:795b1612b41d 60 //press event_1 coltrol button 1 (Blue)
toh2018 4:795b1612b41d 61
toh2018 4:795b1612b41d 62 if (pressEvent) {
toh2018 4:795b1612b41d 63 time1 = !time1;
toh2018 4:795b1612b41d 64 if(time1==1){
WilliamMarshQMUL 0:a66a8cb0012c 65 led = !led ;
toh2018 4:795b1612b41d 66 }
toh2018 4:795b1612b41d 67 pressEvent = 0 ; // Clear the event variable
toh2018 4:795b1612b41d 68
WilliamMarshQMUL 0:a66a8cb0012c 69 }
toh2018 4:795b1612b41d 70
toh2018 4:795b1612b41d 71 //press even_2 coltrol button 2 (RED)
toh2018 4:795b1612b41d 72 if (s_pressEvent) {
toh2018 4:795b1612b41d 73 time2 = !time2;
toh2018 4:795b1612b41d 74 if(time2==1){
toh2018 4:795b1612b41d 75 s_led = !s_led ;
toh2018 4:795b1612b41d 76 }
toh2018 4:795b1612b41d 77 s_pressEvent = 0 ; // Clear the event variable
toh2018 4:795b1612b41d 78
toh2018 4:795b1612b41d 79 }
toh2018 4:795b1612b41d 80 Thread::wait(500) ;
WilliamMarshQMUL 0:a66a8cb0012c 81 }
WilliamMarshQMUL 0:a66a8cb0012c 82 }