00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <avr/io.h>
00019 #include <avr/interrupt.h>
00020 #include <avr/pgmspace.h>
00021 #include <avr/sleep.h>
00022
00023 #include "global.h"
00024 #include "timer.h"
00025
00026 #include "rprintf.h"
00027
00028
00029
00030
00031 unsigned short __attribute__ ((progmem)) TimerPrescaleFactor[] = {0,1,8,64,256,1024};
00032
00033
00034 unsigned short __attribute__ ((progmem)) TimerRTCPrescaleFactor[] = {0,1,8,32,64,128,256,1024};
00035
00036
00037
00038 volatile unsigned long TimerPauseReg;
00039 volatile unsigned long Timer0Reg0;
00040 volatile unsigned long Timer2Reg0;
00041
00042 typedef void (*voidFuncPtr)(void);
00043 volatile static voidFuncPtr TimerIntFunc[TIMER_NUM_INTERRUPTS];
00044
00045
00046
00047
00048 void delay_us(unsigned short time_us)
00049 {
00050 unsigned short delay_loops;
00051 register unsigned short i;
00052
00053 delay_loops = (time_us+3)/5*CYCLES_PER_US;
00054
00055
00056 for (i=0; i < delay_loops; i++) {};
00057 }
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 void timerInit(void)
00077 {
00078 u08 intNum;
00079
00080 for(intNum=0; intNum<TIMER_NUM_INTERRUPTS; intNum++)
00081 timerDetach(intNum);
00082
00083
00084 timer0Init();
00085 timer1Init();
00086 #ifdef TCNT2 // support timer2 only if it exists
00087 timer2Init();
00088 #endif
00089
00090 sei();
00091 }
00092
00093 void timer0Init()
00094 {
00095
00096 timer0SetPrescaler( TIMER0PRESCALE );
00097 outb(TCNT0, 0);
00098 sbi(TIMSK, TOIE0);
00099
00100 timer0ClearOverflowCount();
00101 }
00102
00103 void timer1Init(void)
00104 {
00105
00106 timer1SetPrescaler( TIMER1PRESCALE );
00107 outb(TCNT1H, 0);
00108 outb(TCNT1L, 0);
00109 sbi(TIMSK, TOIE1);
00110 }
00111
00112 #ifdef TCNT2 // support timer2 only if it exists
00113 void timer2Init(void)
00114 {
00115
00116 timer2SetPrescaler( TIMER2PRESCALE );
00117 outb(TCNT2, 0);
00118 sbi(TIMSK, TOIE2);
00119
00120 timer2ClearOverflowCount();
00121 }
00122 #endif
00123
00124 void timer0SetPrescaler(u08 prescale)
00125 {
00126
00127 outb(TCCR0, (inb(TCCR0) & ~TIMER_PRESCALE_MASK) | prescale);
00128 }
00129
00130 void timer1SetPrescaler(u08 prescale)
00131 {
00132
00133 outb(TCCR1B, (inb(TCCR1B) & ~TIMER_PRESCALE_MASK) | prescale);
00134 }
00135
00136 #ifdef TCNT2 // support timer2 only if it exists
00137 void timer2SetPrescaler(u08 prescale)
00138 {
00139
00140 outb(TCCR2, (inb(TCCR2) & ~TIMER_PRESCALE_MASK) | prescale);
00141 }
00142 #endif
00143
00144 u16 timer0GetPrescaler(void)
00145 {
00146
00147 return (pgm_read_word(TimerPrescaleFactor+(inb(TCCR0) & TIMER_PRESCALE_MASK)));
00148 }
00149
00150 u16 timer1GetPrescaler(void)
00151 {
00152
00153 return (pgm_read_word(TimerPrescaleFactor+(inb(TCCR1B) & TIMER_PRESCALE_MASK)));
00154 }
00155
00156 #ifdef TCNT2 // support timer2 only if it exists
00157 u16 timer2GetPrescaler(void)
00158 {
00159
00160
00161
00162
00163 return (pgm_read_word(TimerRTCPrescaleFactor+(inb(TCCR2) & TIMER_PRESCALE_MASK)));
00164 }
00165 #endif
00166
00167 void timerAttach(u08 interruptNum, void (*userFunc)(void) )
00168 {
00169
00170 if(interruptNum < TIMER_NUM_INTERRUPTS)
00171 {
00172
00173
00174 TimerIntFunc[interruptNum] = userFunc;
00175 }
00176 }
00177
00178 void timerDetach(u08 interruptNum)
00179 {
00180
00181 if(interruptNum < TIMER_NUM_INTERRUPTS)
00182 {
00183
00184 TimerIntFunc[interruptNum] = 0;
00185 }
00186 }
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 void timerPause(unsigned short pause_ms)
00205 {
00206
00207 u08 timerThres;
00208 u32 ticRateHz;
00209 u32 pause;
00210
00211
00212 timerThres = inb(TCNT0);
00213
00214 TimerPauseReg = 0;
00215
00216
00217 ticRateHz = F_CPU/timer0GetPrescaler();
00218
00219
00220
00221 if( ((ticRateHz < 429497) && (pause_ms <= 10000)) )
00222 pause = (pause_ms*ticRateHz)/1000;
00223 else
00224 pause = pause_ms*(ticRateHz/1000);
00225
00226
00227 while( ((TimerPauseReg<<8) | inb(TCNT0)) < (pause+timerThres) )
00228 {
00229 if( TimerPauseReg < (pause>>8));
00230 {
00231
00232 set_sleep_mode(SLEEP_MODE_IDLE);
00233 sleep_mode();
00234 }
00235 }
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 }
00248
00249 void timer0ClearOverflowCount(void)
00250 {
00251
00252 Timer0Reg0 = 0;
00253 }
00254
00255 long timer0GetOverflowCount(void)
00256 {
00257
00258
00259 return Timer0Reg0;
00260 }
00261
00262 #ifdef TCNT2 // support timer2 only if it exists
00263 void timer2ClearOverflowCount(void)
00264 {
00265
00266 Timer2Reg0 = 0;
00267 }
00268
00269 long timer2GetOverflowCount(void)
00270 {
00271
00272
00273 return Timer2Reg0;
00274 }
00275 #endif
00276
00277 void timer1PWMInit(u08 bitRes)
00278 {
00279
00280
00281
00282
00283 if(bitRes == 9)
00284 {
00285 sbi(TCCR1A,PWM11);
00286 cbi(TCCR1A,PWM10);
00287 }
00288 else if( bitRes == 10 )
00289 {
00290 sbi(TCCR1A,PWM11);
00291 sbi(TCCR1A,PWM10);
00292 }
00293 else
00294 {
00295 cbi(TCCR1A,PWM11);
00296 sbi(TCCR1A,PWM10);
00297 }
00298
00299
00300 outb(OCR1AH, 0);
00301 outb(OCR1AL, 0);
00302
00303 outb(OCR1BH, 0);
00304 outb(OCR1BL, 0);
00305 }
00306
00307 #ifdef WGM10
00308
00309
00310 void timer1PWMInitICR(u16 topcount)
00311 {
00312
00313 cbi(TCCR1A,WGM10);
00314 sbi(TCCR1A,WGM11);
00315 sbi(TCCR1B,WGM12);
00316 sbi(TCCR1B,WGM13);
00317
00318
00319 ICR1 = topcount;
00320
00321
00322 OCR1A = 0;
00323
00324 OCR1B = 0;
00325
00326 }
00327 #endif
00328
00329 void timer1PWMOff(void)
00330 {
00331
00332 cbi(TCCR1A,PWM11);
00333 cbi(TCCR1A,PWM10);
00334
00335 timer1PWMAOff();
00336 timer1PWMBOff();
00337 }
00338
00339 void timer1PWMAOn(void)
00340 {
00341
00342
00343 sbi(TCCR1A,COM1A1);
00344 cbi(TCCR1A,COM1A0);
00345 }
00346
00347 void timer1PWMBOn(void)
00348 {
00349
00350
00351 sbi(TCCR1A,COM1B1);
00352 cbi(TCCR1A,COM1B0);
00353 }
00354
00355 void timer1PWMAOff(void)
00356 {
00357
00358
00359 cbi(TCCR1A,COM1A1);
00360 cbi(TCCR1A,COM1A0);
00361 }
00362
00363 void timer1PWMBOff(void)
00364 {
00365
00366
00367 cbi(TCCR1A,COM1B1);
00368 cbi(TCCR1A,COM1B0);
00369 }
00370
00371 void timer1PWMASet(u16 pwmDuty)
00372 {
00373
00374
00375
00376
00377
00378
00379
00380 OCR1A = pwmDuty;
00381 }
00382
00383 void timer1PWMBSet(u16 pwmDuty)
00384 {
00385
00386
00387
00388
00389
00390
00391
00392 OCR1B = pwmDuty;
00393 }
00394
00395
00396 TIMER_INTERRUPT_HANDLER(SIG_OVERFLOW0)
00397 {
00398 Timer0Reg0++;
00399
00400
00401 TimerPauseReg++;
00402
00403
00404 if(TimerIntFunc[TIMER0OVERFLOW_INT])
00405 TimerIntFunc[TIMER0OVERFLOW_INT]();
00406 }
00407
00408
00409 TIMER_INTERRUPT_HANDLER(SIG_OVERFLOW1)
00410 {
00411
00412 if(TimerIntFunc[TIMER1OVERFLOW_INT])
00413 TimerIntFunc[TIMER1OVERFLOW_INT]();
00414 }
00415
00416 #ifdef TCNT2 // support timer2 only if it exists
00417
00418 TIMER_INTERRUPT_HANDLER(SIG_OVERFLOW2)
00419 {
00420 Timer2Reg0++;
00421
00422
00423 if(TimerIntFunc[TIMER2OVERFLOW_INT])
00424 TimerIntFunc[TIMER2OVERFLOW_INT]();
00425 }
00426 #endif
00427
00428 #ifdef OCR0
00429
00430
00431 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE0)
00432 {
00433
00434 if(TimerIntFunc[TIMER0OUTCOMPARE_INT])
00435 TimerIntFunc[TIMER0OUTCOMPARE_INT]();
00436 }
00437 #endif
00438
00439
00440 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE1A)
00441 {
00442
00443 if(TimerIntFunc[TIMER1OUTCOMPAREA_INT])
00444 TimerIntFunc[TIMER1OUTCOMPAREA_INT]();
00445 }
00446
00447
00448 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE1B)
00449 {
00450
00451 if(TimerIntFunc[TIMER1OUTCOMPAREB_INT])
00452 TimerIntFunc[TIMER1OUTCOMPAREB_INT]();
00453 }
00454
00455
00456 TIMER_INTERRUPT_HANDLER(SIG_INPUT_CAPTURE1)
00457 {
00458
00459 if(TimerIntFunc[TIMER1INPUTCAPTURE_INT])
00460 TimerIntFunc[TIMER1INPUTCAPTURE_INT]();
00461 }
00462
00463
00464 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE2)
00465 {
00466
00467 if(TimerIntFunc[TIMER2OUTCOMPARE_INT])
00468 TimerIntFunc[TIMER2OUTCOMPARE_INT]();
00469 }