AVR Timers TIMER0. Based on:

Similar documents
TIMSK=0b ; /* enables the T/C0 overflow interrupt in the T/C interrupt mask register for */

Software debouncing of buttons

Topic 11: Timer ISMAIL ARIFFIN FKE UTM SKUDAI JOHOR

INTERRUPTS in microprocessor systems

Chapter 6 PROGRAMMING THE TIMERS

8-bit Microcontroller. Application Note. AVR134: Real-Time Clock (RTC) using the Asynchronous Timer. Features. Theory of Operation.

School of Electrical, Computer and Telecommunications Engineering University of Wollongong Australia

UNIVERSITY OF CONNECTICUT. ECE 3411 Microprocessor Application Lab: Fall Quiz IV

Unit 14 Timers and Counters 14.1

Laboratory 4 Usage of timers

Newbie s Guide to AVR Timers

Chapter 2. Overview of Architecture and Microcontroller-Resources

EE 308: Microcontrollers

Interrupts Arduino, AVR, and deep dark programming secrets. What is an Interrupt?

ADC: Analog to Digital Conversion

AVR134: Real Time Clock (RTC) Using the Asynchronous Timer. Features. Introduction. AVR 8-bit Microcontrollers APPLICATION NOTE

Supplementary Materials: Fabrication of a Lab on Chip Device Using Material Extrusion (3D Printing) and Demonstration via Malaria Ab ELISA

INTERRUPT, TIMER/COUNTER. KONKUK UNIV. VLSI Design Lab. LSI Design Lab

Newbie's Guide to AVR Timers (C) Dean Camera, 2007

Embedded Systems and Software

8-bit Microcontroller. Application Note. AVR134: Real-Time Clock (RTC) using the Asynchronous Timer. Features. Theory of Operation.

Interrupts (II) Lecturer: Sri Parameswaran Notes by: Annie Guo

Distributed Real-Time Control Systems. Chapter 10 Real-Time Digital Control

UNIVERSITY OF CONNECTICUT. ECE 3411 Microprocessor Application Lab: Fall Quiz V

Fundamental concept in computation Interrupt execution of a program to handle an event

Microprocessors & Interfacing

COMP2121: Microprocessors and Interfacing

Marten van Dijk, Syed Kamran Haider

Distributed Real- Time Control Systems. Lecture 7 Real- Time Control

Physics 120B: Lecture 11. Timers and Scheduled Interrupts

Newbie s Guide to AVR Interrupts

Chapter 6 PROGRAMMING THE TIMERS

Processor and compiler dependent

Topic 11: Interrupts ISMAIL ARIFFIN FKE UTM SKUDAI JOHOR

8-bit Microcontroller with 8K Bytes Programmable Flash AT90C8534. Preliminary

UNIVERSITY OF CONNECTICUT. ECE 3411 Microprocessor Application Lab: Fall Lab Test III

8-bit Microcontroller with 2K Bytes of In-System Programmable Flash AT90S2323 AT90LS2323 AT90S2343 AT90S/LS2323. Features.

Physics 124: Lecture 12. Timer Basics. Timers and Scheduled Interrupts

8-bit Microcontroller with 2K Bytes of In-System Programmable Flash. ATtiny22 ATtiny22L. Preliminary. Features. Description

8-bit Microcontroller with 2K Bytes of In-System Programmable Flash AT90S2313

Kod. #define white PA5 #define green PA6 #define red PA7

Unit 13 Timers and Counters

Atmel AVR Timers and Interrupts

8-bit Microcontroller with 1K Byte of In-System Programmable Flash AT90S1200

AGH University of Science and Technology Cracow Department of Electronics

ATmega Interrupts. Reading. The AVR Microcontroller and Embedded Systems using Assembly and C) by Muhammad Ali Mazidi, Sarmad Naimi, and Sepehr Naimi

- Open-source and open-hardware modular robotic platform specially created for educational purposes.

Timers and Interrupts. Mark Neil - Microprocessor Course

Interrupts and timers

12.1. Unit 12. Exceptions & Interrupts

Introduction to Embedded Systems

X X X. VGA - Standard:

Microprocessor Fundamentals. Topic 7 Timing: the Timer/Counter

UNIVERSITY OF CONNECTICUT. ECE 3411 Microprocessor Application Lab: Fall Lab Test III

