Main Page   Data Structures   File List   Data Fields   Globals  

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      : 5/6/2002
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 #endif
00024 
00025 #include "global.h"
00026 #include "timer.h"
00027 
00028 // Program ROM constants
00029 // the prescale division values stored in 2^n format
00030 // STOP, CLK, CLK/8, CLK/64, CLK/256, CLK/1024
00031 short __attribute__ ((progmem)) TimerPrescaleFactor[] = {0,0,3,6,8,10};
00032 
00033 // Global variables
00034 // time registers
00035 volatile unsigned long Timer0PauseReg;
00036 volatile unsigned long Timer0Reg0;
00037 volatile unsigned long Timer0Reg1;
00038 volatile unsigned long Timer2Reg0;
00039 volatile unsigned long Timer2Reg1;
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(unsigned short us) 
00048 {
00049     unsigned short delay_loops;
00050     register unsigned short i;
00051 
00052     delay_loops = (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 timerInit(void)
00059 {
00060     u08 intNum;
00061     // detach all user functions from interrupts
00062     for(intNum=0; intNum<TIMER_NUM_INTERRUPTS; intNum++)
00063         timerDetach(intNum);
00064 
00065     // initialize all timers
00066     timer0Init();
00067     timer1Init();
00068     timer2Init();
00069     // enable interrupts
00070     sei();
00071 }
00072 
00073 void timer0Init()
00074 {
00075     // initialize timer 0
00076     timer0SetPrescaler( TIMER0PRESCALE );   // set prescaler
00077     outp(0, TCNT0);                         // reset TCNT0
00078     sbi(TIMSK, TOIE0);                      // enable TCNT0 overflow interrupt
00079 
00080     timer0ClearOverflowCount();             // initialize time registers
00081 }
00082 
00083 void timer1Init(void)
00084 {
00085     // initialize timer 1
00086     timer1SetPrescaler( TIMER1PRESCALE );   // set prescaler
00087     outp(0, TCNT1H);                        // reset TCNT1
00088     outp(0, TCNT1L);
00089     sbi(TIMSK, TOIE1);                      // enable TCNT1 overflow
00090 }
00091 
00092 void timer2Init(void)
00093 {
00094     // initialize timer 2
00095     timer2SetPrescaler( TIMER2PRESCALE );   // set prescaler
00096     outp(0, TCNT2);                         // reset TCNT2
00097     sbi(TIMSK, TOIE2);                      // enable TCNT2 overflow
00098 
00099     timer2ClearOverflowCount();             // initialize time registers
00100 }
00101 
00102 void timer0SetPrescaler(u08 prescale)
00103 {
00104     // set prescaler on timer 0
00105     outp( (inp(TCCR0) & ~TIMER_PRESCALE_MASK) | prescale, TCCR0);
00106 }
00107 
00108 void timer1SetPrescaler(u08 prescale)
00109 {
00110     // set prescaler on timer 1
00111     outp( (inp(TCCR1B) & ~TIMER_PRESCALE_MASK) | prescale, TCCR1B);
00112 }
00113 
00114 void timer2SetPrescaler(u08 prescale)
00115 {
00116     // set prescaler on timer 2
00117     outp( (inp(TCCR2) & ~TIMER_PRESCALE_MASK) | prescale, TCCR2);
00118 }
00119 
00120 void timerAttach(u08 interruptNum, void (*userFunc)(void) )
00121 {
00122     // make sure the interrupt number is within bounds
00123     if(interruptNum < TIMER_NUM_INTERRUPTS)
00124     {
00125         // set the interrupt function to run
00126         // the supplied user's function
00127         TimerIntFunc[interruptNum] = userFunc;
00128     }
00129 }
00130 
00131 void timerDetach(u08 interruptNum)
00132 {
00133     // make sure the interrupt number is within bounds
00134     if(interruptNum < TIMER_NUM_INTERRUPTS)
00135     {
00136         // set the interrupt function to run nothing
00137         TimerIntFunc[interruptNum] = 0;
00138     }
00139 }
00140 /*
00141 u32 timerMsToTics(u16 ms)
00142 {
00143     // calculate the prescaler division rate
00144     u16 prescaleDiv = 1<<(PRG_RDB(TimerPrescaleFactor+inp(TCCR0)));
00145     // calculate the number of timer tics in x milliseconds
00146     return (ms*(F_CPU/(prescaleDiv*256)))/1000;
00147 }
00148 
00149 u16 timerTicsToMs(u32 tics)
00150 {
00151     // calculate the prescaler division rate
00152     u16 prescaleDiv = 1<<(PRG_RDB(TimerPrescaleFactor+inp(TCCR0)));
00153     // calculate the number of milliseconds in x timer tics
00154     return (tics*1000*(prescaleDiv*256))/F_CPU;
00155 }
00156 */
00157 void timerPause(unsigned short pause_ms)
00158 {
00159     // pauses for exactly <pause_ms> number of milliseconds
00160 
00161     // calculate delay for [pause_ms] milliseconds
00162     u16 prescaleDiv = 1<<(PRG_RDB(TimerPrescaleFactor+inp(TCCR0)));
00163     u32 pause = (pause_ms*(F_CPU/(prescaleDiv*256)))/1000;
00164     
00165     Timer0PauseReg = 0;
00166     while(Timer0PauseReg < pause);
00167 }
00168 
00169 void timer0ClearOverflowCount(void)
00170 {
00171     // clear the timer overflow counter registers
00172     Timer0Reg0 = 0; // initialize time registers
00173     Timer0Reg1 = 0; // initialize time registers
00174 }
00175 
00176 long timer0GetOverflowCount(void)
00177 {
00178     // return the current timer overflow count
00179     // (this is since the last timer0ClearOverflowCount() command was called)
00180     return Timer0Reg0;
00181 }
00182 
00183 void timer2ClearOverflowCount(void)
00184 {
00185     // clear the timer overflow counter registers
00186     Timer2Reg0 = 0; // initialize time registers
00187     Timer2Reg1 = 0; // initialize time registers
00188 }
00189 
00190 long timer2GetOverflowCount(void)
00191 {
00192     // return the current timer overflow count
00193     // (this is since the last timer2ClearOverflowCount() command was called)
00194     return Timer2Reg0;
00195 }
00196 
00197 void timer1PWMInit(u08 bitRes)
00198 {
00199     // configures timer1 for use with PWM output
00200     // on OC1A and OC1B pins
00201 
00202     // enable timer1 as 8,9,10bit PWM
00203     if(bitRes == 9)
00204     {   // 9bit mode
00205         sbi(TCCR1A,PWM11);
00206         cbi(TCCR1A,PWM10);
00207     }
00208     else if( bitRes == 10 )
00209     {   // 10bit mode
00210         sbi(TCCR1A,PWM11);
00211         sbi(TCCR1A,PWM10);
00212     }
00213     else
00214     {   // default 8bit mode
00215         cbi(TCCR1A,PWM11);
00216         sbi(TCCR1A,PWM10);
00217     }
00218 
00219     // set clear-timer-on-compare-match
00220     sbi(TCCR1B,CTC1);
00221     // clear output compare value A
00222     outp(0, OCR1AH);
00223     outp(0, OCR1AL);
00224     // clear output compare value B
00225     outp(0, OCR1BH);
00226     outp(0, OCR1BL);
00227 }
00228 
00229 void timer1PWMOff(void)
00230 {
00231     // turn off timer1 PWM mode
00232     cbi(TCCR1A,PWM11);
00233     cbi(TCCR1A,PWM10);
00234     // clear (disable) clear-timer-on-compare-match
00235     cbi(TCCR1B,CTC1);
00236     // set PWM1A/B (OutputCompare action) to none
00237     timer1PWMAOff();
00238     timer1PWMBOff();
00239 }
00240 
00241 void timer1PWMAOn(void)
00242 {
00243     // turn on channel A (OC1A) PWM output
00244     // set PWM1A as non-inverted PWM
00245     sbi(TCCR1A,COM1A1);
00246     cbi(TCCR1A,COM1A0);
00247 }
00248 
00249 void timer1PWMBOn(void)
00250 {
00251     // turn on channel B (OC1B) PWM output
00252     // set PWM1B as non-inverted PWM
00253     sbi(TCCR1A,COM1B1);
00254     cbi(TCCR1A,COM1B0);
00255 }
00256 
00257 void timer1PWMAOff(void)
00258 {
00259     // turn off channel A (OC1A) PWM output
00260     // set PWM1A (OutputCompare action) to none
00261     cbi(TCCR1A,COM1A1);
00262     cbi(TCCR1A,COM1A0);
00263 }
00264 
00265 void timer1PWMBOff(void)
00266 {
00267     // turn off channel B (OC1B) PWM output
00268     // set PWM1B (OutputCompare action) to none
00269     cbi(TCCR1A,COM1B1);
00270     cbi(TCCR1A,COM1B0);
00271 }
00272 
00273 void timer1PWMASet(u16 pwmDuty)
00274 {
00275     // set PWM (output compare) duty for channel A
00276     // this PWM output is generated on OC1A pin
00277     // NOTE:    pwmDuty should be in the range 0-255 for 8bit PWM
00278     //          pwmDuty should be in the range 0-511 for 9bit PWM
00279     //          pwmDuty should be in the range 0-1023 for 10bit PWM
00280     outp( (pwmDuty>>8), OCR1AH);        // set the high 8bits of OCR1A
00281     outp( (pwmDuty&0x00FF), OCR1AL);    // set the low 8bits of OCR1A
00282 }
00283 
00284 void timer1PWMBSet(u16 pwmDuty)
00285 {
00286     // set PWM (output compare) duty for channel B
00287     // this PWM output is generated on OC1B pin
00288     // NOTE:    pwmDuty should be in the range 0-255 for 8bit PWM
00289     //          pwmDuty should be in the range 0-511 for 9bit PWM
00290     //          pwmDuty should be in the range 0-1023 for 10bit PWM
00291     outp( (pwmDuty>>8), OCR1BH);        // set the high 8bits of OCR1B
00292     outp( (pwmDuty&0x00FF), OCR1BL); // set the low 8bits of OCR1B
00293 }
00294 
00295 //! Interrupt handler for tcnt0 overflow interrupt
00296 SIGNAL(SIG_OVERFLOW0)
00297 {
00298     Timer0Reg0++;           // increment low-order counter
00299     if(!Timer0Reg0)     // if low-order counter rollover
00300         Timer0Reg1++;       // increment high-order counter 
00301 
00302     // increment pause counter
00303     Timer0PauseReg++;
00304 
00305     // if a user function is defined, execute it too
00306     if(TimerIntFunc[TIMER0OVERFLOW_INT])
00307         TimerIntFunc[TIMER0OVERFLOW_INT]();
00308 }
00309 
00310 //! Interrupt handler for tcnt1 overflow interrupt
00311 SIGNAL(SIG_OVERFLOW1)
00312 {
00313     // if a user function is defined, execute it
00314     if(TimerIntFunc[TIMER1OVERFLOW_INT])
00315         TimerIntFunc[TIMER1OVERFLOW_INT]();
00316 }
00317 
00318 //! Interrupt handler for tcnt2 overflow interrupt
00319 SIGNAL(SIG_OVERFLOW2)
00320 {
00321     Timer2Reg0++;           // increment low-order counter
00322     if(!Timer2Reg0)     // if low-order counter rollover
00323         Timer2Reg1++;       // increment high-order counter 
00324 
00325     // if a user function is defined, execute it
00326     if(TimerIntFunc[TIMER2OVERFLOW_INT])
00327         TimerIntFunc[TIMER2OVERFLOW_INT]();
00328 }
00329 
00330 //! Interrupt handler for CutputCompare1A match (OC1A) interrupt
00331 SIGNAL(SIG_OUTPUT_COMPARE1A)
00332 {
00333     // if a user function is defined, execute it
00334     if(TimerIntFunc[TIMER1OUTCOMPAREA_INT])
00335         TimerIntFunc[TIMER1OUTCOMPAREA_INT]();
00336 }
00337 
00338 //! Interrupt handler for OutputCompare1B match (OC1B) interrupt
00339 SIGNAL(SIG_OUTPUT_COMPARE1B)
00340 {
00341     // if a user function is defined, execute it
00342     if(TimerIntFunc[TIMER1OUTCOMPAREB_INT])
00343         TimerIntFunc[TIMER1OUTCOMPAREB_INT]();
00344 }
00345 
00346 //! Interrupt handler for InputCapture1 (IC1) interrupt
00347 SIGNAL(SIG_INPUT_CAPTURE1)
00348 {
00349     // if a user function is defined, execute it
00350     if(TimerIntFunc[TIMER1INPUTCAPTURE_INT])
00351         TimerIntFunc[TIMER1INPUTCAPTURE_INT]();
00352 }
00353 
00354 //! Interrupt handler for OutputCompare2 match (OC2) interrupt
00355 SIGNAL(SIG_OUTPUT_COMPARE2)
00356 {
00357     // if a user function is defined, execute it
00358     if(TimerIntFunc[TIMER2OUTCOMPARE_INT])
00359         TimerIntFunc[TIMER2OUTCOMPARE_INT]();
00360 }

Generated on Tue Feb 4 20:18:37 2003 for Procyon AVRlib by doxygen1.3-rc2