LED flasher using timer.

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

Hi all,

Having a little problem with this bit of code.

#include  

int main (void) 
{ 
   DDRB |= (1 << 0); // Set LED as output 

   TCCR1B |= (1 << CS10); // Set up timer 

   for (;;) 
   { 
      // Check timer value in if statement, true when count matches 1/20 of a second 
      if (TCNT1 >= 20000) 
      { 
         PORTB ^= (1 << 0); // Toggle the LED 

         TCNT1 = 0; // Reset timer value 
      } 
   } 
} 

The code is from Dean's timer tutorial and works as it should i think but the LED is visibly blinking and if i speed it up by changing this value of TCNT1i can get it to blink quicker until the LED is on solid.

Could this have something to do with the STK500? I am using a ATMega16 with the on board 1Mhz oscillator.
All help is ppreciated.

I can get it to fade from like in PWM.

Thanks in advance.

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

That code is for slow blinking, certainly not for PWM. Also it has a fixed duty, because you are only togelling the pin on each compare. I suggest you read the section of the data sheet on the timer, paying particular attention to the PWM mode descriptions.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

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

Ok thanks for that. I am trying to work my way up to PWM and then hopefully a PWM RGB LED but trying to learn about the timers first. Just a bit confusing reading the data sheets.

Ok I will look into it a little more and see how i go.

Thanks

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

If you look into the simple avr-libc demo, it contains a LED
flasher that uses PWM to ramp a LED up and down.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.