100% found this document useful (1 vote)
56 views5 pages

ATmega16 Timer1 Configuration Guide

The ATmega16 Timer 1 is a 16-bit timer with two channels that can generate longer delays than Timer 0 and includes an input capture unit. It operates in Normal and CTC modes, controlled by specific bits in the Timer/Counter Control Registers, and utilizes a prescaler to adjust the timer's clock speed. An example program demonstrates how to use Timer 1 to blink LEDs connected to Port A every 5 seconds using CTC mode and a calculated output compare register value.

Uploaded by

david1milad1982
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
56 views5 pages

ATmega16 Timer1 Configuration Guide

The ATmega16 Timer 1 is a 16-bit timer with two channels that can generate longer delays than Timer 0 and includes an input capture unit. It operates in Normal and CTC modes, controlled by specific bits in the Timer/Counter Control Registers, and utilizes a prescaler to adjust the timer's clock speed. An example program demonstrates how to use Timer 1 to blink LEDs connected to Port A every 5 seconds using CTC mode and a calculated output compare register value.

Uploaded by

david1milad1982
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ATmega16 Timer 1

Microcontroller Timer/Counter:
- The timer and counter functions in the microcontroller simply count
(increment TCNT) in sync with the microcontroller clock. However, the
counter can only count up to either 256 (8-bit counter), or 65535 (16-bit
counter).

ATmega16 Timer 1:
- It is a 16-bit timer.
- It contains two channels (A and B).
- It can generate a delay time greater than what timer0 can generate.
- It contains an input capture unit to store the value of TCNT1 when it
receives an input capture event.

Prescaler:
- It is simply a way for the counter to skip a certain number of
microcontroller clock ticks. The AVR microcontrollers allow prescaling
(skipping) numbers of: 8, 64, 256 and 1024
- Changing CS10, CS11 and CS12 bits of the timer control registers results
in changing the effective prescaler or choosing to sync with the external
clock signal connected to pin T1.

TA: Abdalla Tawfik


Modes of Operations:
- Changing WGM10, WGM11, WGM12, and WGM13 bits of the timer
control registers results in changing the operating mode, mostly used
modes of Timer1 are Normal and CTC modes:

WGM13 WGM12 WGM11 WGM10 Mode


0 0 0 0 Normal
0 1 0 0 CTC

- Normal Mode
• The counter starts to increase until it reaches its maximum value
then it resets to zero and sets the timer1 overflow flag.

- CTC Mode (Clear Timer on Compare)


• The counter starts to increase until it reaches the value stored in
the OCR1A or OCR1B Register then it resets to zero and sets the
timer1 output compare match flag of Channel A or B.

Timer 1 Control Registers:

Timer/Counter1 Control Register A (TCCR1A):

The most important bits of the registers are highlighted in yellow.

- WGM10 and WGM11 bits are used in combination with WGM12 and
WGM13 found in TCCR1B to set the mode of operation of Timer 1.

Timer/Counter1 Control Register B (TCCR1B):

The most important bits of the registers are highlighted in green and yellow.

TA: Abdalla Tawfik


- Clock Select Bits – CS12:0 used to select the prescaler factor as
described above.
- WGM12 and WGM13 bits are used in combination with WGM10 and
WGM11 found in TCCR1A to set the mode of operation of Timer 1.

Timer Interrupt Mask:


- It contains flags to enable or disable the timer interrupt signals
• TOIE1: (Timer1 Overflow Interrupt Enable Flag)
• OCIE1B: (Timer1 Output Compare Match Interrupt Enable Flag for
Channel B)
• OCIE1A: (Timer1 Output Compare Match Interrupt Enable Flag for
Channel A)
• TICIE1: (Timer1 Input Capture Interrupt Enable Flag)

Timer Interrupt Flags Register:


- These flags are set when the timer reaches the overflow, output
compare match states of channel A or B or when an input capture event
is occurred.

Status Register:
- The seventh bit of the status register is the global interrupt flag, that
must be set to one to allow any interrupt signal

TA: Abdalla Tawfik


Interrupt Vectors Table:
- An interrupt vector is the memory location of an interrupt handler,
which prioritizes interrupts and saves them in a queue if more than one
interrupt is waiting to be handled.

Timer1 Example:
A MikroC program for the ATmega16 to blink 8 LEDs connected to Port A every
5 seconds using timer1 for timing the blinking operation. Assume that
ATmega16 is running at 1 MHz.

Solution:

Usually we use CTC mode with channel A to generate a specific time delay such
as 5 seconds.

Using the next equations to calculate the value that should be stored in OCR1A
register and the appropriate prescaler.

TA: Abdalla Tawfik


Note: You can choose any prescaler value that makes the OCR1A less than its
maximum value (65535).

𝟏
𝑴𝑪 𝑪𝒍𝒐𝒄𝒌 𝑻𝒊𝒎𝒆 𝑷𝒆𝒓𝒊𝒐𝒅 =
𝑴𝑪 𝑪𝒍𝒐𝒄𝒌 𝑹𝒂𝒕𝒆

𝑻𝒊𝒎𝒆𝒓 𝑪𝒍𝒐𝒄𝒌 𝑻𝒊𝒎𝒆 𝑷𝒆𝒓𝒊𝒐𝒅 = 𝑴𝑪 𝑪𝒍𝒐𝒄𝒌 𝑻𝒊𝒎𝒆 𝑷𝒆𝒓𝒊𝒐𝒅 ∗ 𝑷𝒓𝒆𝒔𝒄𝒂𝒍𝒆𝒓

𝑹𝒆𝒒𝒖𝒊𝒓𝒆𝒅 𝑫𝒆𝒍𝒂𝒚
𝑻𝒊𝒎𝒆𝒓 𝑪𝒐𝒖𝒏𝒕 (𝑶𝑪𝑹𝟏𝑨) = −𝟏
𝑻𝒊𝒎𝒆𝒓 𝑪𝒍𝒐𝒄𝒌 𝑻𝒊𝒎𝒆 𝑷𝒆𝒓𝒊𝒐𝒅

𝑻𝒊𝒎𝒆𝒓 𝑪𝒐𝒖𝒏𝒕 (𝑶𝑪𝑹𝟏𝑨) = 𝟏𝟗𝟓𝟑𝟎 𝒊𝒇 𝒑𝒓𝒆𝒔𝒄𝒂𝒍𝒆𝒓 = 𝟐𝟓𝟔

void main () {
// Configure Port A as O/P
DDRA = 0xFF;
// Initialize Port A with 0x00 value (switch off the leds)
PORTA = 0x00;

// Enable the Global Interrupt Flag in Status Register


SREG |= 0x80;
// Enable the Output Compare Match of channel A Interrupt Flag
TIMSK = 0x10;
// Choose the CTC mode of operation and the prescaler of 256
TCCR1A = 0x00;
TCCR1B = 0x0C;
// Set the Output Compare Register with the calculated value
OCR1A = 19530;

// Infinite loop to force the microcontroller not to Halt and continue


executing the program as long as it’s connected to a power source
while (1);
}

// Interrupt Service Routine for the Output Compare Interrupt Signal


TIMER1_CTC_ISR () org 0x00C {
// Toggle the state of Port A
PORTA = ~PORTA;
}

TA: Abdalla Tawfik

You might also like