Lab 8
Lab 8
Lab 8
Name Reg. No. Lab Tasks Marks Viva Marks Total Marks
Lab 8:
“Timers in ATMEGA328P”
Objective:
The objective of this lab to understand timers in Atmega328P and control the DC motor
using Arduino.
Introduction:
Timers are standard features available in almost all microcontrollers. The AVR
microcontroller has powerful and multifunction timers. Its timers can be used to generate a
delay, count events, and generate PWM waveforms, among their other uses.
Explanation:
A timer is a simple counter! The input clock of microcontroller and operation of the timer is independent of the
program execution.All the Atmel microcontrollers have Timers as an inbuilt peripheral. In this tutorial our focus
is on ATmega8 microcontroller. ATmega8 comes with two 8-bit and one 16-bit timer. This means that there are
3 sets of timers, each with the ability to count at different rates. The two 8-bit counters can count to 255 whilst
the 16- bit counter can count to 65,536. We learned that the simplest timer in Atmeag8 is TIMER0 with an 8-bit
resolution (0-255). Timers can run asynchronous to the main AVR core hence timers are totally independent of
CPU. A timer is usually specified by the maximum value to which it can count, beyond which it overflows and
resets to zero. Speed of the counting can be controlled by varying the speed of clock input to it. As we know, all
the components of a microcontroller unit (MCU) are someway connected to the central processing unit (CPU)
and some registers, to share information beween them.
The TCNTn register is compared to the OCRn register, when a compare match occurs the TOVn bit is set in the
TIFR register. Interestingly, Timer/Counter1 can run in Normal mode (0xFFFF) and two CTC modes! The
difference between the two CTC modes is that mode 4 uses the OCR1A register for its compare value, and mode
12 uses the ICR1 register. In normal mode TOV1 can generate an Overflow interrupt, in CTC (mode 4) mode
OCIF1A can generate an interrupt when it detects a compare match, and in CTC (mode 12) mode TICIE1 can
generate an interrupt when it detects a compare match. The 3rd, Timer/Counter2 (8-Bit) is the preferred one for
short time delays. It can run in Normal mode or CTC mode.
c. What is the maximum time delay you can generate with Timer 0 on ATMEGA328P using the
Arduino board available in the lab? Use the polling method to check the TOV0 flag and toggle
the pin. While generating maximum time delay, you may ignore the overheads in the calculation.
Show your calculation and implementation to the lab instructor.
Code:
sbi DDRB, 5
ldi R18,1<<5 // R16 = 0b_0010_0000 (Pin 5 of PORTB)
ldi R17,0
out PORTB, R17
BEGIN:
rcall DELAY //3
eor R17, R18 //1
out PORTB, R17 //1
rjmp BEGIN //2
DELAY:
ldi R16, 0 //1[;p
out TCNT0, R16 //1
ldi R16, 0b00000000 // 1; COM0A1, COM0A0, COM0B1, COM0B0, -, -, WGM01, WGM00 = TCCR0A
out TCCR0A, R16 //1
ldi R16, 0b00000101 // 1; FOC0A, FOC0B, -, -, WGM02, CS02, CS01, CS00 = TCCR0B
out TCCR0B, R16 // 1; counter starts running after execution of this instruction
AGAIN:
in R20, TIFR0 //1
sbrs R20, TOV0 //1/2
rjmp AGAIN //2
ldi R20, 0x00 //1
out TCCR0B, R20 //1
ldi R20, (1<<TOV0) //1
out TIFR0, R20 //1
RET //4
Output:
Lab Task 2: Timer 0 and Interrupt.
Generate a frequency of 1Khz using the Timer 0 and overflow interrupt on ATMEGA328P using the
Arduino board available in the lab. Show your calculation and implementation to the lab instructor.
Mention the frequency measured using oscilloscope. Calculate variation (if any) from 1Khz.
Code:
.org 0x0000 ; interrupt vector for reset
rjmp start
.org 0x0020 ; interrupt vector for TOV0
rjmp ISR_TOV0
start:
.org 0x0100 ; our 'main' code starts here
ldi r17, 0xFF ;
ldi R16, 0b00000001 ; we are unmasking the interrupt for Timer 0 overflow
sts TIMSK0, R16 ; note that unless we set the 'I: global interrupt enable'
; bit in SREG, none of the interrupts will work
ISR_TOV0: ;this is isr for TOV0, note that upon entering in isr,
;all the interrupts are disabled, i.e., I bit in SREG is cleared
out PIND, r17 ; toggle output of PORT D
reti ; reti returns as well as sets the I bit in SREG, enables all the interrupts
Output:
Lab Task 3: Timer 0 CTC Mode.
What is the maximum frequency you can generate on OC0A pin using the Timer 0 in CTC mode on
ATMEGA328P using the Arduino board available in the lab? Refer to datasheet for finding out
frequency in CTC mode. Show your calculation and implementation to the lab instructor.
Code:
ldi R16, 0xFF
out DDRD, R16
ldi R16, 0b00000000
out TCNT0, R16
Code i:
rjmp start
.org 0x0100
start:
ldi R16, 0xFF
out DDRD, R16
ldi R16, 0b_0000_0000
out TCNT0, R16
ldi R16, 0b_0000_0001 ; FOC0A, FOC0B, -, -, WGM02, CS02, CS01, CS00 = TCCR0B
out TCCR0B, R16
ldi R16, 0b10000011 ; COM0A1, COM0A0, COM0B1, COM0B0, -, -, WGM01, WGM00 = TCCR0A
out TCCR0A, R16
ldi R16, 0b00000001 ; FOC0A, FOC0B, -, -, WGM02, CS02, CS01, CS00 = TCCR0B
out TCCR0B, R16
here:
call Mydelay
rjmp here
Mydelay:
LDI R23, 255
LDI R18,0
inc R19
out OCR0A, R19
rcall delay
cpse R19, R23
rjmp here
L10:
dec R19
out OCR0A,R19
rcall delay
cpse R19, R18
RJMP L10
delay:
ldi r22, 10
L6:ldi r20, 40
L4: ldi r21, 60
L5: dec r21
brne L5
dec r20
brne L4
dec r22
brne L6
ret
b. Control the brightness of a LED / DC motor (via L298D driver) connected to OC0A pin, using any of
the above mentioned PWM modes. The brightness / speed of LED / DC motor should gradually increase from
minimum to maximum, and then decrease from maximum to minimum, repeatedly. Show your
implementation to the lab instructor.
Code:
//.org 0x0100
//start:
int i;
//here: rjmp here
while(1)
{
//OCR0A = 0;
TCNT0=0;
TCCR0A=0x83;
TCCR0B=0b00000011;
for(i=0;i<100;i=i+20)
{
//ldi R16, 0b_0100_0000 ; compare A value
//out OCR0A, R16
OCR0A=100-i;
//i=i+1;
}
_delay_ms(10000);
for(i=0;i<100;i=i+20)
{
//ldi R16, 0b_0100_0000 ; compare A value
//out OCR0A, R16
OCR0A=i;
Code:
rjmp start
start:
ldi R16, 0xFF
out DDRB, R16
ldi R16, 0b00000000 ; COM0A1, COM0A0, COM0B1, COM0B0, -, -, WGM01, WGM00 = TCCR0A
out TCCR0A, R16
ldi R16, 0b00000111 ; FOC0A, FOC0B, -, -, WGM02, CS02, CS01, CS00 = TCCR0B
out TCCR0B, R16
here:
in R17, TCNT0
out PORTB, R17
rjmp here
Conclusion:
Timers can be used to measure the time elapsed or the external events occurring for a specific time interval.
They are used to maintain the operation of the embedded system in sync with the clock. The clock can be an
external clock or the system clock.