Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

LAB 2-1

USE TIMER TO GENERATE DELAY AND PULSES


OBJECTIVE:

• Understanding the operating modes of a timer:

• Understanding how to use a timer for creating delays and generating pulses.

REFERENCES:

• Lab manual Chapter 4, 5


• Atmel-2505-Setup-and-Use-of-AVR-Timers_ApplicationNote_AVR130.pdf

EXPERIMENT 1:

a) Write a delay subroutine of 1 ms using Timer 0. Use this subroutine to generate a 500 Hz
pulse on pin PA0.
b) Simulate and modify the program to achieve accurate pulse generation.
c) Connect pin PA0 to an oscilloscope to verify.

EXPERIMENT 2:

a) Write a program to generate a 64 us square wave using Timer 0 in Normal mode. Use pin
OC0 as the output.
b) Write a program to generate a 64 us square wave using Timer 1 in CTC mode. Use pin
OC0 as the output.
c) Connect pin OC0 to an oscilloscope and observe.

EXPERIMENT 3:

a) Given the program for generating two PWM pulses on OC0A and OC0B, connect pins
OC0A and OC0B to two oscilloscope channels, measure and record the waveform, and
explain the obtained waveform.

.org 00

call initTimer0

https://doe.dee.hcmut.edu.vn/
LAB 2-1
USE TIMER TO GENERATE DELAY AND PULSES
start:

rjmp start

initTimer0:

// Set OC0A (PB3) and OC0B (PB4) pins as outputs

ldi r16, (1 << PB3) | (1 << PB4);

out DDRB,r16

ldi r16, (1 << COM0B1)|(1 << COM0A1) | (1 << WGM00)|(1 << WGM01)

out TCCR0A,r16 // setup TCCR0A

ldi r16, (1 << CS01)

out TCCR0B,r16 // setup TCCR0B

ldi r16, 100

out OCR0A,r16 //OCRA = 100

ldi r16, 75

out OCR0B,r16 //OCRB = 75

ret

EXPERIMENT 4:

a) Modify the program for different combinations of TCCR0A and TCCR0B registers as
described in the table:
TCCR0A TCCR0B

7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0

1 COM0A1 COM0A0 COM0B1 COM0B0 WGM01 WGM00 FOC0A FOC0B WGM02 CS02 CS01 CS00

2 1 0 1 0 1 1 0 0 1 0

3 1 0 1 0 1 1 1 0 1 0

4 1 0 1 0 0 1 0 0 1 0

https://doe.dee.hcmut.edu.vn/
LAB 2-1
USE TIMER TO GENERATE DELAY AND PULSES
b) Connect pins OC0A and OC0B to two oscilloscope channels, measure and record the
waveforms, and explain the obtained results.

EXPERIMENT 5:

a) Write a program to generate a 1 kHz square wave with a duty cycle of 25% on pin OC0B.
b) Connect to an oscilloscope and measure the output waveform.
c) Connect OC0B to the R channel of an RGB LED. Write a program to increase the duty
cycle on OC0B from 0% to 100% and then decrease it back to 0% over 10 ms, with a 1%
increment.

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:
Class group: Subject:

EXPERIMENT 1:

1. Answer the following questions:


a. What is the maximum delay achievable using Timer 0 with an 8 MHz frequency?
Explain the calculation.
b. What is the maximum delay achievable using Timer 1 with an 8 MHz frequency?
Explain the calculation.
c. Explain how to calculate the prescaler values and the values loaded into Timer0
registers for this experiment.
d. Source code for the program with comments.
........................................................................................................................

EXPERIMENT 2:

1. Answer the following questions:


a. In Normal mode, when is the TOVx bit set to 1?
b. In CTC mode, when is the OCFx bit set to 1?
c. Provide the register configurations for Timer 0 for both cases.
d. Source code for the programs in both cases.
........................................................................................................................

EXPERIMENT 3:

1. Answer the following questions:


a. Describe the waveform on the oscilloscope (capture and insert it).
b. Explain the reasons for the observed waveform (frequency, duty cycle, phase).

EXPERIMENT 4:

1. Answer the following questions:


a. Identify the working modes of Timer 0 corresponding to the values in the table.

https://doe.dee.hcmut.edu.vn/
LAB REPORT
Group:
Class group: Subject:

2. Capture images of the waveforms corresponding to the different working modes and
explain the results.

EXPERIMENT 5:

1. Answer the following questions:


a. In which mode is Timer 0 operating?
b. What values are loaded into Timer 0 registers, and why?
2. Present the source code with comments.
........................................................................................................................

https://doe.dee.hcmut.edu.vn/

You might also like