UNIVERSITY OF CONNECTICUT. ECE 3411 Microprocessor Application Lab: Fall Quiz III

8-bit Microcontroller with 1K Bytes Flash. ATtiny10 ATtiny11 ATtiny12. Preliminary. Features. Pin Configuration

8-bit Microcontroller with 1K Byte Flash. ATtiny15L

Introduction to Embedded Systems

Microprocessors and Microcontrollers (EE-231)

LPC4088 Timer Interrupts CM0506 Small Embedded Systems

Chapter 09. Programming in Assembly

Lecture #13 Interrupts Embedded System Engineering Philip Koopman Monday, 29-Feb-2016

[TUT] ATTiny817, 12-bit timer, TCD, servos, 1 & 2 Ramp PWM, HXT900. There is a One Ramp mode example and a Two Ramp mode example.

Interrupts and Time. Real-Time Systems, Lecture 5. Martina Maggio 28 January Lund University, Department of Automatic Control

8-bit Microcontroller with 8K Bytes In-System Programmable Flash AT90S8515

Interrupts and Time. Interrupts. Content. Real-Time Systems, Lecture 5. External Communication. Interrupts. Interrupts

UNIVERSITY OF MANITOBA Midterm

8-bit Microcontroller with 4K/8K bytes In-System Programmable Flash AT90S4414 AT90S8515. Features. Pin Configurations

Interrupts & Interrupt Service Routines (ISRs)

UNIVERSITY OF CONNECTICUT. ECE 3411 Microprocessor Application Lab: Fall Quiz II

Module 3. Embedded Systems I/O. Version 2 EE IIT, Kharagpur 1

8-bit Microcontroller with 4K Bytes of In-System Programmable Flash AT90S4433 AT90LS4433. Features. Not Recommend for New Designs. Use ATmega8.

Interrupts & Interrupt Service Routines (ISRs)

Embedded Systems Lab 2 - Introduction to interrupts

AT90S Bit Microcontroller with 1K bytes Downloadable Flash AT90S1200. Features. Description. Pin Configuration

Källkod. int H1 = 0; int H2 = 0; int M1 = 0; int M2 = 0; int S1 = 0; int S2 = 0; int SNM1 = 0; int SNM2 = 0;

onetesla Interrupter Firmware Guide General Overview

8-bit Microcontroller with 2K Bytes of Flash. ATtiny28L ATtiny28V

CSCE374 Robotics Fall 2013 Notes on the irobot Create

HC12 Built-In Hardware

Embedded Systems and Software

IAS0430 MICROPROCESSOR SYSTEMS

Lesson-3: Counters and Timers

AVR Simulation with the ATMEL AVR Studio 4 Updated: 8/3/2005

Microcontroller basics

C Language Programming, Interrupts and Timer Hardware

AN HONORS UNIVERSITY IN MARYLAND UMBC. AvrX. Yousef Ebrahimi Professor Ryan Robucci

Use a semaphore to avoid the enqueue and dequeue functions from accessing and modifying count variable at the same time.

Mark Redekopp, All rights reserved. EE 357 Unit 10b. Interrupts Timers

Interrupts, timers and counters

L A M P I R A N UNIVERSITAS KRISTEN MARANTHA

original M68K requests/priorities Interrupts to execute important code interrupts (n will always be I) I-bits bits in SR accomplish this

There are a number of ways of doing this, and we will examine two of them. Fig1. Circuit 3a Flash two LEDs.

EE Embedded Systems Design. Lessons Exceptions - Resets and Interrupts

Introduction to the MC9S12 Hardware Subsystems

Lab 9: On-Board Time Generation and Interrupts

C Language Programming, Interrupts and Timer Hardware

EE 308 Spring A software delay

Analog Output with a Digital to Analog Converter

Using Timers of Microchip PIC18F Microcontrollers

Transcription:

