0% found this document useful (0 votes)
24 views

ATmega16 Timer 0

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
0% found this document useful (0 votes)
24 views

ATmega16 Timer 0

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
You are on page 1/ 6

ATmega16 Timer 0

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).

Block Diagram:

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 CS00, CS01 and CS02 bits of the timer control register results
in changing the effective prescaler or choosing to sync with the external
clock signal connected to pin T0

TA: Abdalla Tawfik


Modes of Operations:
- Changing WGM00 and WGM01 bits of the timer control register results
in changing the operating mode

WGM00 WGM01 Mode


0 0 Normal
0 1 CTC
1 0 PWM, Phase Correct
1 1 Fast PWM

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

- CTC Mode (Clear Timer on Compare)


• The counter starts to increase until it reaches the value stored in
the OCR0 Register then it resets to zero and sets the timer0
output compare match flag.

Timer/Counter Control Register:


- It is used to change the prescaler and the timer operation mode

TA: Abdalla Tawfik


Timer Interrupt Mask:
- It contains flags to enable or disable the timer interrupt signals flags
• TOIE0: (Timer0 Overflow Interrupt Enable Flag)
• OCIE0: (Timer0 Output Compare Match Interrupt Enable Flag)

Timer Interrupt Flags Register:


- These flags are set when the timer reaches the overflow or output
compare match states
• TOV0: This bit is set (one) whenever TIMER0 overflows.
▪ This bit is reset (zero) whenever the Interrupt Service
Routine (ISR) is executed. If there is no ISR to execute, we
can clear it manually by writing one to it.
• OCF0: This bit is set (one) whenever TIMER0 makes a compare
match.
▪ This bit is reset (zero) whenever the Interrupt Service
Routine (ISR) is executed. If there is no ISR to execute, we
can clear it manually by writing one to it.

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.

TA: Abdalla Tawfik


Timer0 Example:
A MikroC program for the ATmega16 to blink 8 LEDs connected to Port A every
250 milliseconds, using timer0 for timing the blinking operation. Assume that
ATmega16 is running at 1 MHz.
𝟏
𝑴𝑪 𝑪𝒍𝒐𝒄𝒌 𝑻𝒊𝒎𝒆 𝑷𝒆𝒓𝒊𝒐𝒅 =
𝑴𝑪 𝑪𝒍𝒐𝒄𝒌 𝑹𝒂𝒕𝒆

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

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

𝑻𝒊𝒎𝒆𝒓 𝑪𝒐𝒖𝒏𝒕 (𝑶𝑪𝑹𝑶) = 𝟐𝟒𝟒

Using Interrupt:

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 Interrupt Enable Flag
TIMSK = 0x02;
// Choose the CTC mode of operation and the prescaler of 1024
TCCR0 = 0x0D;
// Set the Output Compare Register with the calculated value
OCR0 = 244;

// 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


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

TA: Abdalla Tawfik


Using Polling:

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

// Choose the CTC mode of operation and the prescaler of 1024


TCCR0 = 0x0D;
// Set the Output Compare Register with the calculated value
OCR0 = 244;
// Clear timer 0 counter register to start counting from zero
TCNT0 = 0x00;

// 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) {
// Force the microcontroller to wait as long as the timer is running
while (TIFR & (1 << OCF0) == 0);

// Once the timer is finished the OCF bit will be set to one and the
microcontroller will break from the while loop and continue executing
the remaining code

// Clear the OCF0 bit by writing 1 to it


TIFR |= (1<<OCF0);

// Toggle the state of Port A


PORTA = ~PORTA;
}
}

TA: Abdalla Tawfik

You might also like