Perform an LED toggle by interrupt over an existing sequence. PRints out to LCD upon occurence of interrupt.
main.cpp@0:d2238a225cb8, 2020-07-31 (annotated)
- Committer:
- saltire78
- Date:
- Fri Jul 31 12:45:22 2020 +0000
- Revision:
- 0:d2238a225cb8
online posting
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
saltire78 | 0:d2238a225cb8 | 1 | #include"mbed.h" // mbed header |
saltire78 | 0:d2238a225cb8 | 2 | #include "C12832.h" // lcd header |
saltire78 | 0:d2238a225cb8 | 3 | |
saltire78 | 0:d2238a225cb8 | 4 | InterruptIn joystickcenter(p14); // initialise interrupt pin on pcb |
saltire78 | 0:d2238a225cb8 | 5 | InterruptIn button(p9); // initialise external interrupt pin |
saltire78 | 0:d2238a225cb8 | 6 | DigitalOut led(LED1); // initialise interrupt led |
saltire78 | 0:d2238a225cb8 | 7 | DigitalOut flash(LED4); // initialise main program led |
saltire78 | 0:d2238a225cb8 | 8 | C12832 lcd(p5, p7, p6, p8, p11); // initialise lcd |
saltire78 | 0:d2238a225cb8 | 9 | Timer debounce; // create debouce timer variable |
saltire78 | 0:d2238a225cb8 | 10 | int state; // create interrupt activation variable |
saltire78 | 0:d2238a225cb8 | 11 | int i=0; // create interrupt activation iterable variable |
saltire78 | 0:d2238a225cb8 | 12 | |
saltire78 | 0:d2238a225cb8 | 13 | void flip(){ |
saltire78 | 0:d2238a225cb8 | 14 | debounce.start(); // start the timer |
saltire78 | 0:d2238a225cb8 | 15 | led=!led; // toggles the led when the joystick button is pressed. |
saltire78 | 0:d2238a225cb8 | 16 | state= 1; // changes global variable when interrupt activated |
saltire78 | 0:d2238a225cb8 | 17 | if (debounce.read_ms() >= 500){ // determine the timer limits - greater than or equal incase the exact count was missed by timer |
saltire78 | 0:d2238a225cb8 | 18 | debounce.reset(); // reset the timer for the next use |
saltire78 | 0:d2238a225cb8 | 19 | } |
saltire78 | 0:d2238a225cb8 | 20 | |
saltire78 | 0:d2238a225cb8 | 21 | } |
saltire78 | 0:d2238a225cb8 | 22 | |
saltire78 | 0:d2238a225cb8 | 23 | int main(){ |
saltire78 | 0:d2238a225cb8 | 24 | joystickcenter.rise(&flip); //attach the function address to the rising edge |
saltire78 | 0:d2238a225cb8 | 25 | button.mode(PullUp); // With this, no external pullup resistor needed |
saltire78 | 0:d2238a225cb8 | 26 | button.rise(&flip); // attach the function address to the rising edge |
saltire78 | 0:d2238a225cb8 | 27 | |
saltire78 | 0:d2238a225cb8 | 28 | while(1){ //continuous run |
saltire78 | 0:d2238a225cb8 | 29 | if (state == 1){ // if interrupt activated |
saltire78 | 0:d2238a225cb8 | 30 | i++; // iterate the variable i |
saltire78 | 0:d2238a225cb8 | 31 | lcd.locate(0,0); // home the lcd |
saltire78 | 0:d2238a225cb8 | 32 | lcd.printf("Interrupt Activated x %d", i); // add lcd text |
saltire78 | 0:d2238a225cb8 | 33 | state = 0; // reset the state variable from the interrupt |
saltire78 | 0:d2238a225cb8 | 34 | } |
saltire78 | 0:d2238a225cb8 | 35 | else // normal operation |
saltire78 | 0:d2238a225cb8 | 36 | { |
saltire78 | 0:d2238a225cb8 | 37 | flash=!flash; // turns LED4 on ifoff, off if on |
saltire78 | 0:d2238a225cb8 | 38 | wait(0.25); // the instruction to wait for a quarter-second |
saltire78 | 0:d2238a225cb8 | 39 | } |
saltire78 | 0:d2238a225cb8 | 40 | } |
saltire78 | 0:d2238a225cb8 | 41 | } |