AVR Timers TIMER0 Based on: http://maxembedded.wordpress.com/2011/06/24/avr-timers-timer0-2/ The basic concepts of AVR Timers. Let me summarize it: We have seen how timers are made up of registers, whose value automatically increases/decreases. Thus, the terms timer/counter are used interchangeably. In AVR, there are three types of timers TIMER0, TIMER1 and TIMER2. Of these, TIMER1 is a 16-bit timer whereas others are 8-bit timers. We have seen how prescalers are used to trade duration with resolution. We have also discussed how to choose an appropriate value of a prescaler. And then, to finish off, we learnt about interrupts. So, I will move towards its implementation directly. I have assumed that you have understood the concepts discussed above. In this tutorial, we will learn to use TIMER0. Since timer is a peripheral, it can be activated by setting some bits in some registers. Instead of discussing all the registers at once, we will be discussing them as and when necessary. Problem Statement Let s define a problem statement for us. The simplest one being the LED flasher. Let s say, we need to flash an LED every 6 ms and we are have a CPU clock frequency of 32 khz. Well, I know that an LED flashing at every 6 ms will be always visible as on by our eye, but I could not find any simpler example which does not include prescalers. Take this as a demonstration. Methodology Now, as per the following formula, with a clock frequency of 32kHz and 8-bit counter, the maximum delay possible is of 8ms. This is quite low (for us, but not for the MCU). Hence for a delay of 6 ms, we need a timer count of 191. This can easily be achieved with an 8-bit counter (MAX = 255). Thus, what we need to do is quite simple. We need to keep a track of the counter value. As soon as it reaches 191, we toggle the LED value and reset the counter. For this, we need the help of the following registers. TCNT0 Register The Timer/Counter Register TCNT0 is as follows: TCNT0 Register This is where the uint 8-bit counter of the timer resides. The value of the counter is stored here and increases/decreases automatically. Data can be both read/written from this register.

Now we know where the counter value lies. But this register won t be activated unless we activate the timer! Thus we need to set the timer up. How? Read on TCCR0 Register The Timer/Counter Control Register TCCR0 is as follows: TCCR0 Register Right now, we will concentrate on the highlighted bits. The other bits will be discussed as and when necessary. By selecting these three Clock Select Bits, CS02:00, we set the timer up by choosing proper prescaler. The possible combinations are shown below. Clock Select Bit Description For this problem statement, we choose No Prescaling. Ignore the bits highlighted in grey. We will be using it later in this tutorial. Thus, we initialize the counter as: TCCR0 = (1 << CS00); Please note that if you do not initialize this register, all the bits will remain as zero and the timer/counter will remain stopped. Code #include <avr/io.h> #define F_CPU 32000ul void timer0_init() // set up timer with no prescaling TCCR0 = (1 << CS00); // initialize counter TCNT0 = 0; int main(void) // check if the timer count reaches 191 if (TCNT0 >= 191)

PORTC ^= (1 << 0); // toggles the led I guess the code is pretty simple and straightforward. It doesn t need any explanation. Or maybe one thing needs explanation. In the if statement, I have used if (TCNT0 >= 191) instead of if (TCNT0 == 191) This is because sometimes due to missed compares or unexpected increment, this condition may never be true. Thus to remain on the safer side, we use >= instead of ==. Problem Statement Redefined Now let s change the above problem statement to the following. We need to flash an LED every 8 ms and we have an XTAL of 16 MHz. Well, 8 ms is still low, but it s good enough for the following illustration. Methodology Using Prescalers Now since the CPU clock frequency is 16 MHz, the maximum time delay that it can measure is 16 µs! But 8 ms (which is quite a small duration for us) is way much larger. So what do we do? Yes, you guessed right. We use a prescaler in order to trade duration with resolution. Now the following table summarizes the results of using different prescalers 1 (no prescaler), 8, 64, 256 and 1024. Presclaer Clock Frequency Clock Time Timer Count Period 1 16MHz 0.0625us 127999 8 2MHz 0.5us 15999 64 250kHz 4us 1999 256 62.5KHz 16us 499 1024 15.625kHz 64us 124 Required Delay=8ms, F_CPU=16MHz From the values of the counter, we can easily rule out the top three prescalers as they are above the maximum limit of an 8-bit counter (which is 255). Thus we use a prescaler of 1024. Now refer to the descriptions of clock select bits as shown in the TCCR0 register. Have a look at the selection highlighted in grey. This implements a prescaler of 1024. The rest remains the same. Moving to the coding part, we simply change the initialize function and the compare value. The rest remains the same. Code #include <avr/io.h> #define F_CPU 16000000ul void timer0_init() // set up timer with prescaler = 1024 TCCR0 = (1 << CS02) (1 << CS00); // initialize counter TCNT0 = 0;

