A Ton (Timer On) is used to provide an \'on delay\' system so as to avoid using wait()
Dependents: NEWlcd_menu_v2 Garage_Control
example1.h@3:123a1b30970a, 2011-03-04 (annotated)
- Committer:
- AjK
- Date:
- Fri Mar 04 14:15:47 2011 +0000
- Revision:
- 3:123a1b30970a
- Parent:
- 0:748acff4e3c8
1.3 See ChangeLog.h
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AjK | 0:748acff4e3c8 | 1 | /* |
AjK | 0:748acff4e3c8 | 2 | * This example DOES NOT use a Ton timer. It shows how |
AjK | 0:748acff4e3c8 | 3 | * beginners often "abuse" wait() to wait for a period |
AjK | 0:748acff4e3c8 | 4 | * of time before actioning something. As shown here we |
AjK | 0:748acff4e3c8 | 5 | * delay the switching on an LED after an input is asserted. |
AjK | 0:748acff4e3c8 | 6 | * |
AjK | 0:748acff4e3c8 | 7 | * The problem is that they "interfer" with each other. Using |
AjK | 0:748acff4e3c8 | 8 | * wait() to wait around causes the entire system to hold up |
AjK | 0:748acff4e3c8 | 9 | * waiting some period of time. As you can see, this holds up |
AjK | 0:748acff4e3c8 | 10 | * the main() systems while(1) loop preventing any other task |
AjK | 0:748acff4e3c8 | 11 | * or process from being performed. |
AjK | 0:748acff4e3c8 | 12 | * |
AjK | 0:748acff4e3c8 | 13 | * THIS IS AN EXAMPLE OF HOW NOT TO DO IT! |
AjK | 0:748acff4e3c8 | 14 | * |
AjK | 0:748acff4e3c8 | 15 | * To see how the Ton object can fix this issue see example2.h |
AjK | 0:748acff4e3c8 | 16 | */ |
AjK | 0:748acff4e3c8 | 17 | #include "mbed.h" |
AjK | 0:748acff4e3c8 | 18 | |
AjK | 0:748acff4e3c8 | 19 | DigitalIn ip1(p19); |
AjK | 0:748acff4e3c8 | 20 | DigitalIn ip2(p20); |
AjK | 0:748acff4e3c8 | 21 | |
AjK | 0:748acff4e3c8 | 22 | DigitalOut led1(LED1); |
AjK | 0:748acff4e3c8 | 23 | DigitalOut led2(LED2); |
AjK | 0:748acff4e3c8 | 24 | |
AjK | 0:748acff4e3c8 | 25 | void f1(void) { |
AjK | 0:748acff4e3c8 | 26 | if (ip1) { |
AjK | 0:748acff4e3c8 | 27 | wait(2); |
AjK | 0:748acff4e3c8 | 28 | led1 = 1; |
AjK | 0:748acff4e3c8 | 29 | } |
AjK | 0:748acff4e3c8 | 30 | else { |
AjK | 0:748acff4e3c8 | 31 | led1 = 0; |
AjK | 0:748acff4e3c8 | 32 | } |
AjK | 0:748acff4e3c8 | 33 | } |
AjK | 0:748acff4e3c8 | 34 | |
AjK | 0:748acff4e3c8 | 35 | void f2(void) { |
AjK | 0:748acff4e3c8 | 36 | if (ip2) { |
AjK | 0:748acff4e3c8 | 37 | wait(2); |
AjK | 0:748acff4e3c8 | 38 | led2 = 1; |
AjK | 0:748acff4e3c8 | 39 | } |
AjK | 0:748acff4e3c8 | 40 | else { |
AjK | 0:748acff4e3c8 | 41 | led2 = 0; |
AjK | 0:748acff4e3c8 | 42 | } |
AjK | 0:748acff4e3c8 | 43 | } |
AjK | 0:748acff4e3c8 | 44 | |
AjK | 0:748acff4e3c8 | 45 | int main() { |
AjK | 0:748acff4e3c8 | 46 | while(1) { |
AjK | 0:748acff4e3c8 | 47 | f1(); // Do process f1() |
AjK | 0:748acff4e3c8 | 48 | f2(); // Do process f2() |
AjK | 0:748acff4e3c8 | 49 | } |
AjK | 0:748acff4e3c8 | 50 | } |