Lab 4 Timer
Lab 4 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.
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.
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
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
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
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
unsigned int i;
void setup() {
LED_DIR = 0x60;
LED_OUT = 0x00;
//init Timer
TCCR0A = 0x00; //clear TCCR0A
TCCR0B = 0x00; //clear TCCR0B
TCCR1A = 0x00; //normal mode
TCCR1B = 0x00; //clear TCCR1B
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);
}
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);