My simple blinky code not working.

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

I wrote a simple blinky program but not working. The led stays OFF all the time. I use arduino Uno hardware with atmega328p mcu running at 16 mhz.

Edit: It blinks now. But there is huge lag in timer. I can measure around 6 seconds of lag in just 5 minutes. Here is the updated code.

 #include "Arduino.h"
#include 
#include 


void toggle_led(void);

unsigned long volatile millis_count = 0;
volatile char state = 1;
unsigned long volatile current_count = 0;
unsigned int volatile count = 0;

ISR(TIMER0_COMPA_vect) {        //Timer interrupt ISR

    millis_count++;
    if (millis_count - current_count == 1000) {
        current_count = millis_count;
        toggle_led();
        Serial.println(count++);  //prints the count
    }

}


int main(void) {

    init();
    
    //Timer settings for interrupt at every millisecond
    TCCR0B = 0b11;  //prescaler set to clk/64 
    OCR0A = 249;    //Compare match A register to 249
    TIMSK0 |= 0b10; //generate interrupt on Output Compare Match

    sei();
    Serial.begin(9600);
    DDRB |= 1<<5;

    for (;;) {



    }
}

void toggle_led(void) {

    PORTB ^= (1<<5);
} 
Last Edited: Sun. Jan 8, 2012 - 05:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

OOps I found the mistake..

(_count - current_count) should be equal to 1000 not 0.
silly mistakes.. >_<

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

How many iterations of the timer interrupt do you think need to occur before this condition will be met?
if (_count - current_count == 0)

count will have to increment through it's full range and back to 0 before this can happen, which would be over 4 billion. What's the timer interval set to? Note that commenting your code would be a big help here. No running to the datasheet to check every little thing.

Why all the extra variables? Can't a simple test like this work for you?

ISR(TIMER0_COMPA_vect) {

   _count++;
   if (_count > somenumber) {
      _count = 0;
      toggle_led();
   }

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

Thanks for the suggestions. I tried to implement a lot of other function as a part of learning. Thats why there is so many unused variables.

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

Now it blinks. But there is lag error in counting. There is around 6 seconds lag in 5 minute. See the original post for the updated code. I have included a little documentation.

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

Think about it. You are getting an IRQ on TIM0_COMPA. The TCNT0 continues to count 249, 250, ..., 255, 0, ...

Hint. Look up CTC mode.

Why are you maintaining so many 'counts' ?

David.

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

Don't call routines that require a considerable amount of time to execute from within an ISR.

Hint, your interrupts are disabled while you are in the ISR...

cheers,
george.

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

So if I put Serial.print() inside the 'for' loop in the main() instead in the ISR, does that will solve the lag?

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

david.prentice wrote:
Think about it. You are getting an IRQ on TIM0_COMPA. The TCNT0 continues to count 249, 250, ..., 255, 0, ...

Hint. Look up CTC mode.

Why are you maintaining so many 'counts' ?

David.

OMG... I didnt think about that.. my bad :shock:

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

David, that totally worked. I added "TCCR0A = 0b10;" in the main(). And now its working fine. thanks a lot.