Sie sind auf Seite 1von 4

28/07/13

Latest Technology Latest Gadgets AVR Projects Sensors

AVR Timers - An introduction | zembedded


C pit falls About Us

HOME

CONTROLLERS

EMBEDDED C

EMBED TOOLS

EMBED INTERFACE

RTOS

LINUX

Search

Home 2012 December 01 AVR Timers An introduction


Share

Recent Post

AVR Timers An introduction


ON: DECEMBER 1, 2012, BY: ADMIN, IN: AVR MICROCONTROLLERS, 3 COMMENTS

Whats a Compiler and Compilation?


What's a Compiler and Compilation? A compiler i... more

Hi Friends, today we are going to start a very important series of five tutorials based on AVR Timers. In first tutorial we are going to see an introduction to AVR timers. Next Two will be specifically for 8-bit timer and 16-bit timers and rest two practical use of timers. So here we start . What is Timer? Timers are an important concept in the field of electronics. All the microcontrollers work at some predefined clock frequency, they all have a provision to set up timers. Timers can do counting, monitoring external events, keeping time, frequency generation, generating accurately timed pulses, increment, decrement, Pulse width modulation (PWM) and for registering the timestamp of external events(Input capture mode). We are going to have a series of tutorial on Timers. In first tutorial it is going to be an introduction to AVR Timers, in second tutorial we would be having in depth discussion about AVR Timer Registers. And remaining two tutorials we would practically trying to understand how to use timers 8bit and 16bit in different modes. So here we start..
more

AVR LCD interfacing : LCD part I


Hi Guys this is my first tutorial on zemebdded sit... more

Arduino MEGA and LEONARDO Pin-Out Diagrams!


Hi Friends please check out these inspiring Ardu...

ARM : Example Setting PLL for LPC2148


Hi friends this is Partha, this is my second tutor... more

Cortex-M3: Operation Modes & Access Levels


Thanks for the response you guys have show n in ear... more

CORTEX -M3 : Registers in depth


Thanks for the overw helm response you show in our ... more

Samsung Galaxy IV : 5 screen, 13MP camera


Today at its Galaxy S event in New

In principle, a timer is a simple counter. The input clock of microcontroller and operation of the timer is independent of the program execution. The deterministic clock makes it possible to measure time by counting the elapsed cycles and take the input frequency of the timer into account. All the Atmel microcontrollers have Timers as an inbuilt peripheral. In this article we are going to use the target controller ATmega16. ATmega16 comes with two 8 bit and one 16 bit timer. This means that there are 3 sets of timers, each with the ability to count at different rates. The two 8-bit counters can count to 255 whilst the 16 bit counter can count to 65,536. We are also considering that we have LEDs connected to the PORTB of the Atmega16. Timers can run asynchronous to the main AVR core it means timers are totally independent of CPU. A timer is usually specified by the maximum value to which it can count called MAX beyond which it overflows and resets to zero is called BOTTOM. The speed of counting can be controlled by varying the speed of clock input to it. In Atmega16 we have three different kinds of timers as follows Timer 0 8 bit counter Timer 1 16 bit counter Timer 2 8 bit counter Most of us know this following formula Can count up to 255 Can count up to 65,536 Can count up to 255 Categories

York, Samsung h... more

An intro to Cortex M3 by NXP


In this series of tutorials w e take a look into th... more

In depth LED Blink with NXP LPC17xx


This is going to be a very interesting tutorial fo... more

Pointers In C
Hi friend this is going to be a series of tutorial... more

8051 Microcontroller (3) Android Tutorials (3) ARM Microcontrollers (16) Let us first have a look at the basics of how a timer works. There are basically two types of timers, 8 bit(counter 0) and 16 bit(counter 1) timers. The only major difference between them is that they have different maximum values up to which they can count. As you may already know, an 8 bit binary number can have a maximum value of 255 and a 16 bit timer can go up to 65535. The 16 bit register stores the value of the count using two 8 bit registers. So the time that our timer takes for one count is 1/1000000 sec, i.e. 1.000001sec. The required time period for the flashing LED is 1/20 i.e. .05 sec. Therefore, in order for the frequency to be 20Hz, the LED should be toggled every 0.025 sec. Let us find out the number of timer counts needed to reach 0.025sec. AVR Microcontrollers (14) AVR Projects (3) C pit falls (1) Embed Interface (6) Embed Tools (7)

