Yahoo Groups archive

AVR-Chat

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

Thread

Setting timer0 on Atmega48

Setting timer0 on Atmega48

2011-09-24 by Philippe Habib

This should be a simple thing but I'm having trouble getting it to work.  

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;
	TCNT0 = 177;
	TIMSK = 0x01;

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:

#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?

Thanks.

Re: [AVR-Chat] Setting timer0 on Atmega48

2011-09-24 by Jim Wagner

Hello, Phillipe

First, signal is deprecated for use in gcc.

Second, 8MHz divided by 1024 gives a tick every 128uS. You cannot get  
1ms from that,  because it would take 7.8125 clock "ticks".

Third, by far the easier way to count a specific number of clock ticks  
is to set the compare value in a compare register and have the counter  
reset. You can then count, additionally, on the overflow interrupt. No  
counter reloading is needed.

Jim Wagner
Oregon Research Electronics

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 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;
> TCNT0 = 177;
> TIMSK = 0x01;
>
> 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:
>
> #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?
>
> Thanks.
> 



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

Re: [AVR-Chat] Setting timer0 on Atmega48

2011-09-24 by Philippe Habib

Thanks for the reply.

I wanted a tick every (about) 10ms so I got 78 ticks, subtracted that from the overflow value of 255 and came up with the 177 which I used as my start value.

So I think what you're suggesting is that I set 78 in a compare register and then have my isr get called on compare.  That's fine too.

Am I on the right track as far as the registers I've set?

Thanks.
Show quoted textHide quoted text
----- Original Message -----
From: "Jim Wagner" <wagnerj@proaxis.com>
To: AVR-Chat@yahoogroups.com
Sent: Saturday, September 24, 2011 3:23:19 PM
Subject: Re: [AVR-Chat] Setting timer0 on Atmega48

Hello, Phillipe

First, signal is deprecated for use in gcc.

Second, 8MHz divided by 1024 gives a tick every 128uS. You cannot get  
1ms from that,  because it would take 7.8125 clock "ticks".

Third, by far the easier way to count a specific number of clock ticks  
is to set the compare value in a compare register and have the counter  
reset. You can then count, additionally, on the overflow interrupt. No  
counter reloading is needed.

Jim Wagner
Oregon Research Electronics

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 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;
> TCNT0 = 177;
> TIMSK = 0x01;
>
> 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:
>
> #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?
>
> Thanks.
> 



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



------------------------------------

Yahoo! Groups Links

Re: [AVR-Chat] Setting timer0 on Atmega48

2011-09-24 by Jim Wagner

Yes, by far easier to set the 78 in a compare register. Look at the  
Timer0 mode list for the bit combination to reset on compare; you may  
not be able to do that with all compare registers (for that timer).

Please use the ISR() construction. Many of the newer chips do NOT have  
signal vector definitions. The table of vector names is in the avr- 
libc document.

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

> Thanks for the reply.
>
> I wanted a tick every (about) 10ms so I got 78 ticks, subtracted  
> that from the overflow value of 255 and came up with the 177 which I  
> used as my start value.
>
> So I think what you're suggesting is that I set 78 in a compare  
> register and then have my isr get called on compare. That's fine too.
>
> Am I on the right track as far as the registers I've set?
>
> Thanks.
>
> ----- Original Message -----
> From: "Jim Wagner" <wagnerj@proaxis.com>
> To: AVR-Chat@yahoogroups.com
> Sent: Saturday, September 24, 2011 3:23:19 PM
> Subject: Re: [AVR-Chat] Setting timer0 on Atmega48
>
> Hello, Phillipe
>
> First, signal is deprecated for use in gcc.
>
> Second, 8MHz divided by 1024 gives a tick every 128uS. You cannot get
> 1ms from that, because it would take 7.8125 clock "ticks".
>
> Third, by far the easier way to count a specific number of clock ticks
> is to set the compare value in a compare register and have the counter
> reset. You can then count, additionally, on the overflow interrupt. No
> counter reloading is needed.
>
> Jim Wagner
> Oregon Research Electronics
>
> 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 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;
> > TCNT0 = 177;
> > TIMSK = 0x01;
> >
> > 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:
> >
> > #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?
> >
> > Thanks.
> >
>
> [Non-text portions of this message have been removed]
>
> ------------------------------------
>
> Yahoo! Groups Links
>
> 



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

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]

Re: [AVR-Chat] Setting timer0 on Atmega48

2011-09-24 by R E Purcella

