Yahoo Groups archive

AVR-Chat

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

Thread

My timer does not seem to be incrementing an integer

My timer does not seem to be incrementing an integer

2010-05-08 by Andrew

Hi,

I have a project using an Atmega16 where the timer should be interrupting once a second, but my sconds variable in not being incremented. If I use this line:

PORTD = ~PORTD;

The leds connected to that port flash, if I write my seconds variable to that port only bianry one is indicated, whcih suggests to me that the timer is not running or the firmware is being reset.

Could someone shed some light on this for me please? I have disabled the watch dog timer too My code is below:

Andrew

const uint16_t interval = 4000000/65536;//Work out interval from CPU frequency

void Init_Timer1()
{
	// Set up counter.  Fclk = 4Mz
	TCCR1B = 1 << WGM12 | 1 << CS12;	// Fclk / 256, clear on match
	OCR1A  = interval;				// Timer ticks at 4000000/(256*interval) (1s)	
	TIMSK  |= (1 << OCIE1A | 1 << TOIE1);
	asm("sei");
}

ISR(TIMER1_COMPA_vect)
{	
	wdt_reset();
	power_save_timer++;
	seconds = seconds+1;
	PORTD = seconds << 2;// Used to verify timer is running	

	if (seconds>59)
	{
		seconds=0;
		minutes++;
	}
		
	if(minutes>59)
	{
		minutes=0;
		hours++;
	}

	if(hours>23) hours=0;
}

Re: My timer does not seem to be incrementing an integer

2010-05-15 by Donald H

--- In AVR-Chat@yahoogroups.com, "Andrew" <ajellisuk@...> wrote:
>
> Hi,
> 
> I have a project using an Atmega16 where the timer should be interrupting once a second, but my sconds variable in not being incremented. If I use this line:
> 
> PORTD = ~PORTD;
See: http://www.atmel.com/dyn/resources/prod_documents/doc2466.pdf
Figure 23 for help

> 
> The leds connected to that port flash, if I write my seconds variable to that port only bianry one is indicated, whcih suggests to me that the timer is not running or the firmware is being reset.
> 
> Could someone shed some light on this for me please? I have disabled the watch dog timer too My code is below:

What is your compiler ?
Show quoted textHide quoted text
> 
> Andrew
> 
> const uint16_t interval = 4000000/65536;//Work out interval from CPU frequency
> 
> void Init_Timer1()
> {
> 	// Set up counter.  Fclk = 4Mz
> 	TCCR1B = 1 << WGM12 | 1 << CS12;	// Fclk / 256, clear on match
> 	OCR1A  = interval;				// Timer ticks at 4000000/(256*interval) (1s)	
> 	TIMSK  |= (1 << OCIE1A | 1 << TOIE1);
> 	asm("sei");
> }
> 
> ISR(TIMER1_COMPA_vect)
> {	
> 	wdt_reset();
> 	power_save_timer++;
> 	seconds = seconds+1;
> 	PORTD = seconds << 2;// Used to verify timer is running	
> 
> 	if (seconds>59)
> 	{
> 		seconds=0;
> 		minutes++;
> 	}
> 		
> 	if(minutes>59)
> 	{
> 		minutes=0;
> 		hours++;
> 	}
> 
> 	if(hours>23) hours=0;
> }
>

Re: [AVR-Chat] My timer does not seem to be incrementing an integer

2010-05-15 by Ranjit Puri

Please can you also share the code snippets where you are initializing PORTD
and also the ones where you are declaring the variables being used in the
ISR?

Ranjit


On Sat, May 8, 2010 at 10:54 AM, Andrew <ajellisuk@yahoo.co.uk> wrote:

