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