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