Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: [AVR-Chat] Setting timer0 on Atmega48

2011-09-24 by Clark Martin

On Sep 24, 2011, at 3:05 PM, Philippe Habib wrote:

> This should be a simple thing but I'm having trouble getting it to work. 
> 
I don't have the data sheet for the 48 handy so I looked at the 168 but they are similar.

> 
> I want a regular interrupt every 10ms while using the internal 8Mhz oscillator. I'm using AVR studio with the default (GCC?) compiler.
> 
> Can someone let me know what I'm doing wrong?
> 
> I try to set the various timer registers like this:
> 
> TCCR0A = 0x00;
> TCCR0B = 0x05;
> 
Your setup here is configuring the counter for Normal operation.  You can do it this way but it s simpler and more accurate to use CTC mode.  Once set it will interrupt every 10mS (approx) without having to set any registers.

For the 168 this means:
TCCR0A	= 0x02;
TCCR0B	= 0x05;

> TCNT0 = 177;
> 
And 
OCR0A	= 78;

> TIMSK = 0x01;
> 
TIMSK0		= 0x02;



Note that it's more readable to use the bit constants (defined in the various ioXXX.h files) ie:

TCCR0A	= (1 << WGM01) | (0 << WGM00);
TCCR0B	= (0 << WGM02) | (1 << CS02) | (0 << CS02) | (1 << CS02);
TIMSK0		= (1 << OCIE0A);

It makes it clearer what the bits are doing.  It also makes it easier to port it to other processors.  If the new processor doesn't have certain bits the compiler will flag it as an error.  A hex constant won't.



> 
> What I'm hoping for here is a timer that will use the div1024 start at 177 and set off the timer int when it overflows at 255.
> 
> Then I turn on interrupts
> 
> SEI();
> 
> Here is my ISR:
> 
> 
And here you'd use the OCR0A interrupt

> #if __GCC__
> SIGNAL(SIG_OVERFLOW0)
> #else
> #pragma vector=TIMER0_OVF_vect
> __interrupt void TIMER0_OVF(void)
> #endif
> {
> uccounter++;
> TCNT0 = 177;	// reset the timer
> return;
> }
> 
> uccounter is a global unsigned char. I just use it to toggle a port when its 0.
> 
> Am I missing something and is there a library call that makes setting all this easier?
> 
You don't actually say here what if anything happened when you did this.

> 
> 

Clark Martin
Redwood City, CA, USA
Macintosh / Internet Consulting

"I'm a designated driver on the Information Super Highway"



[Non-text portions of this message have been removed]

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.