Main Page   Data Structures   File List   Data Fields   Globals  

timer128.c

Go to the documentation of this file.
00001 /*! \file timer128.c \brief System Timer function library for Mega128. */
00002 //*****************************************************************************
00003 //
00004 // File Name    : 'timer128.c'
00005 // Title        : System Timer function library for Mega128
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 "timer128.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 TimerPauseReg;
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     timer3Init();
00070     // enable interrupts
00071     sei();
00072 }
00073 
00074 void timer0Init()
00075 {
00076     // initialize timer 0
00077     timer0SetPrescaler( TIMER0PRESCALE );   // set prescaler
00078     outp(0, TCNT0);                         // reset TCNT0
00079     sbi(TIMSK, TOIE0);                      // enable TCNT0 overflow interrupt
00080 
00081     timer0ClearOverflowCount();             // initialize time registers
00082 }
00083 
00084 void timer1Init(void)
00085 {
00086     // initialize timer 1
00087     timer1SetPrescaler( TIMER1PRESCALE );   // set prescaler
00088     outp(0, TCNT1H);                        // reset TCNT1
00089     outp(0, TCNT1L);
00090     sbi(TIMSK, TOIE1);                      // enable TCNT1 overflow
00091 }
00092 
00093 void timer2Init(void)
00094 {
00095     // initialize timer 2
00096     timer2SetPrescaler( TIMER2PRESCALE );   // set prescaler
00097     outp(0, TCNT2);                         // reset TCNT2
00098     sbi(TIMSK, TOIE2);                      // enable TCNT2 overflow
00099 
00100     timer2ClearOverflowCount();             // initialize time registers
00101 }
00102 
00103 void timer3Init(void)
00104 {
00105     // initialize timer 3
00106     timer3SetPrescaler( TIMER3PRESCALE );   // set prescaler
00107     outp(0, TCNT3H);                        // reset TCNT3
00108     outp(0, TCNT3L);
00109     sbi(TIMSK, TOIE3);                      // enable TCNT3 overflow
00110 }
00111 
00112 void timer0SetPrescaler(u08 prescale)
00113 {
00114     // set prescaler on timer 0
00115     outp( (inp(TCCR0) & ~TIMER_PRESCALE_MASK) | prescale, TCCR0);
00116 }
00117 
00118 void timer1SetPrescaler(u08 prescale)
00119 {
00120     // set prescaler on timer 1
00121     outp( (inp(TCCR1B) & ~TIMER_PRESCALE_MASK) | prescale, TCCR1B);
00122 }
00123 
00124 void timer2SetPrescaler(u08 prescale)
00125 {
00126     // set prescaler on timer 2
00127     outp( (inp(TCCR2) & ~TIMER_PRESCALE_MASK) | prescale, TCCR2);
00128 }
00129 
00130 void timer3SetPrescaler(u08 prescale)
00131 {
00132     // set prescaler on timer 2
00133     outp( (inp(TCCR3B) & ~TIMER_PRESCALE_MASK) | prescale, TCCR3B);
00134 }
00135 
00136 void timerAttach(u08 interruptNum, void (*userFunc)(void) )
00137 {
00138     // make sure the interrupt number is within bounds
00139     if(interruptNum < TIMER_NUM_INTERRUPTS)
00140     {
00141         // set the interrupt function to run
00142         // the supplied user's function
00143         TimerIntFunc[interruptNum] = userFunc;
00144     }
00145 }
00146 
00147 void timerDetach(u08 interruptNum)
00148 {
00149     // make sure the interrupt number is within bounds
00150     if(interruptNum < TIMER_NUM_INTERRUPTS)
00151     {
00152         // set the interrupt function to run nothing
00153         TimerIntFunc[interruptNum] = 0;
00154     }
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     TimerPauseReg = 0;
00166     while(TimerPauseReg < 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 
00198 void timer1PWMInit(u08 bitRes)
00199 {
00200     // configures timer1 for use with PWM output
00201     // on pins OC1A, OC1B, and OC1C
00202 
00203     // enable Timer1 as 8,9,10bit PWM
00204     if(bitRes == 9)
00205     {   // 9bit mode
00206         sbi(TCCR1A,WGMA1);
00207         cbi(TCCR1A,WGMA0);
00208     }
00209     else if( bitRes == 10 )
00210     {   // 10bit mode
00211         sbi(TCCR1A,WGMA1);
00212         sbi(TCCR1A,WGMA0);
00213     }
00214     else
00215     {   // default 8bit mode
00216         cbi(TCCR1A,WGMA1);
00217         sbi(TCCR1A,WGMA0);
00218     }
00219 
00220     // set clear-timer-on-compare-match
00221     //cbi(TCCR1B,CTC1);
00222     // set PWM1A as non-inverted PWM
00223     sbi(TCCR1A,COMA1);
00224     cbi(TCCR1A,COMA0);
00225     // set PWM1B as non-inverted PWM
00226     sbi(TCCR1A,COMB1);
00227     cbi(TCCR1A,COMB0);
00228     // set PWM1B as non-inverted PWM
00229     sbi(TCCR1A,COMC1);
00230     cbi(TCCR1A,COMC0);
00231     // clear output compare value A
00232     outp(0, OCR1AH);
00233     outp(0, OCR1AL);
00234     // clear output compare value B
00235     outp(0, OCR1BH);
00236     outp(0, OCR1BL);
00237     // clear output compare value B
00238     outp(0, OCR1CH);
00239     outp(0, OCR1CL);
00240 }
00241 
00242 void timer1PWMOff(void)
00243 {
00244     // turn off PWM on OC1A and OC1B
00245     cbi(TCCR1A,WGMA1);
00246     cbi(TCCR1A,WGMA0);
00247     // clear (disable) clear-timer-on-compare-match
00248     //cbi(TCCR1B,CTC1);
00249     // set PWM1A (OutputCompare action) to none
00250     cbi(TCCR1A,COMA1);
00251     cbi(TCCR1A,COMA0);
00252     // set PWM1B (OutputCompare action) to none
00253     cbi(TCCR1A,COMB1);
00254     cbi(TCCR1A,COMB0);
00255 }
00256 
00257 void timer1PWMASet(u16 pwmDuty)
00258 {
00259     // set PWM (output compare) duty for channel A
00260     // this PWM output is generated on OC1A pin
00261     // NOTE:    pwmDuty should be in the range 0-255 for 8bit PWM
00262     //          pwmDuty should be in the range 0-511 for 9bit PWM
00263     //          pwmDuty should be in the range 0-1023 for 10bit PWM
00264     outp( (pwmDuty>>8), OCR1AH);        // set the high 8bits of OCR1A
00265     outp( (pwmDuty&0x00FF), OCR1AL);    // set the low 8bits of OCR1A
00266 }
00267 
00268 void timer1PWMBSet(u16 pwmDuty)
00269 {
00270     // set PWM (output compare) duty for channel B
00271     // this PWM output is generated on OC1B pin
00272     // NOTE:    pwmDuty should be in the range 0-255 for 8bit PWM
00273     //          pwmDuty should be in the range 0-511 for 9bit PWM
00274     //          pwmDuty should be in the range 0-1023 for 10bit PWM
00275     outp( (pwmDuty>>8), OCR1BH);        // set the high 8bits of OCR1B
00276     outp( (pwmDuty&0x00FF), OCR1BL); // set the low 8bits of OCR1B
00277 }
00278 
00279 void timer1PWMCSet(u16 pwmDuty)
00280 {
00281     // set PWM (output compare) duty for channel B
00282     // this PWM output is generated on OC1B pin
00283     // NOTE:    pwmDuty should be in the range 0-255 for 8bit PWM
00284     //          pwmDuty should be in the range 0-511 for 9bit PWM
00285     //          pwmDuty should be in the range 0-1023 for 10bit PWM
00286     outp( (pwmDuty>>8), OCR1CH);        // set the high 8bits of OCR1B
00287     outp( (pwmDuty&0x00FF), OCR1CL); // set the low 8bits of OCR1B
00288 }
00289 
00290 
00291 void timer3PWMInit(u08 bitRes)
00292 {
00293     // configures timer1 for use with PWM output
00294     // on pins OC1A, OC1B, and OC1C
00295 
00296     // enable Timer1 as 8,9,10bit PWM
00297     if(bitRes == 9)
00298     {   // 9bit mode
00299         sbi(TCCR3A,WGMA1);
00300         cbi(TCCR3A,WGMA0);
00301     }
00302     else if( bitRes == 10 )
00303     {   // 10bit mode
00304         sbi(TCCR3A,WGMA1);
00305         sbi(TCCR3A,WGMA0);
00306     }
00307     else
00308     {   // default 8bit mode
00309         cbi(TCCR3A,WGMA1);
00310         sbi(TCCR3A,WGMA0);
00311     }
00312 
00313     // set clear-timer-on-compare-match
00314     //cbi(TCCR1B,CTC1);
00315     // set PWM1A as non-inverted PWM
00316     sbi(TCCR3A,COMA1);
00317     cbi(TCCR3A,COMA0);
00318     // set PWM1B as non-inverted PWM
00319     sbi(TCCR3A,COMB1);
00320     cbi(TCCR3A,COMB0);
00321     // set PWM1B as non-inverted PWM
00322     sbi(TCCR3A,COMC1);
00323     cbi(TCCR3A,COMC0);
00324     // clear output compare value A
00325     outp(0, OCR3AH);
00326     outp(0, OCR3AL);
00327     // clear output compare value B
00328     outp(0, OCR3BH);
00329     outp(0, OCR3BL);
00330     // clear output compare value B
00331     outp(0, OCR3CH);
00332     outp(0, OCR3CL);
00333 }
00334 
00335 void timer3PWMOff(void)
00336 {
00337     // turn off PWM on OC1A and OC1B
00338     cbi(TCCR3A,WGMA1);
00339     cbi(TCCR3A,WGMA0);
00340     // clear (disable) clear-timer-on-compare-match
00341     //cbi(TCCR1B,CTC1);
00342     // set PWM1A (OutputCompare action) to none
00343     cbi(TCCR3A,COMA1);
00344     cbi(TCCR3A,COMA0);
00345     // set PWM1B (OutputCompare action) to none
00346     cbi(TCCR3A,COMB1);
00347     cbi(TCCR3A,COMB0);
00348 }
00349 
00350 void timer3PWMASet(u16 pwmDuty)
00351 {
00352     // set PWM (output compare) duty for channel A
00353     // this PWM output is generated on OC1A pin
00354     // NOTE:    pwmDuty should be in the range 0-255 for 8bit PWM
00355     //          pwmDuty should be in the range 0-511 for 9bit PWM
00356     //          pwmDuty should be in the range 0-1023 for 10bit PWM
00357     outp( (pwmDuty>>8), OCR3AH);        // set the high 8bits of OCR1A
00358     outp( (pwmDuty&0x00FF), OCR3AL);    // set the low 8bits of OCR1A
00359 }
00360 
00361 void timer3PWMBSet(u16 pwmDuty)
00362 {
00363     // set PWM (output compare) duty for channel B
00364     // this PWM output is generated on OC1B pin
00365     // NOTE:    pwmDuty should be in the range 0-255 for 8bit PWM
00366     //          pwmDuty should be in the range 0-511 for 9bit PWM
00367     //          pwmDuty should be in the range 0-1023 for 10bit PWM
00368     outp( (pwmDuty>>8), OCR3BH);        // set the high 8bits of OCR1B
00369     outp( (pwmDuty&0x00FF), OCR3BL); // set the low 8bits of OCR1B
00370 }
00371 
00372 void timer3PWMCSet(u16 pwmDuty)
00373 {
00374     // set PWM (output compare) duty for channel B
00375     // this PWM output is generated on OC1B pin
00376     // NOTE:    pwmDuty should be in the range 0-255 for 8bit PWM
00377     //          pwmDuty should be in the range 0-511 for 9bit PWM
00378     //          pwmDuty should be in the range 0-1023 for 10bit PWM
00379     outp( (pwmDuty>>8), OCR3CH);        // set the high 8bits of OCR1B
00380     outp( (pwmDuty&0x00FF), OCR3CL); // set the low 8bits of OCR1B
00381 }
00382 
00383 
00384 //! Interrupt handler for tcnt0 overflow interrupt
00385 SIGNAL(SIG_OVERFLOW0)
00386 {
00387     Timer0Reg0++;           // increment low-order counter
00388     if(!Timer0Reg0)     // if low-order counter rollover
00389         Timer0Reg1++;       // increment high-order counter 
00390 
00391     // increment pause counter
00392     TimerPauseReg++;
00393 
00394     // if a user function is defined, execute it too
00395     if(TimerIntFunc[TIMER0OVERFLOW_INT])
00396         TimerIntFunc[TIMER0OVERFLOW_INT]();
00397 }
00398 
00399 //! Interrupt handler for Timer1 overflow interrupt
00400 SIGNAL(SIG_OVERFLOW1)
00401 {
00402     // if a user function is defined, execute it
00403     if(TimerIntFunc[TIMER1OVERFLOW_INT])
00404         TimerIntFunc[TIMER1OVERFLOW_INT]();
00405 }
00406 
00407 //! Interrupt handler for Timer2 overflow interrupt
00408 SIGNAL(SIG_OVERFLOW2)
00409 {
00410     Timer2Reg0++;           // increment low-order counter
00411     if(!Timer2Reg0)     // if low-order counter rollover
00412         Timer2Reg1++;       // increment high-order counter 
00413 
00414     // if a user function is defined, execute it
00415     if(TimerIntFunc[TIMER2OVERFLOW_INT])
00416         TimerIntFunc[TIMER2OVERFLOW_INT]();
00417 }
00418 
00419 //! Interrupt handler for Timer3 overflow interrupt
00420 SIGNAL(SIG_OVERFLOW3)
00421 {
00422     // if a user function is defined, execute it
00423     if(TimerIntFunc[TIMER3OVERFLOW_INT])
00424         TimerIntFunc[TIMER3OVERFLOW_INT]();
00425 }
00426 
00427 //! Interrupt handler for OutputCompare0 match (OC0) interrupt
00428 SIGNAL(SIG_OUTPUT_COMPARE0)
00429 {
00430     // if a user function is defined, execute it
00431     if(TimerIntFunc[TIMER0OUTCOMPARE_INT])
00432         TimerIntFunc[TIMER0OUTCOMPARE_INT]();
00433 }
00434 
00435 //! Interrupt handler for OutputCompare1A match (OC1A) interrupt
00436 SIGNAL(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 SIGNAL(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 OutputCompare1C match (OC1C) interrupt
00452 SIGNAL(SIG_OUTPUT_COMPARE1C)
00453 {
00454     // if a user function is defined, execute it
00455     if(TimerIntFunc[TIMER1OUTCOMPAREC_INT])
00456         TimerIntFunc[TIMER1OUTCOMPAREC_INT]();
00457 }
00458 
00459 //! Interrupt handler for InputCapture1(IC1) interrupt
00460 SIGNAL(SIG_INPUT_CAPTURE1)
00461 {
00462     // if a user function is defined, execute it
00463     if(TimerIntFunc[TIMER1INPUTCAPTURE_INT])
00464         TimerIntFunc[TIMER1INPUTCAPTURE_INT]();
00465 }
00466 
00467 //! Interrupt handler for OutputCompare2 match (OC2) interrupt
00468 SIGNAL(SIG_OUTPUT_COMPARE2)
00469 {
00470     // if a user function is defined, execute it
00471     if(TimerIntFunc[TIMER2OUTCOMPARE_INT])
00472         TimerIntFunc[TIMER2OUTCOMPARE_INT]();
00473 }
00474 
00475 //! Interrupt handler for OutputCompare3A match (OC3A) interrupt
00476 SIGNAL(SIG_OUTPUT_COMPARE3A)
00477 {
00478     // if a user function is defined, execute it
00479     if(TimerIntFunc[TIMER3OUTCOMPAREA_INT])
00480         TimerIntFunc[TIMER3OUTCOMPAREA_INT]();
00481 }
00482 
00483 //! Interrupt handler for OutputCompare3B match (OC3B) interrupt
00484 SIGNAL(SIG_OUTPUT_COMPARE3B)
00485 {
00486     // if a user function is defined, execute it
00487     if(TimerIntFunc[TIMER1OUTCOMPAREB_INT])
00488         TimerIntFunc[TIMER1OUTCOMPAREB_INT]();
00489 }
00490 
00491 //! Interrupt handler for OutputCompare3C match (OC3C) interrupt
00492 SIGNAL(SIG_OUTPUT_COMPARE3C)
00493 {
00494     // if a user function is defined, execute it
00495     if(TimerIntFunc[TIMER3OUTCOMPAREC_INT])
00496         TimerIntFunc[TIMER3OUTCOMPAREC_INT]();
00497 }
00498 
00499 //! Interrupt handler for InputCapture3 (IC3) interrupt
00500 SIGNAL(SIG_INPUT_CAPTURE3)
00501 {
00502     // if a user function is defined, execute it
00503     if(TimerIntFunc[TIMER3INPUTCAPTURE_INT])
00504         TimerIntFunc[TIMER3INPUTCAPTURE_INT]();
00505 }

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