www.zembedded.com/avr-timers-an-introduction/

1/4

28/07/13

AVR Timers - An introduction | zembedded


Embedded C (16) Latest Gadgets (12) Latest Technology (46) Linux (3) PIC microcontrollers (2) RTOS (3) Scholars Tips (3)

Timer count=0.025/0.000001=25000. But since the counting starts from 0 and not from 1 , we need to go only up to 24999. As this value cannot be accommodated inside an 8 bit timer we will have to use counter 1 which is of 16 bits. Relevant registers for Timer0 and Timer 2

Timer 0 Timer 2 TCCR0A TCCR2A TCCR0B TCCR2B TCNT0 TCNT2 OCR0A OCR2A OCR0B OCR2B TIMSK0 TIMSK2 TIFR0 TIFR2 Relevant registers for Timer1 Timer 1 TCNT1 TCCR1A TCCR1B OCR1A OCR1B TIMSK TIFR0 Timer Modes: Description 16-bit counter register

Description Timer/Counter Control Register A Timer/Counter Control Register B Timer/Counter Register Output Compare Register A Output Compare Register B Timer/Counter Interrupt Mask Register Timer/Counter Interrupt Flag Register

Sensors (1)

Recent Posts Whats a Compiler and Compilation? JUNE 2, 2013 AVR LCD interfacing : LCD part I MAY 7, 2013 Arduino MEGA and LEONARDO Pin-Out Diagrams!
APRIL 10, 2013

Mode of operation and other settings Mode of operation, prescaler and other settings 16-bit Compare Register A 16 bit Compare Register B Interrupt Mask Register Timer/Counter Interrupt Flag Register

CORTEX -M3 : Registers in depth MARCH 20, 2013 Samsung Galaxy IV : 5 screen, 13MP camera
MARCH 15, 2013

Pointers In C FEBRUARY 28, 2013 COMPLEX STRUCTURE DEFINITIONS


FEBRUARY 21, 2013

Timers are usually used in one of the following modes: Normal CTC Fast PWM Phase correct PWM (add image hrere Image number two) As we discussed earlier there are three different timers available in Atmega16 and all of them works in almost same way. As we know a timer is an 8 or 16 bit register that keeps on increasing its value, so one of the basic condition is when timer register overflows, means it counts reaches to its max value (255 for 8bit and 65535 for 16bit timers) and gets reset back to 0. At this situation timer can issue an interrupt. What is Prescaler? A technique to derive a lower frequency from F_CPU, without effecting actual F_CPU to run timer is called prescaler. In other words it is a mechanism for generating clock for timer by F_CPU clock. Atmega series of microcontrollers are available in several frequencies such as 1MHz, 8MHz, and 12MHz etc. Why we need prescaler? Assume we supply a 2MHz signal to timer and a 16 bit timer which can count max upto 65535. Using the formula, we discussed that we can get a maximum delay of 32ms. Just imagine, what if we need delay greater than 32ms ? One of the easiest thing we can do is to use the timers prescaler. Prescaler allows us to divide up the incoming clock signal by power of 2. It reduces the resolution which means that the accuracy has decreased but giving us the longer timer range. Prescalar can be set to produce the following clocks: No Clock Timer Stop No prescaling Clock frequency = F_CPU F_CPU/8 F_CPU/64 F_CPU/256 F_CPU/1024 Timer in Compare Mode (CTC Mode) There is another mode of operation for AVR timers are called CTC (Clear on Timer Capture mode ). This mode is called Clear Timer on Compare Match, or CTC. Instead of counting until an overflow occurs, the timer compares its count to a value that was previously stored in a register. When the count matches that value, the timer can either set a flag or trigger an interrupt, just like the overflow case. Here we finish with introduction of AVR Timers. Please click here AVR Timers: In depth timer registers 8-bit to continue with this series of tutorials One sincere request to all the readers, please comment down and correct us, this site is trying to help people to understand embedded technology, this is only possible with your comments and corrections. Please subscribe to the newsletter to get latest project/article updates right in your email inbox. Take few moments and like zembedded facebook page, follow on tweeter, thanks!

