I'm trying to call a timer interrupt with Attiny 24. I've gone through the documentation, done the work; the code is:
cli();
//TCCR0A - set 0b0000RR10 - pins disconnected, CTC mode, reset at OCRA
TCCR0A = 0;
TCCR0A |= (1<<WGM01);
//TCCR0B set to 0b00RR0101 - prescaler 1024,
TCCR0B = 0;
TCCR0B |= (1<<CS02);
TCCR0B |= (1<<CS00);
//initial value in counter 0, count up to 244
TCNT0 = 0;
OCR0A = 244;
//enable interrupt on A
TIMSK0 = 0;
TIMSK0 |= (1<<OCIE0A);
sei();
The idea is: prescaler 1024 at 1MHz clock counts up approximately 1000 times per second; I count up to about 250 -> about 4 interrupts per second. The IRQ handler is just toggling an LED:
The LED lights up once and then stays on. What am I doing wrong with the setup? Is there a register I forgot? Should I use Timer1 instead, or something like that?
thanks for the prompt. A full code that doesn't work is below; all setup I have is an LED on pin 10. In the setup, its blinks once and then stays on even though the interrupts should turn it off. Once I have this I can start changing the ISR to actually do something meaningful but as of now, I don't really know if the interrupt is triggered or not.
int output = 10;
ISR(TIMER0_COMPA_vect) {
digitalWrite(output, digitalRead(output) ^ 1); //toggle pin ON/OFF
}
}
void setup() {
//test connection - LED at pin 10 should blink once ON-OFF
pinMode(output,OUTPUT);
digitalWrite(output,HIGH);
delay(1000);
digitalWrite(output, digitalRead(output) ^ 1);
delay(1000);
//setup the timer 1
cli();
//TCCR0A - set 0b0000RR10
TCCR0A = 0;
TCCR0A |= (1<<WGM01);
//TCCR0B set to 0b00RR0101
TCCR0B = 0;
TCCR0B |= (1<<CS02);
TCCR0B |= (1<<CS00);
TCNT0 = 0;
OCR0A = 244;
//enable interrupt on A
TIMSK0 = 0;
TIMSK0 |= (1<<OCIE0A);
sei();
}
void loop() {
}
I've modified the ISR without testing it, hence the missing bracket; before I had a simple if-else counter there.
The volatile doesn't seem to work; it just turns the LED on once and then doesn't do the toggling, hence why I think it doesn't call the interrupt correctly repeatedly but only once.
Maybe the settings it wrong and it has too long/too short period? Is it possible that after it's called once some flags are reset that disable further calls?
I still don't understand why the timer doesn't work but my problem with determining the timings of things in the system was thus resolved. In case anyone else encounters a similar issue, feel free to use this alternative.