Ok, I see a change in the thread from mega 8 (which doesn't have compare 
function in timer0) and now to mega48 (which does)
rep
Show quoted textHide quoted text
On 9/24/2011 3:48 PM, Jim Wagner wrote:
> Yes, by far easier to set the 78 in a compare register. Look at the
> Timer0 mode list for the bit combination to reset on compare; you may
> not be able to do that with all compare registers (for that timer).
>
> Please use the ISR() construction. Many of the newer chips do NOT have
> signal vector definitions. The table of vector names is in the avr-
> libc document.
>
> Jim
> On Sep 24, 2011, at 3:41 PM, Philippe Habib wrote:
>
>> Thanks for the reply.
>>
>> I wanted a tick every (about) 10ms so I got 78 ticks, subtracted
>> that from the overflow value of 255 and came up with the 177 which I
>> used as my start value.
>>
>> So I think what you're suggesting is that I set 78 in a compare
>> register and then have my isr get called on compare. That's fine too.
>>
>> Am I on the right track as far as the registers I've set?
>>
>> Thanks.
>>
>> ----- Original Message -----
>> From: "Jim Wagner"<wagnerj@proaxis.com>
>> To: AVR-Chat@yahoogroups.com
>> Sent: Saturday, September 24, 2011 3:23:19 PM
>> Subject: Re: [AVR-Chat] Setting timer0 on Atmega48
>>
>> Hello, Phillipe
>>
>> First, signal is deprecated for use in gcc.
>>
>> Second, 8MHz divided by 1024 gives a tick every 128uS. You cannot get
>> 1ms from that, because it would take 7.8125 clock "ticks".
>>
>> Third, by far the easier way to count a specific number of clock ticks
>> is to set the compare value in a compare register and have the counter
>> reset. You can then count, additionally, on the overflow interrupt. No
>> counter reloading is needed.
>>
>> Jim Wagner
>> Oregon Research Electronics
>>
>> 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 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;
>>> TCNT0 = 177;
>>> TIMSK = 0x01;
>>>
>>> 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:
>>>
>>> #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?
>>>
>>> Thanks.
>>>
>> [Non-text portions of this message have been removed]
>>
>> ------------------------------------
>>
>> Yahoo! Groups Links
>>
>>
>
>
> [Non-text portions of this message have been removed]
>
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>
>

Re: [AVR-Chat] Setting timer0 on Atmega48

2011-09-25 by Philippe Habib

Thank you.  I had to quit working on this yesterday, but its working now.  I'm used to using compilers that have all of these functions brought out in an idiot ready interface so I'm afraid I don't know all of the ins and outs of the registers and all of their interactions as well as I'd like.
Show quoted textHide quoted text
----- Original Message -----
From: "Clark Martin" <cmmac@sonic.net>
To: AVR-Chat@yahoogroups.com
Sent: Saturday, September 24, 2011 3:53:43 PM
Subject: Re: [AVR-Chat] Setting timer0 on Atmega48


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]



------------------------------------

Yahoo! Groups Links

Re: Setting timer0 on Atmega48

2011-09-25 by greggy

The data sheet can often throw some light on the chips abilities and workings also...

--- In AVR-Chat@yahoogroups.com, M W <rd232d@...> wrote:
Show quoted textHide quoted text
>
> I find that it helps a LOT sometimes to read the .h files for the specific micro I'm on, lets me know what's in there & usually then I can figure out why. Helps :)
> 
>   Mark
> 
> 
> ----- Original Message -----
> From: Philippe Habib <phabib@...>
> To: AVR-Chat@yahoogroups.com
> Cc: AVR-Chat@yahoogroups.com
> Sent: Sunday, September 25, 2011 9:31 AM
> Subject: Re: [AVR-Chat] Setting timer0 on Atmega48
> 
> Thank you.  I had to quit working on this yesterday, but its working now.  I'm used to using compilers that have all of these functions brought out in an idiot ready interface so I'm afraid I don't know all of the ins and outs of the registers and all of their interactions as well as I'd like.
>

Re: [AVR-Chat] Setting timer0 on Atmega48

2011-09-25 by M W

I find that it helps a LOT sometimes to read the .h files for the specific micro I'm on, lets me know what's in there & usually then I can figure out why. Helps :)

  Mark
Show quoted textHide quoted text
----- Original Message -----
From: Philippe Habib <phabib@well.com>
To: AVR-Chat@yahoogroups.com
Cc: AVR-Chat@yahoogroups.com
Sent: Sunday, September 25, 2011 9:31 AM
Subject: Re: [AVR-Chat] Setting timer0 on Atmega48

Thank you.  I had to quit working on this yesterday, but its working now.  I'm used to using compilers that have all of these functions brought out in an idiot ready interface so I'm afraid I don't know all of the ins and outs of the registers and all of their interactions as well as I'd like.

Re: [AVR-Chat] Setting timer0 on Atmega48

2011-09-25 by Clark Martin

On Sep 25, 2011, at 12:53 PM, M W wrote:

> I find that it helps a LOT sometimes to read the .h files for the specific micro I'm on, lets me know what's in there & usually then I can figure out why. Helps :)
> 
I rarely need to go to the .h file.  I use the Atmel PDF docs and look at the Register Description.  The avrlibc include files match up quite nicely.  The one exception is the IRQ vector names so I do go into the include files for vector names, that is when I can't guess it.

> 
>   Mark
> 
> ----- Original Message -----
> From: Philippe Habib <phabib@well.com>
> To: AVR-Chat@yahoogroups.com
> Cc: AVR-Chat@yahoogroups.com
> Sent: Sunday, September 25, 2011 9:31 AM
> Subject: Re: [AVR-Chat] Setting timer0 on Atmega48
> 
> Thank you.  I had to quit working on this yesterday, but its working now.  I'm used to using compilers that have all of these functions brought out in an idiot ready interface so I'm afraid I don't know all of the ins and outs of the registers and all of their interactions as well as I'd like.
> 
> 

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]

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.