# Solved : count pulses with timer1 or timer2

I have a basic program that generates a frequency of 40 kHz with timer0 in pin5 and 6 and I would like to count 300 pulses and the time it takes to generate 300 pulses. If you could help me would be very grateful. a greeting

The time it takes to generate 300 pulses at 40 kHz…
( 300 pulses / 40000 pulses per second ) * 1000 milliseconds per second = 7.5 milliseconds

No code necessary. Just basic unit conversion.

You are generating the pulses so you can count them by incrementing a variable as each one is produced. As to the time taken to produce 300 pulses, save micros() before you start generating the pulses and save it again when the pulse counter reaches 300. Take the start value from the end value and that is the elapsed time.

This sounds dreadfully simple. Post your current program here for more help.

This would be my code: All he does is to generate a square wave at 40 kHz with timer0 on pin 5 and 6

``````void setup()
{
Serial.begin(9600);

DDRD = 0b01100000;
TCCR0A = _BV(COM0A0)| _BV(COM0B0) | _BV(WGM01)  ; //modo ctc
TCCR0B = _BV(WGM02) | _BV(CS00);
OCR0A = ((F_CPU / 2) / 40000L) - 1;
OCR0B = ((F_CPU / 2) / 40000L) - 1;
}

void loop()
{}
``````

You can, as it happens, use pin 5 as a count input to timer1 (this is a hardwired pin assignment on the ATmega328). But its rather unnecessary as the timers are accurate w.r.t. the system clock.

Reprogramming timer0 to generate 40kHz will foul up the behaviour of millis(), micros() and delay(), its best to disable timer0 interrupts if doing this using

``````TIMSK0 = 0 ;
``````

to stop 40,000 interrupts a second happening.

Then understand that it is better to generate a square wave with another timer 40 khz than zero right ?. I have generated a square wave with timer2 and would be something like:

``````void setup(){
pinMode(3,OUTPUT);
pinMode(11,OUTPUT);
TCCR2A = _BV(COM2A0)| _BV(COM2B0) | _BV(WGM21)  ; //modo ctc
TCCR2B = _BV(WGM22) | _BV(CS20);
OCR2A = ((F_CPU / 2) / 40000L) - 1;// 199
OCR2B = ((F_CPU / 2) / 40000L) - 1;
}
void loop(){}
``````

but not how to start counting pulses and time to generate these pulses, eg 100 pulses

Can anyone help? Does it not occur to anyone anything?

Thank you

For those in need, I’ve already solved. This would be the code:

``````//const int hardwareCounterPin = 5;   // Cuenta en el pin5 a la fuerza
//TIMER2 PARA GENERAR ONDA 40 KHZ PIN3 Y 11
//TIMER1 PARA CONTAR PULSOS EN EL PIN5

const int tiempo = 7;   // tiempo en milisegundos
unsigned int numeropulsos;

void setup()
{
Serial.begin(9600);

pinMode(3,OUTPUT);
pinMode(11,OUTPUT);
TCCR2A = _BV(COM2A0)| _BV(COM2B0) | _BV(WGM21)  ; //modo ctc
TCCR2B = _BV(WGM22) | _BV(CS20);
OCR2A = ((F_CPU / 2) / 40000L) - 1;// 199
OCR2B = ((F_CPU / 2) / 40000L) - 1;

TCCR1A=0;        // Reseteamos timer1

}

void loop()
{

// Empezamos a contar
TCCR1B = bit(CS12) | bit(CS11); //Activamos contador en timer1 en flanco de bajada
delay(tiempo); // Sin esto no funciona

TCCR1B = 0;

numeropulsos = TCNT1;
TCNT1 = 0;  // Reseteamos el contador hardware
if(numeropulsos > 0)
Serial.println(numeropulsos);
}
``````

A greeting.

``````   delay(tiempo); // Sin esto no funciona
``````

Can you explain what this is for as the comment says that it does not work ?

With delay I hope the time you want to count, if no account remove the delay, it is logical. Maybe it's not a very wise comment

someone would know how to do it with interruptions?