Basic Structures in C FEBRUARY 18, 2013

Subscribe to Blog via Email Enter your email address to subscribe to this blog and receive notifications of new posts by email. Join 41 other subscribers Email Address Subscribe

Related posts:

www.zembedded.com/avr-timers-an-introduction/

2/4

28/07/13

AVR Timers - An introduction | zembedded

What's a Compiler and Compilation?

Atmel Studio 6: Bringing Convenience of Apps Store to Embedded Design

AVR Timers - In depth Timer Registers 8-bit

AVR: Interrupts Part I

Share this post:

Tw eet

About author
This article was written by admin Admin has over twenty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles. Working presently as Development Manager in India. A firm Believer in Knowledge grows when it shared.

Related Posts
AVR LCD interfacing : LCD part I - May 7, 2013 Arduino MEGA and LEONARDO Pin-Out Diagrams! - April 10, 2013 LED Backlight Technology - January 7, 2013 AVR Programming In Depth - January 6, 2013 AVR Register Manipulation (Accessing I/O Memory) - December 28, 2012 AVR: Interrupts Part I - December 25, 2012 AVR Registers explained - December 24, 2012 AVR Fuse Bits - December 24, 2012 AVR Timers In depth 16-bit AVR Timer Register - December 2, 2012 AVR Timers In depth Timer Registers 8-bit - December 1, 2012

Comments

Comments (3)

ATmega16, ATmega32, ATmega8, Atmel AVR, AVR, AVR Studio6, AVR Timers, AVR tutorials | zembedded says - POSTED: JANUARY 2, 2013
[...] to understand the basics of AVR timers. If you are strting straight here, then i request you to click here and read the first [...] Reply

amir maharjan says - POSTED: APRIL 27, 2013


where are the tutorials regarding the use of timers ? l Reply

admin says - POSTED: APRIL 27, 2013


Thanks Amir for practical examples we have designed our boards which is taking some time. But as we realized it is going to take more time, next week on words we are going to release STK600 board based example programjust wait for one or two daysthanks! If still you have any queries please mail to admin@zembedded.comthanks! Reply

Leave a Reply
Enter your comment here...

RECENT POST
Whats a Compiler and Compilation? June 2, 2013 AVR LCD interfacing : LCD part I May 7, 2013

RECENT COMMENTS
jalaja on In depth LED Blink with NXP LPC17xx jalaja on Pointers In C

DISCLAIMERS AND NOTICES www.zembedded.com do not take any responsibility to the contents added in this site. we just search and collect data from popular article directories and use

ZEMBEDDED

www.zembedded.com/avr-timers-an-introduction/

3/4

28/07/13
Arduino MEGA and LEONARDO PinOut Diagrams! April 10, 2013 ARM : Example Setting PLL for LPC2148 April 4, 2013 Cortex-M3: Operation Modes & Access Levels March 22, 2013 CORTEX -M3 : Registers in depth March 20, 2013 Samsung Galaxy IV : 5 screen, 13MP camera March 15, 2013 An intro to Cortex M3 by NXP March 9, 2013 Development Environment

AVR Timers - An introduction | zembedded


jalaja on Setting-up ARM Cortex-M3

jal on An intro to Cortex M3 by NXP jalaja on Android virtual device Emulator jalaja on IAR IDE with ECLIPSE

search engine to collect the best information for you. We really strict about copyright but however, if you feel that any of these contents are violating the copyright rules, please let us know. we will exclude it as soon as possible. Please contact us with your exact grievance/problem with respect to those third party links. We assure you that appropriate action will be taken immediately. Thanks Email: admin@zembedded.com

Zembedded
Curtir 777 pessoas curtiram Zembedded.

P lug-in social do F acebook

- Facebook Members WordPress Plugin

Copyrightzembedded.com

Back to top

www.zembedded.com/avr-timers-an-introduction/

4/4

Das könnte Ihnen auch gefallen