Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

Timer Function Library for ATmega128 (timer128.c)
[Drivers (for AVR peripherals)]


Detailed Description

 #include "timer128.h" 
Overview
This library provides functions for use with the timers internal to the AVR ATmega128.  Functions include initialization, set prescaler, calibrated pause function (in milliseconds), attaching and detaching of user functions to interrupts, overflow counters, and PWM.
About Timers
The Atmel AVR-series processors each contain at least one hardware timer/counter. Many of the processors contain 2 or 3 timers. Generally speaking, a timer is a hardware counter inside the processor which counts at a rate related to the main CPU clock frequency. Because the counter value increasing (counting up) at a precise rate, we can use it as a timer to create or measure precise delays, schedule events, or generate signals of a certain frequency or pulse-width.
As an example, the ATmega163 processor has 3 timer/counters. Timer0, Timer1, and Timer2 are 8, 16, and 8 bits wide respectively. This means that they overflow, or roll over back to zero, at a count value of 256 for 8bits or 65536 for 16bits. A prescaler is avaiable for each timer, and the prescaler allows you to pre-divide the main CPU clock rate down to a slower speed before feeding it to the counting input of a timer. For example, if the CPU clock frequency is 3.69MHz, and Timer0's prescaler is set to divide-by-8, then Timer0 will "tic" at 3690000/8 = 461250Hz. Because Timer0 is an 8bit timer, it will count to 256 in just 256/461250Hz = 0.555ms. In fact, when it hits 255, it will overflow and start again at zero. In this case, Timer0 will overflow 461250/256 = 1801.76 times per second.
Timer0 can be used a number of ways simultaneously. First, the value of the timer can be read by accessing the CPU register TCNT0. We could, for example, figure out how long it takes to execute a C command by recording the value of TCNT0 before and after execution, then subtract (after-before) = time elapsed. Or we can enable the overflow interrupt which goes off every time T0 overflows and count out longer delays (multiple overflows), or execute a special periodic function at every overflow.
The other timers (Timer1 and Timer2) offer all the abilities of Timer0 and many more features. Both T1 and T2 can operate as general-purpose timers, but T1 has special hardware allowing it to generate PWM signals, while T2 is specially designed to help count out real time (like hours, minutes, seconds). See the Timer/Counter section of the processor datasheet for more info.


Defines

#define TIMER_CLK_STOP   0x00
 Timer Stopped.
#define TIMER_CLK_DIV1   0x01
 Timer clocked at F_CPU.
#define TIMER_CLK_DIV8   0x02
 Timer clocked at F_CPU/8.
#define TIMER_CLK_DIV64   0x03
 Timer clocked at F_CPU/64.
#define TIMER_CLK_DIV256   0x04
 Timer clocked at F_CPU/256.
#define TIMER_CLK_DIV1024   0x05
 Timer clocked at F_CPU/1024.
#define TIMER_CLK_T_FALL   0x06
 Timer clocked at T falling edge.
#define TIMER_CLK_T_RISE   0x07
 Timer clocked at T rising edge.
#define TIMER_PRESCALE_MASK   0x07
 Timer Prescaler Bit-Mask.
#define TIMERRTC_CLK_STOP   0x00
 RTC Timer Stopped.
#define TIMERRTC_CLK_DIV1   0x01
 RTC Timer clocked at F_CPU.
#define TIMERRTC_CLK_DIV8   0x02
 RTC Timer clocked at F_CPU/8.
#define TIMERRTC_CLK_DIV32   0x03
 RTC Timer clocked at F_CPU/32.
#define TIMERRTC_CLK_DIV64   0x04
 RTC Timer clocked at F_CPU/64.
#define TIMERRTC_CLK_DIV128   0x05
 RTC Timer clocked at F_CPU/128.
#define TIMERRTC_CLK_DIV256   0x06
 RTC Timer clocked at F_CPU/256.
#define TIMERRTC_CLK_DIV1024   0x07
 RTC Timer clocked at F_CPU/1024.
#define TIMERRTC_PRESCALE_MASK   0x07
 RTC Timer Prescaler Bit-Mask.
#define TIMER0PRESCALE   TIMERRTC_CLK_DIV64
 timer 0 prescaler default
#define TIMER1PRESCALE   TIMER_CLK_DIV64
 timer 1 prescaler default
#define TIMER2PRESCALE   TIMER_CLK_DIV8
 timer 2 prescaler default
#define TIMER3PRESCALE   TIMER_CLK_DIV64
 timer 3 prescaler default
#define TIMER0OVERFLOW_INT   0
#define TIMER0OUTCOMPARE_INT   1
#define TIMER1OVERFLOW_INT   2
#define TIMER1OUTCOMPAREA_INT   3
#define TIMER1OUTCOMPAREB_INT   4
#define TIMER1OUTCOMPAREC_INT   5
#define TIMER1INPUTCAPTURE_INT   6
#define TIMER2OVERFLOW_INT   7
#define TIMER2OUTCOMPARE_INT   8
#define TIMER3OVERFLOW_INT   9
#define TIMER3OUTCOMPAREA_INT   10
#define TIMER3OUTCOMPAREB_INT   11
#define TIMER3OUTCOMPAREC_INT   12
#define TIMER3INPUTCAPTURE_INT   13
#define TIMER_NUM_INTERRUPTS   14
#define TIMER_INTERRUPT_HANDLER   SIGNAL
#define delay   delay_us
#define delay_ms   timerPause

