]> sigrok.org Git - sigrok-firmware-fx2lafw.git/blame - fx2lib/examples/timers/timers.c
Import fx2lib into fx2lafw directly.
[sigrok-firmware-fx2lafw.git] / fx2lib / examples / timers / timers.c
CommitLineData
3608c106
UH
1/**
2 * Copyright (C) 2010 Ubixum, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 **/
18
19#include <fx2regs.h>
20#include <fx2macros.h>
21#include <fx2ints.h>
22#include <lights.h>
23
24volatile bit d2;
25volatile bit d3;
26volatile bit d4;
27volatile bit d5;
28
29void timer0_isr() interrupt TF0_ISR {
30 d2 = !d2;
31 if (d2) { d2on(); } else { d2off(); }
32}
33
34void timer1_isr() interrupt TF1_ISR {
35 d3 = !d3;
36 if (d3) { d3on(); } else { d3off(); }
37}
38
39void timer2_isr() interrupt TF2_ISR {
40 d4 = !d4;
41 if (d4) { d4on(); } else { d4off(); }
42
43 CLEAR_TIMER2(); // This one is not done automatically!
44}
45
46
47void main(void)
48{
49 WORD counter=0;
50
51 SETCPUFREQ(CLK_12M);
52
53 // enable timer 0 and timer 1 to be 16 bit counters
54 TMOD = 0x11;
55
56 // enable timer 2 to also be a 16 bit counter
57 T2CON = 0;
58 RCAP2L = 0; // reload values for t2
59 RCAP2H = 0;
60
61 EA=1; // enable interrupts
62 ENABLE_TIMER0();
63 ENABLE_TIMER1();
64 ENABLE_TIMER2();
65 TR0=1; // start t0
66 TR1=1; // start t1
67 TR2=1; // start t2
68
69 // and blink d5
70 while (TRUE) {
71 ++counter;
72 if (!counter) {
73 d5 = !d5;
74 if (d5) { d5off(); } else { d5on(); }
75 }
76 }
77}
78
79