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