Avr timers

1,375 views

Published on

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,375
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
136
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Avr timers

  1. 1.   A timer is a circuit that counts. This is said from an electronics point of view. It is widely used and is a very important component of microprocessors and microcontrollers.
  2. 2.     Timers are classified based on their mode of operation into synchronous and asynchronous timers. Synchronous timers count with respect to the clock while asynchronous timers depend upon the change in the input. They are also classified according to the maximum number they can count. Eg. 8 bit timer,16 bit timers etc Timers are easy to implement and are done using basic flip flop circuits .
  3. 3.    They are used everywhere from modulating signals to implement digital clocks, gaming, phone/pc/tablet applications etc. Small projects like range finders etc will also use timers. IC’s such as 555 are readily available in the market and can be used to easily implement timers.
  4. 4.   There are 3 timers in the AVR of which 2 are 8 bit timers the other one is a 16 bit timer. The timers found in the AVR or mostly in a microcontroller or processor is of synchronous type.
  5. 5.   what do they mean? An 8 bit timer can count to 2 to the power of 8 and a 16 bit timer can count upto the 2 to the power of 16. Basically timers in the controller are registers and an 8 bit timer is a 8bit register and 16 bit timer is 16bit register.
  6. 6.     The 8 bit timer starts counting from zero and goes upto 255.(that’s 256 counts) The 16 bit timer starts counting from zero and goes uptp 65535(65536 counts) Once the timer reaches the maximum value it “overflows” i.e. it restarts. ANALOGY
  7. 7. For the timer to increase by one it takes one clock. That is the time period gives the time it takes for the circuit to increment the timer register by one
  8. 8.  Consider a processor with 4MHz clock. We need a delay of 10ms. What will the timer count be??  What will the timer count be when the “required delay” is 50us  Which timer will you use ??  Can a delay of 100ms be directly implemented with one of AVR timers.
  9. 9.  So to know the maximum delay you could get from the processor with a given clock substitute TOP value of that particular timer in the formula. Maximum delay for a 4MHz processor 16bit timer? 8 bit timer? 
  10. 10.  What will we do for cases like the 100ms delay? The solution lies in reducing the frequency. How do we do that? That’s where tht prescaler comes into play.Do understand that we don’t actually reduce the frequency of the clock but we make the timer to behave as if it is in a reduced frequency.
  11. 11.   Also note that there will be a trade-off between resolution and accuracy if you use a precaler. The prescaler is set by manipulating some bits.
  12. 12.  Now get the timer count for 100ms using one of these prescalers.
  13. 13.  PROBLEM STATEMENT: Make an LED flash for every 10ms with your atmega 8 internal oscillator featuring a 1MHz clock. Use only 8 bit timer TIMER
  14. 14. Do the calculations first.  Without using a prescaler maximum delay=256us  Using a prescaler of 8 we’ll get a maximum delay =2048us  Using a prescaler of 64 max delay= 16.3ms our requirement of 10ms delay fits in this range. 
  15. 15.   No that we know our prescaler we should now calculate our timer count. Substituting 10ms in the formula we ge timer count to be 155.25. we’ll round it out to 156 counts.
  16. 16. The most important one is TCCR0 register which is the Timer/counter Control register for timer 0. First we start the clock and set the prescalar which is done by setting the three high lighted bits
  17. 17. Thus we have to set bits CS01 and CS00 to 1. TCCR0|=1<<CS00|1<<CS01;
  18. 18.  The register where the counting takes place is the TCNT0 register.It counts automatically and overflows and restarts again. We intitalise this too. TCNT0=0;
  19. 19.  #include <avr/io.h>      void timer0_init() { // set up timer with no prescaling TCCR0 |= ((1 << CS00)|(1<<CS01));         // initialize counter TCNT0 = 0; } int main(void) { // connect led to pin PC0 DDRC |= (1 << 0);     // initialize timer timer0_init();
  20. 20.            } // loop forever while(1) { // check if the timer count reaches 156 if (TCNT0 >= 156) { PORTC ^= (1 << 0); // toggles the led TCNT0 = 0; // reset counter } }
  21. 21.   There’s an alternative way of doing this. Without using the prescalar or if the delay cannot be got with the given prescalers. Let’s do the same example without using prescalars.
  22. 22.     We previously calculated that without prescaler maximum delay we get is 256us A point to be noted is that everytime the timer overflows an optional ISR is executed. We will use that interrupts to get the delay Let’s see how!
  23. 23. 10ms/256us=39.0625  So when the timer overflows 39 times it would have counted 256*10^-6 *39= 9.984ms  The remaining time is 10ms- 9.984ms=16us  Now we substitute this again in the formula and we’ll get 15 as the timer count.  Thus at the 40th iteration and 15th tick we’ll achieve our 10ms delay. 
  24. 24.   // 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++; } void timer0_init() { // set up timer with no prescaling TCCR0 |= (1 << CS00); sei(); TIMSK|=1<<TOIE0; // initialize counter TCNT0 = 0; }      int main(void) { // connect led to pin PC0 DDRC |= (1 << 0);      // initialize timer timer0_init();
  25. 25.                 // loop forever while(1) { // check if no. of overflows = 39 if (tot_overflow >= 39) // NOTE: '>=' is used { // check if the timer count reaches 15 if (TCNT0 >= 15) { PORTC ^= (1 << 0); // toggles the led TCNT0 = 0; // reset counter tot_overflow = 0; // reset overflow counter } } } }
  26. 26. The concept is the same with very minute differences. For example the counting register TCNT1 will be a 16 bit register. The control register is split into 2 8bit registers TCCR1A and TCCR1B. For normal mode of operation knowledge about TCCR1B will suffice
  27. 27.  TIMSK-TIMER/COUNTER INTERRUPT MASK REGISTER This is used to enable the timer interrupt.TOIE0 is timer overflow interrupt enable for 0 and TOIE1 is for timer 1. It is necessary to set that bit inorder to enable the interrupt and execute the ISR
  28. 28.  TIFR- timer interrupt flag register. We are interested in the TOV bit. The Timer overflow bit. This is set to 1 whenever the timer overflows . It is cleared after it overflows. The above two registers are shared by all the three timers.
  29. 29.    CTC stands for Clear timer on Compare. Remember previous example with the 10ms with timer0? The comparing can be done by the processor itself and the corresponding instructions can be executed.
  30. 30. Both CTC mode are almost the same except the fact that they store the compare value in different registers.
  31. 31. The value to be compared is stored over here.
  32. 32. The OCF1A/B bit is set to 1 when the value stored in OCR1A/B equals TCNT1 register.
  33. 33. Set these bits if you are using the interrupts. ISR (TIMER1_COMPA_vect) { //to do }
  34. 34. Those pins can be used to directly be set or cleared without any extra code.
  35. 35. We go back to TCCR1A register.` Setting the above bits will enable the hardware mode.
  36. 36.  FINAL coding lies in your hands. You can use the timer as you wish.  Hope you guys enjoyed it.  Image Courtesy: maxembedded.com

×