Atmega168 Timer Interrupts - Protostack

2y ago
8 Views
3 Downloads
788.12 KB
11 Pages
Last View : 10d ago
Last Download : 3m ago
Upload by : Julia Hutchens
Transcription

Atmega168 Timer interrupts - imer-interrupts-on-an.1800 335 330Shopping Cart: EmptyLogin or Create me » Blog » Tutorials » Atmega168 Timer ontrollersAtmega168 Timer interruptsRead comments Leave a comment 24-Sep-2010 5:04amThis tutorial will teach you about the 8 and 16 bit timers on an ATmega168microcontroller. Because the ATmega168 is very similar to the ATmega48,ATmega88 and ATmega328, the examples should also work on these. For otherAVR microcontrollers the general principles will apply but the specifics may vary.Integrated CircuitsPassive ComponentsSemiconductorsLED/LCDButtons andSwitchesCablesSensorsAccessoriesKits & ModulesToolsOtherFeatured.What are the 8 and 16 bit timers?Consider a wrist watch. At regular intervals it ticks and the hand moves to the nextnumber. At any point in time you can read the accumulated count (the time) andonce the time reaches a certain threshold, an event occurs (the alarm rings).Timers on AVR microcontrollers are a little like this.200mm Female to FemaleJumpers - Ribbon of 40wires 8.30Information1 of 11The ATmega168 has two 8-bit and one 16-bit timers. This means that there are 3sets of counters, each with the ability to count at different rates. The two 8-bitcounters can count to 255 whilst the 16 bit counter can count to 65,536.When the counters reach certain thresholds we can trigger interrupts, which causeInterrupt Service Routines (ISRs) to be executed. Timers are very handy becausethey allow operations to run automatically, independently of the main processingthread.8 bit timer registers2/8/15, 1:32 PM

Atmega168 Timer interrupts - ProtostackAbout UsOnline ReviewsShipping & ReturnsContact errupts-on-an.The ATmega168 has 2, 8-bit timers: Counter0 and Counter2. Each of these timersare controlled by the following imer/Counter Control Register ATCCR0BTCCR2BTimer/Counter Control Register BTCNT0TCNT2Timer/Counter RegisterOCR0AOCR2AOutput Compare Register AOCR0BOCR2BOutput Compare Register BTIMSK0TIMSK2Timer/Counter Interrupt Mask RegisterTIFR0TIFR2Timer/Counter Interrupt Flag RegisterWe’ll focus on Counter0 and the most commonly used registers.Each counter has 2 thresholds. for Counter0 these are stored in registers OCR0Aand OCR0B. When these threshold are reached “something” happens. This“something” is defined in TCCR0A, TCCR0B and TIMSK0. TCCR0B also allowsyou to set the rate at which the timer is updated.TCCR0A and TCCR0A are shown 0WGM02, WGM01 and WGM00 control the counter’s mode as shown in the l1001PWM, Phase Correct2010Clear Timer on Compare (CTC)3011Fast PWM4100Reserved5101PWM, Phase Correct6110Reserved7111Fast PWMWe’ll focus on the normal and CTC modes. A detailed discussion of timer modescan be found in section 14.7 of the ATmega168 datasheet.COM0A1, COM0A0, COM0B1 and COM0B0 control the behavior of the OC0A2 of 112/8/15, 1:32 PM

Atmega168 Timer interrupts - imer-interrupts-on-an.(PD6) and OC0B (PD5) pins. These pins can be controlled from the OutputCompare Registers (OCR0A/OCR0B). The settings depend on the modes beingused and is outside the scope of this tutorial. Section 14.9.1 of the ATmega168datasheet describes these bits in detail.An ATmega168 with default fuses runs at 8MHz with the system clock prescalerenabled. This means that the timer can be updated, up to 8,000,000 times persecond. By setting CS02, CS01 and CS00 we can slow down this rate, as shownhere.CS02CS01CS00Description000No clock source (Timer/Counter stopped)001Clock(No prescaling)010Clock/8 (From prescaler)011Clock/64 (From prescaler)100Clock/256 (From prescaler)101Clock/1024 (From prescaler)110External clock source on T0 pin. Clock on fallingedge.111External clock source on T0 pin. Clock on risingedge.TIMSK0 controls which interrupts are enabled. Each counter has 3 interrupts, onefor each Output Compare Register (threshold) and one for overflow. The full list ofavailable interrupts can be found in the AVR libc interrupt.h InterruptsDocumentation under the section labelled “Choosing the vector: Interrupt OIE0Read/WriteRRRRRR/WR/WR/WInitialValue000000008 bit timer exampleIn this example we run run a sweep of the 8 red LEDs on the main routine. We willthen blink the green LED on/off 4 times every second. The circuit diagram andsource code is shown below.3 of 112/8/15, 1:32 PM

