This is an example program that shows how to use timer, ticker and interrupts. It uses LPC1768 board leds and a button to show how interrupts, timers and tickers are implemented to toggle leds.

Dependencies:   mbed

Committer:
BaserK
Date:
Thu Jul 09 09:16:31 2015 +0000
Revision:
0:fe71c302ccbb
Child:
1:f4088d23f670
First commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BaserK 0:fe71c302ccbb 1 /* Interrupt, timer and ticker example on LPC1768
BaserK 0:fe71c302ccbb 2 *
BaserK 0:fe71c302ccbb 3 * @author: Baser Kandehir
BaserK 0:fe71c302ccbb 4 * @date: July 9, 2015
BaserK 0:fe71c302ccbb 5 * @license: Use this code however you'd like
BaserK 0:fe71c302ccbb 6 *
BaserK 0:fe71c302ccbb 7 * @description of the program:
BaserK 0:fe71c302ccbb 8 *
BaserK 0:fe71c302ccbb 9 * This is an example program that shows how to use timer, ticker and interrupts.
BaserK 0:fe71c302ccbb 10 * It uses LPC1768 board leds and a button to show how interrupts, timers and tickers
BaserK 0:fe71c302ccbb 11 * are implemented to toggle leds.
BaserK 0:fe71c302ccbb 12 * A ticker is an interrupt service routine which calls a function at periodic time intervals.
BaserK 0:fe71c302ccbb 13 * There are two tickers in the program. First ticker calls the toggle_led1 function every 100 ms
BaserK 0:fe71c302ccbb 14 * and second ticker calls the toggle_led2 function every 500 ms. A button is connected to show
BaserK 0:fe71c302ccbb 15 * how external interrupts work on the mbed LPC1768. Whenever user pushes the button, it pulls
BaserK 0:fe71c302ccbb 16 * down the pin 18 to 0V and external interrupt occurs at the falling edge of p18. Whenever an external
BaserK 0:fe71c302ccbb 17 * interrupt occurs, it calls the led3_debounced function to toggle led3. Also a timer is used to debounce
BaserK 0:fe71c302ccbb 18 * the button by counting 20 ms. Debounce is required for buttons in order to prevent false readings.
BaserK 0:fe71c302ccbb 19 *
BaserK 0:fe71c302ccbb 20 * @connections: Pull up button with 10k resistor is connected to pin 18.
BaserK 0:fe71c302ccbb 21 *
BaserK 0:fe71c302ccbb 22 */
BaserK 0:fe71c302ccbb 23
BaserK 0:fe71c302ccbb 24 #include "mbed.h"
BaserK 0:fe71c302ccbb 25
BaserK 0:fe71c302ccbb 26 Ticker toggler1; // periodic interrupt routines
BaserK 0:fe71c302ccbb 27 Ticker toggler2;
BaserK 0:fe71c302ccbb 28 Timer debounce; // define debounce timer
BaserK 0:fe71c302ccbb 29 InterruptIn button(p18); // hardware interrupt on button - pin 18
BaserK 0:fe71c302ccbb 30
BaserK 0:fe71c302ccbb 31 /* IMPORTANT NOTE: any digital input can be an interrupt except pin 19 and pin 20 */
BaserK 0:fe71c302ccbb 32
BaserK 0:fe71c302ccbb 33 /* Interesting way of initialize all the 4 leds on the LPC1768 to an array named leds[]. */
BaserK 0:fe71c302ccbb 34 DigitalOut leds[] = {(LED1), (LED2), (LED3), (LED4)};
BaserK 0:fe71c302ccbb 35
BaserK 0:fe71c302ccbb 36 /* Function Prototypes */
BaserK 0:fe71c302ccbb 37 void toggle_led1();
BaserK 0:fe71c302ccbb 38 void toggle_led2();
BaserK 0:fe71c302ccbb 39 void led3_debounced();
BaserK 0:fe71c302ccbb 40
BaserK 0:fe71c302ccbb 41 int main()
BaserK 0:fe71c302ccbb 42 {
BaserK 0:fe71c302ccbb 43 /* Leds are initially OFF */
BaserK 0:fe71c302ccbb 44 for(int i=0; i<4 ;i++)
BaserK 0:fe71c302ccbb 45 leds[i]=0;
BaserK 0:fe71c302ccbb 46
BaserK 0:fe71c302ccbb 47 /* Start debounce timer */
BaserK 0:fe71c302ccbb 48 debounce.start();
BaserK 0:fe71c302ccbb 49
BaserK 0:fe71c302ccbb 50 /* Attach functions to periodic interrupts */
BaserK 0:fe71c302ccbb 51 toggler1.attach(&toggle_led1,0.1); // toggles led1 every 100 ms
BaserK 0:fe71c302ccbb 52 toggler2.attach(&toggle_led2,0.5); // toggles led2 every 500 ms
BaserK 0:fe71c302ccbb 53
BaserK 0:fe71c302ccbb 54 /* Call toggle_led3 falling edge of the button interrupt */
BaserK 0:fe71c302ccbb 55 button.fall(&led3_debounced);
BaserK 0:fe71c302ccbb 56 while(1)
BaserK 0:fe71c302ccbb 57 {
BaserK 0:fe71c302ccbb 58
BaserK 0:fe71c302ccbb 59 }
BaserK 0:fe71c302ccbb 60 }
BaserK 0:fe71c302ccbb 61
BaserK 0:fe71c302ccbb 62 void toggle_led1() {leds[0] = !leds[0];}
BaserK 0:fe71c302ccbb 63 void toggle_led2() {leds[1] = !leds[1];}
BaserK 0:fe71c302ccbb 64
BaserK 0:fe71c302ccbb 65 // whenever user pushes the button, led3 toggles.
BaserK 0:fe71c302ccbb 66 // button is debounced by waiting at least 20 ms.
BaserK 0:fe71c302ccbb 67 void led3_debounced()
BaserK 0:fe71c302ccbb 68 {
BaserK 0:fe71c302ccbb 69 if(debounce.read_ms()>20) // only allow toggle after 20 ms
BaserK 0:fe71c302ccbb 70 {
BaserK 0:fe71c302ccbb 71 leds[2] = !leds[2];
BaserK 0:fe71c302ccbb 72 debounce.reset(); // restart timer after toggle
BaserK 0:fe71c302ccbb 73 }
BaserK 0:fe71c302ccbb 74 }