Final

Committer:
ex19397
Date:
Thu Feb 13 07:26:10 2020 +0000
Revision:
6:8dc45846a8db
Parent:
5:86742cfaf4e4
Final

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
ex19397 6:8dc45846a8db 9 InterruptIn button(PTD0);// Pin must be on ports A or D
ex19397 6:8dc45846a8db 10 InterruptIn button1(PTD5);
ex19397 6:8dc45846a8db 11 DigitalOut led(LED_BLUE);
ex19397 6:8dc45846a8db 12 DigitalOut led1(LED_RED);
WilliamMarshQMUL 0:a66a8cb0012c 13
WilliamMarshQMUL 1:13e0c1956b54 14 volatile int pressEvent = 0 ;
ex19397 6:8dc45846a8db 15 volatile int pressEvent1 = 0 ;
WilliamMarshQMUL 0:a66a8cb0012c 16
WilliamMarshQMUL 3:05b6a1431a6b 17 // This function is invoked when then interrupt occurs
WilliamMarshQMUL 3:05b6a1431a6b 18 // Signal that the button has been pressed
WilliamMarshQMUL 1:13e0c1956b54 19 // Note: bounce may occur
WilliamMarshQMUL 1:13e0c1956b54 20 void buttonCallback(){
ex19397 6:8dc45846a8db 21 pressEvent = !pressEvent;
ex19397 6:8dc45846a8db 22 }
ex19397 6:8dc45846a8db 23 void buttonCallback1(){
ex19397 6:8dc45846a8db 24 pressEvent1 = !pressEvent1 ;
WilliamMarshQMUL 0:a66a8cb0012c 25 }
WilliamMarshQMUL 0:a66a8cb0012c 26
WilliamMarshQMUL 3:05b6a1431a6b 27 /* ---- Main function (default thread) ----
WilliamMarshQMUL 3:05b6a1431a6b 28 Note that if this thread completes, nothing else works
WilliamMarshQMUL 3:05b6a1431a6b 29 */
WilliamMarshQMUL 3:05b6a1431a6b 30 int main() {
WilliamMarshQMUL 3:05b6a1431a6b 31 button.mode(PullUp); // Ensure button i/p has pull up
WilliamMarshQMUL 3:05b6a1431a6b 32 button.fall(&buttonCallback) ; // Attach function to falling edge
ex19397 6:8dc45846a8db 33 button1.mode(PullUp); // Ensure button i/p has pull up
ex19397 6:8dc45846a8db 34 button1.fall(&buttonCallback1) ; // Attach function to falling edge
ex19397 6:8dc45846a8db 35
WilliamMarshQMUL 0:a66a8cb0012c 36 while(true) {
WilliamMarshQMUL 3:05b6a1431a6b 37 // Toggle the LED every time the button is pressed
ex19397 6:8dc45846a8db 38 if (pressEvent) {
WilliamMarshQMUL 0:a66a8cb0012c 39 led = !led ;
WilliamMarshQMUL 0:a66a8cb0012c 40 }
ex19397 6:8dc45846a8db 41 if (pressEvent1) {
ex19397 6:8dc45846a8db 42 led1 = !led1 ;
ex19397 6:8dc45846a8db 43 }
ex19397 6:8dc45846a8db 44 ThisThread::sleep_for(500) ; // delay for 100ms
WilliamMarshQMUL 0:a66a8cb0012c 45 }
WilliamMarshQMUL 0:a66a8cb0012c 46 }