| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 6 | kaklik | /*! \file timer128.h \brief System Timer function library for Mega128. */ |
| 2 | //***************************************************************************** |
||
| 3 | // |
||
| 4 | // File Name : 'timer128.h' |
||
| 5 | // Title : System Timer function library for Mega128 |
||
| 6 | // Author : Pascal Stang - Copyright (C) 2000-2003 |
||
| 7 | // Created : 11/22/2000 |
||
| 8 | // Revised : 02/10/2003 |
||
| 9 | // Version : 1.1 |
||
| 10 | // Target MCU : Atmel AVR Series |
||
| 11 | // Editor Tabs : 4 |
||
| 12 | // |
||
| 13 | // This code is distributed under the GNU Public License |
||
| 14 | // which can be found at http://www.gnu.org/licenses/gpl.txt |
||
| 15 | // |
||
| 16 | /// \ingroup driver_avr |
||
| 17 | /// \defgroup timer128 Timer Function Library for ATmega128 (timer128.c) |
||
| 18 | /// \code #include "timer128.h" \endcode |
||
| 19 | /// \par Overview |
||
| 20 | /// This library provides functions for use with the timers internal to the |
||
| 21 | /// AVR ATmega128. Functions include initialization, set prescaler, |
||
| 22 | /// calibrated pause function (in milliseconds), attaching and detaching |
||
| 23 | /// of user functions to interrupts, overflow counters, and PWM. |
||
| 24 | /// |
||
| 25 | /// \par About Timers |
||
| 26 | /// The Atmel AVR-series processors each contain at least one |
||
| 27 | /// hardware timer/counter. Many of the processors contain 2 or 3 |
||
| 28 | /// timers. Generally speaking, a timer is a hardware counter inside |
||
| 29 | /// the processor which counts at a rate related to the main CPU clock |
||
| 30 | /// frequency. Because the counter value increasing (counting up) at |
||
| 31 | /// a precise rate, we can use it as a timer to create or measure |
||
| 32 | /// precise delays, schedule events, or generate signals of a certain |
||
| 33 | /// frequency or pulse-width. |
||
| 34 | /// \par |
||
| 35 | /// As an example, the ATmega163 processor has 3 timer/counters. |
||
| 36 | /// Timer0, Timer1, and Timer2 are 8, 16, and 8 bits wide respectively. |
||
| 37 | /// This means that they overflow, or roll over back to zero, at a |
||
| 38 | /// count value of 256 for 8bits or 65536 for 16bits. A prescaler is |
||
| 39 | /// avaiable for each timer, and the prescaler allows you to pre-divide |
||
| 40 | /// the main CPU clock rate down to a slower speed before feeding it to |
||
| 41 | /// the counting input of a timer. For example, if the CPU clock |
||
| 42 | /// frequency is 3.69MHz, and Timer0's prescaler is set to divide-by-8, |
||
| 43 | /// then Timer0 will "tic" at 3690000/8 = 461250Hz. Because Timer0 is |
||
| 44 | /// an 8bit timer, it will count to 256 in just 256/461250Hz = 0.555ms. |
||
| 45 | /// In fact, when it hits 255, it will overflow and start again at |
||
| 46 | /// zero. In this case, Timer0 will overflow 461250/256 = 1801.76 |
||
| 47 | /// times per second. |
||
| 48 | /// \par |
||
| 49 | /// Timer0 can be used a number of ways simultaneously. First, the |
||
| 50 | /// value of the timer can be read by accessing the CPU register \c TCNT0. |
||
| 51 | /// We could, for example, figure out how long it takes to execute a |
||
| 52 | /// C command by recording the value of \c TCNT0 before and after |
||
| 53 | /// execution, then subtract (after-before) = time elapsed. Or we can |
||
| 54 | /// enable the overflow interrupt which goes off every time T0 |
||
| 55 | /// overflows and count out longer delays (multiple overflows), or |
||
| 56 | /// execute a special periodic function at every overflow. |
||
| 57 | /// \par |
||
| 58 | /// The other timers (Timer1 and Timer2) offer all the abilities of |
||
| 59 | /// Timer0 and many more features. Both T1 and T2 can operate as |
||
| 60 | /// general-purpose timers, but T1 has special hardware allowing it to |
||
| 61 | /// generate PWM signals, while T2 is specially designed to help count |
||
| 62 | /// out real time (like hours, minutes, seconds). See the |
||
| 63 | /// Timer/Counter section of the processor datasheet for more info. |
||
| 64 | /// |
||
| 65 | //***************************************************************************** |
||
| 66 | //@{ |
||
| 67 | |||
| 68 | #ifndef TIMER128_H |
||
| 69 | #define TIMER128_H |
||
| 70 | |||
| 71 | #include "global.h" |
||
| 72 | |||
| 73 | // constants/macros/typdefs |
||
| 74 | |||
| 75 | // Timer/clock prescaler values and timer overflow rates |
||
| 76 | // tics = rate at which the timer counts up |
||
| 77 | // 8bitoverflow = rate at which the timer overflows 8bits (or reaches 256) |
||
| 78 | // 16bit [overflow] = rate at which the timer overflows 16bits (65536) |
||
| 79 | // |
||
| 80 | // overflows can be used to generate periodic interrupts |
||
| 81 | // |
||
| 82 | // for 8MHz crystal |
||
| 83 | // 0 = STOP (Timer not counting) |
||
| 84 | // 1 = CLOCK tics= 8MHz 8bitoverflow= 31250Hz 16bit= 122.070Hz |
||
| 85 | // 2 = CLOCK/8 tics= 1MHz 8bitoverflow= 3906.25Hz 16bit= 15.259Hz |
||
| 86 | // 3 = CLOCK/64 tics= 125kHz 8bitoverflow= 488.28Hz 16bit= 1.907Hz |
||
| 87 | // 4 = CLOCK/256 tics= 31250Hz 8bitoverflow= 122.07Hz 16bit= 0.477Hz |
||
| 88 | // 5 = CLOCK/1024 tics= 7812.5Hz 8bitoverflow= 30.52Hz 16bit= 0.119Hz |
||
| 89 | // 6 = External Clock on T(x) pin (falling edge) |
||
| 90 | // 7 = External Clock on T(x) pin (rising edge) |
||
| 91 | |||
| 92 | // for 4MHz crystal |
||
| 93 | // 0 = STOP (Timer not counting) |
||
| 94 | // 1 = CLOCK tics= 4MHz 8bitoverflow= 15625Hz 16bit= 61.035Hz |
||
| 95 | // 2 = CLOCK/8 tics= 500kHz 8bitoverflow= 1953.125Hz 16bit= 7.629Hz |
||
| 96 | // 3 = CLOCK/64 tics= 62500Hz 8bitoverflow= 244.141Hz 16bit= 0.954Hz |
||
| 97 | // 4 = CLOCK/256 tics= 15625Hz 8bitoverflow= 61.035Hz 16bit= 0.238Hz |
||
| 98 | // 5 = CLOCK/1024 tics= 3906.25Hz 8bitoverflow= 15.259Hz 16bit= 0.060Hz |
||
| 99 | // 6 = External Clock on T(x) pin (falling edge) |
||
| 100 | // 7 = External Clock on T(x) pin (rising edge) |
||
| 101 | |||
| 102 | // for 3.69MHz crystal |
||
| 103 | // 0 = STOP (Timer not counting) |
||
| 104 | // 1 = CLOCK tics= 3.69MHz 8bitoverflow= 14414Hz 16bit= 56.304Hz |
||
| 105 | // 2 = CLOCK/8 tics= 461250Hz 8bitoverflow= 1801.758Hz 16bit= 7.038Hz |
||
| 106 | // 3 = CLOCK/64 tics= 57625.25Hz 8bitoverflow= 225.220Hz 16bit= 0.880Hz |
||
| 107 | // 4 = CLOCK/256 tics= 14414.063Hz 8bitoverflow= 56.305Hz 16bit= 0.220Hz |
||
| 108 | // 5 = CLOCK/1024 tics= 3603.516Hz 8bitoverflow= 14.076Hz 16bit= 0.055Hz |
||
| 109 | // 6 = External Clock on T(x) pin (falling edge) |
||
| 110 | // 7 = External Clock on T(x) pin (rising edge) |
||
| 111 | |||
| 112 | // for 32.768KHz crystal on timer 2 (use for real-time clock) |
||
| 113 | // 0 = STOP |
||
| 114 | // 1 = CLOCK tics= 32.768kHz 8bitoverflow= 128Hz |
||
| 115 | // 2 = CLOCK/8 tics= 4096kHz 8bitoverflow= 16Hz |
||
| 116 | // 3 = CLOCK/64 tics= 512Hz 8bitoverflow= 2Hz |
||
| 117 | // 4 = CLOCK/256 tics= 128Hz 8bitoverflow= 0.5Hz |
||
| 118 | // 5 = CLOCK/1024 tics= 32Hz 8bitoverflow= 0.125Hz |
||
| 119 | |||
| 120 | #define TIMER_CLK_STOP 0x00 ///< Timer Stopped |
||
| 121 | #define TIMER_CLK_DIV1 0x01 ///< Timer clocked at F_CPU |
||
| 122 | #define TIMER_CLK_DIV8 0x02 ///< Timer clocked at F_CPU/8 |
||
| 123 | #define TIMER_CLK_DIV64 0x03 ///< Timer clocked at F_CPU/64 |
||
| 124 | #define TIMER_CLK_DIV256 0x04 ///< Timer clocked at F_CPU/256 |
||
| 125 | #define TIMER_CLK_DIV1024 0x05 ///< Timer clocked at F_CPU/1024 |
||
| 126 | #define TIMER_CLK_T_FALL 0x06 ///< Timer clocked at T falling edge |
||
| 127 | #define TIMER_CLK_T_RISE 0x07 ///< Timer clocked at T rising edge |
||
| 128 | #define TIMER_PRESCALE_MASK 0x07 ///< Timer Prescaler Bit-Mask |
||
| 129 | |||
| 130 | #define TIMERRTC_CLK_STOP 0x00 ///< RTC Timer Stopped |
||
| 131 | #define TIMERRTC_CLK_DIV1 0x01 ///< RTC Timer clocked at F_CPU |
||
| 132 | #define TIMERRTC_CLK_DIV8 0x02 ///< RTC Timer clocked at F_CPU/8 |
||
| 133 | #define TIMERRTC_CLK_DIV32 0x03 ///< RTC Timer clocked at F_CPU/32 |
||
| 134 | #define TIMERRTC_CLK_DIV64 0x04 ///< RTC Timer clocked at F_CPU/64 |
||
| 135 | #define TIMERRTC_CLK_DIV128 0x05 ///< RTC Timer clocked at F_CPU/128 |
||
| 136 | #define TIMERRTC_CLK_DIV256 0x06 ///< RTC Timer clocked at F_CPU/256 |
||
| 137 | #define TIMERRTC_CLK_DIV1024 0x07 ///< RTC Timer clocked at F_CPU/1024 |
||
| 138 | #define TIMERRTC_PRESCALE_MASK 0x07 ///< RTC Timer Prescaler Bit-Mask |
||
| 139 | |||
| 140 | // default prescale settings for the timers |
||
| 141 | // these settings are applied when you call |
||
| 142 | // timerInit or any of the timer<x>Init |
||
| 143 | #define TIMER0PRESCALE TIMERRTC_CLK_DIV64 ///< timer 0 prescaler default |
||
| 144 | #define TIMER1PRESCALE TIMER_CLK_DIV64 ///< timer 1 prescaler default |
||
| 145 | #define TIMER2PRESCALE TIMER_CLK_DIV8 ///< timer 2 prescaler default |
||
| 146 | #define TIMER3PRESCALE TIMER_CLK_DIV64 ///< timer 3 prescaler default |
||
| 147 | |||
| 148 | // interrupt macros for attaching user functions to timer interrupts |
||
| 149 | // use these with timerAttach( intNum, function ) |
||
| 150 | // timer 0 |
||
| 151 | #define TIMER0OVERFLOW_INT 0 |
||
| 152 | #define TIMER0OUTCOMPARE_INT 1 |
||
| 153 | // timer 1 |
||
| 154 | #define TIMER1OVERFLOW_INT 2 |
||
| 155 | #define TIMER1OUTCOMPAREA_INT 3 |
||
| 156 | #define TIMER1OUTCOMPAREB_INT 4 |
||
| 157 | #define TIMER1OUTCOMPAREC_INT 5 |
||
| 158 | #define TIMER1INPUTCAPTURE_INT 6 |
||
| 159 | // timer 2 |
||
| 160 | #define TIMER2OVERFLOW_INT 7 |
||
| 161 | #define TIMER2OUTCOMPARE_INT 8 |
||
| 162 | // timer 3 |
||
| 163 | #define TIMER3OVERFLOW_INT 9 |
||
| 164 | #define TIMER3OUTCOMPAREA_INT 10 |
||
| 165 | #define TIMER3OUTCOMPAREB_INT 11 |
||
| 166 | #define TIMER3OUTCOMPAREC_INT 12 |
||
| 167 | #define TIMER3INPUTCAPTURE_INT 13 |
||
| 168 | |||
| 169 | #define TIMER_NUM_INTERRUPTS 14 |
||
| 170 | |||
| 171 | // type of interrupt handler to use for timers |
||
| 172 | // *do not change unless you know what you're doing |
||
| 173 | // Value may be SIGNAL or INTERRUPT |
||
| 174 | #ifndef TIMER_INTERRUPT_HANDLER |
||
| 175 | #define TIMER_INTERRUPT_HANDLER SIGNAL |
||
| 176 | #endif |
||
| 177 | |||
| 178 | // functions |
||
| 179 | #define delay delay_us |
||
| 180 | #define delay_ms timerPause |
||
| 181 | void delay_us(unsigned short time_us); |
||
| 182 | |||
| 183 | // initializes timing system |
||
| 184 | // runs all timer init functions |
||
| 185 | // sets all timers to default prescale values #defined in systimer.c |
||
| 186 | void timerInit(void); |
||
| 187 | |||
| 188 | // default initialization routines for each timer |
||
| 189 | void timer0Init(void); |
||
| 190 | void timer1Init(void); |
||
| 191 | void timer2Init(void); |
||
| 192 | void timer3Init(void); |
||
| 193 | |||
| 194 | // Clock prescaler set/get commands for each timer/counter |
||
| 195 | // For setting the prescaler, you should use one of the #defines |
||
| 196 | // above like TIMER_CLK_DIVx, where [x] is the division rate |
||
| 197 | // you want. |
||
| 198 | // When getting the current prescaler setting, the return value |
||
| 199 | // will be the [x] division value currently set. |
||
| 200 | void timer0SetPrescaler(u08 prescale); ///< set timer0 prescaler division index |
||
| 201 | void timer1SetPrescaler(u08 prescale); ///< set timer1 prescaler division index |
||
| 202 | void timer2SetPrescaler(u08 prescale); ///< set timer2 prescaler division index |
||
| 203 | void timer3SetPrescaler(u08 prescale); ///< set timer3 prescaler division index |
||
| 204 | u16 timer0GetPrescaler(void); ///< get timer0 prescaler division rate |
||
| 205 | u16 timer1GetPrescaler(void); ///< get timer1 prescaler division rate |
||
| 206 | u16 timer2GetPrescaler(void); ///< get timer2 prescaler division rate |
||
| 207 | u16 timer3GetPrescaler(void); ///< get timer3 prescaler division rate |
||
| 208 | |||
| 209 | |||
| 210 | // TimerAttach and Detach commands |
||
| 211 | // These functions allow the attachment (or detachment) of any user function |
||
| 212 | // to a timer interrupt. "Attaching" one of your own functions to a timer |
||
| 213 | // interrupt means that it will be called whenever that interrupt happens. |
||
| 214 | // Using attach is better than rewriting the actual INTERRUPT() function |
||
| 215 | // because your code will still work and be compatible if the timer library |
||
| 216 | // is updated. Also, using Attach allows your code and any predefined timer |
||
| 217 | // code to work together and at the same time. (ie. "attaching" your own |
||
| 218 | // function to the timer0 overflow doesn't prevent timerPause from working, |
||
| 219 | // but rather allows you to share the interrupt.) |
||
| 220 | // |
||
| 221 | // timerAttach(TIMER1OVERFLOW_INT, myOverflowFunction); |
||
| 222 | // timerDetach(TIMER1OVERFLOW_INT) |
||
| 223 | // |
||
| 224 | // timerAttach causes the myOverflowFunction() to be attached, and therefore |
||
| 225 | // execute, whenever an overflow on timer1 occurs. timerDetach removes the |
||
| 226 | // association and executes no user function when the interrupt occurs. |
||
| 227 | // myOverflowFunction must be defined with no return value and no arguments: |
||
| 228 | // |
||
| 229 | // void myOverflowFunction(void) { ... } |
||
| 230 | |||
| 231 | void timerAttach(u08 interruptNum, void (*userFunc)(void) ); |
||
| 232 | void timerDetach(u08 interruptNum); |
||
| 233 | |||
| 234 | |||
| 235 | // timing commands |
||
| 236 | // timerPause pauses for the number of milliseconds specified in <pause_ms> |
||
| 237 | void timerPause(unsigned short pause_ms); |
||
| 238 | |||
| 239 | // overflow counters |
||
| 240 | // to be documented |
||
| 241 | void timer0ClearOverflowCount(void); |
||
| 242 | long timer0GetOverflowCount(void); |
||
| 243 | void timer2ClearOverflowCount(void); |
||
| 244 | long timer2GetOverflowCount(void); |
||
| 245 | |||
| 246 | // PWM initialization and set commands for timerX (where X is either 1 or 3) |
||
| 247 | // timerXPWMInit() |
||
| 248 | // configures the timerX hardware for PWM mode on pins OCXA, OCXB, and OCXC. |
||
| 249 | // bitRes should be 8,9,or 10 for 8,9,or 10bit PWM resolution |
||
| 250 | // |
||
| 251 | // timerXPWMOff() |
||
| 252 | // turns off all timerX PWM output and set timer mode to normal state |
||
| 253 | // |
||
| 254 | // timerXPWMAOn(), timerXPWMBOn(), timerXPWMCOn() |
||
| 255 | // turn on output of PWM signals to OCXA,B,C pins |
||
| 256 | // NOTE: Until you define the OCXA,B,C pins as outputs, and run |
||
| 257 | // this "on" command, no PWM output will be output |
||
| 258 | // |
||
| 259 | // timerXPWMAOff(), timerXPWMBOff(), timerXPWMCOff() |
||
| 260 | // turn off output of PWM signals to OCXA,B,C pins |
||
| 261 | // |
||
| 262 | // timerXPWMASet(), timer1PWMBSet(), timerXPWMCSet() |
||
| 263 | // sets the PWM duty cycle for each channel |
||
| 264 | // NOTE: <pwmDuty> should be in the range 0-255 for 8bit PWM |
||
| 265 | // <pwmDuty> should be in the range 0-511 for 9bit PWM |
||
| 266 | // <pwmDuty> should be in the range 0-1023 for 10bit PWM |
||
| 267 | // NOTE: the PWM frequency can be controlled in increments by setting the |
||
| 268 | // prescaler for timer1 |
||
| 269 | |||
| 270 | void timer1PWMInit(u08 bitRes); ///< initialize and set timer1 mode to PWM |
||
| 271 | void timer1PWMInitICR(u16 topcount);///< initialize and set timer1 mode to PWM with specific top count |
||
| 272 | void timer1PWMOff(void); ///< turn off all timer1 PWM output and set timer mode to normal |
||
| 273 | void timer1PWMAOn(void); ///< turn on timer1 Channel A (OC1A) PWM output |
||
| 274 | void timer1PWMBOn(void); ///< turn on timer1 Channel B (OC1B) PWM output |
||
| 275 | void timer1PWMCOn(void); ///< turn on timer1 Channel C (OC1C) PWM output |
||
| 276 | void timer1PWMAOff(void); ///< turn off timer1 Channel A (OC1A) PWM output |
||
| 277 | void timer1PWMBOff(void); ///< turn off timer1 Channel B (OC1B) PWM output |
||
| 278 | void timer1PWMCOff(void); ///< turn off timer1 Channel C (OC1C) PWM output |
||
| 279 | void timer1PWMASet(u16 pwmDuty); ///< set duty of timer1 Channel A (OC1A) PWM output |
||
| 280 | void timer1PWMBSet(u16 pwmDuty); ///< set duty of timer1 Channel B (OC1B) PWM output |
||
| 281 | void timer1PWMCSet(u16 pwmDuty); ///< set duty of timer1 Channel C (OC1C) PWM output |
||
| 282 | |||
| 283 | void timer3PWMInit(u08 bitRes); ///< initialize and set timer3 mode to PWM |
||
| 284 | void timer3PWMInitICR(u16 topcount);///< initialize and set timer3 mode to PWM with specific top count |
||
| 285 | void timer3PWMOff(void); ///< turn off all timer3 PWM output and set timer mode to normal |
||
| 286 | void timer3PWMAOn(void); ///< turn on timer3 Channel A (OC3A) PWM output |
||
| 287 | void timer3PWMBOn(void); ///< turn on timer3 Channel B (OC3B) PWM output |
||
| 288 | void timer3PWMCOn(void); ///< turn on timer3 Channel C (OC3C) PWM output |
||
| 289 | void timer3PWMAOff(void); ///< turn off timer3 Channel A (OC3A) PWM output |
||
| 290 | void timer3PWMBOff(void); ///< turn off timer3 Channel B (OC3B) PWM output |
||
| 291 | void timer3PWMCOff(void); ///< turn off timer3 Channel C (OC3C) PWM output |
||
| 292 | void timer3PWMASet(u16 pwmDuty); ///< set duty of timer3 Channel A (OC3A) PWM output |
||
| 293 | void timer3PWMBSet(u16 pwmDuty); ///< set duty of timer3 Channel B (OC3B) PWM output |
||
| 294 | void timer3PWMCSet(u16 pwmDuty); ///< set duty of timer3 Channel C (OC3C) PWM output |
||
| 295 | |||
| 296 | //@} |
||
| 297 | |||
| 298 | // Pulse generation commands have been moved to the pulse.c library |
||
| 299 | |||
| 300 | #endif |
Powered by WebSVN v2.8.3