>
>
> Hi,
>
> I have a project using an Atmega16 where the timer should be interrupting
> once a second, but my sconds variable in not being incremented. If I use
> this line:
>
> PORTD = ~PORTD;
>
> The leds connected to that port flash, if I write my seconds variable to
> that port only bianry one is indicated, whcih suggests to me that the timer
> is not running or the firmware is being reset.
>
> Could someone shed some light on this for me please? I have disabled the
> watch dog timer too My code is below:
>
> Andrew
>
> const uint16_t interval = 4000000/65536;//Work out interval from CPU
> frequency
>
> void Init_Timer1()
> {
> // Set up counter. Fclk = 4Mz
> TCCR1B = 1 << WGM12 | 1 << CS12; // Fclk / 256, clear on match
> OCR1A = interval; // Timer ticks at 4000000/(256*interval) (1s)
> TIMSK |= (1 << OCIE1A | 1 << TOIE1);
> asm("sei");
> }
>
> ISR(TIMER1_COMPA_vect)
> {
> wdt_reset();
> power_save_timer++;
> seconds = seconds+1;
> PORTD = seconds << 2;// Used to verify timer is running
>
> if (seconds>59)
> {
> seconds=0;
> minutes++;
> }
>
> if(minutes>59)
> {
> minutes=0;
> hours++;
> }
>
> if(hours>23) hours=0;
> }
>
>  
>


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

Re: My timer does not seem to be incrementing an integer

2010-05-16 by stevec

Yes, declare variables accessed by both interrupt and non-interrupt as
volatile.
If the variable is two or more bytes in size (on the 8 bit AVR), then be
sure that the non-interrupt level code does something like this
cli();
<read or modify multi-byte variable used also by ISR>
sei();

And lastly, in the interrupt code, we want it fast. So here's
optimization one normally does:

...
if (++seconds == 60)  {
     seconds = 0;
     if (++minutes == 60)   {
         minutes = 0;
         if (++hours == 24)
            minutes = 0;
     }
}


--- In AVR-Chat@yahoogroups.com, Cat C <catalin_cluj@...> wrote:
>
>
> You're not showing where you declared "seconds" but make it volatile
and it will work:
>
> volatile uint8_t seconds;
>
> Cat
>
> ----------------------------------------
> > To: AVR-Chat@yahoogroups.com
> > From: ajellisuk@...
> > Date: Sat, 8 May 2010 17:54:29 +0000
> > Subject: [AVR-Chat] My timer does not seem to be incrementing an
integer
> >
> > Hi,
> >
> > I have a project using an Atmega16 where the timer should be
interrupting once a second, but my sconds variable in not being
incremented. If I use this line:
> >
> > PORTD = ~PORTD;
> >
> > The leds connected to that port flash, if I write my seconds
variable to that port only bianry one is indicated, whcih suggests to me
that the timer is not running or the firmware is being reset.
> >
> > Could someone shed some light on this for me please? I have disabled
the watch dog timer too My code is below:
> >
> > Andrew
> >
> > const uint16_t interval = 4000000/65536;//Work out interval from CPU
frequency
> >
> > void Init_Timer1()
> > {
> > // Set up counter. Fclk = 4Mz
> > TCCR1B = 1 << WGM12 | 1 << CS12; // Fclk / 256, clear on match
> > OCR1A = interval; // Timer ticks at 4000000/(256*interval) (1s)
> > TIMSK |= (1 << OCIE1A | 1 << TOIE1);
> > asm("sei");
> > }
> >
> > ISR(TIMER1_COMPA_vect)
> > {
> > wdt_reset();
> > power_save_timer++;
> > seconds = seconds+1;
> > PORTD = seconds << 2;// Used to verify timer is running
> >
> > if (seconds>59)
> > {
> > seconds=0;
> > minutes++;
> > }
> >
> > if(minutes>59)
> > {
> > minutes=0;
> > hours++;
> > }
> >
> > if(hours>23) hours=0;
> > }
> >
> >
> >
> >
> > ------------------------------------
> >
> > Yahoo! Groups Links
> >
> >
> >
>



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

RE: [AVR-Chat] Re: My timer does not seem to be incrementing an integer

2010-05-16 by Cat C

