Problem in counting pulse input

Thread Starter

scud123

Joined Mar 20, 2014
4
I'm trying to find RPM of a dc motor using PIC24F microcontroller. I've 200 RPM DC motor and IR sensor. I'm counting the pulse input from IR sensor when black material which attached with DC motor shaft contact with IR sensor. I'm printing pulse count for every 60 seconds using PIC timer interrupt.I don't want to use any delay inside. Each pulse count for each revolution respectively, but the problem is sometimes pulse count twice or thrice has happening for one revolution.

CODE:
When timer interrupt reaches 60 seconds i set count_clr=1

while(1)
{
if(PULSE==0 && state==0)
{
pulse_cnt++;
printf("%d\n",pulse_cnt);
state=1;
}
else if(PULSE==1 && state==1)
{
state=0;
}
if(count_clr)
{
printf("Total Pulse:%d\n",pulse_cnt);
pulse_cnt = 0;
count_clr=0;
}
}
 

MaxHeadRoom

Joined Jul 18, 2013
20,081
I have used the timer capture mode of the Pic for this, the first pulse clears the timer the second pulse is captured and the timer interval read.
There are all kind of examples on the pic site, including some in the Mechatronics board section.
Max.
 
Top