Atmega168 Timer interrupts - imer-interrupts-on-an.1. #include avr/io.h 2. #include util/delay.h 3. #include avr/interrupt.h 4.5. #define green led on() PORTC BV(0)6. #define green led off() PORTC & BV(0)7. #define green led is on() bit is set(PORTC,0)8.9. int main (void)10. {11.DDRB 0b11111111;12.DDRC 0b01111111;// All outputs// All outputs (Although wewill just use PC0 )13.14.TIMSK0 BV(OCIE0A); // Enable InterruptTimerCounter0 Compare Match A (SIG OUTPUT COMPARE0A)15.16.TCCR0A BV(WGM01); // Mode CTCTCCR0B BV(CS02) BV(CS00);// Clock/1024,0.001024 seconds per tick17.OCR0A 244;// 0.001024*244 .25SIG OUTPUT COMPARE0A will be triggered 4 times ;24.}25. }26.27. void sweep()28. {4 of 112/8/15, 1:32 PM

Atmega168 Timer interrupts - Protostack29.30.PORTB 0b10000000;for (int i 0;i 8;i 09/timer-interrupts-on-an.delay ms(100);PORTB 1;}35. }36.37. ISR(SIG OUTPUT COMPARE0A)38. {39.40.41.42.if (green led is on())green led off();elsegreen led on();43. }Line 15 sets the CTC mode. This ensures that the counter is reset when it reachesOCR0A. After we have setup the timer registers, we call sei() to enable the globalregisters.On line 37, you will see “ISR(SIG OUTPUT COMPARE0A)”. This is the interruptservice routine and will get called each time the interrupt is triggered.8 bit timer example 2 – Dual interruptsIn this example we will use TimerCounter0 Compare Match A and Match Binterrupts. We we turn the LED on at Match B and off at Match A. This will producethe following waveform.1. #include avr/io.h 2. #include util/delay.h 3. #include avr/interrupt.h 4.5. #define green led on() PORTC BV(0)6. #define green led off() PORTC & BV(0)7.8. int main (void)9. {10.11.DDRB 0b11111111;DDRC 0b01111111;will just use PC0 )// All outputs// All outputs (Although we12.5 of 112/8/15, 1:32 PM

Atmega168 Timer interrupts - 2010/09/timer-interrupts-on-an.TIMSK0 BV(OCIE0A) BV(OCIE0B); // EnableInterrupt TimerCounter0 Compare Match A & B(SIG OUTPUT COMPARE0A/SIG OUTPUT COMPARE0A)TCCR0A BV(WGM01);// Mode CTCTCCR0B BV(CS02) BV(CS00);// Clock/1024,0.001024 seconds per tick16.OCR0A 244;// 0.001024*244 .25 SIG OUTPUT COMPARE0A will be triggered 4 timesper second.17.OCR0B 220;// 0.001024*220 .225 SIG OUTPUT COMPARE0B will be triggered 25msbefore SIG OUTPUT 24.}25. }26.27. void sweep()28. {29.30.31.32.33.34.35. }PORTB 0b10000000;for (int i 0;i 8;i ){delay ms(100);PORTB 1;}36.37. ISR(SIG OUTPUT COMPARE0A)38. {39.green led off();40. }41.42. ISR(SIG OUTPUT COMPARE0B)43. {44.45. }green led on();16 bit timerThe ATmega168 has a single 16 bit timer, which is referred to as Counter1. Itworks like the 8 bit timer, except the counter has more bits in it. This intervals to beset with longer duration and greater precision. The registers used by this timer are:6 of 11Counter1DescriptionTCCR1ATimer/Counter 1 Control Register ATCCR1BTimer/Counter 1 Control Register B2/8/15, 1:32 PM

