ze
Dependencies: mbed WakeUp OneWire
main.cpp@1:f4088d23f670, 2015-07-21 (annotated)
- Committer:
- BaserK
- Date:
- Tue Jul 21 08:22:46 2015 +0000
- Revision:
- 1:f4088d23f670
- Parent:
- 0:fe71c302ccbb
- Child:
- 2:15f2d7d650bb
MIT license is added
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:f4088d23f670 | 5 | * @license: MIT license |
BaserK | 1:f4088d23f670 | 6 | * |
BaserK | 1:f4088d23f670 | 7 | * Copyright (c) 2015, Baser Kandehir, baser.kandehir@ieee.metu.edu.tr |
BaserK | 1:f4088d23f670 | 8 | * |
BaserK | 1:f4088d23f670 | 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
BaserK | 1:f4088d23f670 | 10 | * of this software and associated documentation files (the "Software"), to deal |
BaserK | 1:f4088d23f670 | 11 | * in the Software without restriction, including without limitation the rights |
BaserK | 1:f4088d23f670 | 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
BaserK | 1:f4088d23f670 | 13 | * copies of the Software, and to permit persons to whom the Software is |
BaserK | 1:f4088d23f670 | 14 | * furnished to do so, subject to the following conditions: |
BaserK | 1:f4088d23f670 | 15 | * |
BaserK | 1:f4088d23f670 | 16 | * The above copyright notice and this permission notice shall be included in |
BaserK | 1:f4088d23f670 | 17 | * all copies or substantial portions of the Software. |
BaserK | 1:f4088d23f670 | 18 | * |
BaserK | 1:f4088d23f670 | 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
BaserK | 1:f4088d23f670 | 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
BaserK | 1:f4088d23f670 | 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
BaserK | 1:f4088d23f670 | 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
BaserK | 1:f4088d23f670 | 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
BaserK | 1:f4088d23f670 | 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
BaserK | 1:f4088d23f670 | 25 | * THE SOFTWARE. |
BaserK | 1:f4088d23f670 | 26 | * |
BaserK | 0:fe71c302ccbb | 27 | * @description of the program: |
BaserK | 0:fe71c302ccbb | 28 | * |
BaserK | 0:fe71c302ccbb | 29 | * This is an example program that shows how to use timer, ticker and interrupts. |
BaserK | 0:fe71c302ccbb | 30 | * It uses LPC1768 board leds and a button to show how interrupts, timers and tickers |
BaserK | 0:fe71c302ccbb | 31 | * are implemented to toggle leds. |
BaserK | 0:fe71c302ccbb | 32 | * A ticker is an interrupt service routine which calls a function at periodic time intervals. |
BaserK | 0:fe71c302ccbb | 33 | * There are two tickers in the program. First ticker calls the toggle_led1 function every 100 ms |
BaserK | 0:fe71c302ccbb | 34 | * and second ticker calls the toggle_led2 function every 500 ms. A button is connected to show |
BaserK | 0:fe71c302ccbb | 35 | * how external interrupts work on the mbed LPC1768. Whenever user pushes the button, it pulls |
BaserK | 0:fe71c302ccbb | 36 | * down the pin 18 to 0V and external interrupt occurs at the falling edge of p18. Whenever an external |
BaserK | 0:fe71c302ccbb | 37 | * interrupt occurs, it calls the led3_debounced function to toggle led3. Also a timer is used to debounce |
BaserK | 0:fe71c302ccbb | 38 | * the button by counting 20 ms. Debounce is required for buttons in order to prevent false readings. |
BaserK | 0:fe71c302ccbb | 39 | * |
BaserK | 0:fe71c302ccbb | 40 | * @connections: Pull up button with 10k resistor is connected to pin 18. |
BaserK | 0:fe71c302ccbb | 41 | * |
BaserK | 0:fe71c302ccbb | 42 | */ |
BaserK | 0:fe71c302ccbb | 43 | |
BaserK | 0:fe71c302ccbb | 44 | #include "mbed.h" |
BaserK | 0:fe71c302ccbb | 45 | |
BaserK | 0:fe71c302ccbb | 46 | Ticker toggler1; // periodic interrupt routines |
BaserK | 0:fe71c302ccbb | 47 | Ticker toggler2; |
BaserK | 0:fe71c302ccbb | 48 | Timer debounce; // define debounce timer |
BaserK | 0:fe71c302ccbb | 49 | InterruptIn button(p18); // hardware interrupt on button - pin 18 |
BaserK | 0:fe71c302ccbb | 50 | |
BaserK | 0:fe71c302ccbb | 51 | /* IMPORTANT NOTE: any digital input can be an interrupt except pin 19 and pin 20 */ |
BaserK | 0:fe71c302ccbb | 52 | |
BaserK | 0:fe71c302ccbb | 53 | /* Interesting way of initialize all the 4 leds on the LPC1768 to an array named leds[]. */ |
BaserK | 0:fe71c302ccbb | 54 | DigitalOut leds[] = {(LED1), (LED2), (LED3), (LED4)}; |
BaserK | 0:fe71c302ccbb | 55 | |
BaserK | 0:fe71c302ccbb | 56 | /* Function Prototypes */ |
BaserK | 0:fe71c302ccbb | 57 | void toggle_led1(); |
BaserK | 0:fe71c302ccbb | 58 | void toggle_led2(); |
BaserK | 0:fe71c302ccbb | 59 | void led3_debounced(); |
BaserK | 0:fe71c302ccbb | 60 | |
BaserK | 0:fe71c302ccbb | 61 | int main() |
BaserK | 0:fe71c302ccbb | 62 | { |
BaserK | 0:fe71c302ccbb | 63 | /* Leds are initially OFF */ |
BaserK | 0:fe71c302ccbb | 64 | for(int i=0; i<4 ;i++) |
BaserK | 0:fe71c302ccbb | 65 | leds[i]=0; |
BaserK | 0:fe71c302ccbb | 66 | |
BaserK | 0:fe71c302ccbb | 67 | /* Start debounce timer */ |
BaserK | 0:fe71c302ccbb | 68 | debounce.start(); |
BaserK | 0:fe71c302ccbb | 69 | |
BaserK | 0:fe71c302ccbb | 70 | /* Attach functions to periodic interrupts */ |
BaserK | 0:fe71c302ccbb | 71 | toggler1.attach(&toggle_led1,0.1); // toggles led1 every 100 ms |
BaserK | 0:fe71c302ccbb | 72 | toggler2.attach(&toggle_led2,0.5); // toggles led2 every 500 ms |
BaserK | 0:fe71c302ccbb | 73 | |
BaserK | 0:fe71c302ccbb | 74 | /* Call toggle_led3 falling edge of the button interrupt */ |
BaserK | 0:fe71c302ccbb | 75 | button.fall(&led3_debounced); |
BaserK | 0:fe71c302ccbb | 76 | while(1) |
BaserK | 0:fe71c302ccbb | 77 | { |
BaserK | 0:fe71c302ccbb | 78 | |
BaserK | 0:fe71c302ccbb | 79 | } |
BaserK | 0:fe71c302ccbb | 80 | } |
BaserK | 0:fe71c302ccbb | 81 | |
BaserK | 0:fe71c302ccbb | 82 | void toggle_led1() {leds[0] = !leds[0];} |
BaserK | 0:fe71c302ccbb | 83 | void toggle_led2() {leds[1] = !leds[1];} |
BaserK | 0:fe71c302ccbb | 84 | |
BaserK | 0:fe71c302ccbb | 85 | // whenever user pushes the button, led3 toggles. |
BaserK | 0:fe71c302ccbb | 86 | // button is debounced by waiting at least 20 ms. |
BaserK | 0:fe71c302ccbb | 87 | void led3_debounced() |
BaserK | 0:fe71c302ccbb | 88 | { |
BaserK | 0:fe71c302ccbb | 89 | if(debounce.read_ms()>20) // only allow toggle after 20 ms |
BaserK | 0:fe71c302ccbb | 90 | { |
BaserK | 0:fe71c302ccbb | 91 | leds[2] = !leds[2]; |
BaserK | 0:fe71c302ccbb | 92 | debounce.reset(); // restart timer after toggle |
BaserK | 0:fe71c302ccbb | 93 | } |
BaserK | 0:fe71c302ccbb | 94 | } |