40 kHz square wave with timer0

Hi, I'm trying to generate a 40 kHz square wave with timer0 but can not get the pin5 goes well, I get double the pin6, which if successful.
Thanks for the help and best regards, I am a beginner.
This is my code:

void setup()
{
  DDRD = 0b01100000; 
  TCCR0A = _BV(COM0A0)| _BV(COM0B1) | _BV(WGM01) | _BV(WGM00) ;
  TCCR0B = _BV(WGM02) | _BV(CS00);
  OCR0A = ((F_CPU / 2) / 40000L) - 1;// 199
  OCR0B = OCR0A/2 ;
}

void loop()
{}

2 things.

  1. I wouldn't do this on Timer0, that's what the Arduino uses for millis() timing. Use timer 1 or 2 instead.

  2. You are setting it to the wrong waveform generation mode. You appear to want CTC mode, but are in fact setting Fast PWM. Get rid of "| _BV(WGM00)".

Jiggy-Ninja perfect, thank you very much and you could guide me how to start to count pulses?, ie, I want to count how many pulses are emitted in a given time

This will give you some ideas:

I'll look, thanks Larry0