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

timer.c

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

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