Maybe you need a volatile variable?

Cat

----------------------------------------
Show quoted textHide quoted text
> To: AVR-Chat@yahoogroups.com
> From: donhamilton2002@yahoo.com
> Date: Sat, 15 May 2010 20:47:01 +0000
> Subject: [AVR-Chat] Re: My timer does not seem to be incrementing an integer
>
>
>
> --- In AVR-Chat@yahoogroups.com, "Andrew"  wrote:
>>
>> Hi,
>>
>> I have a project using an Atmega16 where the timer should be interrupting once a second, but my sconds variable in not being incremented. If I use this line:
>>
>> PORTD = ~PORTD;
> See: http://www.atmel.com/dyn/resources/prod_documents/doc2466.pdf
> Figure 23 for help
>
>>
>> The leds connected to that port flash, if I write my seconds variable to that port only bianry one is indicated, whcih suggests to me that the timer is not running or the firmware is being reset.
>>
>> Could someone shed some light on this for me please? I have disabled the watch dog timer too My code is below:
>
> What is your compiler ?
>
>>
>> Andrew
>>
>> const uint16_t interval = 4000000/65536;//Work out interval from CPU frequency
>>
>> void Init_Timer1()
>> {
>> // Set up counter. Fclk = 4Mz
>> TCCR1B = 1 << WGM12 | 1 << CS12; // Fclk / 256, clear on match
>> OCR1A = interval; // Timer ticks at 4000000/(256*interval) (1s)
>> TIMSK |= (1 << OCIE1A | 1 << TOIE1);
>> asm("sei");
>> }
>>
>> ISR(TIMER1_COMPA_vect)
>> {
>> wdt_reset();
>> power_save_timer++;
>> seconds = seconds+1;
>> PORTD = seconds << 2;// Used to verify timer is running
>>
>> if (seconds>59)
>> {
>> seconds=0;
>> minutes++;
>> }
>>
>> if(minutes>59)
>> {
>> minutes=0;
>> hours++;
>> }
>>
>> if(hours>23) hours=0;
>> }
>>
>
>
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>

RE: [AVR-Chat] My timer does not seem to be incrementing an integer

2010-05-16 by Cat C

You're not showing where you declared "seconds" but make it volatile and it will work:
 
volatile uint8_t seconds;
 
Cat

----------------------------------------
Show quoted textHide quoted text
> To: AVR-Chat@yahoogroups.com
> From: ajellisuk@yahoo.co.uk
> Date: Sat, 8 May 2010 17:54:29 +0000
> Subject: [AVR-Chat] My timer does not seem to be incrementing an integer
>
> Hi,
>
> I have a project using an Atmega16 where the timer should be interrupting once a second, but my sconds variable in not being incremented. If I use this line:
>
> PORTD = ~PORTD;
>
> The leds connected to that port flash, if I write my seconds variable to that port only bianry one is indicated, whcih suggests to me that the timer is not running or the firmware is being reset.
>
> Could someone shed some light on this for me please? I have disabled the watch dog timer too My code is below:
>
> Andrew
>
> const uint16_t interval = 4000000/65536;//Work out interval from CPU frequency
>
> void Init_Timer1()
> {
> // Set up counter. Fclk = 4Mz
> TCCR1B = 1 << WGM12 | 1 << CS12; // Fclk / 256, clear on match
> OCR1A = interval; // Timer ticks at 4000000/(256*interval) (1s)
> TIMSK |= (1 << OCIE1A | 1 << TOIE1);
> asm("sei");
> }
>
> ISR(TIMER1_COMPA_vect)
> {
> wdt_reset();
> power_save_timer++;
> seconds = seconds+1;
> PORTD = seconds << 2;// Used to verify timer is running
>
> if (seconds>59)
> {
> seconds=0;
> minutes++;
> }
>
> if(minutes>59)
> {
> minutes=0;
> hours++;
> }
>
> if(hours>23) hours=0;
> }
>
>
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>

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.