Timer0 issue

Hi,

I am trying to set the timer0 prescalar to 1 and to set compare match register for 4MHz increments in order to obtain around .25us resolution at 24Mhz.
The code stops actually at first serial.print, it only prints “St” . Is something that i am missing in the code below in order to hold
these values in the array? i am storing the values only to have a rough idea of the resolution and repetability of the results.

unsigned long x[100];
extern volatile unsigned long timer0_overflow_count;
void setup()
{
Serial.begin(9600);
Serial.println("Start");
//Serial.println(OCR0A, BIN);
//Serial.println(TCCR0A, BIN);
//Serial.println(TCCR0B, BIN);
//Serial.println(TIMSK0, BIN);
}


void loop()
{
startQnano();
for(byte i = 0; i < 100; i++)
  x[i] = ((timer0_overflow_count << 8) + TCNT0);
endQnano();
for(byte i = 0; i < 100; i++)
  Serial.println(x[i]);
delay(1000);
}
void startQnano()
{
//set timer0 interrupt at 4MHz
cli(); // disable all interrupts
TCCR0A = 0;
TCCR0B = 0;
TCNT0 = 0;
// set compare match register for 4MHz increments
OCR0A = 5; // (24*10^6) / (4*10^6*1) - 1
// turn on CTC mode
TCCR0A |= (1 << WGM01);
// Set CS00 bits for 1 prescaler
TCCR0B |= (1 << CS00);   
// enable timer compare interrupt
TIMSK0 |= (1 << OCIE0A);
sei();//allow interrupts
}
void endQnano()
{
//set timer0 interrupt at 4MHz
cli(); // disable all interrupts
TCCR0A = 0;
TCCR0B = 0;
TCNT0 = 0;
// set compare match register for 4MHz increments
OCR0A = 0; // (24*10^6) / (4*10^6*1) - 1 (must be <256)
// turn on CTC mode
TCCR0A = 3;
// Set CS01 and CS00 bits for 64 prescaler
TCCR0B = 3;   
// enable timer compare interrupt
TIMSK0 = 1;
sei();//allow interrupts
}

What Arduino is this?

The Uno runs at 16MHz.

Hello Mike, Its a home made one, but i have tested the code also on a UNO@16MHz to see if the issue is frequency related and the behavior is the same

So you do know that:-

I am trying to set the timer0 prescalar to 1 and to set compare match register for 4MHz increments in order to obtain around .25us resolution at 24Mhz.

Is impossible on a 16MHz processor?

You are generating interrupts so fast you can't get in and out before the next one comes along. You only have 4 machine cycles between interrupts.

Even so, the counter for timer0 will not be incremented?

Not on time. And even then, the next IRQ runs immediately, repeat.

Get a Due?

no, i don't, i will try to slow it down by using a divider of 8 and see if it hepls

no, i don't

Not sure what you mean or if you know what I meant.

Due is an 84MHz Arduino. It will handle 0.25 us resolution if you don't try too much else. http://arduino.cc/en/Main/ArduinoBoardDue