vendredi 10 mai 2019

Atmega168 randomly generates interrupt in while loop

i cuted my main code for minimum and i have a problem with more often interrupt. Timer is configuring for generates interrupt for 500ms but sometimes is faster e.g 250ms.

Led in while loop sometimes blinking faster. When i remove delay line then led is blinking correctly.

Where's problem?

#include <avr/interrupt.h>
#define SET_BIT(r,x) r|=_BV(x)
#define CLR_BIT(r,x) r&=~_BV(x)
#define TOG_BIT(r,x) r^=_BV(x)

#include <avr/io.h>
#include <util/delay.h>

#define LED PD4
#define LED_PORT PORTD
#define LED_DIR DDRD
#define LED_ON CLR_BIT(LED_PORT,LED)
#define LED_OFF SET_BIT(LED_PORT,LED)
#define LED_CHECK !(LED_PORT&_BV(LED))

volatile unsigned int LedTime;

void init(void);

int main(void)
{

init();

while (1)
{

    if(LedTime==0) 
    {
        TOG_BIT(LED_PORT,LED);
        LedTime=500;

    }
    _delay_us(1);
}

}

void init(void)
{

SET_BIT(LED_DIR,LED);
LED_ON;

TCCR1B |= (1<<WGM12);//ctc
////TCCR1B|=1;//1
//TCCR1B|=2;//8
//TCCR1B|=3;//64
//TCCR1B|=4;//256
TCCR1B|=5;//1024

OCR1A=(F_CPU/1024/1000)-1;
TIMSK1|=_BV(OCIE1A);

sei();

}

ISR(TIMER1_COMPA_vect)//1ms system tick
{

 if (LedTime>0) LedTime--;

}




Aucun commentaire:

Enregistrer un commentaire