P
P
Phoenix322015-08-16 16:44:50
Arduino
Phoenix32, 2015-08-16 16:44:50

How to count impulses for a period of time?

Hello. Interested in this question. I want to make a digital tachometer, I don’t want to count the pulse period. I want to try to count the number of pulses for a certain period of time, for example 100 milliseconds. How can this be implemented?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Armenian Radio, 2015-08-16
@gbg

Atmega has at least two timers. Set the first timer to work from an external clock source with a 1:1 divider, set the second timer to work from the controller clock generator with a divider that suits you (for example, 1:64 is suitable for megahertz) and activate an overflow interrupt.
In the handler of this interrupt, look at the value that the first timer counted.

A
Alexander Gusev, 2015-08-16
@Sanchogus

Sketches of a clumsy bad option, I will not say anything about accuracy and performance
(they strongly depend on the frequency of measurements and the duration of the measurement, we also believe that no interference is flying to the input):

int time_stop; //время прекращения измерений
int time;//для текущего времени
int count;//счетчик
int t1,t2;//для определения того, что прошел импульс
//t1 - текущее, t2 - предыдущее состояния

count=0;//обнуляем всё
t1=0; t2=0;
time=millis();//считываем текущее время
time_stop=time+100; //через сколько прекратить замер в данном случае +100мс

while(time<=time_stop)//пока время меньше времени останова
{
t1=digitalRead(PINx);//читаем какой-то пин
if(t1!=t2)//если t1 и t2 не равны
  {
  if(t1>t2)count++;//если t1>t2 (т.е. передний фронт), то увеличим счетчик
  t2=t1; //предыдущее состояние = текущему
  }
time=millis();//обновляем текущее время
}

In count at the end should get something very close to the number of pulses.
Of the minuses - the cycle eats time, not allowing anything else to be done at this moment.
If you only count, then display it, then maybe it will work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question