Mismatch 1Mhz on Oscilloscope by TIMER-1 normal mode

Hi Experts,

I’m learning “TIMER” recently, and trying to verify the signal by Oscilloscope to see if the code are right. Unfortunately, they are NOT the same. Can anyone kindly let me know where the problem is in the code, or have something coding wrong else?
By the way, I knew there are more examples on this purpose with “PWM setting” and “enable overflow interrupt setting”, but I just want to know what is the difference.

const int pin = 3;

void setup() {
  
  pinMode(pin,OUTPUT);
  TCCR1A = 0x00;                // Normal mode, just as a Timer
  TCCR1B = 0x01;               // no prescaler
  TCNT1 = 0;
}

void loop() {
  digitalWrite(pin,HIGH);
  delays();
  digitalWrite(pin,LOW);
  delays();
  
}
void delays(){
  while(TCNT1<=15){        // Would like to have 1Mhz, 
  }
  TCNT1=0;
}

I think you are trying to delay for 1 microsecond? That would give a period of 1 + 1 microseconds = a 500kHz signal.

But here is something else for for you to try:

while (1)
{
   digitalWrite(pin,HIGH);
   digitalWrite(pin,LOW);
}

What frequency does that give you?

So, what does that tell you?

Speed it up, digitalWrite is slow:

while (1)
{
   PIND = PIND | 0b00001000; // toggle D3 by writing to input register
}
void delays(){
  while(TCNT1<=15){        // Would like to have 1Mhz,
  }
  TCNT1=0;
}

In the ATMega data sheet, read up on the Clear Timer on Compare Match (CTC) mode of Timer1, which does this in hardware.

CrossRoads:
Speed it up, digitalWrite is slow:

while (1)

{
  PIND = PIND | 0b00001000; // toggle D3 by writing to input register
}

Better would be

while (1)
{
   PIND = 0b00001000; // toggle D3 by writing to input register
}

ozcar:
I think you are trying to delay for 1 microsecond? That would give a period of 1 + 1 microseconds = a 500kHz signal.

But here is something else for for you to try:

while (1)

{
  digitalWrite(pin,HIGH);
  digitalWrite(pin,LOW);
}




What frequency does that give you?

So, what does that tell you?

Thanks for the point, I capture the digitalWrite waveform by scope, the speed is bigger than 1 (us). The speed around is 8 us as attachment. So, that’s why the 10 us was observed in my codes.

CrossRoads:
Speed it up, digitalWrite is slow:

while (1)

{
  PIND = PIND | 0b00001000; // toggle D3 by writing to input register
}

Thanks for suggestion and solution. So, setting register is the better way to get the extreme data, right?