Atmega328 Timer/Counter Usage: Sistemas Embebidos Oscar Acevedo, PHD

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

ATMega328 Timer/Counter Usage

Sistemas Embebidos
Oscar Acevedo, PhD
Timer/Counter Usage

• You should define the timer operation mode, clock source and prescaler, interrupts and
waveform generation

• Add code for interrupt service routine in your program

• Although timer 1 (16-bits) is used, the procedure is the same for other timers
• Only registers related to basic timer functionality will be presented
Timer1 Configuration – TCCR1A

COM1A[1:0] and COM1B[1:0]: Compare Output Mode


Control the Output Compare pins OC1A and OC1B behavior. These outputs
may override the normal port functionality of the I/O pin it is connected to. The
DDR bit corresponding to the OC1A or OC1B pin must be set in order
to enable the output driver.
Timer1 Configuration – TCCR1A

WGM10, WGM11: Waveform Generation Mode


These bits control the counting sequence of the counter, the source for
maximum (TOP) counter value, and what type of waveform generation to be
used. Modes of operation supported by the Timer/Counter unit are: Normal
mode (counter), Clear Timer on Compare match (CTC) mode, and three types
of Pulse Width Modulation (PWM) modes.
Timer 1 Configuration – TCCR1B

CS10, CS11, CS12: Clock Select


Select the clock source to be used by the Timer/Counter
About the Prescaler

• Arduino clock is 16MHz, or 62.5nS period


• It will take 65535*62,5nS = 4.0959375mS a full count

• The largest prescaler value is 1024, so the timer may count at 16MHz/1024 = 15625Hz,
or 64uS period
• It will take 4.19424 seconds a full count
• To count up to 1S, the counter should count up to 1S/64uS= 15625
More Timer 1 Registers
TCNT1: TC1 counter value

OCR1A: output compare register 1 A


More Timer 1 Registers

OCR1B: output compare register 1 B

TIMSK1: TC1 interrupt mask register


#include <avr/interrupt.h>

int main(void)
{

// timer1 configuration
TCCR1A= 0B00000000;
TCCR1B= 0B00000110;
TCCR1C= 0;
TIMSK1= 0B00000111;
OCR1A= 85;
OCR1B= 170;
TCNT1= 0;

sei(); //enable interrupts

while (1) //main program


{ }
return 0;
}

ISR(TIMER1_COMPA_vect) // interrupt routine for TMR1 compare A


{ }

ISR(TIMER1_COMPB_vect) // interrupt routine for TMR1 compare B


{ }

ISR(TIMER1_OVF_vect) // interrupt routine for TMR1 overflow (timer resets)


{ }
Fin de la presentación

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy