Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:0853f795e63b, 2020-06-20 (annotated)
- Committer:
- ciaranom
- Date:
- Sat Jun 20 11:16:58 2020 +0000
- Revision:
- 0:0853f795e63b
- Child:
- 1:a97fbfb0d587
Lab exercise 2.3 Interrupt;
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| ciaranom | 0:0853f795e63b | 1 | #include "mbed.h" | 
| ciaranom | 0:0853f795e63b | 2 | #include "C12832.h" | 
| ciaranom | 0:0853f795e63b | 3 | |
| ciaranom | 0:0853f795e63b | 4 | InterruptIn joystickcenter(p14); | 
| ciaranom | 0:0853f795e63b | 5 | InterruptIn button(p9); | 
| ciaranom | 0:0853f795e63b | 6 | DigitalOut led(LED1); | 
| ciaranom | 0:0853f795e63b | 7 | DigitalOut flash(LED4); | 
| ciaranom | 0:0853f795e63b | 8 | C12832 lcd(p5, p7, p6, p8, p11); | 
| ciaranom | 0:0853f795e63b | 9 | |
| ciaranom | 0:0853f795e63b | 10 | |
| ciaranom | 0:0853f795e63b | 11 | DigitalIn fire(p14); | 
| ciaranom | 0:0853f795e63b | 12 | PwmOut spkr(p26); | 
| ciaranom | 0:0853f795e63b | 13 | AnalogIn pot1(p19); | 
| ciaranom | 0:0853f795e63b | 14 | |
| ciaranom | 0:0853f795e63b | 15 | |
| ciaranom | 0:0853f795e63b | 16 | |
| ciaranom | 0:0853f795e63b | 17 | int x = 0; | 
| ciaranom | 0:0853f795e63b | 18 | Timer debounce; | 
| ciaranom | 0:0853f795e63b | 19 | |
| ciaranom | 0:0853f795e63b | 20 | void flip() { | 
| ciaranom | 0:0853f795e63b | 21 | led = !led; // toggles the led when the joystick button is pressed. | 
| ciaranom | 0:0853f795e63b | 22 | x = 1; | 
| ciaranom | 0:0853f795e63b | 23 | lcd.printf("debounce = "+debounce.read_ms()); | 
| ciaranom | 0:0853f795e63b | 24 | if(debounce.read_ms()==1) | 
| ciaranom | 0:0853f795e63b | 25 | { | 
| ciaranom | 0:0853f795e63b | 26 | lcd.printf("ms 150"); | 
| ciaranom | 0:0853f795e63b | 27 | //assignment | 
| ciaranom | 0:0853f795e63b | 28 | } | 
| ciaranom | 0:0853f795e63b | 29 | debounce.reset(); | 
| ciaranom | 0:0853f795e63b | 30 | } | 
| ciaranom | 0:0853f795e63b | 31 | int main() { | 
| ciaranom | 0:0853f795e63b | 32 | |
| ciaranom | 0:0853f795e63b | 33 | joystickcenter.rise(&flip); // attach the function address to the rising edge | 
| ciaranom | 0:0853f795e63b | 34 | button.mode(PullUp); // With this, no external pullup resistor needed | 
| ciaranom | 0:0853f795e63b | 35 | button.rise(&flip); // attach the function address to the rising edge | 
| ciaranom | 0:0853f795e63b | 36 | |
| ciaranom | 0:0853f795e63b | 37 | while(1) { // wait around, interrupts will interrupt this! | 
| ciaranom | 0:0853f795e63b | 38 | flash = !flash; // turns LED4 on if off, off if on | 
| ciaranom | 0:0853f795e63b | 39 | //wait(0.25); // the instruction to wait for a quarter-second | 
| ciaranom | 0:0853f795e63b | 40 | if(x==1) // check loop | 
| ciaranom | 0:0853f795e63b | 41 | { | 
| ciaranom | 0:0853f795e63b | 42 | //lcd.printf("Variable set"); | 
| ciaranom | 0:0853f795e63b | 43 | x = 0; | 
| ciaranom | 0:0853f795e63b | 44 | } | 
| ciaranom | 0:0853f795e63b | 45 | |
| ciaranom | 0:0853f795e63b | 46 | for (float i=2000.0; i<1000.0; i+=100) { | 
| ciaranom | 0:0853f795e63b | 47 | spkr.period(1.0/i); | 
| ciaranom | 0:0853f795e63b | 48 | spkr=0.5; | 
| ciaranom | 0:0853f795e63b | 49 | wait(0.1); | 
| ciaranom | 0:0853f795e63b | 50 | } | 
| ciaranom | 0:0853f795e63b | 51 | spkr=0.0; | 
| ciaranom | 0:0853f795e63b | 52 | while(pot1.read() < 0.0000005) {} // this uses the pot to control the program | 
| ciaranom | 0:0853f795e63b | 53 | |
| ciaranom | 0:0853f795e63b | 54 | |
| ciaranom | 0:0853f795e63b | 55 | |
| ciaranom | 0:0853f795e63b | 56 | |
| ciaranom | 0:0853f795e63b | 57 | } | 
| ciaranom | 0:0853f795e63b | 58 | } | 
| ciaranom | 0:0853f795e63b | 59 | |
| ciaranom | 0:0853f795e63b | 60 | |
| ciaranom | 0:0853f795e63b | 61 | //What's needed is a global variable with its own name, for example: | 
| ciaranom | 0:0853f795e63b | 62 | //int x; | 
| ciaranom | 0:0853f795e63b | 63 | |
| ciaranom | 0:0853f795e63b | 64 | //Now, you set x to 1 inside the flip() function. In main(), inside the infinite loop starting with while (1) { , you check whether x ==1 and if it is, you print to the LCD and set x to 0. | 
| ciaranom | 0:0853f795e63b | 65 | |
| ciaranom | 0:0853f795e63b | 66 | //(Be very careful about this. x=1 sets x to 1. x==1 compares x to 1.) |