-2

I'm trying to blink an led without using the delay function. I came across using the timer interrupt and I tried it, and it compiles fine. But the output is fixed on PORTA = 0x01; so, I believe the ISR function is not working. Is there anything I am missing in the code? Thanks.

#include <asf.h>
#include <avr/interrupt.h>

volatile unsigned int count=0;
volatile unsigned int tTime=0;

void port_init(void) {
    PORTA = 0xff;
}

ISR(TIMER0_OVF_vect)
{
    TCNT0 = 206;
    count++;
    if (count < 625) {
        PORTA=0x01;
    }
    else if ((count > 625) && (count < 1250)) {
        PORTA=0x00;
    }
    else {
        count=0;
    }
}

int main (void)
{ 
    board_init();
    port_init();

    TCCR0 = 0x06; //setting dispensing ratio
    TCNT0 = 206; //initial value of register
    TIMSK = 0x01; //enabling interrupt
    SREG=0x80;

    DDRA=0xff;
    while(1){
    }
}
Andrew Cottrell
  • 3,312
  • 3
  • 26
  • 41
Nicky
  • 23
  • 3
  • What do you mean by the "program doesn't seem to work"? Can you please elaborate? Please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly http://idownvotedbecau.se/itsnotworking/ – Some programmer dude Dec 08 '17 at 07:22
  • Sorry about that. It's my first time asking a question in this site! The problem is that the program is compiling without any errors, but the led does not blink. I believe it is fixed on PORTA=0x01; and the ISR function is not working. Is there a problem in my coding? – Nicky Dec 08 '17 at 07:28
  • @NicholasLee there is a problem with your debugging. Use your JTAG, or whatever, debug interface to put a breakpoint on the interrupt handler. If you do not have a debug interface, get one. You wil need it. – Martin James Dec 08 '17 at 08:29

1 Answers1

1

You have a logic error in your ISR() function. Once count hits 625 the first two if/else-if clauses will be false (because 625 is neither less than, nor greater than, 625), thus the final else clause will execute and reset count to zero. The result of this is that the else-if clause that sets PORTA=0x00 will never execute.

To fix, change the first if from < to <=, like so:

if (count <= 625) {
Andrew Cottrell
  • 3,312
  • 3
  • 26
  • 41