Main Page   Data Structures   File List   Data Fields   Globals  

/timer128.h

Go to the documentation of this file.
00001 /*! \file timer128.h \brief System Timer function library for Mega128. */
00002 //*****************************************************************************
00003 //
00004 // File Name    : 'timer128.h'
00005 // Title        : System Timer function library for Mega128
00006 // Author       : Pascal Stang - Copyright (C) 2000-2003
00007 // Created      : 11/22/2000
00008 // Revised      : 02/10/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 // Notes:   The Atmel AVR Series Processors each contain at least one
00018 //          hardware timer/counter.  Many of the processors contain 2 or 3
00019 //          timers.  Generally speaking, a timer is a hardware counter inside
00020 //          the processor which counts at a rate related to the main CPU clock
00021 //          frequency.  Because the counter value increasing (counting up) at
00022 //          a precise rate, we can use it as a timer to create or measure 
00023 //          precise delays, schedule events, or generate signals of a certain
00024 //          frequency or pulse-width.
00025 //              As an example, the ATmega163 processor has 3 timer/counters.
00026 //          Timer0, Timer1, and Timer2 are 8, 16, and 8 bits wide respectively.
00027 //          This means that they overflow, or roll over back to zero, at a
00028 //          count value of 256 for 8bits or 65536 for 16bits.  A prescaler is
00029 //          avaiable for each timer, and the prescaler allows you to pre-divide
00030 //          the main CPU clock rate down to a slower speed before feeding it to
00031 //          the counting input of a timer.  For example, if the CPU clock
00032 //          frequency is 3.69MHz, and Timer0's prescaler is set to divide-by-8,
00033 //          then Timer0 will "tic" at 3690000/8 = 461250Hz.  Because Timer0 is
00034 //          an 8bit timer, it will count to 256 in just 256/461250Hz = 0.555ms.
00035 //          In fact, when it hits 255, it will overflow and start again at
00036 //          zero.  In this case, Timer0 will overflow 461250/256 = 1801.76
00037 //          times per second.
00038 //              Timer0 can be used a number of ways simultaneously.  First, the
00039 //          value of the timer can be read by accessing the CPU register TCNT0.
00040 //          We could, for example, figure out how long it takes to execute a
00041 //          C command by recording the value of TCNT0 before and after
00042 //          execution, then subtract (after-before) = time elapsed.  Or we can
00043 //          enable the overflow interrupt which goes off every time T0
00044 //          overflows and count out longer delays (multiple overflows), or
00045 //          execute a special periodic function at every overflow.
00046 //              The other timers (Timer1 and Timer2) offer all the abilities of
00047 //          Timer0 and many more features.  Both T1 and T2 can operate as
00048 //          general-purpose timers, but T1 has special hardware allowing it to
00049 //          generate PWM signals, while T2 is specially designed to help count
00050 //          out real time (like hours, minutes, seconds).  See the
00051 //          Timer/Counter section of the processor datasheet for more info.
00052 //
00053 //*****************************************************************************
00054 
00055 #ifndef TIMER128_H
00056 #define TIMER128_H
00057 
00058 #include "global.h"
00059 
00060 // constants/macros/typdefs
00061 
00062 // Timer/clock prescaler values and timer overflow rates
00063 // tics = rate at which the timer counts up
00064 // 8bitoverflow = rate at which the timer overflows 8bits (or reaches 256)
00065 // 16bit [overflow] = rate at which the timer overflows 16bits (65536)
00066 // 
00067 // overflows can be used to generate periodic interrupts
00068 //
00069 // for 8MHz crystal
00070 // 0 = STOP (Timer not counting)
00071 // 1 = CLOCK        tics= 8MHz          8bitoverflow= 31250Hz       16bit= 122.070Hz
00072 // 2 = CLOCK/8      tics= 1MHz          8bitoverflow= 3906.25Hz     16bit=  15.259Hz
00073 // 3 = CLOCK/64     tics= 125kHz        8bitoverflow=  488.28Hz     16bit=   1.907Hz
00074 // 4 = CLOCK/256    tics= 31250Hz       8bitoverflow=  122.07Hz     16bit=  0.477Hz
00075 // 5 = CLOCK/1024   tics= 7812.5Hz      8bitoverflow=   30.52Hz     16bit=   0.119Hz
00076 // 6 = External Clock on T(x) pin (falling edge)
00077 // 7 = External Clock on T(x) pin (rising edge)
00078 
00079 // for 4MHz crystal
00080 // 0 = STOP (Timer not counting)
00081 // 1 = CLOCK        tics= 4MHz          8bitoverflow= 15625Hz       16bit=  61.035Hz
00082 // 2 = CLOCK/8      tics= 500kHz        8bitoverflow= 1953.125Hz    16bit=   7.629Hz
00083 // 3 = CLOCK/64     tics= 62500Hz       8bitoverflow=  244.141Hz    16bit=   0.954Hz
00084 // 4 = CLOCK/256    tics= 15625Hz       8bitoverflow=   61.035Hz    16bit=   0.238Hz
00085 // 5 = CLOCK/1024   tics= 3906.25Hz     8bitoverflow=   15.259Hz    16bit=   0.060Hz
00086 // 6 = External Clock on T(x) pin (falling edge)
00087 // 7 = External Clock on T(x) pin (rising edge)
00088 
00089 // for 3.69MHz crystal
00090 // 0 = STOP (Timer not counting)
00091 // 1 = CLOCK        tics= 3.69MHz       8bitoverflow= 14414Hz       16bit=  56.304Hz
00092 // 2 = CLOCK/8      tics= 461250Hz      8bitoverflow= 1801.758Hz    16bit=   7.038Hz
00093 // 3 = CLOCK/64     tics= 57625.25Hz    8bitoverflow=  225.220Hz    16bit=   0.880Hz
00094 // 4 = CLOCK/256    tics= 14414.063Hz   8bitoverflow=   56.305Hz    16bit=   0.220Hz
00095 // 5 = CLOCK/1024   tics=  3603.516Hz   8bitoverflow=   14.076Hz    16bit=   0.055Hz
00096 // 6 = External Clock on T(x) pin (falling edge)
00097 // 7 = External Clock on T(x) pin (rising edge)
00098 
00099 // for 32.768KHz crystal on timer 2 (use for real-time clock)
00100 // 0 = STOP
00101 // 1 = CLOCK        tics= 32.768kHz 8bitoverflow= 128Hz
00102 // 2 = CLOCK/8      tics= 4096kHz       8bitoverflow=  16Hz
00103 // 3 = CLOCK/64     tics= 512Hz         8bitoverflow=   2Hz
00104 // 4 = CLOCK/256    tics= 128Hz         8bitoverflow=   0.5Hz
00105 // 5 = CLOCK/1024   tics= 32Hz          8bitoverflow=   0.125Hz
00106 
00107 #define TIMER_CLK_STOP          0x00
00108 #define TIMER_CLK_DIV1          0x01
00109 #define TIMER_CLK_DIV8          0x02
00110 #define TIMER_CLK_DIV64         0x03
00111 #define TIMER_CLK_DIV256        0x04
00112 #define TIMER_CLK_DIV1024       0x05
00113 #define TIMER_CLK_T_FALL        0x06
00114 #define TIMER_CLK_T_RISE        0x07
00115 #define TIMER_PRESCALE_MASK     0x07
00116 
00117 #define TIMERRTC_CLK_STOP       0x00
00118 #define TIMERRTC_CLK_DIV1       0x01
00119 #define TIMERRTC_CLK_DIV8       0x02
00120 #define TIMERRTC_CLK_DIV32      0x03
00121 #define TIMERRTC_CLK_DIV64      0x04
00122 #define TIMERRTC_CLK_DIV128     0x05
00123 #define TIMERRTC_CLK_DIV256     0x06
00124 #define TIMERRTC_CLK_DIV1024    0x07
00125 #define TIMERRTC_PRESCALE_MASK  0x07
00126 
00127 // default prescale settings for the timers
00128 // these settings are applied when you call
00129 // timerInit or any of the timer<x>Init
00130 #define TIMER0PRESCALE      TIMERRTC_CLK_DIV64  ///< timer 0 prescaler default
00131 #define TIMER1PRESCALE      TIMER_CLK_DIV64     ///< timer 1 prescaler default
00132 #define TIMER2PRESCALE      TIMER_CLK_DIV8      ///< timer 2 prescaler default
00133 #define TIMER3PRESCALE      TIMER_CLK_DIV64     ///< timer 3 prescaler default
00134 
00135 // interrupt macros for attaching user functions to timer interrupts
00136 // use these with timerAttach( intNum, function )
00137 // timer 0
00138 #define TIMER0OVERFLOW_INT          0
00139 #define TIMER0OUTCOMPARE_INT        1
00140 // timer 1
00141 #define TIMER1OVERFLOW_INT          2
00142 #define TIMER1OUTCOMPAREA_INT       3
00143 #define TIMER1OUTCOMPAREB_INT       4
00144 #define TIMER1OUTCOMPAREC_INT       5
00145 #define TIMER1INPUTCAPTURE_INT      6
00146 // timer 2
00147 #define TIMER2OVERFLOW_INT          7
00148 #define TIMER2OUTCOMPARE_INT        8
00149 // timer 3
00150 #define TIMER3OVERFLOW_INT          9
00151 #define TIMER3OUTCOMPAREA_INT       10
00152 #define TIMER3OUTCOMPAREB_INT       11
00153 #define TIMER3OUTCOMPAREC_INT       12
00154 #define TIMER3INPUTCAPTURE_INT      13
00155 
00156 #define TIMER_NUM_INTERRUPTS        14
00157 
00158 // type of interrupt handler to use for timers
00159 // *do not change unless you know what you're doing
00160 // Value may be SIGNAL or INTERRUPT
00161 #ifndef TIMER_INTERRUPT_HANDLER
00162 #define TIMER_INTERRUPT_HANDLER     SIGNAL
00163 #endif
00164 
00165 // functions
00166 void delay(unsigned short us);
00167 
00168 // initializes timing system
00169 // runs all timer init functions
00170 // sets all timers to default prescale values #defined in systimer.c
00171 void timerInit(void);
00172 
00173 // default initialization routines for each timer
00174 void timer0Init(void);
00175 void timer1Init(void);
00176 void timer2Init(void);
00177 void timer3Init(void);
00178 
00179 // Clock prescaler set commands for each timer/counter
00180 // you may use one of the #defines above like TIMER_CLK_DIVxxx
00181 // to set the prescale value
00182 void timer0SetPrescaler(u08 prescale);
00183 void timer1SetPrescaler(u08 prescale);
00184 void timer2SetPrescaler(u08 prescale);
00185 void timer3SetPrescaler(u08 prescale);
00186 
00187 // TimerAttach and Detach commands
00188 //      These functions allow the attachment (or detachment) of any user function
00189 //      to a timer interrupt.  "Attaching" one of your own functions to a timer
00190 //      interrupt means that it will be called whenever that interrupt happens.
00191 //      Using attach is better than rewriting the actual INTERRUPT() function
00192 //      because your code will still work and be compatible if the timer library
00193 //      is updated.  Also, using Attach allows your code and any predefined timer
00194 //      code to work together and at the same time.  (ie. "attaching" your own
00195 //      function to the timer0 overflow doesn't prevent timerPause from working,
00196 //      but rather allows you to share the interrupt.)
00197 //
00198 //      timerAttach(TIMER1OVERFLOW_INT, myOverflowFunction);
00199 //      timerDetach(TIMER1OVERFLOW_INT)
00200 //
00201 //      timerAttach causes the myOverflowFunction() to be attached, and therefore
00202 //      execute, whenever an overflow on timer1 occurs.  timerDetach removes the
00203 //      association and executes no user function when the interrupt occurs.
00204 //      myOverflowFunction must be defined with no return value and no arguments:
00205 //
00206 //      void myOverflowFunction(void) { ... }
00207 
00208 void timerAttach(u08 interruptNum, void (*userFunc)(void) );
00209 void timerDetach(u08 interruptNum);
00210 
00211 
00212 // timing commands
00213 // timerPause pauses for the number of milliseconds specified in <pause_ms>
00214 void timerPause(unsigned short pause_ms);
00215 
00216 // overflow counters
00217 // to be documented
00218 void timer0ClearOverflowCount(void);
00219 long timer0GetOverflowCount(void);
00220 void timer2ClearOverflowCount(void);
00221 long timer2GetOverflowCount(void);
00222 
00223 // PWM initialization and set commands for timerX (where X is either 1 or 3)
00224 // timerXPWMInit()
00225 //      configures the timerX hardware for PWM mode on pins OCXA, OCXB, and OCXC.
00226 //      bitRes should be 8,9,or 10 for 8,9,or 10bit PWM resolution
00227 //
00228 // timerXPWMOff()
00229 //      turns off all timerX PWM output and set timer mode to normal state
00230 //
00231 // timerXPWMAOn(), timerXPWMBOn(), timerXPWMCOn()
00232 //      turn on output of PWM signals to OCXA,B,C pins
00233 //      NOTE: Until you define the OCXA,B,C pins as outputs, and run
00234 //      this "on" command, no PWM output will be output
00235 //
00236 // timerXPWMAOff(), timerXPWMBOff(), timerXPWMCOff()
00237 //      turn off output of PWM signals to OCXA,B,C pins
00238 //
00239 // timerXPWMASet(), timer1PWMBSet(), timerXPWMCSet()
00240 //      sets the PWM duty cycle for each channel
00241 //  NOTE:   <pwmDuty> should be in the range 0-255 for 8bit PWM
00242 //          <pwmDuty> should be in the range 0-511 for 9bit PWM
00243 //          <pwmDuty> should be in the range 0-1023 for 10bit PWM
00244 // NOTE: the PWM frequency can be controlled in increments by setting the
00245 //          prescaler for timer1
00246 
00247 void timer1PWMInit(u08 bitRes);     ///< initialize and set timer1 mode to PWM
00248 void timer1PWMOff(void);            ///< turn off all timer1 PWM output and set timer mode to normal
00249 void timer1PWMAOn(void);            ///< turn on timer1 Channel A (OC1A) PWM output
00250 void timer1PWMBOn(void);            ///< turn on timer1 Channel B (OC1B) PWM output
00251 void timer1PWMCOn(void);            ///< turn on timer1 Channel C (OC1C) PWM output
00252 void timer1PWMAOff(void);           ///< turn off timer1 Channel A (OC1A) PWM output
00253 void timer1PWMBOff(void);           ///< turn off timer1 Channel B (OC1B) PWM output
00254 void timer1PWMCOff(void);           ///< turn off timer1 Channel C (OC1C) PWM output
00255 void timer1PWMASet(u16 pwmDuty);    ///< set duty of timer1 Channel A (OC1A) PWM output
00256 void timer1PWMBSet(u16 pwmDuty);    ///< set duty of timer1 Channel B (OC1B) PWM output
00257 void timer1PWMCSet(u16 pwmDuty);    ///< set duty of timer1 Channel C (OC1C) PWM output
00258 
00259 void timer3PWMInit(u08 bitRes);     ///< initialize and set timer3 mode to PWM
00260 void timer3PWMOff(void);            ///< turn off all timer3 PWM output and set timer mode to normal
00261 void timer3PWMAOn(void);            ///< turn on timer3 Channel A (OC3A) PWM output
00262 void timer3PWMBOn(void);            ///< turn on timer3 Channel B (OC3B) PWM output
00263 void timer3PWMCOn(void);            ///< turn on timer3 Channel C (OC3C) PWM output
00264 void timer3PWMAOff(void);           ///< turn off timer3 Channel A (OC3A) PWM output
00265 void timer3PWMBOff(void);           ///< turn off timer3 Channel B (OC3B) PWM output
00266 void timer3PWMCOff(void);           ///< turn off timer3 Channel C (OC3C) PWM output
00267 void timer3PWMASet(u16 pwmDuty);    ///< set duty of timer3 Channel A (OC3A) PWM output
00268 void timer3PWMBSet(u16 pwmDuty);    ///< set duty of timer3 Channel B (OC3B) PWM output
00269 void timer3PWMCSet(u16 pwmDuty);    ///< set duty of timer3 Channel C (OC3C) PWM output
00270 
00271 // Pulse generation commands have been moved to the pulse.c library
00272 
00273 #endif

Generated on Fri Aug 1 10:42:42 2003 for Procyon AVRlib by doxygen1.2.18