1 / 10

AVR Programming: Timers October 8, 2010

AVR Programming: Timers October 8, 2010. What is a timer?. A register that keeps track of a current value This value is updated at a certain frequency Allows applications to act in a precise and timely manner What time is it? Do something every X ms Wait X ms and then act

boris
Télécharger la présentation

AVR Programming: Timers October 8, 2010

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AVR Programming: Timers October 8, 2010

  2. What is a timer? • A register that keeps track of a current value • This value is updated at a certain frequency • Allows applications to act in a precise and timely manner • What time is it? • Do something every X ms • Wait X ms and then act • Generate a waveform • ATmega128 • 2 8-bit timers and 2 16-bit timers • ATmega128RFA1 (scout) • 2 8-bit timers and 4 16-bit timers

  3. What does this look like?An few things we’ll cover: • A control register • TCCR • Current time • TCNT • A clock signal • With prescalar • Output • Overflow (TOV) • Output compare (OC)

  4. The current timer value • TCNTn is a register that holds the current value of the timer • n is the number of the timer you are accessing (0,2 are 8-bit and 1,3 are 16-bit) • You can read from and also write to this register to set your own time • For most applications you shouldn’t need to write this value because it is updated in hardware

  5. Timer/Counter Control Register • FOCn – Force output compare • Only used in non-PWM modes • WGMnn – Waveform generation mode • We’ll save this for another Friday (Table 64 in the datasheet if you’re curious) • COMnn – Compare Output Mode • Another Friday as well (or Table 65 in the datasheet) • CSnn – Clock Select (next slide)

  6. Clock Select and Prescalars We could simply clock the timer at the processor frequency 8 bits will overflow in 32 microseconds at 8MHz Great if we’re trying to sample a high frequency signal Terrible if we’re trying to count seconds Use CSnn to create a less frequent clock tick based on the values of CSn2,CSn1,CSn0 001 = no prescaler (clk/1), 010 = clk/8, 011 = clk/64, 100 = clk/256, 101 = clk/1024 Example: (clk/8) 6

  7. How can we synchronize behaviors to the timer? Timer Overflow Interrupts • Enable in the Timer Interrupt Mask Register (TIMSK) by setting TOIEn (n is your timer #) • This works for 8-bit (read the datasheet for 16-bit) • TIMSK |= _BV(TOIEn); • Interrupt header is: ISR(TIMERn_OVF_vect){/*isr here*/;}

  8. How does this look in code? #include <avr/interrupt.h> #include <avr/io.h> #include <avr/timer.h> int main(void){ volatile int x = 0; DDRB |= _BV(PC0) | _BV(PC1) | _BV(PC4) | _BV(PC5); TIMSK |= _BV(TOIE0); //enable timer overflow interrupt 7 TCCR0 |= _BV(CS00) | _BV(CS02); //select clk/1024, normal mode, no output compare sei(); //global interrupt enable while(1){;} } ISR(TIMER0_OVF_vect){ PORTC |= _BV(PC1) | _BV(PC5) | _BV(PC0) | _BV(PC4); //turn lights off if(x){ PORTC &= ~(_BV(PC1) | _BV(PC5)); //turn lights green x = 0; } else{ PORTC &= ~(_BV(PC0) | _BV(PC4)); //turn lights red x = 1; } }

  9. Stuff to try out • Output your own clock frequency • Output your own signals on the bomleds or orbs • Poll one of the push buttons every x amount of time • Have the colony robots keep track of time of day • Make a dance that ACTUALLY syncs with music (down to the microsecond)

  10. Help/More Info Datasheet: http://www.atmel.com/dyn/resources/prod_documents/doc2467.pdf

More Related