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;
}
}
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;
}
}