lab 2 interrupts

Committer:
anair12345
Date:
Thu Feb 07 09:56:08 2019 +0000
Revision:
5:63fd9de15f27
Parent:
4:728667196916
lab 2 part 2 full version

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);
anair12345 5:63fd9de15f27 10 InterruptIn button1(PTD2);
anair12345 5:63fd9de15f27 11 DigitalOut led(LED_BLUE);
anair12345 5:63fd9de15f27 12 DigitalOut led1(LED_RED);
WilliamMarshQMUL 0:a66a8cb0012c 13
WilliamMarshQMUL 1:13e0c1956b54 14 volatile int pressEvent = 0 ;
anair12345 5:63fd9de15f27 15 volatile int pressEvent1 = 0 ;
anair12345 5:63fd9de15f27 16
anair12345 5:63fd9de15f27 17 bool ledflashing = true;
anair12345 5:63fd9de15f27 18 bool led1flashing = true;
anair12345 5:63fd9de15f27 19
anair12345 5:63fd9de15f27 20 enum blueledState { Flashing, NotFlashing };
anair12345 5:63fd9de15f27 21 enum redledState { Flashing1, NotFlashing1 };
WilliamMarshQMUL 0:a66a8cb0012c 22
WilliamMarshQMUL 3:05b6a1431a6b 23 // This function is invoked when then interrupt occurs
WilliamMarshQMUL 3:05b6a1431a6b 24 // Signal that the button has been pressed
WilliamMarshQMUL 1:13e0c1956b54 25 // Note: bounce may occur
WilliamMarshQMUL 1:13e0c1956b54 26 void buttonCallback(){
WilliamMarshQMUL 1:13e0c1956b54 27 pressEvent = 1 ;
WilliamMarshQMUL 0:a66a8cb0012c 28 }
WilliamMarshQMUL 0:a66a8cb0012c 29
anair12345 5:63fd9de15f27 30 void buttonCallback1(){
anair12345 5:63fd9de15f27 31 pressEvent1 = 1 ;
anair12345 5:63fd9de15f27 32 }
anair12345 5:63fd9de15f27 33
WilliamMarshQMUL 3:05b6a1431a6b 34 /* ---- Main function (default thread) ----
WilliamMarshQMUL 3:05b6a1431a6b 35 Note that if this thread completes, nothing else works
WilliamMarshQMUL 3:05b6a1431a6b 36 */
WilliamMarshQMUL 3:05b6a1431a6b 37 int main() {
WilliamMarshQMUL 3:05b6a1431a6b 38 button.mode(PullUp); // Ensure button i/p has pull up
WilliamMarshQMUL 3:05b6a1431a6b 39 button.fall(&buttonCallback) ; // Attach function to falling edge
anair12345 5:63fd9de15f27 40
anair12345 5:63fd9de15f27 41 button1.mode(PullUp); // Ensure button i/p has pull up
anair12345 5:63fd9de15f27 42 button1.fall(&buttonCallback1) ; // Attach function to falling edge
anair12345 5:63fd9de15f27 43
anair12345 5:63fd9de15f27 44 led = 1;
anair12345 5:63fd9de15f27 45 led1 = 1;
WilliamMarshQMUL 3:05b6a1431a6b 46
anair12345 5:63fd9de15f27 47 blueledState State1 = Flashing;
anair12345 5:63fd9de15f27 48 redledState State2 = Flashing1;
anair12345 5:63fd9de15f27 49 int count = 5;
anair12345 5:63fd9de15f27 50 int count1 = 5;
WilliamMarshQMUL 0:a66a8cb0012c 51 while(true) {
anair12345 5:63fd9de15f27 52 wait(0.1);
anair12345 5:63fd9de15f27 53 switch(State1){
anair12345 5:63fd9de15f27 54 case Flashing:
anair12345 5:63fd9de15f27 55 if(pressEvent){
anair12345 5:63fd9de15f27 56 State1 = NotFlashing;
anair12345 5:63fd9de15f27 57 pressEvent = 0;
anair12345 5:63fd9de15f27 58 }
anair12345 5:63fd9de15f27 59 else{
anair12345 5:63fd9de15f27 60 if(count!=0){
anair12345 5:63fd9de15f27 61 count-- ;
anair12345 5:63fd9de15f27 62 }
anair12345 5:63fd9de15f27 63 else if(count == 0){
anair12345 5:63fd9de15f27 64 led = !led ;
anair12345 5:63fd9de15f27 65 count = 5;
anair12345 5:63fd9de15f27 66 }
anair12345 5:63fd9de15f27 67 }
anair12345 5:63fd9de15f27 68 case NotFlashing:
anair12345 5:63fd9de15f27 69 if(pressEvent){
anair12345 5:63fd9de15f27 70 State1 = Flashing;
anair12345 5:63fd9de15f27 71 count = 5;
anair12345 5:63fd9de15f27 72 pressEvent = 0;
anair12345 5:63fd9de15f27 73 }
anair12345 5:63fd9de15f27 74 }
anair12345 5:63fd9de15f27 75 switch(State2){
anair12345 5:63fd9de15f27 76 case Flashing1:
anair12345 5:63fd9de15f27 77 if(pressEvent1){
anair12345 5:63fd9de15f27 78 State2 = NotFlashing1;
anair12345 5:63fd9de15f27 79 pressEvent1 = 0;
anair12345 5:63fd9de15f27 80 }
anair12345 5:63fd9de15f27 81 else{
anair12345 5:63fd9de15f27 82 if(count1!=0){
anair12345 5:63fd9de15f27 83 count1-- ;
anair12345 5:63fd9de15f27 84 }
anair12345 5:63fd9de15f27 85 else if(count1 == 0){
anair12345 5:63fd9de15f27 86 led1 = !led1 ;
anair12345 5:63fd9de15f27 87 count1 = 5;
anair12345 5:63fd9de15f27 88 }
anair12345 5:63fd9de15f27 89 }
anair12345 5:63fd9de15f27 90 break;
anair12345 5:63fd9de15f27 91 case NotFlashing1:
anair12345 5:63fd9de15f27 92 if(pressEvent1){
anair12345 5:63fd9de15f27 93 State2 = Flashing1;
anair12345 5:63fd9de15f27 94 count1 = 5;
anair12345 5:63fd9de15f27 95 pressEvent1 = 0;
anair12345 5:63fd9de15f27 96 }
anair12345 5:63fd9de15f27 97 break;
WilliamMarshQMUL 0:a66a8cb0012c 98 }
anair12345 5:63fd9de15f27 99
WilliamMarshQMUL 0:a66a8cb0012c 100 }
WilliamMarshQMUL 0:a66a8cb0012c 101 }