Functions

void delay_us (unsigned short time_us)
void timerInit (void)
 initializes timing system (all timers)
void timer0Init (void)
 initialize timer0
void timer1Init (void)
 initialize timer1
void timer2Init (void)
void timer3Init (void)
void timer0SetPrescaler (u08 prescale)
 set timer0 prescaler division index
void timer1SetPrescaler (u08 prescale)
 set timer1 prescaler division index
void timer2SetPrescaler (u08 prescale)
 set timer2 prescaler division index
void timer3SetPrescaler (u08 prescale)
 set timer3 prescaler division index
u16 timer0GetPrescaler (void)
 get timer0 prescaler division rate
u16 timer1GetPrescaler (void)
 get timer1 prescaler division rate
u16 timer2GetPrescaler (void)
 get timer2 prescaler division rate
u16 timer3GetPrescaler (void)
 get timer3 prescaler division rate
void timerAttach (u08 interruptNum, void(*userFunc)(void))
 Attach a user function to a timer interrupt.
void timerDetach (u08 interruptNum)
 Detach a user function from a timer interrupt.
void timerPause (unsigned short pause_ms)
void timer0ClearOverflowCount (void)
 Clear timer0's overflow counter.
long timer0GetOverflowCount (void)
 read timer0's overflow counter
void timer2ClearOverflowCount (void)
long timer2GetOverflowCount (void)
void timer1PWMInit (u08 bitRes)
 initialize and set timer1 mode to PWM
void timer1PWMInitICR (u16 topcount)
 initialize and set timer1 mode to PWM with specific top count
void timer1PWMOff (void)
 turn off all timer1 PWM output and set timer mode to normal
void timer1PWMAOn (void)
 turn on timer1 Channel A (OC1A) PWM output
void timer1PWMBOn (void)
 turn on timer1 Channel B (OC1B) PWM output
void timer1PWMCOn (void)
 turn on timer1 Channel C (OC1C) PWM output
void timer1PWMAOff (void)
 turn off timer1 Channel A (OC1A) PWM output
void timer1PWMBOff (void)
 turn off timer1 Channel B (OC1B) PWM output
void timer1PWMCOff (void)
 turn off timer1 Channel C (OC1C) PWM output
void timer1PWMASet (u16 pwmDuty)
 set duty of timer1 Channel A (OC1A) PWM output
void timer1PWMBSet (u16 pwmDuty)
 set duty of timer1 Channel B (OC1B) PWM output
void timer1PWMCSet (u16 pwmDuty)
 set duty of timer1 Channel C (OC1C) PWM output
void timer3PWMInit (u08 bitRes)
 initialize and set timer3 mode to PWM
void timer3PWMInitICR (u16 topcount)
 initialize and set timer3 mode to PWM with specific top count
void timer3PWMOff (void)
 turn off all timer3 PWM output and set timer mode to normal
void timer3PWMAOn (void)
 turn on timer3 Channel A (OC3A) PWM output
void timer3PWMBOn (void)
 turn on timer3 Channel B (OC3B) PWM output
void timer3PWMCOn (void)
 turn on timer3 Channel C (OC3C) PWM output
void timer3PWMAOff (void)
 turn off timer3 Channel A (OC3A) PWM output
void timer3PWMBOff (void)
 turn off timer3 Channel B (OC3B) PWM output
void timer3PWMCOff (void)
 turn off timer3 Channel C (OC3C) PWM output
void timer3PWMASet (u16 pwmDuty)
 set duty of timer3 Channel A (OC3A) PWM output
void timer3PWMBSet (u16 pwmDuty)
 set duty of timer3 Channel B (OC3B) PWM output
void timer3PWMCSet (u16 pwmDuty)
 set duty of timer3 Channel C (OC3C) PWM output


Function Documentation

void timer1PWMInit u08  bitRes  ) 
 

Enter standard PWM Mode on timer1.

Parameters:
bitRes indicates the period/resolution to use for PWM output in timer bits. Must be either 8, 9, or 10 bits corresponding to PWM periods of 256, 512, or 1024 timer tics.

Definition at line 277 of file timer.c.

void timer1PWMInitICR u16  topcount  ) 
 

Enter PWM Mode on timer1 with a specific top-count value.

Parameters:
topcount indicates the desired PWM period in timer tics. Can be a number between 1 and 65535 (16-bit).

Definition at line 297 of file timer128.c.

void timerPause unsigned short  pause_ms  ) 
 

A timer-based delay/pause function

Parameters:
pause_ms Number of integer milliseconds to wait.

Definition at line 204 of file timer.c.


Generated on Sun Oct 29 03:41:09 2006 for Procyon AVRlib by  doxygen 1.4.2