0% found this document useful (0 votes)
12 views8 pages

Lab 08

Uploaded by

Rasham Saqib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

Lab 08

Uploaded by

Rasham Saqib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Department of Electrical and Computer Engineering

Course Instructor: Dr. Asadullah Bukhari Dated: ________________

Semester: 4th Section: EE2

ECE-261L: Introduction to Embedded Systems Lab

Lab 08: Timers in ATMEGA328P

Name Reg. No. Lab Tasks Viva Marks Total Marks


Marks

Rasham
Saqib B21F0476EE019

ECE-261L: Introduction to Embedded SystemsPage 1


Timers in ATMEGA328P

Lab Task 1: Timer 0 Normal Mode


a. List the registers related to Timer 0.
b. What are the different modes in which Timer 0 can be run?
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. A sample code is given below:

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, 246 ;1


out TCNT0, R16 ;1

ldi R16, 0b_0000_0000 ; 1; COM0A1, COM0A0, COM0B1, COM0B0, -, -, WGM01, WGM00 =


TCCR0A
out TCCR0A, R16 ;1

ldi R16, 0b_0000_0001 ; 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

ECE-261L: Introduction to Embedded SystemsPage 2


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. A sample code is given below:

.org 0x0000 ; interrupt vector for reset


rjmp start

.org 0x0020 ; interrupt vector for TOV0


rjmp ISR_TOV0

.org 0x0100 ; our 'main' code starts here


start:
ldi r17, 0xFF ;

ldi R16, 0xFF ; PORT D as output


out DDRD, R16

ldi R16, 0b_0000_0000 ; initial value of counter


out TCNT0,
ECE-261L: R16
Introduction to Embedded SystemsPage 3
ldi R16, 0b_0000_0000 ; COM0A1, COM0A0, COM0B1, COM0B0, -, -, WGM01, WGM00 = TCCR0A
out TCCR0A, R16

ldi R16, 0b_0000_0001 ; we are unmasking the interrupt for Timer 0 overflow
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. A sample code is given below:

;CTC MODE WITH COMPARE AND TOGGLE


ldi R16, 0xFF
out DDRD, R16

ldi R16, 0b_0000_0000


out TCNT0, R16

ldi R16, 0b_1111_0000 ; compare A value


out OCR0A, R16

ldi R16, 0b_0100_0010 ; COM0A1, COM0A0, COM0B1, COM0B0, -, -, WGM01, WGM00 =


TCCR0A
out TCCR0A, R16

ldi R16, 0b_0000_0001 ; FOC0A, FOC0B, -, -, WGM02, CS02, CS01, CS00 = TCCR0B
out TCCR0B, R16

here: rjmp here

ECE-261L: Introduction to Embedded SystemsPage 4


Lab Task 4: PWM using Timer 0.
a. Explain the following modes of Timer 0 with the help of timing diagrams:
i. Fast PWM ({WGM2:0 = 011, COM0A1:0 = 10}, {WGM2:0 = 011, COM0A1:0 = 11})
ii. Phase-Correct PWM ({WGM2:0 = 001, COM0A1:0 = 10}, {WGM2:0 = 001,
COM0A1:0 = 11}
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. A sample code is given below:

;FAST PWM
.org 0x0000
rjmp start

.org 0x0100
start:

ldi R16, 0xFF


out DDRD, R16

ldi R16, 0b_0000_0000


out TCNT0, R16

ldi R16, 0b_0100_0000 ; compare A value


out OCR0A, R16

ldi R16, 0b_1000_0011 ; COM0A1, COM0A0, COM0B1, COM0B0, -, -, WGM01, WGM00 =


TCCR0A
out TCCR0A, R16

ldi R16, 0b_0000_0001 ; FOC0A, FOC0B, -, -, WGM02, CS02, CS01, CS00 = TCCR0B
out TCCR0B, R16

here: rjmp here

ECE-261L: Introduction to Embedded SystemsPage 5


;PHASE CORRECT PWM
.org 0x0000
rjmp start

.org 0x0100
start:

ldi R16, 0xFF


out DDRD, R16

ldi R16, 0b_0000_0000


out TCNT0, R16

ldi R16, 0b_0100_0000 ; compare A value


out OCR0A, R16

ldi R16, 0b_1000_0001 ; COM0A1, COM0A0, COM0B1, COM0B0, -, -, WGM01, WGM00 =


TCCR0A
out TCCR0A, R16

ldi R16, 0b_0000_0001 ; FOC0A, FOC0B, -, -, WGM02, CS02, CS01, CS00 = TCCR0B
out TCCR0B, R16

here: rjmp here

ECE-261L: Introduction to Embedded SystemsPage 6


Lab Task 5: Counting external pulses using Timer 0.
Count the number of pulses being fed to T0 pin and display the count on 2-digit BCD 7-segment
display. Show your implementation to the lab instructor. A sample code is given below:

; ;EXTERNAL PULSE
.org 0x0000
rjmp start

.org 0x0100
start:

ldi R16, 0xFF


out DDRB, R16

ldi R16, 0xFF


out PORTB, R16

ldi R16, 0b_0000_0000


out DDRD, R16

ldi R16, 0b_0000_0000


out TCNT0, R16

ldi R16, 0b_0000_0000 ; COM0A1, COM0A0, COM0B1, COM0B0, -, -, WGM01, WGM00 =


TCCR0A
out TCCR0A, R16

ldi R16, 0b_0000_0111 ; FOC0A, FOC0B, -, -, WGM02, CS02, CS01, CS00 = TCCR0B
out TCCR0B, R16

here:
in R17, TCNT0
out PORTB, R17
rjmp here

ECE-261L: Introduction to Embedded SystemsPage 7


Brightness:

const int ledPin = 9; // Pin connected to the LED

void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
TCCR1A = 0b10100001; // Configure Timer1 for Fast PWM mode
TCCR1B = 0b00001001; // Set prescaler to 1
}

void loop() {
// Increase brightness from minimum to maximum
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(ledPin, brightness); // Set the LED brightness
delay(10); // Delay for smoother transition
}

// Decrease brightness from maximum to minimum


for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(ledPin, brightness); // Set the LED brightness
delay(10); // Delay for smoother transition
}
}

ECE-261L: Introduction to Embedded SystemsPage 8

You might also like