int main(void) // check if the timer count reaches 124 if (TCNT0 >= 124) PORTC ^= (1 << 0); // toggles the led 8ms delay Problem Statement Redefined Again! Now let s change the problem statement to something you can actually see! Let s flash an LED every 50 ms (you can surely see the LED flashing this time). We have an XTAL of 16MHz. Methodology Using Interrupts So now, we have to flash the LED every 50ms. With CPU frequency 16MHz, even a maximum delay of 16.384 ms can be achieved using a 1024 prescaler. So what do we do now? Well, we use interrupts. The concept here is that the hardware generates an interrupt every time the timer overflows. Since the required delay is greater than the maximum possible delay, obviously the timer will overflow. And whenever the timer overflows, an interrupt is fired. Now the question is how many times should the interrupt be fired? For this, let s do some calculation. Let s choose a prescaler, say 256. Thus, as per the calculations, it should take 4.096 ms for the timer to overflow. Now as soon as the timer overflows, an interrupt is fired and an Interrupt Service Routine (ISR) is executed. Now, 50 ms 4.096 ms = 12.207 Thus, in simple terms, by the time the timer has overflown 12 times, 49.152 ms would have passed. After that, when the timer undergoes 13th iteration, it would achieve a delay of 50ms. Thus, in the 13th iteration, we need a delay of 50 49.152 = 0.848ms. At a frequency of 62.5kHz (prescaler = 256), each tick takes 0.016ms. Thus to achieve a delay of 0.848ms, it would require 53 ticks. Thus, in the 13th iteration, we only allow the timer to count up to 53, and then reset it. All this can be achieved in the ISR as follows: // global variable to count the number of overflows volatile uint8_t tot_overflow; // TIMER0 overflow interrupt service routine // called whenever TCNT0 overflows ISR(TIMER0_OVF_vect) // keep a track of number of overflows tot_overflow++; int main(void)

// check if no. of overflows = 12 if (tot_overflow >= 12) // NOTE: '>=' is used // check if the timer count reaches 53 if (TCNT0 >= 53) PORTC ^= (1 << 0); // toggles the led tot_overflow = 0; // reset overflow counter Please note that the code is not yet ready. Not until you learn how to enable the interrupt feature. For this, you should be aware of the following registers. TIMSK Register The Timer/Counter Interrupt Mask TIMSK Register is as follows. It is a common register for all the three timers. For TIMER0, bits 1 and 0 are allotted. Right now, we are interested in the 0th bit TOIE0. Setting this bit to 1 enables the TIMER0 overflow interrupt. TIMSK Register TIFR Register The Timer/Counter Interrupt Flag Register- TIFR is as follows. Even though we are not using it in our code, you should be aware of it. TIFR Register This is also a register shared by all the timers. Even here, bits 1 and 0 are allotted for TIMER0. At present we are interested in the 0th bit TOV0 bit. This bit is set (one) whenever TIMER0 overflows. This bit is reset (zero) whenever the Interrupt Service Routine (ISR) is executed. If there is no ISR to execute, we can clear it manually by writing one to it. In this example, since we are using ISR, we need not care about this bit (thus this register as a whole). Enabling Global Interrupts In the AVRs, there s only one single bit which handles all the interrupts. Thus, to enable it, we need to enable the global interrupts. This is done by calling a function named sei(). Don t worry much about it, we simply need to call it once, that s all.

Final Code /* * Timer0_irq_1.c */ #define F_CPU 16000000ul #include <avr/io.h> #include <avr/interrupt.h> // global variable to count the number of overflows volatile uint8_t tot_overflow;, interrupt and variable void timer0_init() // set up timer with prescaler = 256 TCCR0 = (1 << CS02); // initialize counter TCNT0 = 0; // enable overflow interrupt TIMSK = (1 << TOIE0); // enable global interrupts sei(); // initialize overflow counter variable tot_overflow = 0; // TIMER0 overflow interrupt service routine // called whenever TCNT0 overflows ISR(TIMER0_OVF_vect) // keep a track of number of overflows tot_overflow++; int main(void) // check if no. of overflows = 12 if (tot_overflow >= 12) // NOTE: '>=' is used // check if the timer count reaches 53 if (TCNT0 >= 53) PORTC ^= (1 << 0); // toggles the led 50ms delay tot_overflow = 0; // reset overflow counter