//***************************************************************************** // // File Name : 'timer.c' // Title : System Timer function library // Author : Pascal Stang - Copyright (C) 2000-2002 // Created : 11/22/2000 // Revised : 5/6/2002 // Version : 1.1 // Target MCU : Atmel AVR Series // Editor Tabs : 3 // // This code is distributed under the GNU Public License // which can be found at http://www.gnu.org/licenses/gpl.txt // //***************************************************************************** #ifndef WIN32 #include #include #include #include #endif #include "global.h" #include "timer.h" // Program ROM constants // the prescale division values stored in 2^n format // STOP, CLK, CLK/8, CLK/64, CLK/256, CLK/1024 short __attribute__ ((progmem)) TimerPrescaleFactor[] = {0,0,3,6,8,10}; // Global variables // time registers volatile unsigned long Timer0PauseReg; volatile unsigned long Timer0Reg0; volatile unsigned long Timer0Reg1; volatile unsigned long Timer2Reg0; volatile unsigned long Timer2Reg1; typedef void (*voidFuncPtr)(void); volatile static voidFuncPtr TimerIntFunc[TIMER_NUM_INTERRUPTS]; // delay for a minimum of microseconds // the time resolution is dependent on the time the loop takes // e.g. with 4Mhz and 5 cycles per loop, the resolution is 1.25 us void delay(unsigned short us) { unsigned short delay_loops; register unsigned short i; delay_loops = (us+3)/5*CYCLES_PER_US; // +3 for rounding up (dirty) // one loop takes 5 cpu cycles for (i=0; i < delay_loops; i++) {}; } void timerInit(void) { u08 intNum; // detach all user functions from interrupts for(intNum=0; intNum number of milliseconds // calculate delay for [pause_ms] milliseconds u16 prescaleDiv = 1<<(PRG_RDB(TimerPrescaleFactor+inp(TCCR0))); u32 pause = (pause_ms*(F_CPU/(prescaleDiv*256)))/1000; Timer0PauseReg = 0; while(Timer0PauseReg < pause); } void timer0ClearOverflowCount(void) { // clear the timer overflow counter registers Timer0Reg0 = 0; // initialize time registers Timer0Reg1 = 0; // initialize time registers } long timer0GetOverflowCount(void) { // return the current timer overflow count // (this is since the last timer0ClearOverflowCount() command was called) return Timer0Reg0; } void timer2ClearOverflowCount(void) { // clear the timer overflow counter registers Timer2Reg0 = 0; // initialize time registers Timer2Reg1 = 0; // initialize time registers } long timer2GetOverflowCount(void) { // return the current timer overflow count // (this is since the last timer2ClearOverflowCount() command was called) return Timer2Reg0; } void timer1PWMInit(u08 bitRes) { // configures timer1 for use with PWM output // on pins OC1A and OC1B // enable Timer1 as 8,9,10bit PWM if(bitRes == 9) { // 9bit mode sbi(TCCR1A,PWM11); cbi(TCCR1A,PWM10); } else if( bitRes == 10 ) { // 10bit mode sbi(TCCR1A,PWM11); sbi(TCCR1A,PWM10); } else { // default 8bit mode cbi(TCCR1A,PWM11); sbi(TCCR1A,PWM10); } // set clear-timer-on-compare-match sbi(TCCR1B,CTC1); // set PWM1A as non-inverted PWM sbi(TCCR1A,COM1A1); cbi(TCCR1A,COM1A0); // set PWM1B as non-inverted PWM sbi(TCCR1A,COM1B1); cbi(TCCR1A,COM1B0); // clear output compare value A outp(0, OCR1AH); outp(0, OCR1AL); // clear output compare value B outp(0, OCR1BH); outp(0, OCR1BL); } void timer1PWMOff(void) { // turn off PWM on OC1A and OC1B cbi(TCCR1A,PWM11); cbi(TCCR1A,PWM10); // clear (disable) clear-timer-on-compare-match cbi(TCCR1B,CTC1); // set PWM1A (OutputCompare action) to none cbi(TCCR1A,COM1A1); cbi(TCCR1A,COM1A0); // set PWM1B (OutputCompare action) to none cbi(TCCR1A,COM1B1); cbi(TCCR1A,COM1B0); } void timer1PWMASet(u16 pwmDuty) { // set PWM (output compare) duty for channel A // this PWM output is generated on OC1A pin // NOTE: pwmDuty should be in the range 0-255 for 8bit PWM // pwmDuty should be in the range 0-511 for 9bit PWM // pwmDuty should be in the range 0-1023 for 10bit PWM outp( (pwmDuty>>8), OCR1AH); // set the high 8bits of OCR1A outp( (pwmDuty&0x00FF), OCR1AL); // set the low 8bits of OCR1A } void timer1PWMBSet(u16 pwmDuty) { // set PWM (output compare) duty for channel B // this PWM output is generated on OC1B pin // NOTE: pwmDuty should be in the range 0-255 for 8bit PWM // pwmDuty should be in the range 0-511 for 9bit PWM // pwmDuty should be in the range 0-1023 for 10bit PWM outp( (pwmDuty>>8), OCR1BH); // set the high 8bits of OCR1B outp( (pwmDuty&0x00FF), OCR1BL); // set the low 8bits of OCR1B } // interrupt handler for tcnt0 overflow interrupt SIGNAL(SIG_OVERFLOW0) { Timer0Reg0++; // increment low-order counter if(!Timer0Reg0) // if low-order counter rollover Timer0Reg1++; // increment high-order counter // increment pause counter Timer0PauseReg++; // if a user function is defined, execute it too if(TimerIntFunc[TIMER0OVERFLOW_INT]) TimerIntFunc[TIMER0OVERFLOW_INT](); } // interrupt handler for tcnt1 overflow interrupt SIGNAL(SIG_OVERFLOW1) { // if a user function is defined, execute it if(TimerIntFunc[TIMER1OVERFLOW_INT]) TimerIntFunc[TIMER1OVERFLOW_INT](); } // interrupt handler for tcnt2 overflow interrupt SIGNAL(SIG_OVERFLOW2) { Timer2Reg0++; // increment low-order counter if(!Timer2Reg0) // if low-order counter rollover Timer2Reg1++; // increment high-order counter // if a user function is defined, execute it if(TimerIntFunc[TIMER2OVERFLOW_INT]) TimerIntFunc[TIMER2OVERFLOW_INT](); } // interrupt handler for CutputCompare1A match (OC1A) interrupt SIGNAL(SIG_OUTPUT_COMPARE1A) { // if a user function is defined, execute it if(TimerIntFunc[TIMER1OUTCOMPAREA_INT]) TimerIntFunc[TIMER1OUTCOMPAREA_INT](); } // interrupt handler for OutputCompare1B match (OC1B) interrupt SIGNAL(SIG_OUTPUT_COMPARE1B) { // if a user function is defined, execute it if(TimerIntFunc[TIMER1OUTCOMPAREB_INT]) TimerIntFunc[TIMER1OUTCOMPAREB_INT](); } SIGNAL(SIG_INPUT_CAPTURE1) { // if a user function is defined, execute it if(TimerIntFunc[TIMER1INPUTCAPTURE_INT]) TimerIntFunc[TIMER1INPUTCAPTURE_INT](); } // added by MG // interrupt handler for OutputCompare2 match (OC2) interrupt SIGNAL(SIG_OUTPUT_COMPARE2) { // if a user function is defined, execute it if(TimerIntFunc[TIMER2OUTCOMPARE_INT]) TimerIntFunc[TIMER2OUTCOMPARE_INT](); } void timer2SetRTCMode(void) { sbi(ASSR,AS2); } void timer2EnableOutputCompare(void) { sbi(TIMSK, OCIE2); } void timer2SetOutCompare(u08 compare) /* Set Output Compare value */ { outp(compare,OCR2); }