-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathtimer1.cpp
97 lines (73 loc) · 3.13 KB
/
timer1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* Copyright (c) 2014, Nordic Semiconductor ASA
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <Arduino.h>
extern uint8_t timer1_f;
/*** FUNC
Name: Timer1start
Function: Start timer 1 to interrupt periodically. Call this from
the Arduino setup() function.
Description: The pre-scaler and the timer count divide the timer-counter
clock frequency to give a timer overflow interrupt rate:
Interrupt rate = 16MHz / (prescaler * (255 - TCNT2))
TCCR2B[b2:0] Prescaler Freq [KHz], Period [usec] after prescale
0x0 (TC stopped) 0 0
0x1 1 16000. 0.0625
0x2 8 2000. 0.500
0x3 32 500. 2.000
0x4 64 250. 4.000
0x5 128 125. 8.000
0x6 256 62.5 16.000
0x7 1024 15.625 64.000
Parameters: void
Returns: void
FUNC ***/
void Timer1start(void)
{
// Setup Timer1 overflow to fire every 4000ms
// period [sec] = (1 / f_clock [sec]) * prescale * (count)
// (1/16000000) * 1024 * (count) = 4000 ms
TCCR1B = 0x00; // Disable Timer1 while we set it up
TCNT1H = 11; // Approx 4000ms when prescaler is set to 1024
TCNT1L = 0;
TIFR1 = 0x00; // Timer1 INT Flag Reg: Clear Timer Overflow Flag
TIMSK1 = 0x01; // Timer1 INT Reg: Timer1 Overflow Interrupt Enable
TCCR1A = 0x00; // Timer1 Control Reg A: Wave Gen Mode normal
TCCR1B = 0x05; // Timer1 Control Reg B: Timer Prescaler set to 1024
}
void Timer1stop(void)
{
TCCR1B = 0x00;
TIMSK1 = 0x00;
}
/*** FUNC
Name: Timer1 ISR
Function: Handles the Timer1-overflow interrupt
FUNC ***/
ISR(TIMER1_OVF_vect)
{
if (0 == timer1_f)
{
timer1_f = 1;
}
TCNT1H = 11; // Approx 4000 ms - Reload
TCNT1L = 0;
TIFR1 = 0x00; // timer1 int flag reg: clear timer overflow flag
};