Strange ISR timing


hello guys,
i've set isr routine, , wanted measure time elapsed between 2 consecutive isrs. i've written program
code: [select]

void setup(){
 //set pwm: fastpwm, oc2a top, oc2b sets @ bottom, clears on compare
 //com2b1=1 sets pwm active on pin3, com2b1=0 disables it
 serial.begin(9600);
 pinmode(3, output);
 pinmode(11, output);
 tccr2a = _bv(com2a0) | _bv(wgm21) | _bv(wgm20);
 tccr2b = _bv(wgm22) | _bv(cs21);
 ocr2a = 49; // gives 20khz pwm on pin11
 ocr2b = 24; // gives 40khz pwm on pin3
 timsk2 |= _bv(toie2); //activate interrupt on overflow (on counter = ocr2a)
 
}

[other stuff here]

isr(timer2_ovf_vect){
  timer++;
  = micros();
  if(timer==2){
    serial.println(now-previous);
    timer=0;  
  }
  previous = now;
}


curious results
code: [select]
20
12
20
12
24
20
12
20
12
24
20
12
20
12
24

it looks 20,12,20,12,24 repeated many times, , have no idea why.

a simpler code such one:
code: [select]

isr(timer2_ovf_vect){
 serial.println(micros());
}


outputs this:
code: [select]
1236
1332
1428
1524
1620
1716
1812

from code looks 96usec pass between 2 consectuive isrs. how strange.

according calculations, isr should executed every 25usec, because 1/40 000 = 0,000 025.

do have idea why these strange results? how can calculate time elapsed precisely?

a serial.print very slow thing, , not put 1 inside 1 isr.


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Strange ISR timing


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums