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