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