I am trying to use the Timer0 overflow interrupt to make a little project. I used such a timer in the ATtiny45 and it worked, but with ATtiny10 no result.
So I tried an easy LED blinking program and even that didn't work.
Here is the simple code:
#define F_CPU 1000000UL // 1 MHz
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/delay.h>
volatile uint16_t overflow_val=0;
ISR (TIMER0_OVF_vect){
overflow_val++;
}
int main(void){
DDRB = (1<<PB1);
// Timer0 normal mode
TCCR0A = 0x00;
// Start timer with presc 1:1024
TCCR0B = (1<<CS02) | (1<<CS00);
TCNT0 = 0;
TIMSK0 |= (1 << TOIE0);
sei();
for(;;){
if(overflow_val >= 1)
{
if(TCNT0 >= 145){
PORTB ^= (1 << PB1);
TCNT0 = 0;
overflow_val = 0;
}
}
}
return 0;
}
Hope someone can help me. Thanks!
EDIT1:
I changed line
TCCR0B = (1<<CS02) | (1<<CS00);
to line
TCCR0B = (1 << CS01); // clk/8
in my code to achieve an overflow every 0.524sec. But the LED is not blinking at all.
EDIT2:
Here is the size:
>avr-size test_attiny10_2.ino.elf
text data bss dec hex filename
138 0 2 140 8c test_attiny10_2.ino.elf
EDIT3:
I changed my if statement in the main as follows:
int main(void){
...
TCCR0B = (1 << CS01);
...
for(;;){
if(TCNT0 >= 0xFFFE)
{
PORTB ^= (1 << PB1);
TCNT0 = 0;
}
}
return 0;
}
Now my LED is flashing all 0.524 seconds as it should!
So something is wrong with my volatile unit16_t variable overflow_val. Not only i tested it directly on hardware, but did also a simulation with atmelstudio. Also in the simulation the program is never entering the if statement if i use the variable overflow_val to toggle the LED.
EDIT 4
I think something is not right with my ISR fkt. I testet the following code and i never got the LED switched on.
#define F_CPU 1000000UL // 1 MHz
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
volatile uint8_t ovf_counter = 0;
void ioinit(void){
// PB1->outputs
DDRB = (1<<PB1);
// Start timer with presc 1:64
TCCR0B = (1<<CS01) | (1<<CS00);
// Initialize counter Overflow at 0xFFFF
TCNT0 = 0;
// Enable global interrupts
sei();
}
int main(void){
ioinit();
TIMSK0 |= (1<<TOIE0);
while(1){
if(ovf_counter>=2){
PORTB = (1<<PB1);
ovf_counter=0;
}
}
}
ISR(TIMER0_OVF_vect){
ovf_counter++;
TCNT0 = 0;
}
i dont know what is wrong.
If i check in the while(1) loop the status of the counter register TCNT0 it works. So the counter is overflowing as it should, but it never enters on a overflow event the ISR(TIMER0_OVF_vect) function.
sei();
call does that. \$\endgroup\$