root / trunk / cardbox / timer.c @ 213
History | View | Annotate | Download (2.16 KB)
1 |
/********
|
---|---|
2 |
* This file is part of Tooltron.
|
3 |
*
|
4 |
* Tooltron is free software: you can redistribute it and/or modify
|
5 |
* it under the terms of the Lesser GNU General Public License as published by
|
6 |
* the Free Software Foundation, either version 3 of the License, or
|
7 |
* (at your option) any later version.
|
8 |
*
|
9 |
* Tooltron 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
|
12 |
* Lesser GNU General Public License for more details.
|
13 |
* You should have received a copy of the Lesser GNU General Public License
|
14 |
* along with Tooltron. If not, see <http://www.gnu.org/licenses/>.
|
15 |
*
|
16 |
* Copyright 2009 Kevin Woo <kwoo@2ndt.com>
|
17 |
*
|
18 |
********/
|
19 |
/** @file timer.c
|
20 |
*
|
21 |
* @brief Implements the timer functions. See timer.h for full details
|
22 |
*
|
23 |
* @author Kevin Woo (kwoo)
|
24 |
*/
|
25 |
|
26 |
#include "timer.h" |
27 |
|
28 |
#include <avr/io.h> |
29 |
#include <avr/interrupt.h> |
30 |
|
31 |
char timeout_flag=0; |
32 |
|
33 |
char get_timeout_flag(void){ |
34 |
return timeout_flag;
|
35 |
} |
36 |
|
37 |
//Initializes all the timer1 registers
|
38 |
void init_timer() {
|
39 |
//Clears the control registers for timer 1
|
40 |
TCCR1A = 0x00;
|
41 |
TCCR1B = 0x00;
|
42 |
TCCR1C = 0x00;
|
43 |
TIMSK1 = 0x00;
|
44 |
|
45 |
/*****
|
46 |
* Output control register A set to clear timer on compare match.
|
47 |
* Output control register B not connected.
|
48 |
* WGM is in normal operation.
|
49 |
* Clock is divided by 256. (This sets it to disconnected, connects
|
50 |
* in start_timer to above value).
|
51 |
*****/
|
52 |
TCCR1A |= _BV(COM1A1); |
53 |
|
54 |
//Compares at 1 second with an 8MHz Clock and a 256 prescale. (31250)
|
55 |
OCR1AH = 0x7A;
|
56 |
OCR1AL = 0x12;
|
57 |
|
58 |
//Enable the output compare register 1A interrupt
|
59 |
TIMSK1 |= _BV(OCIE1A); |
60 |
|
61 |
timeout_flag = 0;
|
62 |
|
63 |
return;
|
64 |
} |
65 |
|
66 |
void start_timer() {
|
67 |
//Enables the timer. Sets prescale to 256.
|
68 |
TCCR1B |= _BV(CS12); |
69 |
|
70 |
return;
|
71 |
} |
72 |
|
73 |
void stop_timer() {
|
74 |
//Disables timer.
|
75 |
TCCR1B &= ~_BV(CS12); |
76 |
seconds = 0;
|
77 |
minutes = 0;
|
78 |
|
79 |
return;
|
80 |
} |
81 |
|
82 |
void reset_timer() {
|
83 |
seconds = 0;
|
84 |
minutes = 0;
|
85 |
return;
|
86 |
} |
87 |
|
88 |
void set_timeout_flag() {
|
89 |
timeout_flag = 1;
|
90 |
return;
|
91 |
} |
92 |
|
93 |
void reset_timeout_flag() {
|
94 |
timeout_flag = 0;
|
95 |
return;
|
96 |
} |
97 |
|
98 |
ISR(TIMER1_COMPA_vect) { |
99 |
if (seconds == 59) { |
100 |
minutes++; |
101 |
seconds = 0;
|
102 |
} else {
|
103 |
seconds++; |
104 |
} |
105 |
} |