timer 0 does not work

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi i am using an ATMEGA16 on STK500..

the code i have is as follows:

#include 
#include 
#include 

SIGNAL ( SIG_OVERFLOW0 )
{

PORTB = ~ PORTB;

}

int main(void)

{


TCCR0 = 0x05;

TIFR = (1 << TOV0);

TIMSK = (1 << TOIE0);

TCNT0 = 0x00;

DDRB = 0xFF;

PORTB = 0x00;

return 1;

}

i am using timer0 in interrupt mode..i am using a frequency of 500 kHz (which i am setting in AVR Studio) and prescalar value of 1024.. output is LED's connected to port B and i am toggling it in the ISR.. I expect to see LED's blink on port B once every second.. but the LED's are just staying ON.. any ideas what could be wrong? thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No "sei();" perhaps? ;)

By the way SIGNAL() is deprecated in favour of ISR() - suggest you have a look at the user manual (click my sig to get there quickly)

Also don't let it get to the return in main() as you have no idea where it might go next. Instead, if the program has nothing more to do end it with:

while(1);

which holds execution in an infinite loop.

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

npat_avr wrote:
hi i am using an ATMEGA16 on STK500..

the code i have is as follows:

int main(void)

{


TCCR0 = 0x05;

TIFR = (1 << TOV0);

TIMSK = (1 << TOIE0);

TCNT0 = 0x00;

DDRB = 0xFF;

PORTB = 0x00;

return 1;

}

the LED's are just staying ON.. any ideas what could be wrong? thanks

Once you get an infinite loop running in main, you want to do initialization stuff outside of that loop. And do not write to PORTB in the loop - let the ISR do it all.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

also, bit shift notation is much more legible :)