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

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


Detailed Description

 #include "timer.h" 
Overview
This library provides functions for use with the timers internal to the AVR processors. Functions include initialization, set prescaler, calibrated pause function (in milliseconds), attaching and detaching of user functions to interrupts, overflow counters, PWM. Arbitrary frequency generation has been moved to the Pulse Library.
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.


Modules

 Timer PWM Commands

Defines

#define PWM10   WGM10
#define PWM11   WGM11
#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   TIMER_CLK_DIV8
 timer 0 prescaler default
#define TIMER1PRESCALE   TIMER_CLK_DIV64
 timer 1 prescaler default
#define TIMER2PRESCALE   TIMERRTC_CLK_DIV64
 timer 2 prescaler default
#define TIMER0OVERFLOW_INT   0
#define TIMER1OVERFLOW_INT   1
#define TIMER1OUTCOMPAREA_INT   2
#define TIMER1OUTCOMPAREB_INT   3
#define TIMER1INPUTCAPTURE_INT   4
#define TIMER2OVERFLOW_INT   5
#define TIMER2OUTCOMPARE_INT   6
#define TIMER_NUM_INTERRUPTS   7
#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 timer0SetPrescaler (u08 prescale)
 set timer0 prescaler
u16 timer0GetPrescaler (void)
 get timer0 prescaler
void timer1SetPrescaler (u08 prescale)
 set timer1 prescaler
u16 timer1GetPrescaler (void)
 get timer0 prescaler
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


Function Documentation

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