Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

timerx8.c

Go to the documentation of this file.
00001 /*! \file timerx8.c \brief Timer function library for ATmegaXX8 Processors. */
00002 //*****************************************************************************
00003 //
00004 // File Name    : 'timerx8.c'
00005 // Title        : Timer function library for ATmegaXX8 Processors
00006 // Author       : Pascal Stang - Copyright (C) 2000-2005
00007 // Created      : 11/22/2000
00008 // Revised      : 06/15/2005
00009 // Version      : 1.0
00010 // Target MCU   : Atmel AVR Series
00011 // Editor Tabs  : 4
00012 //
00013 // This code is distributed under the GNU Public License
00014 //      which can be found at http://www.gnu.org/licenses/gpl.txt
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 "timerx8.h"
00025 
00026 // Program ROM constants
00027 // the prescale division values stored in order of timer control register index
00028 // STOP, CLK, CLK/8, CLK/64, CLK/256, CLK/1024
00029 unsigned short __attribute__ ((progmem)) TimerPrescaleFactor[] = {0,1,8,64,256,1024};
00030 // the prescale division values stored in order of timer control register index
00031 // STOP, CLK, CLK/8, CLK/32, CLK/64, CLK/128, CLK/256, CLK/1024
00032 unsigned short __attribute__ ((progmem)) TimerRTCPrescaleFactor[] = {0,1,8,32,64,128,256,1024};
00033 
00034 // Global variables
00035 // time registers
00036 volatile unsigned long TimerPauseReg;
00037 volatile unsigned long Timer0Reg0;
00038 volatile unsigned long Timer2Reg0;
00039 
00040 typedef void (*voidFuncPtr)(void);
00041 volatile static voidFuncPtr TimerIntFunc[TIMER_NUM_INTERRUPTS];
00042 
00043 // delay for a minimum of <us> microseconds 
00044 // the time resolution is dependent on the time the loop takes 
00045 // e.g. with 4Mhz and 5 cycles per loop, the resolution is 1.25 us 
00046 void delay_us(unsigned short time_us) 
00047 {
00048     unsigned short delay_loops;
00049     register unsigned short i;
00050 
00051     delay_loops = (time_us+3)/5*CYCLES_PER_US; // +3 for rounding up (dirty) 
00052 
00053     // one loop takes 5 cpu cycles 
00054     for (i=0; i < delay_loops; i++) {};
00055 }
00056 /*
00057 void delay_ms(unsigned char time_ms)
00058 {
00059     unsigned short delay_count = F_CPU / 4000;
00060 
00061     unsigned short cnt;
00062     asm volatile ("\n"
00063                   "L_dl1%=:\n\t"
00064                   "mov %A0, %A2\n\t"
00065                   "mov %B0, %B2\n"
00066                   "L_dl2%=:\n\t"
00067                   "sbiw %A0, 1\n\t"
00068                   "brne L_dl2%=\n\t"
00069                   "dec %1\n\t" "brne L_dl1%=\n\t":"=&w" (cnt)
00070                   :"r"(time_ms), "r"((unsigned short) (delay_count))
00071     );
00072 }
00073 */
00074 void timerInit(void)
00075 {
00076     u08 intNum;
00077     // detach all user functions from interrupts
00078     for(intNum=0; intNum<TIMER_NUM_INTERRUPTS; intNum++)
00079         timerDetach(intNum);
00080 
00081     // initialize all timers
00082     timer0Init();
00083     timer1Init();
00084     #ifdef TCNT2    // support timer2 only if it exists
00085     timer2Init();
00086     #endif
00087     // enable interrupts
00088     sei();
00089 }
00090 
00091 void timer0Init()
00092 {
00093     // initialize timer 0
00094     timer0SetPrescaler( TIMER0PRESCALE );   // set prescaler
00095     TCNT0 = 0;                              // reset TCNT0
00096     sbi(TIMSK0, TOIE0);                     // enable TCNT0 overflow interrupt
00097 
00098     timer0ClearOverflowCount();             // initialize time registers
00099 }
00100 
00101 void timer1Init(void)
00102 {
00103     // initialize timer 1
00104     timer1SetPrescaler( TIMER1PRESCALE );   // set prescaler
00105     TCNT1 = 0;                              // reset TCNT1
00106     sbi(TIMSK1, TOIE1);                     // enable TCNT1 overflow
00107 }
00108 
00109 #ifdef TCNT2    // support timer2 only if it exists
00110 void timer2Init(void)
00111 {
00112     // initialize timer 2
00113     timer2SetPrescaler( TIMER2PRESCALE );   // set prescaler
00114     TCNT2 = 0;                              // reset TCNT2
00115     sbi(TIMSK2, TOIE2);                     // enable TCNT2 overflow
00116 
00117     timer2ClearOverflowCount();             // initialize time registers
00118 }
00119 #endif
00120 
00121 void timer0SetPrescaler(u08 prescale)
00122 {
00123     // set prescaler on timer 0
00124     TCCR0B = ((TCCR0B & ~TIMER_PRESCALE_MASK) | prescale);
00125 }
00126 
00127 void timer1SetPrescaler(u08 prescale)
00128 {
00129     // set prescaler on timer 1
00130     TCCR1B = ((TCCR1B & ~TIMER_PRESCALE_MASK) | prescale);
00131 }
00132 
00133 #ifdef TCNT2    // support timer2 only if it exists
00134 void timer2SetPrescaler(u08 prescale)
00135 {
00136     // set prescaler on timer 2
00137     TCCR2B = ((TCCR2B & ~TIMER_PRESCALE_MASK) | prescale);
00138 }
00139 #endif
00140 
00141 u16 timer0GetPrescaler(void)
00142 {
00143     // get the current prescaler setting
00144     return (pgm_read_word(TimerPrescaleFactor+(TCCR0B & TIMER_PRESCALE_MASK)));
00145 }
00146 
00147 u16 timer1GetPrescaler(void)
00148 {
00149     // get the current prescaler setting
00150     return (pgm_read_word(TimerPrescaleFactor+(TCCR1B & TIMER_PRESCALE_MASK)));
00151 }
00152 
00153 #ifdef TCNT2    // support timer2 only if it exists
00154 u16 timer2GetPrescaler(void)
00155 {
00156     //TODO: can we assume for all 3-timer AVR processors,
00157     // that timer2 is the RTC timer?
00158 
00159     // get the current prescaler setting
00160     return (pgm_read_word(TimerRTCPrescaleFactor+(TCCR2B & TIMER_PRESCALE_MASK)));
00161 }
00162 #endif
00163 
00164 void timerAttach(u08 interruptNum, void (*userFunc)(void) )
00165 {
00166     // make sure the interrupt number is within bounds
00167     if(interruptNum < TIMER_NUM_INTERRUPTS)
00168     {
00169         // set the interrupt function to run
00170         // the supplied user's function
00171         TimerIntFunc[interruptNum] = userFunc;
00172     }
00173 }
00174 
00175 void timerDetach(u08 interruptNum)
00176 {
00177     // make sure the interrupt number is within bounds
00178     if(interruptNum < TIMER_NUM_INTERRUPTS)
00179     {
00180         // set the interrupt function to run nothing
00181         TimerIntFunc[interruptNum] = 0;
00182     }
00183 }
00184 /*
00185 u32 timerMsToTics(u16 ms)
00186 {
00187     // calculate the prescaler division rate
00188     u16 prescaleDiv = 1<<(pgm_read_byte(TimerPrescaleFactor+inb(TCCR0)));
00189     // calculate the number of timer tics in x milliseconds
00190     return (ms*(F_CPU/(prescaleDiv*256)))/1000;
00191 }
00192 
00193 u16 timerTicsToMs(u32 tics)
00194 {
00195     // calculate the prescaler division rate
00196     u16 prescaleDiv = 1<<(pgm_read_byte(TimerPrescaleFactor+inb(TCCR0)));
00197     // calculate the number of milliseconds in x timer tics
00198     return (tics*1000*(prescaleDiv*256))/F_CPU;
00199 }
00200 */
00201 void timerPause(unsigned short pause_ms)
00202 {
00203     // pauses for exactly <pause_ms> number of milliseconds
00204     u08 timerThres;
00205     u32 ticRateHz;
00206     u32 pause;
00207 
00208     // capture current pause timer value
00209     timerThres = TCNT0;
00210     // reset pause timer overflow count
00211     TimerPauseReg = 0;
00212     // calculate delay for [pause_ms] milliseconds
00213     // prescaler division = 1<<(pgm_read_byte(TimerPrescaleFactor+inb(TCCR0)))
00214     ticRateHz = F_CPU/timer0GetPrescaler();
00215     // precision management
00216     // prevent overflow and precision underflow
00217     //  -could add more conditions to improve accuracy
00218     if( ((ticRateHz < 429497) && (pause_ms <= 10000)) )
00219         pause = (pause_ms*ticRateHz)/1000;
00220     else
00221         pause = pause_ms*(ticRateHz/1000);
00222 
00223     // loop until time expires
00224     while( ((TimerPauseReg<<8) | (TCNT0)) < (pause+timerThres) )
00225     {
00226         if( TimerPauseReg < (pause>>8));
00227         {
00228             // save power by idling the processor
00229             set_sleep_mode(SLEEP_MODE_IDLE);
00230             sleep_mode();
00231         }
00232     }
00233 
00234     /* old inaccurate code, for reference
00235     
00236     // calculate delay for [pause_ms] milliseconds
00237     u16 prescaleDiv = 1<<(pgm_read_byte(TimerPrescaleFactor+inb(TCCR0)));
00238     u32 pause = (pause_ms*(F_CPU/(prescaleDiv*256)))/1000;
00239     
00240     TimerPauseReg = 0;
00241     while(TimerPauseReg < pause);
00242 
00243     */
00244 }
00245 
00246 void timer0ClearOverflowCount(void)
00247 {
00248     // clear the timer overflow counter registers
00249     Timer0Reg0 = 0; // initialize time registers
00250 }
00251 
00252 long timer0GetOverflowCount(void)
00253 {
00254     // return the current timer overflow count
00255     // (this is since the last timer0ClearOverflowCount() command was called)
00256     return Timer0Reg0;
00257 }
00258 
00259 #ifdef TCNT2    // support timer2 only if it exists
00260 void timer2ClearOverflowCount(void)
00261 {
00262     // clear the timer overflow counter registers
00263     Timer2Reg0 = 0; // initialize time registers
00264 }
00265 
00266 long timer2GetOverflowCount(void)
00267 {
00268     // return the current timer overflow count
00269     // (this is since the last timer2ClearOverflowCount() command was called)
00270     return Timer2Reg0;
00271 }
00272 #endif
00273 
00274 void timer1PWMInit(u08 bitRes)
00275 {
00276     // configures timer1 for use with PWM output
00277     // on OC1A and OC1B pins
00278 
00279     // enable timer1 as 8,9,10bit PWM
00280     if(bitRes == 9)
00281     {   // 9bit mode
00282         sbi(TCCR1A,PWM11);
00283         cbi(TCCR1A,PWM10);
00284     }
00285     else if( bitRes == 10 )
00286     {   // 10bit mode
00287         sbi(TCCR1A,PWM11);
00288         sbi(TCCR1A,PWM10);
00289     }
00290     else
00291     {   // default 8bit mode
00292         cbi(TCCR1A,PWM11);
00293         sbi(TCCR1A,PWM10);
00294     }
00295 
00296     // clear output compare value A
00297     OCR1A = 0;
00298     // clear output compare value B
00299     OCR1B = 0;
00300 }
00301 
00302 #ifdef WGM10
00303 // include support for arbitrary top-count PWM
00304 // on new AVR processors that support it
00305 void timer1PWMInitICR(u16 topcount)
00306 {
00307     // set PWM mode with ICR top-count
00308     cbi(TCCR1A,WGM10);
00309     sbi(TCCR1A,WGM11);
00310     sbi(TCCR1B,WGM12);
00311     sbi(TCCR1B,WGM13);
00312     
00313     // set top count value
00314     ICR1 = topcount;
00315     
00316     // clear output compare value A
00317     OCR1A = 0;
00318     // clear output compare value B
00319     OCR1B = 0;
00320 
00321 }
00322 #endif
00323 
00324 void timer1PWMOff(void)
00325 {
00326     // turn off timer1 PWM mode
00327     cbi(TCCR1A,PWM11);
00328     cbi(TCCR1A,PWM10);
00329     // set PWM1A/B (OutputCompare action) to none
00330     timer1PWMAOff();
00331     timer1PWMBOff();
00332 }
00333 
00334 void timer1PWMAOn(void)
00335 {
00336     // turn on channel A (OC1A) PWM output
00337     // set OC1A as non-inverted PWM
00338     sbi(TCCR1A,COM1A1);
00339     cbi(TCCR1A,COM1A0);
00340 }
00341 
00342 void timer1PWMBOn(void)
00343 {
00344     // turn on channel B (OC1B) PWM output
00345     // set OC1B as non-inverted PWM
00346     sbi(TCCR1A,COM1B1);
00347     cbi(TCCR1A,COM1B0);
00348 }
00349 
00350 void timer1PWMAOff(void)
00351 {
00352     // turn off channel A (OC1A) PWM output
00353     // set OC1A (OutputCompare action) to none
00354     cbi(TCCR1A,COM1A1);
00355     cbi(TCCR1A,COM1A0);
00356 }
00357 
00358 void timer1PWMBOff(void)
00359 {
00360     // turn off channel B (OC1B) PWM output
00361     // set OC1B (OutputCompare action) to none
00362     cbi(TCCR1A,COM1B1);
00363     cbi(TCCR1A,COM1B0);
00364 }
00365 
00366 void timer1PWMASet(u16 pwmDuty)
00367 {
00368     // set PWM (output compare) duty for channel A
00369     // this PWM output is generated on OC1A pin
00370     // NOTE:    pwmDuty should be in the range 0-255 for 8bit PWM
00371     //          pwmDuty should be in the range 0-511 for 9bit PWM
00372     //          pwmDuty should be in the range 0-1023 for 10bit PWM
00373     //outp( (pwmDuty>>8), OCR1AH);      // set the high 8bits of OCR1A
00374     //outp( (pwmDuty&0x00FF), OCR1AL);  // set the low 8bits of OCR1A
00375     OCR1A = pwmDuty;
00376 }
00377 
00378 void timer1PWMBSet(u16 pwmDuty)
00379 {
00380     // set PWM (output compare) duty for channel B
00381     // this PWM output is generated on OC1B pin
00382     // NOTE:    pwmDuty should be in the range 0-255 for 8bit PWM
00383     //          pwmDuty should be in the range 0-511 for 9bit PWM
00384     //          pwmDuty should be in the range 0-1023 for 10bit PWM
00385     //outp( (pwmDuty>>8), OCR1BH);      // set the high 8bits of OCR1B
00386     //outp( (pwmDuty&0x00FF), OCR1BL);  // set the low 8bits of OCR1B
00387     OCR1B = pwmDuty;
00388 }
00389 
00390 //! Interrupt handler for tcnt0 overflow interrupt
00391 TIMER_INTERRUPT_HANDLER(SIG_OVERFLOW0)
00392 {
00393     Timer0Reg0++;           // increment low-order counter
00394 
00395     // increment pause counter
00396     TimerPauseReg++;
00397 
00398     // if a user function is defined, execute it too
00399     if(TimerIntFunc[TIMER0OVERFLOW_INT])
00400         TimerIntFunc[TIMER0OVERFLOW_INT]();
00401 }
00402 
00403 //! Interrupt handler for tcnt1 overflow interrupt
00404 TIMER_INTERRUPT_HANDLER(SIG_OVERFLOW1)
00405 {
00406     // if a user function is defined, execute it
00407     if(TimerIntFunc[TIMER1OVERFLOW_INT])
00408         TimerIntFunc[TIMER1OVERFLOW_INT]();
00409 }
00410 
00411 #ifdef TCNT2    // support timer2 only if it exists
00412 //! Interrupt handler for tcnt2 overflow interrupt
00413 TIMER_INTERRUPT_HANDLER(SIG_OVERFLOW2)
00414 {
00415     Timer2Reg0++;           // increment low-order counter
00416 
00417     // if a user function is defined, execute it
00418     if(TimerIntFunc[TIMER2OVERFLOW_INT])
00419         TimerIntFunc[TIMER2OVERFLOW_INT]();
00420 }
00421 #endif
00422 
00423 #ifdef OCR0
00424 // include support for Output Compare 0 for new AVR processors that support it
00425 //! Interrupt handler for OutputCompare0 match (OC0) interrupt
00426 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE0)
00427 {
00428     // if a user function is defined, execute it
00429     if(TimerIntFunc[TIMER0OUTCOMPARE_INT])
00430         TimerIntFunc[TIMER0OUTCOMPARE_INT]();
00431 }
00432 #endif
00433 
00434 //! Interrupt handler for CutputCompare1A match (OC1A) interrupt
00435 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE1A)
00436 {
00437     // if a user function is defined, execute it
00438     if(TimerIntFunc[TIMER1OUTCOMPAREA_INT])
00439         TimerIntFunc[TIMER1OUTCOMPAREA_INT]();
00440 }
00441 
00442 //! Interrupt handler for OutputCompare1B match (OC1B) interrupt
00443 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE1B)
00444 {
00445     // if a user function is defined, execute it
00446     if(TimerIntFunc[TIMER1OUTCOMPAREB_INT])
00447         TimerIntFunc[TIMER1OUTCOMPAREB_INT]();
00448 }
00449 
00450 //! Interrupt handler for InputCapture1 (IC1) interrupt
00451 TIMER_INTERRUPT_HANDLER(SIG_INPUT_CAPTURE1)
00452 {
00453     // if a user function is defined, execute it
00454     if(TimerIntFunc[TIMER1INPUTCAPTURE_INT])
00455         TimerIntFunc[TIMER1INPUTCAPTURE_INT]();
00456 }
00457 
00458 //! Interrupt handler for OutputCompare2A match (OC2A) interrupt
00459 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE2A)
00460 {
00461     // if a user function is defined, execute it
00462     if(TimerIntFunc[TIMER2OUTCOMPARE_INT])
00463         TimerIntFunc[TIMER2OUTCOMPARE_INT]();
00464 }
00465 
00466 //! Interrupt handler for OutputCompare2B match (OC2B) interrupt
00467 TIMER_INTERRUPT_HANDLER(SIG_OUTPUT_COMPARE2B)
00468 {
00469     // if a user function is defined, execute it
00470     if(TimerIntFunc[TIMER2OUTCOMPARE_INT])
00471         TimerIntFunc[TIMER2OUTCOMPARE_INT]();
00472 }

Generated on Sun Oct 29 03:41:07 2006 for Procyon AVRlib by  doxygen 1.4.2