Atmega168 Timer interrupts - imer-interrupts-on-an.TCCR1CTimer/Counter 1 Control Register CTCNT1HTimer/Counter 1 High RegisterTCNT1LTimer/Counter 1 Low RegisterOCR1AHOutput Compare Register 1 A HighOCR1ALOutput Compare Register 1 A LowOCR1BHOutput Compare Register 1 B HighOCR1BLOutput Compare Register 1 B LowICR1HInput Capture Register 1 HighICR1LInput Capture Register 1 LowTIMSK1Timer/Counter Interrupt Mask RegisterTIFR1Timer/Counter Interrupt Flag RegisterIn some ways this list looks like the registers used by the 8 bit timers. We will nowexamine the important differences.TCCR1A, TCCR1B and TCCR1C play a similar role to TCCR0A and TCCR0B.These are shown riteR/WR/WRRRRRRInitial00000000ValueSome of the differences include4 mode bits instead of 3 (ie more modes)The Force Output Compare bits are in TCCR1CInput Capture Noise Canceler bit in TCCR1B (outside the scope of thistutorial)Input Capture Edge Select in TCCR1B (outside the scope of this tutorial)TCNT1H and TCNT1L are similar to TCNT0, but being a 16 bit counter they aresplit across 2 registers. Similarly with OCR1AH, OCR1AL, OCR1BH and OCR1BL.ICR1H and ICR1L don’t have any equivalent in the 8 bit timers and allow you tocapture the timer value on certain events.7 of 112/8/15, 1:32 PM

Atmega168 Timer interrupts - imer-interrupts-on-an.16 Bit exampleThis example is similar to the previous one. Because we are using the 16 bit timer,we can increase the cycle time.1. #include avr/io.h 2. #include util/delay.h 3. #include avr/interrupt.h 4.5. #define green led on() PORTC BV(0)6. #define green led off()7.8. int main (void)9. {10.DDRB 0b11111111;11.DDRC 0b01111111;will just use PC0 )12.13.PORTC & BV(0)// All outputs// All outputs (Although weTIMSK1 BV(OCIE1A) BV(OCIE1B); // EnableInterrupt Timer/Counter1, Output Compare A & B(SIG OUTPUT COMPARE1A/SIG OUTPUT COMPARE1B)14.TCCR1B BV(CS12) BV(CS10) BV(WGM12);Clock/1024, 0.001024 seconds per tick, Mode CTC15.OCR1A 1954;//0.001024*1954 2 SIG OUTPUT COMPARE1A will betriggered every 2 seconds16.OCR1B 1929;//0.001024*1929 1.975 SIG OUTPUT COMPARE1B will betriggered 25ms before SIG OUTPUT COMPARE1A//17.18.19.20.21.22.23.24. }25.sei();while(1){sweep();}26. void sweep()27. {28.PORTB 0b10000000;29.for (int i 0;i 8;i )30.31.32.33.{delay ms(100);PORTB 1;}34. }35.36. ISR(SIG OUTPUT COMPARE1A)37. {38.8 of 11green led off();2/8/15, 1:32 PM

Atmega168 Timer interrupts - imer-interrupts-on-an.39. }40.41. ISR(SIG OUTPUT COMPARE1B)42. {43.green led on();44. }On line 15 and 16, where we set the value for the Output Compare Registers, wedon’t need to set value for the high and low registers. The AVR Libclibrary abstracts these as a single 16 bit value.More InformationI hope you’ve enjoyed this tutorial. Timers are a complex subject and I have tried tokeep them simple by just looking at a slice of what they can do. I encourage toread the documentation and experiment. I’ve listed 2 good resources below.AVR libc avr/interrupt.h Interrupts DocumentationATmega48/88/168/328 datasheetRelated posts:1. External Interrupts on an ATmega1689 of 112/8/15, 1:32 PM

