Maker Pro
Maker Pro

stuck in RPM measurement using pic 18f452

ansari haris

Apr 1, 2014
1
Joined
Apr 1, 2014
Messages
1
i simply want to measure rpm of a motor using simple infrared LED and photodiode. hardware is working fine but stuck in coding.can somebody please help me about coding
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Count the pulses you receive in a second.
 

gorgon

Jun 6, 2011
603
Joined
Jun 6, 2011
Messages
603
Or you can use the timer/ counter to count the ticks between the start-to-start flank of one or a number of pulses. Then calculate the elapsd time and divide it on a second. This is faster, and closer to realtime.
 

Anish

Feb 5, 2011
46
Joined
Feb 5, 2011
Messages
46
Use the CCP module in the PIC.
Look for rising edge (interrupt), change edge, start timer,
Look for falling edge (interrupt), change edge, stop timer.
This way you can measure the width of a pulse. Then use the method told by gorgon to find the rpm
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Use the CCP module in the PIC.
Look for rising edge (interrupt), change edge, start timer,
Look for falling edge (interrupt), change edge, stop timer.
This way you can measure the width of a pulse. Then use the method told by gorgon to find the rpm

Actually you would look for the timing between two rising edges or two falling edges in most cases because it's not the length of the pulse, the time between them. The time "between" them is not actually the interval between pulses, but the interval between like events on the pulses (e.g. it may be the time between the starts of the pulses)
 

gorgon

Jun 6, 2011
603
Joined
Jun 6, 2011
Messages
603
Normally there will be some jitter on the pulse train, and averaging the period time over some pulses will filter out the most prominent jitter.
Don't use too many samples since changes in RPM will then be delayed. It depends on the quality of your measurement setup. If your program monitor the stability of the values, and they are good, you can reduce the sample interval, and speed up the update. It all depends of the dynamic of the motor and the RPM changes.

As (*steve*) said measure the whole period as one sample.

In short the procedure is simple.
The timer system has a freerunning counter that can be read. You setup the interrupt service routine for interrupt at the positive or negative flank of your input, and log the counter at every interrupt. The value should be put into a small ringbuffer, and a new value flagged, or you can calculate the ticks directly. In your background program you process the values in the ringbuffer, and subtract the buffer value(n-1) from value(n). This gives you the number of counter ticks one period has lasted.
You know how fast your counter are running, from your setup of the timer unit, and you can calculate the time(p) of the period. If you have one period/rotation, your RPM = 60/time(p)(in seconds). If your time(p)=0.01s (10ms) your RPM is 60/0.01=6000
 
Last edited:
Top