Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

count pulse twice or thrice for one revolution-PIC

Status
Not open for further replies.

maheshece28

Junior Member level 2
Joined
Jun 13, 2013
Messages
24
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
249
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
Code:
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;
			}					
	}

Thanks
 

Hi,

You have to verify your IR sensor signal with a scope.

Best is a digital scope with digutal phosphor function.
Zoom in to the falling edge.....with another measurement zoom in to the rising edge.

Klaus
 

Most likely the problem is electrical as Klaus suggests but to some degree it depends on the speed of your processor so I'm assuming it is fast enough. The issue is almost certainly the output of the sensor isn't cleanly changing logic states as the light is interrupted. There will be a condition as the sensor is partially obscured when your black material isn't completely blocking the light and the edge of the pulse may oscillate. Your count input is probably seeing the oscillation and counting it's cycles. Depending on your circuit, it might need nothing more than a small capacitor across the sensor output to filter out very short pulses but the sure way is to use the sensor output to trigger a monostable so a clean and minimum length pulse is produced for counting.

Brian.
 

Interrupt based counting is better. It is better to configure the controller for rising or falling edge triggering. And it is better to add an RC filter at the interrupt pin for eliminating any bouncing of signals


Lok
 

Although you have not faced to that issue up to now, your code is likely to lose counts. The variable pulse_cnt within the main() loop will be not incremented once in awhile, due to be sequentiated by the printf() function, particularly for low baud rates. You should check PULSE variable and increment counts within the ISR vector.
 

Hi Guys,
Thanks for the replys. Is betwixt said about this? R=10k and C=100nF ceramic capacitor. My sensor output is 5v.

debounce.jpg
 

Hi,

Filter:
One R connection to the sensor, the other R connection to the input port.
One C connection to the input port, the other to GND.

*******
But you could filter in software also.
With 200 rpm you get about 6.7 pulses per second.
For a duty cycle range 30% to 70% it is sufficient to read the input 25 times per second.
If you do this, then it is very unlikely to count erroneous pulses. (Even without RC filter)
Here an interrupt solution is recommended.
*********

But in your case counting pulses is slow, you need to wait 60 seconds for the next update..
Thereford i recommend to use atime measurement solution:
Here it is essential to use the RC filter.
Use a counter running at 1kHz. (I selected this frequency to get about the same resolution as your method. Adjust it as you like.)
Measure the counter ticks from one rising sensor edge to the next rising edge.
(In one minute = 60 seconds you expect 60 x 1000 ticks = 60000 ticks.)
To get the RPM you just have to calculate: rpm = 60000/counter_ticks

If you don't want to divide, then you may use a lookup table.

******
Example: if you count 250 ticks (from one rising sensor edge to the next rising) then
Rpm = 60000 / 250 = 240 RPM

Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top