Rising ISR function with flags and call ISR

Dependencies:   mbed

Committer:
fpucher
Date:
Sat Dec 14 07:54:12 2019 +0000
Revision:
0:79f063524a94
copy from simulator

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fpucher 0:79f063524a94 1 #include "mbed.h"
fpucher 0:79f063524a94 2 #define BUTTON1 p14 // push joystick pin
fpucher 0:79f063524a94 3
fpucher 0:79f063524a94 4 InterruptIn button(BUTTON1);
fpucher 0:79f063524a94 5 DigitalOut myled(LED1);
fpucher 0:79f063524a94 6 bool _pressed = false; // definition of the flag _pressed
fpucher 0:79f063524a94 7
fpucher 0:79f063524a94 8 int checkFlag() {
fpucher 0:79f063524a94 9 if( _pressed ) {
fpucher 0:79f063524a94 10 _pressed = false; // delete flag
fpucher 0:79f063524a94 11 return 1;
fpucher 0:79f063524a94 12 }
fpucher 0:79f063524a94 13 return 0;
fpucher 0:79f063524a94 14 }
fpucher 0:79f063524a94 15
fpucher 0:79f063524a94 16 void risingISR() {
fpucher 0:79f063524a94 17 _pressed = true; // set flag _pressed
fpucher 0:79f063524a94 18 }
fpucher 0:79f063524a94 19
fpucher 0:79f063524a94 20 void callISR() { // function callISR instead an ISR
fpucher 0:79f063524a94 21 myled = ! myled; // now you can use ...
fpucher 0:79f063524a94 22 printf("Pressed\n"); // ... printf
fpucher 0:79f063524a94 23 wait_ms(500); // ... wait
fpucher 0:79f063524a94 24 myled = ! myled; // etc.
fpucher 0:79f063524a94 25 }
fpucher 0:79f063524a94 26
fpucher 0:79f063524a94 27
fpucher 0:79f063524a94 28 int main() {
fpucher 0:79f063524a94 29 printf("Hello Interrupt-Flag v0.1\n");
fpucher 0:79f063524a94 30 button.rise(&risingISR); // call ISR in OS2
fpucher 0:79f063524a94 31 //button.rise(callback(&risingISR)); // callback in OS5
fpucher 0:79f063524a94 32
fpucher 0:79f063524a94 33 while(1) {
fpucher 0:79f063524a94 34 if(checkFlag()) // if set then call the function ISR
fpucher 0:79f063524a94 35 callISR();
fpucher 0:79f063524a94 36 wait_ms(10); // only for simulator
fpucher 0:79f063524a94 37 }
fpucher 0:79f063524a94 38 }