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

Generated on Tue Sep 20 03:11:43 2005 for Procyon AVRlib by  doxygen 1.4.2