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

Lab 4 Timer

The document outlines an experiment focused on using the AVR Timer in microcontroller systems, detailing objectives such as understanding the timer's functionality, programming with C, and interfacing with external devices. It covers various timer modes including Normal, CTC, Fast PWM, and Phase Correct PWM, along with practical applications using Arduino hardware and software. Additionally, it provides code examples for implementing LED control using timers and PWM signals.

Uploaded by

Valerian
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)
21 views8 pages

Lab 4 Timer

The document outlines an experiment focused on using the AVR Timer in microcontroller systems, detailing objectives such as understanding the timer's functionality, programming with C, and interfacing with external devices. It covers various timer modes including Normal, CTC, Fast PWM, and Phase Correct PWM, along with practical applications using Arduino hardware and software. Additionally, it provides code examples for implementing LED control using timers and PWM signals.

Uploaded by

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

EXPERIMENT 4

Using AVR Timer

I. Objectives
1. Students are able to understand Timer of AVR
2. Students are able to build a proram using C Compiler and debug it.
3. Students are able to implement the use of the data, control and status bus in AVR to
interface the timer with External Devices
II. Scope
A. Theory
Introduction
Timing is essential for the operation of microcontroller systems, either for
generating signals with precisely determined duration, or for counting external events.
For this reason, the timer subsystem is present in all microcontroller implementations,
and covers a wide range of functions including:
 Generation of precise time intervals
 Measurement of duration of external events
 Counting external events.
Most microcontrollers are provided with dedicated timers, or use the general purpose
timer to implement the following additional functions:
 Real time clock
 Generation of Pulse Width Modulated (PWM) signals
 Watchdog for detecting program runaway situations.

Although there are significant variations between different implementations of


the general-purpose timer in different microcontrollers, there are many similarities in
the principles of operation and the structure of the timer subsystem.
Figure 4.1 shows a general block diagram of the timer system, illustrating the
principles of implementation of most MCU timers.

Introduction to Microcontroller Computer Engineering Laboratory


Experiment 4 Page : 1 of 8
Figure 4.1 General Timer Subsystem

The central element of the timer subsystem is a counter, TCNT (8 or 16-bits in


length), which may be read or (sometimes) written by software. The clock for TCNT is
obtained either from the system clock, divided by a programmable prescaler, or an
external clock applied to one of the MCU pins. The software control upon the timer is
performed by means of the control register TCTL and information regarding various
events related to the timer may be read from the status register TFLG.

The following definitions are used throughout the section:


Constant Description
BOTTOM The counter reaches the BOTTOM when it becomes zero (0x00 for 8-bit
counters, or 0x0000 for 16-bit counters)
MAX The counter reaches its Maximum when it becomes 0xFF (decimal 255, for
8-bit counter) OF 0xFFFF (decimal 65535, for 16-bit counter)
TOP The counter reaches the TOP when it becomes equal to the highest value in
the count sequence. The TOP calue can be assigned to be the fixed value
MAX or the value stored in the OCRx Register

Several operating modes are possible for the timer:


1. Normal Mode. The simplest mode of operation is the normal mode, in this mode
the counting direction is always up (incrementing), and no counter clear is
peformed. The counter simply overruns when it passes its maximum 8/16 bit value
(TOP=0xFF/0xFFFF) and then restarts from the bottom. In this mode

Introduction to Microcontroller Computer Engineering Laboratory


Experiment 4 Page : 2 of 8
Timer/Counter Overflow Flag (TOV) will be set in the same clock cycle in which
the TCNT becomes zero

2. Clear Timer on Compare Match (CTC) Mode. In this operating mode, OCRx
Register is used to manipulate the counter resolution: the counter is cleared to
ZERO when the counter value TCNT matces the OCRx register. The OCRx defines
the top value for the counter, hence also its resolution. This mode allows greater
control of the compare match output frequency it also simplifies the counting of
external events.

Figure 4.2 Clear Timer on Compare Match Mode, Timing Diagram

3. Fast PWM Mode. this operating mode provide a high frequency PWM waveform
generation option. The Fast PWM modes different from the other PWM options by
their single-slope operation. The counter counts from BOTTOM to TOP, then
restarts from BOTTOM. Value of TOP could be assign in this mode