Atmega168 Timer interrupts - Protostack18 er-interrupts-on-an.!ProtostackLoginShare Favorite Sort by BestJoin the discussion Dmitri Karpovich 4 years agoThanks! Great tutorial. I believe there is a small mistake there:instead of "TCCR0A and TCCR0A are shown below." should be"TCCR0A and TCCR0B are shown below." Reply Share ›1Farha 4 years agoSorry to bother you but Iam really confuded about how come8000000/1024 is equal to 0.001024 seconds.I am coming uo with a different value which is 0.128ms.Thanks for any help.TCCR0B BV(CS02) BV(CS00); // Clock/1024, 0.001024seconds per tick Reply Share ›Fahad Farha 3 years agoI know its a little late but to answer the question of FarhaThe atmega168 comes with fuse setting that divide the clockfrequency by 8. It means that by default the if atmega168running at 8MHz clock/oscillator it is actually running at1MHz. which divided by 1024 gives .001024. Reply Share ›Erkko Fahad 4 months agoYou both have your units confused. 1 MHz divided by1024 equals 976.5625 Hz and the inverse of that is1.024 ms per cycle. (Hz 1/s) Reply Share ›Tooba Fawad 4 years agoHello,I am looking forward to run HS-322 HD servo from Atmega168, Ihave used Atmega128 but never used it for timers and PWM, canyou please help me out in this. Thanks Reply Share ›protostackadmin 4 years agoI really need to proof read this more. Thanks for the heads up. Reply Share ›fell10 of 11 4 years ago2/8/15, 1:32 PM

Atmega168 Timer interrupts - Protostack11 of errupts-on-an.InformationBlog CategoriesTagsFollow UsShipping & ReturnsPrivacy PolicyTutorialsNew ProductsATMega8, AVRATMega168, LCDFacebookTwitterContact UsEmail UnsubscribePress CentreAnnouncementsMiscellaneousATMega328, HD44780 Google BreadboardAll prices displayed in US Dollars (USD)Copyright 2013 Protostack Pty LtdWebsite Handcrafted by Zero Point Labs2/8/15, 1:32 PM

available interrupts can be found in the AVR libc interrupt.h Interrupts Documentation under the section labelled “Choosing the vector: Interrupt vector names”. bit 7 6 5 4 3 2 1 0 TIMSK0 - - - - - OCIE0B OCIE0A

Related Documents:

to four timers. Timer 1 brings out a third match output, timers 2 and 3 bring out all four match outputs, timer 4 has one match output, and timer 5 has no inputs or outputs. 32-bit millisecond timer driven from th e RTC clock. This timer can generate interrupts using two match registers. WatchDog timer clocked by the peripheral clock.

Da Vinci-History Design Jot Design Museum Collection of iPad DrawCast Drawing Lessons IBM Think . Best Kitchen Timer Best Sand Timer Egg Timer Giant Timer Hourglass Sand Timer Just Timer Wave Timer . Pixlr Express Retromatic HD Split Pic Photo Edito

In Powertrain applications there are two dominant timer implementation methods: One uses a more peripheral timer approach where the timer module consists of capture/compare units and counters (ex. EMIOS/Etimer). This approach has the problem of the main core needs to service the interrupts from the timer module interrupting other processing.

Chapter 4. Interrupts and Exceptions An interrupt is usually defined as an event that alters the sequence of instructions executed by a processor. Such events correspond to electrical signals generated by hardware circuits both inside and outside the CPU chip. Interrupts are often divided into synchronous and asynchronous interrupts :

that is visualized. If the Timer in use is programmed on an infinite cycle, the booked Timer will begin at the end of the first pause of the working Timer. To make a booking, press the key of the Timer-x to be booked and then the Timer-Booking key. Start and Stop of a Blind Timer It is possible to activate any one of the five

Timer B 0 SIP Timer B (INVITE Transaction timeout timer) 500 : 65535 Timer F 0 SIP Timer F (non-INVITE Transaction timerout timer) 500 : 65535 Use_User_Agent 0 Add User-Agent Header in SIP Message or not 0 : not use, 1 : use User_Agent_Name "WPU-7800" String in User-Agent Header 1 60 string Use_Version_On_User_Agent 0 Add version in User-Agent .

The ATmega168 AVR instruction set. (2 of 3) Comparison of Instruction Sets Figure 5-35. The ATmega168 AVR instruction set. (3 of 3) The OMAP4430 ARM CPU Instructions Figure 5-34. The primary OMAP4430 ARM CPU integer instructions. (2 of 2) Sequential Flow of Control and Branches

Click 500 series – Customizable devices built on our Click 500 platform This user guide covers the Click 500 series. For the Click 100–400 series, please see the Click 100–400 Series User Guide. Using this Manual This manual is divided into two parts: Part I: Introduction to the Click Series – This part contains information common to the Click line, beginning with basic module .