hi dears.
i try to write a code in atmel studio to make a real one second. actually i want to toggle one pin in each second.
i use atmega16 and 2MHz frequency. i want to divide by 64 prescaler. by formulas i calculate that timer should 122 time overflow and 18 clock to reach 1 second.
i decide to use overflow ISR and ctc mode. that mean i have a variable named overflow, i count overflowes and then compare OCR. i dont know if this is true or not.
my code is below but dont work truely. where is my fault.?
#define F_CPU 2000000 #include <avr/io.h> #include <avr/interrupt.h> volatile overflow; void init_timer0(){ TCCR0 |= (1 << WGM01)|(1 << CS00)|(1 << CS01); TCNT0 = 0; OCR0 = 18; TIMSK |=(1<<TOIE0); sei(); overflow=0; } ISR (TIMER0_OVF_vect){ overflow++; } void main(void) { DDRB|=(1<<0); init_timer0(); while (1) { if(overflow>=122){ if(TIFR&(1<<OCF0)){ PORTB ^=(1<<0); TCNT0=0; overflow=0; TIFR|=(1<<OCF0); } } }
1000ms/8.192ms=122.07 0.07*255=18 122 overflow + 18clock
}