ATmega328 Timer_2 Problem


i've tried program timer 2 setting in atmega 328 there problem. expect word "interrupt..." printed every 1 second. arduino duemilanove runs @ 16mhz, atmega 328 has 10 bit adc , timer 2 runs @ 8 bit. number of timer_2 overflows, use formula:

1 / ( 16000000 / (1024*256) ) = 1 / 61

so means arduino timer 2 has 61 overflows per second. not case.  the word "interrupt..." printed out every 2 seconds.

below code

code: [select]


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

#define init_timer_count 0
#define reset_timer2 tcnt2 = init_timer_count

int int_counter = 0;

// aruino runs @ 16 mhz, have 61 overflows per second...
// 1/ ((16000000 / 1024) / 256) = 1 / 61
isr(timer2_ovf_vect)
{
 int_counter += 1;
 if (int_counter == 61)
 {
   int_counter = 0;
   
   showdata();
 }
};

void showdata()
{
     serial.print("interrupt...");
}

void setup() {
 
 serial.begin(9600);
 
 //timer2 settings:  timer prescaler /1024
 tccr2b |= ((1 << cs22) | (1 << cs21) | (1 << cs20));
 
 //timer2 overflow interrupt enable
 timsk2 |= (1 << toie2);
 
 reset_timer2;
 
 sei();
}



any suggestion , kindly appreciated. thanks! :)

perhaps prints every other second because takes while print, , missing interrupt because bust printing, , interrupts disable in interrupt service routine.

serial.print in isr not recommended. set flag indicates needs printed, , then, in loop, see if flag set, and, if is, printing.


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > ATmega328 Timer_2 Problem


arduino

Comments