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

Lecture_10 (Part-II)_Timer0 and Timer1

Uploaded by

Mustaq Mujahid
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)
7 views

Lecture_10 (Part-II)_Timer0 and Timer1

Uploaded by

Mustaq Mujahid
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/ 14

EEE/ECE 365

Microprocessor

Lecture #10 (Part-II)


Timer0, Timer1
Md Rakibul Hasan
Lecturer, Dept. of EEE, BRAC University

Slide Courtesy of Imtiaz Ahmed, Lecturer, Dept. of EEE, BRAC University


An Example of Using Timer0

6 November 2022
• Let us assume that it is required to generate a square wave having 20 ms time
period and 50% duty cycle.
• Since time period is 20 ms so half of the time period is 10 ms.
• In a output pin if we can send high for 10 ms and low for next 10ms and
repeat it, the output will be a square of 20 ms (TP).
• We shall send a high in a pin say PC.0 and start Timer0 of ATmega32.
• As soon as the time counting of 10 ms is finished, we shall toggle the same
pin.
• And start the timer again and do the same repeatedly.
• Now the question is how to generate 10mS time interval.

2
An exercise on generation of time delay (contd.)

6 November 2022
• Now to measure 10ms accurately, we have to choose a slower clock (in the
order of kHz).
• Let us choose a prescaler of 1024.
• Hence clock frequency will be 16MHz/1024 or 15.625 kHz.
• So the time period of the clock pulse will be (1/15.625) ms or 0.064 ms.
• So to measure 10 ms we need 10 ms/0.064 ms or 156 number of clock pulse.
• If we load TCNT0 with 0 and Compare register, OCR0=155, the after 155+1=156
number of clock pulses OCF0 flag will be raised.
• Then the flag bit will be monitored by the uC. When it is found to be set, the
program will go to its ISR.
• In the ISR. we shall write code to toggle the output pin.
3
6 November 2022
Interfacing Circuit

4
Timer0 in Output Compare mode

6 November 2022
#include <mega32.h>
TCNT0=0;
interrupt [TIM0_COMP] void OCR0=155;
timer0_comp_isr(void)
TIMSK=(0<<OCIE2) | (0<<TOIE2) |
{ (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) |
PORTC.0=~PORTC.0; (0<<TOIE1) | (1<<OCIE0) | (0<<TOIE0);
TCNT0=0; #asm("sei")
} PORTC.0=0;
void main(void) while (1)
{
{
DDRC.0=1;
}
TCCR0=(0<<WGM00) | (0<<COM01) |
(0<<COM00) | (0<<WGM01) | (1<<CS02) | }
(0<<CS01) | (1<<CS00);
5
6 November 2022
Timer1 Programming
• Timer1 is a 16-bit timer and has lots of capabilities.
• It is split into two bytes. These are referred to TCNT1L and TCNT1H.
• Timer1 has two control registers, namely TCCR1A (8-bit) and
TCCR1B (8-bit).
• TOV1 flag bit goes high when overflow occurs.
• There are two OCR registers, namely OCR1A(16-bit) and OCR1B(16-
bit).
• There are two separate flags for each of two OCR registers, OCF1A
and OCF1B which act independently. The figure in the next slide
explains how they work.
6 November 2022
Compare and Overflow in Timer1

Ref. Reg. B OCR1BH OCR1BL

= OCF1B
flag
Timer Reg. TCNT1H TCNT1L

flag TOV1 = OCF1A


Ref. Reg. A flag
OCR1AH OCR1AL
TCCR1A & TCCR1B (Timer Counter

6 November 2022
TCCR1A Register
Control Registers)
COM1A1 COM1A0 COM1B1 COM1B0 FOC1A1 FOC1B1 WGM11 WGM10

TCCR1B Register
ICNC1 ICES1 - WGM13 WGM12 CS12 CS11 CS10
FOC1A, FOC1B – Related to force compare
ICNC1, ICES1 – Related to Input Capture
COM1A1, COM1A0, COM1B1, COM1B0
– Related to Waveform Generation
WGM13, WGM12, WGM11, WGM10 – Mode Selection
CS12, CS11, CS10 – Clock Selection
Block Diagram for Timer1 (similar to Timer0)

6 November 2022
CLKI/O STOP
0
CLK
1
CLK/8
2
CLK/64 3
PRESCALER CLK/256 MUX
4
CLK/1024
5
Falling Edge
EDGE 6
DETECTOR Rising Edge 0
7 2 1

T1 CS10
CS11
CS12
6 November 2022
An Exercise
• Write a program to toggle only the PORTB.5 bit continuously
every second. Use timer 1, Normal mode, and 1:256 prescaler to
create the delay. Assume XTAL=8 MHz. Use overflow method.
Solution:
• Prescaler=1:256 → ftimer=(8/256) MHz
• Ttimer = 1/ftimer = 256/8 us = 32 us
• So, number of clock necessary to make a delay of 1 sec is
(1s/32us)=106/32=31250
• Therefore, the number to be loaded in the timer register is
(65535-31250+1)=34286=0x85EE
• So, TCNT1L=0xEE and TCNT1H=0x85
6 November 2022
The Program Using Timer1
#include <mega32.h> void main(void)
{
interrupt [TIM1_OVF] void
TCNT1L=0XEE;
timer1_ovf_isr (void)
TCNT1H=0X85;
{ TCCR1A=0x00;
PORTB.5=~PORTB.5; TCCR1B=0x04;
TCNT1L=0XEE; TIMSK=0x04;
DDRB.5=1;
TCNT1H=0X85; #asm(“sei”);
} while (1) {

}
}
6 November 2022
Compare Method
• There are two compare registers, OCR1A and OCR1B.
• If you use OCR1A register, then OCIE1A bit of TIMSK
register should be enabled and for OCR1B, the OCIE1B bit
should be enabled.
• Let us take, OCR1A=31250-1=31249 (dec), or 0x7A11
• TIMSK=0b00010000 = 0x10
• Name of ISR is [TIM1_COMPA] instead of [TIM1_OVF1]

6 November 2022 Prof. S. M. Luttful Kabir, IICT, BUET 12


6 November 2022
The Program Using Compare method
#include <mega32.h> void main(void)
interrupt [TIM1_COMPA] void {
timer1_compa_isr(void) DDRB.5=1;
{ TCCR1A=0x00;
PORTB.5=~PORTB.5; TCCR1B=0x04;
TCNT1H=0x00; TCNT1H=0x00;
TCNT1L=0x00; TCNT1L=0x00;
} OCR1AH=0x7A;
OCR1AL=0x11;
TIMSK=0x10;
#asm("sei")
while (1)
{

}
}
6 November 2022
14
Thanks

You might also like