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 "timer128.h"
00028
00029
00030
00031
00032 unsigned short __attribute__ ((progmem)) TimerPrescaleFactor[] = {0,1,8,64,256,1024};
00033
00034
00035 unsigned short __attribute__ ((progmem)) TimerRTCPrescaleFactor[] = {0,1,8,32,64,128,256,1024};
00036
00037
00038
00039 volatile unsigned long TimerPauseReg;
00040 volatile unsigned long Timer0Reg0;
00041 volatile unsigned long Timer0Reg1;
00042 volatile unsigned long Timer2Reg0;
00043 volatile unsigned long Timer2Reg1;
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 timer2Init();
00090 timer3Init();
00091
00092 sei();
00093 }
00094
00095 void timer0Init()
00096 {
00097
00098 timer0SetPrescaler( TIMER0PRESCALE );
00099 outb(TCNT0, 0);
00100 sbi(TIMSK, TOIE0);
00101
00102 timer0ClearOverflowCount();
00103 }
00104
00105 void timer1Init(void)
00106 {
00107
00108 timer1SetPrescaler( TIMER1PRESCALE );
00109 outb(TCNT1H, 0);
00110 outb(TCNT1L, 0);
00111 sbi(TIMSK, TOIE1);
00112 }
00113
00114 void timer2Init(void)
00115 {
00116
00117 timer2SetPrescaler( TIMER2PRESCALE );
00118 outb(TCNT2, 0);
00119 sbi(TIMSK, TOIE2);
00120
00121 timer2ClearOverflowCount();
00122 }
00123
00124 void timer3Init(void)
00125 {
00126
00127 timer3SetPrescaler( TIMER3PRESCALE );
00128 outb(TCNT3H, 0);
00129 outb(TCNT3L, 0);
00130 sbi(ETIMSK, TOIE3);
00131 }
00132
00133 void timer0SetPrescaler(u08 prescale)
00134 {
00135
00136 outb(TCCR0, (inb(TCCR0) & ~TIMER_PRESCALE_MASK) | prescale);
00137 }
00138
00139 void timer1SetPrescaler(u08 prescale)
00140 {
00141
00142 outb(TCCR1B, (inb(TCCR1B) & ~TIMER_PRESCALE_MASK) | prescale);
00143 }
00144
00145 void timer2SetPrescaler(u08 prescale)
00146 {
00147
00148 outb(TCCR2, (inb(TCCR2) & ~TIMER_PRESCALE_MASK) | prescale);
00149 }
00150
00151 void timer3SetPrescaler(u08 prescale)
00152 {
00153
00154 outb(TCCR3B, (inb(TCCR3B) & ~TIMER_PRESCALE_MASK) | prescale);
00155 }
00156
00157 u16 timer0GetPrescaler(void)
00158 {
00159
00160 return (pgm_read_word(TimerPrescaleFactor+(inb(TCCR0) & TIMER_PRESCALE_MASK)));
00161 }
00162
00163 u16 timer1GetPrescaler(void)
00164 {
00165
00166 return (pgm_read_word(TimerPrescaleFactor+(inb(TCCR1B) & TIMER_PRESCALE_MASK)));
00167 }
00168
00169 u16 timer2GetPrescaler(void)
00170 {
00171
00172 return (pgm_read_word(TimerPrescaleFactor+(inb(TCCR2) & TIMER_PRESCALE_MASK)));
00173 }
00174
00175 u16 timer3GetPrescaler(void)
00176 {
00177
00178 return (pgm_read_word(TimerPrescaleFactor+(inb(TCCR3B) & TIMER_PRESCALE_MASK)));
00179 }
00180
00181 void timerAttach(u08 interruptNum, void (*userFunc)(void) )
00182 {
00183
00184 if(interruptNum < TIMER_NUM_INTERRUPTS)
00185 {
00186
00187
00188 TimerIntFunc[interruptNum] = userFunc;
00189 }
00190 }
00191
00192 void timerDetach(u08 interruptNum)
00193 {
00194
00195 if(interruptNum < TIMER_NUM_INTERRUPTS)
00196 {
00197
00198 TimerIntFunc[interruptNum] = 0;
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 = inb(TCNT2);
00211
00212 TimerPauseReg = 0;
00213
00214
00215 ticRateHz = F_CPU/timer2GetPrescaler();
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) | inb(TCNT2)) < (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 void timer0ClearOverflowCount(void)
00237 {
00238
00239 Timer0Reg0 = 0;
00240 Timer0Reg1 = 0;
00241 }
00242
00243 long timer0GetOverflowCount(void)
00244 {
00245
00246
00247 return Timer0Reg0;
00248 }
00249
00250 void timer2ClearOverflowCount(void)
00251 {
00252
00253 Timer2Reg0 = 0;
00254 Timer2Reg1 = 0;
00255 }
00256
00257 long timer2GetOverflowCount(void)
00258 {
00259
00260
00261 return Timer2Reg0;
00262 }
00263
00264
00265 void timer1PWMInit(u08 bitRes)
00266 {
00267
00268
00269
00270
00271 if(bitRes == 9)
00272 {
00273 sbi(TCCR1A,WGMA1);
00274 cbi(TCCR1A,WGMA0);
00275 }
00276 else if( bitRes == 10 )
00277 {
00278 sbi(TCCR1A,WGMA1);
00279 sbi(TCCR1A,WGMA0);
00280 }
00281 else
00282 {
00283 cbi(TCCR1A,WGMA1);
00284 sbi(TCCR1A,WGMA0);
00285 }
00286
00287
00288
00289
00290 outb(OCR1AH, 0);
00291 outb(OCR1AL, 0);
00292
00293 outb(OCR1BH, 0);
00294 outb(OCR1BL, 0);
00295
00296 outb(OCR1CH, 0);
00297 outb(OCR1CL, 0);
00298 }
00299
00300 void timer1PWMInitICR(u16 topcount)
00301 {
00302
00303 cbi(TCCR1A,WGM10);
00304 sbi(TCCR1A,WGM11);
00305 sbi(TCCR1B,WGM12);
00306 sbi(TCCR1B,WGM13);
00307
00308
00309 ICR1H = (u08)(topcount>>8);
00310 ICR1L = (u08)topcount;
00311
00312
00313 outb(OCR1AH, 0);
00314 outb(OCR1AL, 0);
00315
00316 outb(OCR1BH, 0);
00317 outb(OCR1BL, 0);
00318
00319 outb(OCR1CH, 0);
00320 outb(OCR1CL, 0);
00321 }
00322
00323 void timer1PWMOff(void)
00324 {
00325
00326 cbi(TCCR1A,WGMA1);
00327 cbi(TCCR1A,WGMA0);
00328
00329
00330
00331 timer1PWMAOff();
00332 timer1PWMBOff();
00333 timer1PWMCOff();
00334 }
00335
00336 void timer1PWMAOn(void)
00337 {
00338
00339
00340 sbi(TCCR1A,COMA1);
00341 cbi(TCCR1A,COMA0);
00342 }
00343
00344 void timer1PWMBOn(void)
00345 {
00346
00347
00348 sbi(TCCR1A,COMB1);
00349 cbi(TCCR1A,COMB0);
00350 }
00351
00352 void timer1PWMCOn(void)
00353 {
00354
00355
00356 sbi(TCCR1A,COMC1);
00357 cbi(TCCR1A,COMC0);
00358 }
00359
00360 void timer1PWMAOff(void)
00361 {
00362
00363
00364 cbi(TCCR1A,COMA1);
00365 cbi(TCCR1A,COMA0);
00366 }
00367
00368 void timer1PWMBOff(void)
00369 {
00370
00371
00372 cbi(TCCR1A,COMB1);
00373 cbi(TCCR1A,COMB0);
00374 }
00375
00376 void timer1PWMCOff(void)
00377 {
00378
00379
00380 cbi(TCCR1A,COMC1);
00381 cbi(TCCR1A,COMC0);
00382 }
00383
00384 void timer1PWMASet(u16 pwmDuty)
00385 {
00386
00387
00388
00389
00390
00391 outb(OCR1AH, (pwmDuty>>8));
00392 outb(OCR1AL, (pwmDuty&0x00FF));
00393 }
00394
00395 void timer1PWMBSet(u16 pwmDuty)
00396 {
00397
00398
00399
00400
00401
00402 outb(OCR1BH, (pwmDuty>>8));
00403 outb(OCR1BL, (pwmDuty&0x00FF));
00404 }
00405
00406 void timer1PWMCSet(u16 pwmDuty)
00407 {
00408
00409
00410
00411
00412
00413 outb(OCR1CH, (pwmDuty>>8));
00414 outb(OCR1CL, (pwmDuty&0x00FF));
00415 }
00416
00417
00418 void timer3PWMInit(u08 bitRes)
00419 {
00420
00421
00422
00423
00424 if(bitRes == 9)
00425 {
00426 sbi(TCCR3A,WGMA1);
00427 cbi(TCCR3A,WGMA0);
00428 }
00429 else if( bitRes == 10 )
00430 {
00431 sbi(TCCR3A,WGMA1);
00432 sbi(TCCR3A,WGMA0);
00433 }
00434 else
00435 {
00436 cbi(TCCR3A,WGMA1);
00437 sbi(TCCR3A,WGMA0);
00438 }
00439
00440
00441
00442
00443 outb(OCR3AH, 0);
00444 outb(OCR3AL, 0);
00445
00446 outb(OCR3BH, 0);
00447 outb(OCR3BL, 0);
00448
00449 outb(OCR3CH, 0);
00450 outb(OCR3CL, 0);
00451 }
00452
00453 void timer3PWMInitICR(u16 topcount)
00454 {
00455
00456 cbi(TCCR3A,WGM30);
00457 sbi(TCCR3A,WGM31);
00458 sbi(TCCR3B,WGM32);
00459 sbi(TCCR3B,WGM33);
00460
00461
00462 ICR3H = (u08)(topcount>>8);
00463 ICR3L = (u08)topcount;
00464
00465
00466 outb(OCR3AH, 0);
00467 outb(OCR3AL, 0);
00468
00469 outb(OCR3BH, 0);
00470 outb(OCR3BL, 0);
00471
00472 outb(OCR3CH, 0);
00473 outb(OCR3CL, 0);
00474 }
00475
00476 void timer3PWMOff(void)
00477 {
00478
00479 cbi(TCCR3A,WGMA1);
00480 cbi(TCCR3A,WGMA0);
00481
00482
00483
00484 timer3PWMAOff();
00485 timer3PWMBOff();
00486 timer3PWMCOff();
00487 }
00488
00489 void timer3PWMAOn(void)
00490 {
00491
00492
00493 sbi(TCCR3A,COMA1);
00494 cbi(TCCR3A,COMA0);
00495 }
00496
00497 void timer3PWMBOn(void)
00498 {
00499
00500
00501 sbi(TCCR3A,COMB1);
00502 cbi(TCCR3A,COMB0);
00503 }
00504
00505 void timer3PWMCOn(void)
00506 {
00507
00508
00509 sbi(TCCR3A,COMC1);
00510 cbi(TCCR3A,COMC0);
00511 }
00512
00513 void timer3PWMAOff(void)
00514 {
00515
00516
00517 cbi(TCCR3A,COMA1);
00518 cbi(TCCR3A,COMA0);
00519 }
00520
00521 void timer3PWMBOff(void)
00522 {
00523
00524
00525 cbi(TCCR3A,COMB1);
00526 cbi(TCCR3A,COMB0);
00527 }
00528
00529 void timer3PWMCOff(void)
00530 {
00531
00532
00533 cbi(TCCR3A,COMC1);
00534 cbi(TCCR3A,COMC0);
00535 }
00536
00537 void timer3PWMASet(u16 pwmDuty)
00538 {
00539
00540
00541
00542
00543
00544 outb(OCR3AH, (pwmDuty>>8));
00545 outb(OCR3AL, (pwmDuty&0x00FF));
00546 }
00547
00548 void timer3PWMBSet(u16 pwmDuty)
00549 {
00550
00551
00552
00553
00554
00555 outb(OCR3BH, (pwmDuty>>8));
00556 outb(OCR3BL, (pwmDuty&0x00FF));
00557 }
00558
00559 void timer3PWMCSet(u16 pwmDuty)
00560 {
00561
00562
00563
00564
00565
00566 outb(OCR3CH, (pwmDuty>>8));
00567 outb(OCR3CL, (pwmDuty&0x00FF));
00568 }
00569
00570
00571
00572 TIMER_INTERRUPT_HANDLER(SIG_OVERFLOW0)
00573 {
00574 Timer0Reg0++;
00575 if(!Timer0Reg0)
00576 Timer0Reg1++;
00577
00578
00579 if(TimerIntFunc[TIMER0OVERFLOW_INT])
00580 TimerIntFunc[TIMER0OVERFLOW_INT]();
00581 }
00582
00583
00584 TIMER_INTERRUPT_HANDLER(SIG_OVERFLOW1)
00585 {
00586
00587 if(TimerIntFunc[TIMER1OVERFLOW_INT])
00588 TimerIntFunc[TIMER1OVERFLOW_INT]();
00589 }
00590
00591
00592 TIMER_INTERRUPT_HANDLER(SIG_OVERFLOW2)
00593 {
00594 Timer2Reg0++;
00595 if(!Timer2Reg0)
00596 Timer2Reg1++;
00597
00598
00599 TimerPauseReg++;
00600
00601
00602 if(TimerIntFunc[TIMER2OVERFLOW_INT])
00603 TimerIntFunc[TIMER2OVERFLOW_INT]();
00604 }
00605
00606
00607 TIMER_INTERRUPT_HANDLER(SIG_OVERFLOW3)
00608 {
00609
00610 if(TimerIntFunc[TIMER3OVERFLOW_INT])
00611 TimerIntFunc[TIMER3OVERFLOW_INT]();
00612 }
00613
00614
00615 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE0)
00616 {
00617
00618 if(TimerIntFunc[TIMER0OUTCOMPARE_INT])
00619 TimerIntFunc[TIMER0OUTCOMPARE_INT]();
00620 }
00621
00622
00623 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE1A)
00624 {
00625
00626 if(TimerIntFunc[TIMER1OUTCOMPAREA_INT])
00627 TimerIntFunc[TIMER1OUTCOMPAREA_INT]();
00628 }
00629
00630
00631 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE1B)
00632 {
00633
00634 if(TimerIntFunc[TIMER1OUTCOMPAREB_INT])
00635 TimerIntFunc[TIMER1OUTCOMPAREB_INT]();
00636 }
00637
00638
00639 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE1C)
00640 {
00641
00642 if(TimerIntFunc[TIMER1OUTCOMPAREC_INT])
00643 TimerIntFunc[TIMER1OUTCOMPAREC_INT]();
00644 }
00645
00646
00647 TIMER_INTERRUPT_HANDLER(SIG_INPUT_CAPTURE1)
00648 {
00649
00650 if(TimerIntFunc[TIMER1INPUTCAPTURE_INT])
00651 TimerIntFunc[TIMER1INPUTCAPTURE_INT]();
00652 }
00653
00654
00655 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE2)
00656 {
00657
00658 if(TimerIntFunc[TIMER2OUTCOMPARE_INT])
00659 TimerIntFunc[TIMER2OUTCOMPARE_INT]();
00660 }
00661
00662
00663 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE3A)
00664 {
00665
00666 if(TimerIntFunc[TIMER3OUTCOMPAREA_INT])
00667 TimerIntFunc[TIMER3OUTCOMPAREA_INT]();
00668 }
00669
00670
00671 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE3B)
00672 {
00673
00674 if(TimerIntFunc[TIMER3OUTCOMPAREB_INT])
00675 TimerIntFunc[TIMER3OUTCOMPAREB_INT]();
00676 }
00677
00678
00679 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE3C)
00680 {
00681
00682 if(TimerIntFunc[TIMER3OUTCOMPAREC_INT])
00683 TimerIntFunc[TIMER3OUTCOMPAREC_INT]();
00684 }
00685
00686
00687 TIMER_INTERRUPT_HANDLER(SIG_INPUT_CAPTURE3)
00688 {
00689
00690 if(TimerIntFunc[TIMER3INPUTCAPTURE_INT])
00691 TimerIntFunc[TIMER3INPUTCAPTURE_INT]();
00692 }