A Ton (Timer On) is used to provide an \'on delay\' system so as to avoid using wait()

Dependents:   NEWlcd_menu_v2 Garage_Control

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?

UserRevisionLine numberNew contents of line
AjK 0:748acff4e3c8 1 /*
AjK 0:748acff4e3c8 2 * This example shows how to "delay" an action yet still
AjK 0:748acff4e3c8 3 * have the system response to other tasks or processes.
AjK 0:748acff4e3c8 4 *
AjK 0:748acff4e3c8 5 * Note, f1() and f2() do the same thing. They are just
AjK 0:748acff4e3c8 6 * coded in different ways to show how versitile the Ton
AjK 0:748acff4e3c8 7 * object is.
AjK 0:748acff4e3c8 8 *
AjK 0:748acff4e3c8 9 * Note how setting a Ton to non-zero starts the timer.
AjK 0:748acff4e3c8 10 * Reading the value of a Ton object will return zero if
AjK 0:748acff4e3c8 11 * the timer has not yet been reached. It will return a
AjK 0:748acff4e3c8 12 * non-zero value (1) if the timeout value is reached.
AjK 0:748acff4e3c8 13 *
AjK 0:748acff4e3c8 14 * Tons can operate in one of two modes. The default mode
AjK 0:748acff4e3c8 15 * is Ton::InputResets and what this means is setting a Ton
AjK 0:748acff4e3c8 16 * to zero will reset the interval timer back to it's default
AjK 0:748acff4e3c8 17 * value thus forcing a complete retime next time a Ton is
AjK 0:748acff4e3c8 18 * asserted.
AjK 0:748acff4e3c8 19 *
AjK 0:748acff4e3c8 20 * The other mode, Ton::InputPauses doesn't reset the internal
AjK 0:748acff4e3c8 21 * counter, it just halts it. So when a future event starts the
AjK 0:748acff4e3c8 22 * timer again it doesn't time the entire timer value but the
AjK 0:748acff4e3c8 23 * remaining value left since it's last assertion.
AjK 0:748acff4e3c8 24 *
AjK 0:748acff4e3c8 25 * The mode is set when created, eg:-
AjK 0:748acff4e3c8 26 * Ton t1(2000, Ton::InputResets); // (default)
AjK 0:748acff4e3c8 27 * Ton t1(2000, Ton::InputPauses); // (pause mode)
AjK 0:748acff4e3c8 28 */
AjK 0:748acff4e3c8 29 #include "mbed.h"
AjK 0:748acff4e3c8 30 #include "Ton.h"
AjK 0:748acff4e3c8 31
AjK 0:748acff4e3c8 32 DigitalIn ip1(p19);
AjK 0:748acff4e3c8 33 DigitalIn ip2(p20);
AjK 0:748acff4e3c8 34
AjK 0:748acff4e3c8 35 DigitalOut led1(LED1);
AjK 0:748acff4e3c8 36 DigitalOut led2(LED2);
AjK 0:748acff4e3c8 37
AjK 0:748acff4e3c8 38 Ton t1(2000);
AjK 0:748acff4e3c8 39 Ton t2(2000);
AjK 0:748acff4e3c8 40
AjK 0:748acff4e3c8 41 void f1(void) {
AjK 0:748acff4e3c8 42 t1 = ip1;
AjK 0:748acff4e3c8 43 led1 = t1;
AjK 0:748acff4e3c8 44 }
AjK 0:748acff4e3c8 45
AjK 0:748acff4e3c8 46 void f2(void) {
AjK 0:748acff4e3c8 47 t2 = ip2;
AjK 0:748acff4e3c8 48 if (t2) {
AjK 0:748acff4e3c8 49 led2 = 1;
AjK 0:748acff4e3c8 50 }
AjK 0:748acff4e3c8 51 else {
AjK 0:748acff4e3c8 52 led2 = 0;
AjK 0:748acff4e3c8 53 }
AjK 0:748acff4e3c8 54 }
AjK 0:748acff4e3c8 55
AjK 0:748acff4e3c8 56 int main() {
AjK 0:748acff4e3c8 57 while(1) {
AjK 0:748acff4e3c8 58 f1();
AjK 0:748acff4e3c8 59 f2();
AjK 0:748acff4e3c8 60 }
AjK 0:748acff4e3c8 61 }