This is an old revision of the document!
Timers
This library covers large part of the functionality of ATmega128 timers. There are data types and functions which make usage of timers easier. Unfortunately, because of the complexity of AVR timers, there are no common functions to use different timers. Each of the timer has functions which starts with the prefix of “timer” and its index.
Data types
Functions
Initializes timer 0 in normal mode. In this mode timer counts from 0 to 255 (including). Only possible event is timer overflow. Parameters:
Initializes timer 2 in normal mode. In this mode timer counts from 0 to 255 (including). Only possible event is timer overflow. Parameters:
void timer0_stop()
void timer2_stop()
Stops timer 0/2.
Returns timer 0/2 current value. Parameters:
Sets timer 0/2 value. Parameters:
Enables or disables timer 0/2 overflow interrupt. Interrupt vector name is “TIMERn_OVF_vect” where “n” represents 0 or 2. Parameters:
Checks timer 0/2 overflow flag. Parameters:
Resets timer 0/2 overflow flag.
Initializes timer 1/3 in normal mode. In this mode timer counts from 0 to 65535 (including). Only possible event is timer overflow. Parameters:
void timer1_init_ctc(timer1_prescale prescale, timer1_ctc_top top)
void timer3_init_ctc(timer3_prescale prescale, timer3_ctc_top top)
Taimer 1/3 CTC (inglise keeles Clear Timer on Compare Match) režiimi seadistamine. Selles režiimis taimer ei loenda mitte 65535-ni, vaid valitud registri väärtuseni ja tekitab soovi korral sinnani jõudes katkestuse. Parameetrid:
prescale - Taktijaguri tegur.
top - Taimeri maksimaalse väärtuse registri valik. Valida saab kahe registri vahel, mille mõlema muutmiseks on omaette funktsioonid. Mõlemad registrid võib loenduri tippu jõudes katkestust tekitama seadistada.
void timer1_init_fast_pwm(timer1_prescale prescale, timer1_fast_pwm_top top, timer1_fast_pwm_output_mode output_a, timer1_fast_pwm_output_mode output_b, timer1_fast_pwm_output_mode output_c)
void timer3_init_fast_pwm(timer3_prescale prescale, timer3_fast_pwm_top top, timer3_fast_pwm_output_mode output_a, timer3_fast_pwm_output_mode output_b, timer3_fast_pwm_output_mode output_c)
Taimer 1/3 kiire PWM tekitamise režiimi seadistamine. Selles režiimis on valitav väärtus, milleni taimer loendab, ehk PWM signaali periood. Taimeril on 3 PWM signaali genereerimise üksust (A, B ja C), millel kõigil on seadistatav väljund. Parameters:
prescale - Taktijaguri tegur.
top - Taimeri maksimaalse väärtuse valik. Valida saab konstantide ja kahe registri vahel. Mõlemad registrid võib loenduri tippu jõudes katkestust tekitama seadistada.
output_a - Väljundviigu A seadistus.
output_b - Väljundviigu B seadistus.
output_c - Väljundviigu C seadistus.
unsigned short timer1_get_compare_match_unitA_value(void)
unsigned short timer1_get_compare_match_unitB_value(void)
unsigned short timer1_get_compare_match_unitC_value(void)
unsigned short timer3_get_compare_match_unitA_value(void)
unsigned short timer3_get_compare_match_unitB_value(void)
unsigned short timer3_get_compare_match_unitC_value(void)
Taimeri 1/3 signaali genereerimise üksuse A/B/C võrdlusväärtuse tagastamine. Parameters:
void timer1_set_compare_match_unitA_value(unsigned short value)
void timer1_set_compare_match_unitB_value(unsigned short value)
void timer1_set_compare_match_unitC_value(unsigned short value)
void timer3_set_compare_match_unitA_value(unsigned short value)
void timer3_set_compare_match_unitB_value(unsigned short value)
void timer3_set_compare_match_unitC_value(unsigned short value)
Taimeri 1/3 signaali genereerimise üksuse A/B/C võrdlusväärtuse määramine. Parameetrid:
void timer1_overflow_interrupt_enable(bool enable)
void timer3_overflow_interrupt_enable(bool enable)
Taimer 1/3 ületäitumise katkestuse lubamine või keelamine. Katkestuse vektor on TIMERn_OVF_vect, kus “n” on 1 või 3. Parameters:
void timer1_compare_match_unitA_interrupt_enable(bool enable)
void timer1_compare_match_unitB_interrupt_enable(bool enable)
void timer1_compare_match_unitC_interrupt_enable(bool enable)
void timer3_compare_match_unitA_interrupt_enable(bool enable)
void timer3_compare_match_unitB_interrupt_enable(bool enable)
void timer3_compare_match_unitC_interrupt_enable(bool enable)
Taimer 1/3 signaali genereerimise üksuse A/B/C võrdluse sündmuse katkestuse lubamine või keelamine. Katkestuse vektor on TIMERn_COMPx_vect, kus on “n” on 1 või 3 ja “x” on A, B või C. Parameters:
Enables or disables timer 1/3 input capture interrupt. Interrupt vector name is “TIMERn_CAPT_vect”, where “n” represents 1 or 3. Parameters:
Checks timer 1/3 overflow flag. Parameters:
Checks timer 1/3 input capture flag. Parameters:
Resets timer 1/3 overflow flag.
Resets timer 1/3 input capture flag.
Example
In the following program timer 0 is started in normal mode with overflow interrupt.
#include <homelab/timer.h>
#include <avr/interrupt.h>
// Overflow interrupt program.
ISR(TIMER0_OVF_vect)
{
}
int main(void)
{
// Initializing of timer 0 in normal mode.
timer0_init_normal(TIMER0_PRESCALE_32);
// Enabling timer 0 overflow interrupt.
timer0_overflow_interrupt_enable(true);
// Global interrupts enabling.
sei();
}