Rev 1145 Rev 1858
1 /*! \file timer.c \brief System Timer function library. */ 1 /*! \file timer.c \brief System Timer function library. */
2 //***************************************************************************** 2 //*****************************************************************************
3 // 3 //
4 // File Name : 'timer.c' 4 // File Name : 'timer.c'
5 // Title : System Timer function library 5 // Title : System Timer function library
6 // Author : Pascal Stang - Copyright (C) 2000-2002 6 // Author : Pascal Stang - Copyright (C) 2000-2002
7 // Created : 11/22/2000 7 // Created : 11/22/2000
8 // Revised : 07/09/2003 8 // Revised : 07/09/2003
9 // Version : 1.1 9 // Version : 1.1
10 // Target MCU : Atmel AVR Series 10 // Target MCU : Atmel AVR Series
11 // Editor Tabs : 4 11 // Editor Tabs : 4
12 // 12 //
13 // This code is distributed under the GNU Public License 13 // This code is distributed under the GNU Public License
14 // which can be found at http://www.gnu.org/licenses/gpl.txt 14 // which can be found at http://www.gnu.org/licenses/gpl.txt
15 // 15 //
16 //***************************************************************************** 16 //*****************************************************************************
17   17  
18 #include <avr/io.h> 18 #include <avr/io.h>
19 #include <avr/interrupt.h> 19 #include <avr/interrupt.h>
20 #include <avr/pgmspace.h> 20 #include <avr/pgmspace.h>
21 #include <avr/sleep.h> 21 #include <avr/sleep.h>
22   22  
23 #include "global.h" 23 #include "global.h"
24 #include "timer.h" 24 #include "timer.h"
25   25  
26 #include "rprintf.h" 26 #include "rprintf.h"
27   27  
28 // Program ROM constants 28 // Program ROM constants
29 // the prescale division values stored in order of timer control register index 29 // the prescale division values stored in order of timer control register index
30 // STOP, CLK, CLK/8, CLK/64, CLK/256, CLK/1024 30 // STOP, CLK, CLK/8, CLK/64, CLK/256, CLK/1024
31 unsigned short __attribute__ ((progmem)) TimerPrescaleFactor[] = {0,1,8,64,256,1024}; 31 unsigned short __attribute__ ((progmem)) TimerPrescaleFactor[] = {0,1,8,64,256,1024};
32 // the prescale division values stored in order of timer control register index 32 // the prescale division values stored in order of timer control register index
33 // STOP, CLK, CLK/8, CLK/32, CLK/64, CLK/128, CLK/256, CLK/1024 33 // STOP, CLK, CLK/8, CLK/32, CLK/64, CLK/128, CLK/256, CLK/1024
34 unsigned short __attribute__ ((progmem)) TimerRTCPrescaleFactor[] = {0,1,8,32,64,128,256,1024}; 34 unsigned short __attribute__ ((progmem)) TimerRTCPrescaleFactor[] = {0,1,8,32,64,128,256,1024};
35   35  
36 // Global variables 36 // Global variables
37 // time registers 37 // time registers
38 volatile unsigned long TimerPauseReg; 38 volatile unsigned long TimerPauseReg;
39 volatile unsigned long Timer0Reg0; 39 volatile unsigned long Timer0Reg0;
40 volatile unsigned long Timer2Reg0; 40 volatile unsigned long Timer2Reg0;
41   41  
42 typedef void (*voidFuncPtr)(void); 42 typedef void (*voidFuncPtr)(void);
43 volatile static voidFuncPtr TimerIntFunc[TIMER_NUM_INTERRUPTS]; 43 volatile static voidFuncPtr TimerIntFunc[TIMER_NUM_INTERRUPTS];
44   44  
45 // delay for a minimum of <us> microseconds 45 // delay for a minimum of <us> microseconds
46 // the time resolution is dependent on the time the loop takes 46 // the time resolution is dependent on the time the loop takes
47 // e.g. with 4Mhz and 5 cycles per loop, the resolution is 1.25 us 47 // e.g. with 4Mhz and 5 cycles per loop, the resolution is 1.25 us
48 void delay_us(unsigned short time_us) 48 void delay_us(unsigned short time_us)
49 { 49 {
50 unsigned short delay_loops; 50 unsigned short delay_loops;
51 register unsigned short i; 51 register unsigned short i;
52   52  
53 delay_loops = (time_us+3)/5*CYCLES_PER_US; // +3 for rounding up (dirty) 53 delay_loops = (time_us+3)/5*CYCLES_PER_US; // +3 for rounding up (dirty)
54   54  
55 // one loop takes 5 cpu cycles 55 // one loop takes 5 cpu cycles
56 for (i=0; i < delay_loops; i++) {}; 56 for (i=0; i < delay_loops; i++) {};
57 } 57 }
58 /* 58 /*
59 void delay_ms(unsigned char time_ms) 59 void delay_ms(unsigned char time_ms)
60 { 60 {
61 unsigned short delay_count = F_CPU / 4000; 61 unsigned short delay_count = F_CPU / 4000;
62   62  
63 unsigned short cnt; 63 unsigned short cnt;
64 asm volatile ("\n" 64 asm volatile ("\n"
65 "L_dl1%=:\n\t" 65 "L_dl1%=:\n\t"
66 "mov %A0, %A2\n\t" 66 "mov %A0, %A2\n\t"
67 "mov %B0, %B2\n" 67 "mov %B0, %B2\n"
68 "L_dl2%=:\n\t" 68 "L_dl2%=:\n\t"
69 "sbiw %A0, 1\n\t" 69 "sbiw %A0, 1\n\t"
70 "brne L_dl2%=\n\t" 70 "brne L_dl2%=\n\t"
71 "dec %1\n\t" "brne L_dl1%=\n\t":"=&w" (cnt) 71 "dec %1\n\t" "brne L_dl1%=\n\t":"=&w" (cnt)
72 :"r"(time_ms), "r"((unsigned short) (delay_count)) 72 :"r"(time_ms), "r"((unsigned short) (delay_count))
73 ); 73 );
74 } 74 }
75 */ 75 */
76 void timerInit(void) 76 void timerInit(void)
77 { 77 {
78 u08 intNum; 78 u08 intNum;
79 // detach all user functions from interrupts 79 // detach all user functions from interrupts
80 for(intNum=0; intNum<TIMER_NUM_INTERRUPTS; intNum++) 80 for(intNum=0; intNum<TIMER_NUM_INTERRUPTS; intNum++)
81 timerDetach(intNum); 81 timerDetach(intNum);
82   82  
83 // initialize all timers 83 // initialize all timers
84 timer0Init(); 84 timer0Init();
85 timer1Init(); 85 timer1Init();
86 #ifdef TCNT2 // support timer2 only if it exists 86 #ifdef TCNT2 // support timer2 only if it exists
87 timer2Init(); 87 timer2Init();
88 #endif 88 #endif
89 // enable interrupts 89 // enable interrupts
90 sei(); 90 sei();
91 } 91 }
92   92  
93 void timer0Init() 93 void timer0Init()
94 { 94 {
95 // initialize timer 0 95 // initialize timer 0
96 timer0SetPrescaler( TIMER0PRESCALE ); // set prescaler 96 timer0SetPrescaler( TIMER0PRESCALE ); // set prescaler
97 outb(TCNT0, 0); // reset TCNT0 97 outb(TCNT0, 0); // reset TCNT0
98 sbi(TIMSK, TOIE0); // enable TCNT0 overflow interrupt 98 sbi(TIMSK, TOIE0); // enable TCNT0 overflow interrupt
99   99  
100 timer0ClearOverflowCount(); // initialize time registers 100 timer0ClearOverflowCount(); // initialize time registers
101 } 101 }
102   102  
103 void timer1Init(void) 103 void timer1Init(void)
104 { 104 {
105 // initialize timer 1 105 // initialize timer 1
106 timer1SetPrescaler( TIMER1PRESCALE ); // set prescaler 106 timer1SetPrescaler( TIMER1PRESCALE ); // set prescaler
107 outb(TCNT1H, 0); // reset TCNT1 107 outb(TCNT1H, 0); // reset TCNT1
108 outb(TCNT1L, 0); 108 outb(TCNT1L, 0);
109 sbi(TIMSK, TOIE1); // enable TCNT1 overflow 109 sbi(TIMSK, TOIE1); // enable TCNT1 overflow
110 } 110 }
111   111  
112 #ifdef TCNT2 // support timer2 only if it exists 112 #ifdef TCNT2 // support timer2 only if it exists
113 void timer2Init(void) 113 void timer2Init(void)
114 { 114 {
115 // initialize timer 2 115 // initialize timer 2
116 timer2SetPrescaler( TIMER2PRESCALE ); // set prescaler 116 timer2SetPrescaler( TIMER2PRESCALE ); // set prescaler
117 outb(TCNT2, 0); // reset TCNT2 117 outb(TCNT2, 0); // reset TCNT2
118 sbi(TIMSK, TOIE2); // enable TCNT2 overflow 118 sbi(TIMSK, TOIE2); // enable TCNT2 overflow
119   119  
120 timer2ClearOverflowCount(); // initialize time registers 120 timer2ClearOverflowCount(); // initialize time registers
121 } 121 }
122 #endif 122 #endif
123   123  
124 void timer0SetPrescaler(u08 prescale) 124 void timer0SetPrescaler(u08 prescale)
125 { 125 {
126 // set prescaler on timer 0 126 // set prescaler on timer 0
127 outb(TCCR0, (inb(TCCR0) & ~TIMER_PRESCALE_MASK) | prescale); 127 outb(TCCR0, (inb(TCCR0) & ~TIMER_PRESCALE_MASK) | prescale);
128 } 128 }
129   129  
130 void timer1SetPrescaler(u08 prescale) 130 void timer1SetPrescaler(u08 prescale)
131 { 131 {
132 // set prescaler on timer 1 132 // set prescaler on timer 1
133 outb(TCCR1B, (inb(TCCR1B) & ~TIMER_PRESCALE_MASK) | prescale); 133 outb(TCCR1B, (inb(TCCR1B) & ~TIMER_PRESCALE_MASK) | prescale);
134 } 134 }
135   135  
136 #ifdef TCNT2 // support timer2 only if it exists 136 #ifdef TCNT2 // support timer2 only if it exists
137 void timer2SetPrescaler(u08 prescale) 137 void timer2SetPrescaler(u08 prescale)
138 { 138 {
139 // set prescaler on timer 2 139 // set prescaler on timer 2
140 outb(TCCR2, (inb(TCCR2) & ~TIMER_PRESCALE_MASK) | prescale); 140 outb(TCCR2, (inb(TCCR2) & ~TIMER_PRESCALE_MASK) | prescale);
141 } 141 }
142 #endif 142 #endif
143   143  
144 u16 timer0GetPrescaler(void) 144 u16 timer0GetPrescaler(void)
145 { 145 {
146 // get the current prescaler setting 146 // get the current prescaler setting
147 return (pgm_read_word(TimerPrescaleFactor+(inb(TCCR0) & TIMER_PRESCALE_MASK))); 147 return (pgm_read_word(TimerPrescaleFactor+(inb(TCCR0) & TIMER_PRESCALE_MASK)));
148 } 148 }
149   149  
150 u16 timer1GetPrescaler(void) 150 u16 timer1GetPrescaler(void)
151 { 151 {
152 // get the current prescaler setting 152 // get the current prescaler setting
153 return (pgm_read_word(TimerPrescaleFactor+(inb(TCCR1B) & TIMER_PRESCALE_MASK))); 153 return (pgm_read_word(TimerPrescaleFactor+(inb(TCCR1B) & TIMER_PRESCALE_MASK)));
154 } 154 }
155   155  
156 #ifdef TCNT2 // support timer2 only if it exists 156 #ifdef TCNT2 // support timer2 only if it exists
157 u16 timer2GetPrescaler(void) 157 u16 timer2GetPrescaler(void)
158 { 158 {
159 //TODO: can we assume for all 3-timer AVR processors, 159 //TODO: can we assume for all 3-timer AVR processors,
160 // that timer2 is the RTC timer? 160 // that timer2 is the RTC timer?
161   161  
162 // get the current prescaler setting 162 // get the current prescaler setting
163 return (pgm_read_word(TimerRTCPrescaleFactor+(inb(TCCR2) & TIMER_PRESCALE_MASK))); 163 return (pgm_read_word(TimerRTCPrescaleFactor+(inb(TCCR2) & TIMER_PRESCALE_MASK)));
164 } 164 }
165 #endif 165 #endif
166   166  
167 void timerAttach(u08 interruptNum, void (*userFunc)(void) ) 167 void timerAttach(u08 interruptNum, void (*userFunc)(void) )
168 { 168 {
169 // make sure the interrupt number is within bounds 169 // make sure the interrupt number is within bounds
170 if(interruptNum < TIMER_NUM_INTERRUPTS) 170 if(interruptNum < TIMER_NUM_INTERRUPTS)
171 { 171 {
172 // set the interrupt function to run 172 // set the interrupt function to run
173 // the supplied user's function 173 // the supplied user's function
174 TimerIntFunc[interruptNum] = userFunc; 174 TimerIntFunc[interruptNum] = userFunc;
175 } 175 }
176 } 176 }
177   177  
178 void timerDetach(u08 interruptNum) 178 void timerDetach(u08 interruptNum)
179 { 179 {
180 // make sure the interrupt number is within bounds 180 // make sure the interrupt number is within bounds
181 if(interruptNum < TIMER_NUM_INTERRUPTS) 181 if(interruptNum < TIMER_NUM_INTERRUPTS)
182 { 182 {
183 // set the interrupt function to run nothing 183 // set the interrupt function to run nothing
184 TimerIntFunc[interruptNum] = 0; 184 TimerIntFunc[interruptNum] = 0;
185 } 185 }
186 } 186 }
187 /* 187 /*
188 u32 timerMsToTics(u16 ms) 188 u32 timerMsToTics(u16 ms)
189 { 189 {
190 // calculate the prescaler division rate 190 // calculate the prescaler division rate
191 u16 prescaleDiv = 1<<(pgm_read_byte(TimerPrescaleFactor+inb(TCCR0))); 191 u16 prescaleDiv = 1<<(pgm_read_byte(TimerPrescaleFactor+inb(TCCR0)));
192 // calculate the number of timer tics in x milliseconds 192 // calculate the number of timer tics in x milliseconds
193 return (ms*(F_CPU/(prescaleDiv*256)))/1000; 193 return (ms*(F_CPU/(prescaleDiv*256)))/1000;
194 } 194 }
195   195  
196 u16 timerTicsToMs(u32 tics) 196 u16 timerTicsToMs(u32 tics)
197 { 197 {
198 // calculate the prescaler division rate 198 // calculate the prescaler division rate
199 u16 prescaleDiv = 1<<(pgm_read_byte(TimerPrescaleFactor+inb(TCCR0))); 199 u16 prescaleDiv = 1<<(pgm_read_byte(TimerPrescaleFactor+inb(TCCR0)));
200 // calculate the number of milliseconds in x timer tics 200 // calculate the number of milliseconds in x timer tics
201 return (tics*1000*(prescaleDiv*256))/F_CPU; 201 return (tics*1000*(prescaleDiv*256))/F_CPU;
202 } 202 }
203 */ 203 */
204 void timerPause(unsigned short pause_ms) 204 void timerPause(unsigned short pause_ms)
205 { 205 {
206 // pauses for exactly <pause_ms> number of milliseconds 206 // pauses for exactly <pause_ms> number of milliseconds
207 u08 timerThres; 207 u08 timerThres;
208 u32 ticRateHz; 208 u32 ticRateHz;
209 u32 pause; 209 u32 pause;
210   210  
211 // capture current pause timer value 211 // capture current pause timer value
212 timerThres = inb(TCNT0); 212 timerThres = inb(TCNT0);
213 // reset pause timer overflow count 213 // reset pause timer overflow count
214 TimerPauseReg = 0; 214 TimerPauseReg = 0;
215 // calculate delay for [pause_ms] milliseconds 215 // calculate delay for [pause_ms] milliseconds
216 // prescaler division = 1<<(pgm_read_byte(TimerPrescaleFactor+inb(TCCR0))) 216 // prescaler division = 1<<(pgm_read_byte(TimerPrescaleFactor+inb(TCCR0)))
217 ticRateHz = F_CPU/timer0GetPrescaler(); 217 ticRateHz = F_CPU/timer0GetPrescaler();
218 // precision management 218 // precision management
219 // prevent overflow and precision underflow 219 // prevent overflow and precision underflow
220 // -could add more conditions to improve accuracy 220 // -could add more conditions to improve accuracy
221 if( ((ticRateHz < 429497) && (pause_ms <= 10000)) ) 221 if( ((ticRateHz < 429497) && (pause_ms <= 10000)) )
222 pause = (pause_ms*ticRateHz)/1000; 222 pause = (pause_ms*ticRateHz)/1000;
223 else 223 else
224 pause = pause_ms*(ticRateHz/1000); 224 pause = pause_ms*(ticRateHz/1000);
225   225  
226 // loop until time expires 226 // loop until time expires
227 while( ((TimerPauseReg<<8) | inb(TCNT0)) < (pause+timerThres) ) 227 while( ((TimerPauseReg<<8) | inb(TCNT0)) < (pause+timerThres) )
228 { 228 {
229 if( TimerPauseReg < (pause>>8)); 229 if( TimerPauseReg < (pause>>8));
230 { 230 {
231 // save power by idling the processor 231 // save power by idling the processor
232 set_sleep_mode(SLEEP_MODE_IDLE); 232 set_sleep_mode(SLEEP_MODE_IDLE);
233 sleep_mode(); 233 sleep_mode();
234 } 234 }
235 } 235 }
236   236  
237 /* old inaccurate code, for reference 237 /* old inaccurate code, for reference
238 238
239 // calculate delay for [pause_ms] milliseconds 239 // calculate delay for [pause_ms] milliseconds
240 u16 prescaleDiv = 1<<(pgm_read_byte(TimerPrescaleFactor+inb(TCCR0))); 240 u16 prescaleDiv = 1<<(pgm_read_byte(TimerPrescaleFactor+inb(TCCR0)));
241 u32 pause = (pause_ms*(F_CPU/(prescaleDiv*256)))/1000; 241 u32 pause = (pause_ms*(F_CPU/(prescaleDiv*256)))/1000;
242 242
243 TimerPauseReg = 0; 243 TimerPauseReg = 0;
244 while(TimerPauseReg < pause); 244 while(TimerPauseReg < pause);
245   245  
246 */ 246 */
247 } 247 }
248   248  
249 void timer0ClearOverflowCount(void) 249 void timer0ClearOverflowCount(void)
250 { 250 {
251 // clear the timer overflow counter registers 251 // clear the timer overflow counter registers
252 Timer0Reg0 = 0; // initialize time registers 252 Timer0Reg0 = 0; // initialize time registers
253 } 253 }
254   254  
255 long timer0GetOverflowCount(void) 255 long timer0GetOverflowCount(void)
256 { 256 {
257 // return the current timer overflow count 257 // return the current timer overflow count
258 // (this is since the last timer0ClearOverflowCount() command was called) 258 // (this is since the last timer0ClearOverflowCount() command was called)
259 return Timer0Reg0; 259 return Timer0Reg0;
260 } 260 }
261   261  
262 #ifdef TCNT2 // support timer2 only if it exists 262 #ifdef TCNT2 // support timer2 only if it exists
263 void timer2ClearOverflowCount(void) 263 void timer2ClearOverflowCount(void)
264 { 264 {
265 // clear the timer overflow counter registers 265 // clear the timer overflow counter registers
266 Timer2Reg0 = 0; // initialize time registers 266 Timer2Reg0 = 0; // initialize time registers
267 } 267 }
268   268  
269 long timer2GetOverflowCount(void) 269 long timer2GetOverflowCount(void)
270 { 270 {
271 // return the current timer overflow count 271 // return the current timer overflow count
272 // (this is since the last timer2ClearOverflowCount() command was called) 272 // (this is since the last timer2ClearOverflowCount() command was called)
273 return Timer2Reg0; 273 return Timer2Reg0;
274 } 274 }
275 #endif 275 #endif
276   276  
277 void timer1PWMInit(u08 bitRes) 277 void timer1PWMInit(u08 bitRes)
278 { 278 {
279 // configures timer1 for use with PWM output 279 // configures timer1 for use with PWM output
280 // on OC1A and OC1B pins 280 // on OC1A and OC1B pins
281   281  
282 // enable timer1 as 8,9,10bit PWM 282 // enable timer1 as 8,9,10bit PWM
283 if(bitRes == 9) 283 if(bitRes == 9)
284 { // 9bit mode 284 { // 9bit mode
285 sbi(TCCR1A,PWM11); 285 sbi(TCCR1A,PWM11);
286 cbi(TCCR1A,PWM10); 286 cbi(TCCR1A,PWM10);
287 } 287 }
288 else if( bitRes == 10 ) 288 else if( bitRes == 10 )
289 { // 10bit mode 289 { // 10bit mode
290 sbi(TCCR1A,PWM11); 290 sbi(TCCR1A,PWM11);
291 sbi(TCCR1A,PWM10); 291 sbi(TCCR1A,PWM10);
292 } 292 }
293 else 293 else
294 { // default 8bit mode 294 { // default 8bit mode
295 cbi(TCCR1A,PWM11); 295 cbi(TCCR1A,PWM11);
296 sbi(TCCR1A,PWM10); 296 sbi(TCCR1A,PWM10);
297 } 297 }
298   298  
299 // clear output compare value A 299 // clear output compare value A
300 outb(OCR1AH, 0); 300 outb(OCR1AH, 0);
301 outb(OCR1AL, 0); 301 outb(OCR1AL, 0);
302 // clear output compare value B 302 // clear output compare value B
303 outb(OCR1BH, 0); 303 outb(OCR1BH, 0);
304 outb(OCR1BL, 0); 304 outb(OCR1BL, 0);
305 } 305 }
306   306  
307 #ifdef WGM10 307 #ifdef WGM10
308 // include support for arbitrary top-count PWM 308 // include support for arbitrary top-count PWM
309 // on new AVR processors that support it 309 // on new AVR processors that support it
310 void timer1PWMInitICR(u16 topcount) 310 void timer1PWMInitICR(u16 topcount)
311 { 311 {
312 // set PWM mode with ICR top-count 312 // set PWM mode with ICR top-count
313 cbi(TCCR1A,WGM10); 313 cbi(TCCR1A,WGM10);
314 sbi(TCCR1A,WGM11); 314 sbi(TCCR1A,WGM11);
315 sbi(TCCR1B,WGM12); 315 sbi(TCCR1B,WGM12);
316 sbi(TCCR1B,WGM13); 316 sbi(TCCR1B,WGM13);
317 317
318 // set top count value 318 // set top count value
319 ICR1 = topcount; 319 ICR1 = topcount;
320 320
321 // clear output compare value A 321 // clear output compare value A
322 OCR1A = 0; 322 OCR1A = 0;
323 // clear output compare value B 323 // clear output compare value B
324 OCR1B = 0; 324 OCR1B = 0;
325   325  
326 } 326 }
327 #endif 327 #endif
328   328  
329 void timer1PWMOff(void) 329 void timer1PWMOff(void)
330 { 330 {
331 // turn off timer1 PWM mode 331 // turn off timer1 PWM mode
332 cbi(TCCR1A,PWM11); 332 cbi(TCCR1A,PWM11);
333 cbi(TCCR1A,PWM10); 333 cbi(TCCR1A,PWM10);
334 // set PWM1A/B (OutputCompare action) to none 334 // set PWM1A/B (OutputCompare action) to none
335 timer1PWMAOff(); 335 timer1PWMAOff();
336 timer1PWMBOff(); 336 timer1PWMBOff();
337 } 337 }
338   338  
339 void timer1PWMAOn(void) 339 void timer1PWMAOn(void)
340 { 340 {
341 // turn on channel A (OC1A) PWM output 341 // turn on channel A (OC1A) PWM output
342 // set OC1A as non-inverted PWM 342 // set OC1A as non-inverted PWM
343 sbi(TCCR1A,COM1A1); 343 sbi(TCCR1A,COM1A1);
344 cbi(TCCR1A,COM1A0); 344 cbi(TCCR1A,COM1A0);
345 } 345 }
346   346  
347 void timer1PWMBOn(void) 347 void timer1PWMBOn(void)
348 { 348 {
349 // turn on channel B (OC1B) PWM output 349 // turn on channel B (OC1B) PWM output
350 // set OC1B as non-inverted PWM 350 // set OC1B as non-inverted PWM
351 sbi(TCCR1A,COM1B1); 351 sbi(TCCR1A,COM1B1);
352 cbi(TCCR1A,COM1B0); 352 cbi(TCCR1A,COM1B0);
353 } 353 }
354   354  
355 void timer1PWMAOff(void) 355 void timer1PWMAOff(void)
356 { 356 {
357 // turn off channel A (OC1A) PWM output 357 // turn off channel A (OC1A) PWM output
358 // set OC1A (OutputCompare action) to none 358 // set OC1A (OutputCompare action) to none
359 cbi(TCCR1A,COM1A1); 359 cbi(TCCR1A,COM1A1);
360 cbi(TCCR1A,COM1A0); 360 cbi(TCCR1A,COM1A0);
361 } 361 }
362   362  
363 void timer1PWMBOff(void) 363 void timer1PWMBOff(void)
364 { 364 {
365 // turn off channel B (OC1B) PWM output 365 // turn off channel B (OC1B) PWM output
366 // set OC1B (OutputCompare action) to none 366 // set OC1B (OutputCompare action) to none
367 cbi(TCCR1A,COM1B1); 367 cbi(TCCR1A,COM1B1);
368 cbi(TCCR1A,COM1B0); 368 cbi(TCCR1A,COM1B0);
369 } 369 }
370   370  
371 void timer1PWMASet(u16 pwmDuty) 371 void timer1PWMASet(u16 pwmDuty)
372 { 372 {
373 // set PWM (output compare) duty for channel A 373 // set PWM (output compare) duty for channel A
374 // this PWM output is generated on OC1A pin 374 // this PWM output is generated on OC1A pin
375 // NOTE: pwmDuty should be in the range 0-255 for 8bit PWM 375 // NOTE: pwmDuty should be in the range 0-255 for 8bit PWM
376 // pwmDuty should be in the range 0-511 for 9bit PWM 376 // pwmDuty should be in the range 0-511 for 9bit PWM
377 // pwmDuty should be in the range 0-1023 for 10bit PWM 377 // pwmDuty should be in the range 0-1023 for 10bit PWM
378 //outp( (pwmDuty>>8), OCR1AH); // set the high 8bits of OCR1A 378 //outp( (pwmDuty>>8), OCR1AH); // set the high 8bits of OCR1A
379 //outp( (pwmDuty&0x00FF), OCR1AL); // set the low 8bits of OCR1A 379 //outp( (pwmDuty&0x00FF), OCR1AL); // set the low 8bits of OCR1A
380 OCR1A = pwmDuty; 380 OCR1A = pwmDuty;
381 } 381 }
382   382  
383 void timer1PWMBSet(u16 pwmDuty) 383 void timer1PWMBSet(u16 pwmDuty)
384 { 384 {
385 // set PWM (output compare) duty for channel B 385 // set PWM (output compare) duty for channel B
386 // this PWM output is generated on OC1B pin 386 // this PWM output is generated on OC1B pin
387 // NOTE: pwmDuty should be in the range 0-255 for 8bit PWM 387 // NOTE: pwmDuty should be in the range 0-255 for 8bit PWM
388 // pwmDuty should be in the range 0-511 for 9bit PWM 388 // pwmDuty should be in the range 0-511 for 9bit PWM
389 // pwmDuty should be in the range 0-1023 for 10bit PWM 389 // pwmDuty should be in the range 0-1023 for 10bit PWM
390 //outp( (pwmDuty>>8), OCR1BH); // set the high 8bits of OCR1B 390 //outp( (pwmDuty>>8), OCR1BH); // set the high 8bits of OCR1B
391 //outp( (pwmDuty&0x00FF), OCR1BL); // set the low 8bits of OCR1B 391 //outp( (pwmDuty&0x00FF), OCR1BL); // set the low 8bits of OCR1B
392 OCR1B = pwmDuty; 392 OCR1B = pwmDuty;
393 } 393 }
394   394  
395 //! Interrupt handler for tcnt0 overflow interrupt 395 //! Interrupt handler for tcnt0 overflow interrupt
396 TIMER_INTERRUPT_HANDLER(SIG_OVERFLOW0) 396 TIMER_INTERRUPT_HANDLER(SIG_OVERFLOW0)
397 { 397 {
398 Timer0Reg0++; // increment low-order counter 398 Timer0Reg0++; // increment low-order counter
399   399  
400 // increment pause counter 400 // increment pause counter
401 TimerPauseReg++; 401 TimerPauseReg++;
402   402  
403 // if a user function is defined, execute it too 403 // if a user function is defined, execute it too
404 if(TimerIntFunc[TIMER0OVERFLOW_INT]) 404 if(TimerIntFunc[TIMER0OVERFLOW_INT])
405 TimerIntFunc[TIMER0OVERFLOW_INT](); 405 TimerIntFunc[TIMER0OVERFLOW_INT]();
406 } 406 }
407   407  
408 //! Interrupt handler for tcnt1 overflow interrupt 408 //! Interrupt handler for tcnt1 overflow interrupt
409 TIMER_INTERRUPT_HANDLER(SIG_OVERFLOW1) 409 TIMER_INTERRUPT_HANDLER(SIG_OVERFLOW1)
410 { 410 {
411 // if a user function is defined, execute it 411 // if a user function is defined, execute it
412 if(TimerIntFunc[TIMER1OVERFLOW_INT]) 412 if(TimerIntFunc[TIMER1OVERFLOW_INT])
413 TimerIntFunc[TIMER1OVERFLOW_INT](); 413 TimerIntFunc[TIMER1OVERFLOW_INT]();
414 } 414 }
415   415  
416 #ifdef TCNT2 // support timer2 only if it exists 416 #ifdef TCNT2 // support timer2 only if it exists
417 //! Interrupt handler for tcnt2 overflow interrupt 417 //! Interrupt handler for tcnt2 overflow interrupt
418 TIMER_INTERRUPT_HANDLER(SIG_OVERFLOW2) 418 TIMER_INTERRUPT_HANDLER(SIG_OVERFLOW2)
419 { 419 {
420 Timer2Reg0++; // increment low-order counter 420 Timer2Reg0++; // increment low-order counter
421   421  
422 // if a user function is defined, execute it 422 // if a user function is defined, execute it
423 if(TimerIntFunc[TIMER2OVERFLOW_INT]) 423 if(TimerIntFunc[TIMER2OVERFLOW_INT])
424 TimerIntFunc[TIMER2OVERFLOW_INT](); 424 TimerIntFunc[TIMER2OVERFLOW_INT]();
425 } 425 }
426 #endif 426 #endif
427   427  
428 #ifdef OCR0 428 #ifdef OCR0
429 // include support for Output Compare 0 for new AVR processors that support it 429 // include support for Output Compare 0 for new AVR processors that support it
430 //! Interrupt handler for OutputCompare0 match (OC0) interrupt 430 //! Interrupt handler for OutputCompare0 match (OC0) interrupt
431 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE0) 431 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE0)
432 { 432 {
433 // if a user function is defined, execute it 433 // if a user function is defined, execute it
434 if(TimerIntFunc[TIMER0OUTCOMPARE_INT]) 434 if(TimerIntFunc[TIMER0OUTCOMPARE_INT])
435 TimerIntFunc[TIMER0OUTCOMPARE_INT](); 435 TimerIntFunc[TIMER0OUTCOMPARE_INT]();
436 } 436 }
437 #endif 437 #endif
438   438  
439 //! Interrupt handler for CutputCompare1A match (OC1A) interrupt 439 //! Interrupt handler for CutputCompare1A match (OC1A) interrupt
440 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE1A) 440 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE1A)
441 { 441 {
442 // if a user function is defined, execute it 442 // if a user function is defined, execute it
443 if(TimerIntFunc[TIMER1OUTCOMPAREA_INT]) 443 if(TimerIntFunc[TIMER1OUTCOMPAREA_INT])
444 TimerIntFunc[TIMER1OUTCOMPAREA_INT](); 444 TimerIntFunc[TIMER1OUTCOMPAREA_INT]();
445 } 445 }
446   446  
447 //! Interrupt handler for OutputCompare1B match (OC1B) interrupt 447 //! Interrupt handler for OutputCompare1B match (OC1B) interrupt
448 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE1B) 448 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE1B)
449 { 449 {
450 // if a user function is defined, execute it 450 // if a user function is defined, execute it
451 if(TimerIntFunc[TIMER1OUTCOMPAREB_INT]) 451 if(TimerIntFunc[TIMER1OUTCOMPAREB_INT])
452 TimerIntFunc[TIMER1OUTCOMPAREB_INT](); 452 TimerIntFunc[TIMER1OUTCOMPAREB_INT]();
453 } 453 }
454   454  
455 //! Interrupt handler for InputCapture1 (IC1) interrupt 455 //! Interrupt handler for InputCapture1 (IC1) interrupt
456 TIMER_INTERRUPT_HANDLER(SIG_INPUT_CAPTURE1) 456 TIMER_INTERRUPT_HANDLER(SIG_INPUT_CAPTURE1)
457 { 457 {
458 // if a user function is defined, execute it 458 // if a user function is defined, execute it
459 if(TimerIntFunc[TIMER1INPUTCAPTURE_INT]) 459 if(TimerIntFunc[TIMER1INPUTCAPTURE_INT])
460 TimerIntFunc[TIMER1INPUTCAPTURE_INT](); 460 TimerIntFunc[TIMER1INPUTCAPTURE_INT]();
461 } 461 }
462   462  
463 //! Interrupt handler for OutputCompare2 match (OC2) interrupt 463 //! Interrupt handler for OutputCompare2 match (OC2) interrupt
464 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE2) 464 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE2)
465 { 465 {
466 // if a user function is defined, execute it 466 // if a user function is defined, execute it
467 if(TimerIntFunc[TIMER2OUTCOMPARE_INT]) 467 if(TimerIntFunc[TIMER2OUTCOMPARE_INT])
468 TimerIntFunc[TIMER2OUTCOMPARE_INT](); 468 TimerIntFunc[TIMER2OUTCOMPARE_INT]();
469 } 469 }