forked from sensorium/Mozzi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimerOne.h
More file actions
252 lines (228 loc) · 6.66 KB
/
TimerOne.h
File metadata and controls
252 lines (228 loc) · 6.66 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Interrupt and PWM utilities for 16 bit Timer1 on ATmega168/328
* Original code by Jesse Tane for http://labs.ideo.com August 2008
* Modified March 2009 by Jérôme Despatis and Jesse Tane for ATmega328 support
* Modified June 2009 by Michael Polli and Jesse Tane to fix a bug in setPeriod() which caused the timer to stop
* Modified April 2012 by Paul Stoffregen - portable to other AVR chips, use inline functions
* Modified March 2013 by Tim Barrass to use fast PWM for Mozzi, instead of phase and frequency correct.
* This is free software. You can redistribute it and/or modify it under
* the terms of Creative Commons Attribution 3.0 United States License.
* To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/us/
* or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
*
*/
#ifndef TimerOne_h_
#define TimerOne_h_
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "config/known_16bit_timers.h"
#define TIMER1_RESOLUTION 65536UL // Timer1 is 16 bit
// Placing nearly all the code in this .h file allows the functions to be
// inlined by the compiler. In the very common case with constant values
// the compiler will perform all calculations and simply write constants
// to the hardware registers (for example, setPeriod).
// TB2013
// Modes are in ATmega48P/88P/168P/328P technical notes, Table 13-4
// PWM, Phase and Frequency Correct, TOP is ICR1
//#define TCCR1B_PWM_MODE_BITS _BV(WGM13)
// Fast PWM, PWM, TOP is ICR1 ( this should go twice as fast
//#define TCCR1B_PWM_MODE_BITS _BV(WGM13) | _BV(WGM12)
//#define TCCR1A_PWM_MODE_BITS _BV(WGM11)
//const unsigned char TCCR1B_PWM_MODE_BITS = _BV(WGM13) | _BV(WGM12);
//const unsigned char TCCR1A_PWM_MODE_BITS = _BV(WGM11);
// TB2013 todo (to improve the Library): make the mode set-able instead of hard-coded
#define FAST 0
#define PHASE_FREQ_CORRECT 1
class TimerOne
{
public:
//****************************
// Configuration
//****************************
void initialize(unsigned long microseconds=1000000, unsigned char mode = PHASE_FREQ_CORRECT) __attribute__((always_inline))
{
//TB2013
_mode = mode;
if (_mode == FAST){
TCCR1B_PWM_MODE_BITS = _BV(WGM13) | _BV(WGM12);
TCCR1A_PWM_MODE_BITS = _BV(WGM11);
}else if (_mode == PHASE_FREQ_CORRECT){
TCCR1B_PWM_MODE_BITS = _BV(WGM13);
TCCR1A_PWM_MODE_BITS = 0;
}
TCCR1B = TCCR1B_PWM_MODE_BITS; // set mode as fast or phase and frequency correct pwm, stop the timer
TCCR1A = TCCR1A_PWM_MODE_BITS; // set control register A
// end TB2013
setPeriod(microseconds);
}
void setPeriod(unsigned long microseconds) __attribute__((always_inline))
{
unsigned long cycles;
// TB2013
if (_mode == FAST){
cycles = (F_CPU / 1000000) * microseconds; // adjusted for fast pwm
}else if (_mode == PHASE_FREQ_CORRECT){
cycles = (F_CPU / 2000000) * microseconds;
}
// end TB2013
if (cycles < TIMER1_RESOLUTION)
{
clockSelectBits = _BV(CS10);
pwmPeriod = cycles;
}
else
if (cycles < TIMER1_RESOLUTION * 8)
{
clockSelectBits = _BV(CS11);
pwmPeriod = cycles / 8;
}
else
if (cycles < TIMER1_RESOLUTION * 64)
{
clockSelectBits = _BV(CS11) | _BV(CS10);
pwmPeriod = cycles / 64;
}
else
if (cycles < TIMER1_RESOLUTION * 256)
{
clockSelectBits = _BV(CS12);
pwmPeriod = cycles / 256;
}
else
if (cycles < TIMER1_RESOLUTION * 1024)
{
clockSelectBits = _BV(CS12) | _BV(CS10);
pwmPeriod = cycles / 1024;
}
else
{
clockSelectBits = _BV(CS12) | _BV(CS10);
pwmPeriod = TIMER1_RESOLUTION - 1;
}
ICR1 = pwmPeriod;
TCCR1B = TCCR1B_PWM_MODE_BITS | clockSelectBits;
}
//****************************
// Run Control
//****************************
void start() __attribute__((always_inline))
{
TCCR1B = 0;
TCNT1 = 0; // TODO: does this cause an undesired interrupt?
TCCR1B = TCCR1B_PWM_MODE_BITS | clockSelectBits;
}
void stop() __attribute__((always_inline))
{
TCCR1B = TCCR1B_PWM_MODE_BITS;
}
void restart() __attribute__((always_inline))
{
TCCR1B = 0;
TCNT1 = 0;
TCCR1B = TCCR1B_PWM_MODE_BITS | clockSelectBits;
}
void resume() __attribute__((always_inline))
{
TCCR1B = TCCR1B_PWM_MODE_BITS | clockSelectBits;
}
//****************************
// PWM outputs
//****************************
void setPwmDuty(char pin, unsigned int duty) __attribute__((always_inline))
{
unsigned long dutyCycle = pwmPeriod;
dutyCycle *= duty;
dutyCycle >>= 10;
if (pin == TIMER1_A_PIN)
OCR1A = dutyCycle;
#ifdef TIMER1_B_PIN
else if (pin == TIMER1_B_PIN)
OCR1B = dutyCycle;
#endif
#ifdef TIMER1_C_PIN
else if (pin == TIMER1_C_PIN)
OCR1C = dutyCycle;
#endif
}
void pwm(char pin, unsigned int duty) __attribute__((always_inline))
{
if (pin == TIMER1_A_PIN)
{
pinMode(TIMER1_A_PIN, OUTPUT);
TCCR1A |= _BV(COM1A1) | TCCR1A_PWM_MODE_BITS;
}
#ifdef TIMER1_B_PIN
else if (pin == TIMER1_B_PIN)
{
pinMode(TIMER1_B_PIN, OUTPUT);
TCCR1A |= _BV(COM1B1) | TCCR1A_PWM_MODE_BITS;
}
#endif
#ifdef TIMER1_C_PIN
else if (pin == TIMER1_C_PIN)
{
pinMode(TIMER1_C_PIN, OUTPUT);
TCCR1A |= _BV(COM1C1) | TCCR1A_PWM_MODE_BITS;
}
#endif
setPwmDuty(pin, duty);
TCCR1B = TCCR1B_PWM_MODE_BITS | clockSelectBits;
}
void pwm(char pin, unsigned int duty, unsigned long microseconds) __attribute__((always_inline))
{
if (microseconds > 0)
setPeriod(microseconds);
pwm(pin, duty);
}
void disablePwm(char pin) __attribute__((always_inline))
{
if (pin == TIMER1_A_PIN) {
TCCR1A &= ~_BV(COM1A1);
TCCR1A &= ~TCCR1A_PWM_MODE_BITS; }
#ifdef TIMER1_B_PIN
else if (pin == TIMER1_B_PIN) {
TCCR1A &= ~_BV(COM1B1);
TCCR1A &= ~TCCR1A_PWM_MODE_BITS; }
#endif
#ifdef TIMER1_C_PIN
else if (pin == TIMER1_C_PIN) {
TCCR1A &= ~_BV(COM1C1);
TCCR1A &= ~TCCR1A_PWM_MODE_BITS; }
#endif
}
//****************************
// Interrupt Function
//****************************
void attachInterrupt(void (*isr)()) __attribute__((always_inline))
{
isrCallback = isr;
TIMSK1 = _BV(TOIE1);
}
void attachInterrupt(void (*isr)(), unsigned long microseconds) __attribute__((always_inline))
{
if(microseconds > 0)
setPeriod(microseconds);
attachInterrupt(isr);
}
void detachInterrupt() __attribute__((always_inline))
{
TIMSK1 = 0;
}
void (*isrCallback)();
unsigned int getPeriod(){
return pwmPeriod;
}
protected:
// properties
static unsigned int pwmPeriod;
static unsigned char clockSelectBits;
// TB2013
unsigned char _mode;
unsigned char TCCR1B_PWM_MODE_BITS;
unsigned char TCCR1A_PWM_MODE_BITS;
};
extern TimerOne Timer1;
#endif