what's going on in the background ?

so i set a timer on compare mode at 512 with no prescaler at 16mhz , so the timer is supposed to overflow every 32us .

the thing is , the timer overflows every 64us ? exactly double the time i need , what's going on ?

You need to show code. What timer? What is "compare mode"? How do you know when it is overflowing?

One possible answer is that you are measuring frequency with an oscilloscope. It takes two toggles of the pin to make one "period". First toggle: on cycle, second toggle: off cycle.

here is the interrupt init code

	TCCR1A = 0;		// set entire TCCR1A register to 0
	TCCR1B = 0;		// same for TCCR1B


	OCR1A = 512;           // set compare match register to desired timer count:
	TCCR1B |= (1 << WGM12);// turn on CTC mode:
	TCCR1B |= (1 << CS10); // no prescaler:
	TIMSK1 |= (1 << OCIE1A);// enable timer compare interrupt:

well what i did , i that in the ISR i did set a simple function that outputs a frequency relative to the compare match value . so 31250khz is the needed rate , but at OCR1A=512 i get a sound frequency of a rate of 15500 near the half of what i need . the thing is , when i get OCR1A to be equal to 256 . i get the needed frequency (well something around it) . i cant understand this .

btw the code in the ISR does not take more than a couple of microseconds

amine2:
btw the code in the ISR does not take more than a couple of microseconds

Are you sure?

I tried your timer configuration code and it works fine. I get a period of 32us.

the thing is .
at the beginning of the ISR , the device outputs the data for the frequency .. that takes a microsecond or a little less , then it does some processing that takes about 15 to 12 microseconds .

though at the beginning of the 15us part a set the timer to 0 , to record lost time . and the 15ms part only happons twice a second . weather that part happens or not is decided by a "if" condition

Instead of describing your code in words maybe it would be more helpful if you posted an impressionist style oil painting that expressed your emotions about it.

there you go , due to my limited painting skills that took a while . i hope this makes the situation more clear

ISR (TIMER1_COMPA_vect)
  {
  PINB |= bit (5);  // toggle D13
  }

void setup ()
  {
  pinMode (13, OUTPUT);
  TCCR1A = 0; // set entire TCCR1A register to 0
  TCCR1B = 0; // same for TCCR1B
  OCR1A = 512;           // set compare match register to desired timer count:
  TCCR1B |= (1 << WGM12);// turn on CTC mode:
  TCCR1B |= (1 << CS10); // no prescaler:
  TIMSK1 |= (1 << OCIE1A);// enable timer compare interrupt:
  }  // end of setup

void loop ()
  {
  }  // end of loop

I got a pulse width of 32 µs, but a frequency of 15.6 kHz. That's exactly what I described above. It takes two pulses for one period.

Nick, according to his painting (assuming that's really his work), he isn't toggling an output. He's just generating a compare interrupt every 512 timer ticks. So it should be 32us.

My interpretation of his art:

volatile unsigned long t0;

void setup()
{
  Serial.begin(115200);
  
  TCCR1A = 0;		// set entire TCCR1A register to 0
  TCCR1B = 0;		// same for TCCR1B

  OCR1A = 512;           // set compare match register to desired timer count:
  TCCR1B |= (1 << WGM12);// turn on CTC mode:
  TCCR1B |= (1 << CS10); // no prescaler:
  TIMSK1 |= (1 << OCIE1A);// enable timer compare interrupt:
}

ISR (TIMER1_COMPA_vect)
{
  t0 = micros();
}

void loop()
{
  long unsigned t1, t2;
  
  t1 = t0;
  while (t1 == t0) {}
  t1 = t0;
  while ((t2=t0) == t1) {}
  Serial.println(t2-t1);
  
  delay(500);
}

Ah I see.

lovely Jboyton ! :slight_smile: problem solved . thank you Nick