Temporizadores

Esta biblioteca cobre uma boa parte das funcionalidades dos temporizadores do ATmega128. Existem tipos e funções que tornam mais fácil o uso de temporizadores. Infelizmente, devido à complexidade dos temporizadores do AVR, não existem funções comuns para o uso de diferentes temporizadores. Cada um dos temporizadores tem funções cujo nome tem o prefixo “timer” e o seu indíce.

Tipos

  • timer0_prescale
    Enumeração do tipo prescaler Timer 0. Opções:
    • TIMER0_NO_PRESCALE - Sem prescaler (sem divisão).
    • TIMER0_PRESCALE_8 - Clk / 8.
    • TIMER0_PRESCALE_32 - Clk / 32.
    • TIMER0_PRESCALE_64 - Clk / 64.
    • TIMER0_PRESCALE_128 - Clk / 128.
    • TIMER0_PRESCALE_256 - Clk / 256.
    • TIMER0_PRESCALE_1024 - Clk / 1024.
  • timer2_prescale
    Enumeração do tipo prescaler Timer 2. Opções:
    • TIMER2_NO_PRESCALE - Sem prescaler (sem divisão).
    • TIMER2_PRESCALE_8 - Clk / 8.
    • TIMER2_PRESCALE_64 - Clk / 64.
    • TIMER2_PRESCALE_256 - Clk / 256.
    • TIMER2_PRESCALE_1024 - Clk / 1024.
    • TIMER2_PRESCALE_T2_FALLING - Relógio no pin T2 falling edge.
    • TIMER2_PRESCALE_T2_RISING - Relógio no pin T2 rising edge.
  • timer1_prescale
  • timer3_prescale
    Enumeração do tipo prescaler Timer 1/3. Opções (“n” significa 1 ou 3):
    • TIMERn_NO_PRESCALE - Sem prescaler (sem divisão).
    • TIMERn_PRESCALE_8 - Clk / 8.
    • TIMERn_PRESCALE_64 - Clk / 64.
    • TIMERn_PRESCALE_256 - Clk / 256.
    • TIMERn_PRESCALE_1024 - Clk / 1024.
    • TIMERn_PRESCALE_Tn_FALLING - Relógio no pin Tn falling edge.
    • TIMERn_PRESCALE_Tn_RISING - Relógio no Tn rising edge.
  • timer1_ctc_top
  • timer3_ctc_top
    Enumeração do tipo Timer 1/3 CTC em modo top value. Opções (“n” significa 1 ou 3):
    • TIMERn_CTC_TOP_OCRA - Top value from timer 1/3 output compare register A.
    • TIMERn_CTC_TOP_ICR - Top value from timer 1/3 input capture register.
  • timer1_fast_pwm_top
  • timer3_fast_pwm_top
    Timer 1/3 fast PWM mode top value enumeration data type. Options (“n” means 1 or 3):
    • TIMERn_FAST_PWM_TOP_256 - Top value 255.
    • TIMERn_FAST_PWM_TOP_512 - Top value 511.
    • TIMERn_FAST_PWM_TOP_1024 - Top value 1023.
    • TIMERn_FAST_PWM_TOP_ICR - Top value from timer 1/3 input capture register.
    • TIMERn_PAST_PWM_TOP_OCRA - Top value from timer 1/3 output compare register A.
  • timer1_fast_pwm_output_mode
  • timer1_fast_pwm_output_mode
    Timer 1/3 fast PWM mode outputs configuration enumeration data type. Options (“n” means 1 or 3):
    • TIMERn_FAST_PWM_OUTPUT_DISABLE - No output.
    • TIMERn_FAST_PWM_OUTPUT_TOGGLE_ON_MATCH - Output toggles on compare match.
    • TIMERn_FAST_PWM_OUTPUT_CLEAR_ON_MATCH - Output clears on compare match.
    • TIMERn_FAST_PWM_OUTPUT_SET_ON_MATCH - Output sets on compare match.

Funções

  • void timer0_init_normal(timer0_prescale prescale)

Inicializa o timer 0 em modo normal. Neste modo o timer conta de 0 a 255 (incluído). A interrupção de overflow pode ser usada. Parâmetros:

  • prescale - Prescaler.
  • void timer2_init_normal(timer2_prescale prescale)

Inicializa o timer 2 em modo normal. Neste modo o timer conta de 0 a 255 (incluído). A interrupção de overflow pode ser usada. Parâmetros:

  • prescale - Prescaler.
  • void timer0_stop()
  • void timer2_stop()

Pára o timer 0/2.

  • unsigned char timer0_get_value(void)
  • unsigned char timer2_get_value(void)

Devolve o valor actual do timer 0/2. Parâmetros:

  • Devolve um valor de timer de 8 bits.
  • void timer0_set_value(unsigned char value)
  • void timer2_set_value(unsigned char value)

Configura o valor do timer 0/2. Parâmetros:

  • value - Novo valor de 8 bits para o timer.
  • void timer0_overflow_interrupt_enable(bool enable)
  • void timer2_overflow_interrupt_enable(bool enable)

Habilita ou desabilita a interrupção de overflow do timer 0/2. O nome do vector de interrupção é “TIMERn_OVF_vect” onde “n” representa 0 ou 2. Parâmetros:

  • enable - true para habilitar a interrupção, false para desabilitar.
  • bool timer0_overflow_flag_is_set(void)
  • bool timer2_overflow_flag_is_set(void)

Verifica a flag de overflow do timer 0/2. Parâmetros:

  • Devolvetrue quando o overflow acontece, false caso contrário.
  • void timer0_overflow_flag_clear(void)
  • void timer2_overflow_flag_clear(void)

Restabelece a flag de overflow do timer 0/2.

  • void timer1_init_normal(timer1_prescale prescale)
  • void timer3_init_normal(timer3_prescale prescale)

Inicializa o timer 1/3 em modo normal. Neste modo o timer conta de 0 a 65535 (incluído). A interrupção de overflow pode ser usada. Parâmetros:

  • prescale - Prescaler.
  • void timer1_init_ctc(timer1_prescale prescale, timer1_ctc_top top)
  • void timer3_init_ctc(timer3_prescale prescale, timer3_ctc_top top)

Inicializa o timer 1/3 em modo CTC (Clear Timer on Compare Match). Neste modo o timer conta até um valor de topo especificado. A interrupção de overflow pode ser usada. Parâmetros:

  • prescale - Prescaler.
  • top - Selecção do valor de topo do timer. O valor tem de ser especificado através de um registo contendo o valor de topo.
  • 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)

Inicializa o timer 1/3 em modo PWM rápido. Neste modo o timer conta até um determinado valor, que também determina o período do sinal do PWM. O timer 1/3 tem 3 unidades de comparação de saída (A, B and C) para gerar os sinais PWM. Interrupções de overflow e match podem ser usadas. Parâmetros:

  • prescale - Prescaler.
  • top - Selecção do valor de topo para o timer. O valor tem de ser especificado através de um registo contendo o valor de topo.
  • output_a - Configuração da unidade de comparação de saída do pin A.
  • output_b - Configuração da unidade de comparação de saída do pin B.
  • output_c - Configuração da unidade de comparação de saída do pin C.
  • void timer1_stop()
  • void timer3_stop()

Pára o timer 1/3.

  • unsigned char timer1_get_value(void)
  • unsigned char timer3_get_value(void)

Devolve o valor actual do timer 1/3. Parâmetros:

  • Devolve um valor do timer com 16 bits.
  • void timer1_set_value(unsigned char value)
  • void timer3_set_value(unsigned char value)

Configura o valor do timer 0/2. Parâmetros:

  • value - Novo valor do timer em 16 bits.
  • 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)

Devolve o valor do registo de comparação da unidade de saída A/B/C do timer 1/3. Parâmetros:

  • Devolve o valor do registo de comparação em 16 bits.
  • 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)

Configura o valor do registo de comparação da unidade de saída A/B/C do timer 1/3. Parâmetros:

  • value - Novo valor do registo de comparação em 16 bits.
  • unsigned short timer1_get_input_capture_value(void)
  • unsigned short timer3_get_input_capture_value(void)

Devolve o valor do registo de captura da entrada do timer 1/3. Parâmetros:

  • Devolve o valor do registo de captura de entrada em 16 bits.
  • void timer1_set_input_capture_value(unsigned short value)
  • void timer3_set_input_capture_value(unsigned short value)

Configura o valor do registo de captura da entrada do timer 1/3. Parâmetros:

  • value - Novo valor do registo de captura de entrada em 16 bits.
  • void timer1_overflow_interrupt_enable(bool enable)
  • void timer3_overflow_interrupt_enable(bool enable)

Habilita ou desabilita a interrupção de overflow do timer 1/3. O nome do vector de interrupção é “TIMERn_OVF_vect” onde “n” representa 1 ou 3. Parâmetros:

  • enable - true para habilitar a interrupção, false para desabilitar.
  • 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)

Enables or disables timer 1/3 output compare unit A/B/C compare match interrupt. The name of the interrupt vector is “TIMERn_COMPx_vect” where “n” represents 1 or 3 and “x” represents A, B or C. Parameters:

  • enable - true to enable interrupt, false to disable.
  • void timer1_input_capture_interrupt_enable(bool enable)
  • void timer3_input_capture_interrupt_enable(bool enable)

Habilita ou desabilita a interrupção de captura de entrada do timer 1/3. O nme do vector de interrupção é “TIMERn_CAPT_vect”, onde “n” representa 1 ou 3. Parâmetros:

  • enable - true para habilitar a interrupção, false para desabilitar.
  • bool timer1_overflow_flag_is_set(void)
  • bool timer3_overflow_flag_is_set(void)

Verifica a flag de overfloe do timer 1/3. Parâmetros:

  • Devolve true quando o overflow aconteceu, false caso contrário.
  • bool timer1_input_capture_flag_is_set(void)
  • bool timer3_input_capture_flag_is_set(void)

Verifica a flag de captura de entrada do timer 1/3. Parâmetros:

  • Devolve true quando a captura da entrada está concluída, false caso contrário.
  • void timer1_overflow_flag_clear(void)
  • void timer3_overflow_flag_clear(void)

Restabelece a flag de overflow do timer 1/3.

  • void timer1_input_capture_flag_clear(void)
  • void timer3_input_capture_flag_clear(void)

Restabelece a flag de captura de entrada do timer 1/3.

Exemplo

No programa que se segue o timer 0 é inicializado em modo normal com interrupção de overflow.

#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();
}
pt/software/homelab/library/timer.txt · Last modified: by 127.0.0.1
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0