Figure 4.3 Fast PWM Mode, Timing Diagram

4. Phase Correct PWM Mode. this operating mode provide a high resolution, phase
correct PWM waveform generation. The Phase Correct PWM mode is based on
Introduction to Microcontroller Computer Engineering Laboratory
Experiment 4 Page : 3 of 8
dual-slope operation: the counter repeatedly from BOTTOM to TOP, and the from
TOP to BOTTOM. Value of TOP could be assign, in non inverting compare output
mode, the OCx bit is cleared on compare match between TCNT and OCRx while
up-counting and and OCx is set on the compare match while down-counting. In
inverting mode, the operation is inverted

Figure 4.4 Phase Correct PWM Mode, Timing Diagram

B. References
1. Barrnet, Richard. (2003). Embedded C Programming and the Atmel AVR.
1st edition. Delmar learning. ISBN 1401812066.
2. http://www.alldatasheet.com/view.jsp?Searchword=Atmega328

III. Devices (Hardware and Software)


 Personal Computer (Hardware)
 Arduino Uno (Hardware)
 Arduino IDE (Software) - https://www.arduino.cc/en/Main/Software (Link to
download the software)
 Arduino Uno Prototype Shield (Hardware)
 Components : 2 Res 220Ω, 2 LED (Hardware)

Introduction to Microcontroller Computer Engineering Laboratory


Experiment 4 Page : 4 of 8
IV. Instruction of Laboratory
A. Procedure
a. Timer & PWM with LEDs
I/O Configuration :
 PIN D5 to LEDs
 PIN D6 to LEDs
Program file :
 main.ino

Figure 4.5 Circuit for experiment a(1)

Figure 4.6 Circuit for experiment a(2)

Introduction to Microcontroller Computer Engineering Laboratory


Experiment 4 Page : 5 of 8
This Experiment demonstrate using timer in AVR and the application of PWM pin
using 2 leds where the first led will turn in and off every 2 seconds, and the other
led will always turn on but the brightness value starting from 0 to 255.

Bare Metal Code (AVR)


main.ino

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#define LED_OUT PORTD


#define LED_DIR DDRD

unsigned int i;

void setup() {
LED_DIR = 0x60;
LED_OUT = 0x00;

//TCNT(16 Bit Timer) = (1+0xFFFF)-(desire time * Fcpu/prescaler)


//Fpwm = Fcpu / prescaler * (1 + TOP)

//init Timer
TCCR0A = 0x00; //clear TCCR0A
TCCR0B = 0x00; //clear TCCR0B
TCCR1A = 0x00; //normal mode
TCCR1B = 0x00; //clear TCCR1B

\ TCCR0A |= (1 << COM0A1)|(1 << WGM01)|(1 << WGM00); \


//Fast Pwm mode, non inverting
TCCR0B |= (1 << CS02) | (1 << CS00); //prescaler

TCNT1H = 0x85;
TCNT1L = 0xEE;
TCCR1B |= (1 << CS12) | (1 << CS10); //prescaler f_cpu/1024
TIMSK1 |= (1 << TOIE1); //overflow interrupt enable

sei();
}

ISR(TIMER1_OVF_vect) {
TCNT1H = 0x85;
TCNT1L = 0xEE;
LED_OUT ^= (1<<5);
}

Introduction to Microcontroller Computer Engineering Laboratory


Experiment 4 Page : 6 of 8
void loop() {
i++;
if (i >= 255)
i=0;

OCR0A = i;
_delay_ms(10);
}

Arduino Code
main.ino

int dimmingLED = 5;
int led = 6;
int brightness = 0;
const long interval = 2000;
int ledState = LOW;
unsigned long previousMillis = 0;
void setup() {

pinMode(dimmingLED,OUTPUT);
pinMode(led,OUTPUT);
}

\ void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
}
digitalWrite(led,ledState);
interrupts();
analogWrite(dimmingLED,brightness);
brightness++;
if(brightness >= 255) brightness = 0;
delay(10);

Introduction to Microcontroller Computer Engineering Laboratory


Experiment 4 Page : 7 of 8
Introduction to Microcontroller Computer Engineering Laboratory
Experiment 4 Page : 8 of 8

You might also like