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.

Need logic/code for engine speed measurement using PIC18f458

Status
Not open for further replies.

csr1981

Member level 2
Joined
May 10, 2005
Messages
52
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Location
India
Activity points
1,645
pic18f458

Can anybody give me the logic/code for measurement of engine speed using PIC18f458. for this i have square wave input to the microcontroller for the engine speed. I need to convert these incoming square wave pulses to speed(rpm).
the frequency of the square wave varies as the speed varies.
 

Re: PIC18f458

Hello csr1981
This topic might steer you in the right direction:

The solution I explained there applies to PIC16F874 (what I used when I did RPM measurements) but the same principle can be used on most PIC18F MCUs as well.
 

Re: PIC18f458

Hi jumper2high


I went through that post, but didnot get clear idea.

Can you provide the code or the logic for the same, it would be great.

Thanks.
 

Re: PIC18f458

I couldn't help you with the actual code (since different PICs have different ways of doing the same thing).

Essentially, if you have a square-wave signal that represents the motor rotation (1 pulse = 1 full rotation) all you need to do is count the number of times the signal goes from low to high while timing one second with the timer. Once that's done, you multiply the number of pulses by 60 and you've got RPM (Rotations Per Minute). There is a small flaw with this way of doing things; firstly, you are counting pulses for one whole second, which means the update rate (of the RPM) isn't much, and you are multiplying the result by 60 which means if you mistake the pulse count by only 1, the resulting RPM difference will be 60 rpm.

If you had a motor that made 10 pulses per one rotation per minute, you would achieve better precision (and would be able to reduce the counting time to only half a second or less while still maintaining satisfactory precision).


Another method of calculating speed of a motor is to utilize the CCP (Capture/Compare/PWM) module along with a 16-bit Timer. If you set up the CCP to "Capture" the Timer when a rising edge occurs, store it somewhere, then once another rising edge occurs, you subtract the stored value from the new captured value of the Timer, and you'll know exactly how much time has passed between two pulses, which means you'll know exactly the time it takes to do one full rotation. Simple mathematics will then allow you to calculate the actual RPM.


Ex:

Pulse #1 Timer1 captured value = "16000"
waiting for another pulse
Pulse #2 Timer1 captured value = "36000"

or

Pulse #1 -> Timer is Reset
waiting for another pulse
Pulse #2 -> Timer captured value = "20000"

Now, given that we're using a 4Mhz clock, we have a timer difference of 20.000 microseconds, which is 20ms. That means the motor needs 20 miliseconds to do one rotation. If we know that, we know the motor will do 50 rotations in one second, and 3000 rotations in one minute.

The only limitation for this method is the RPM range. You can't measure very slow speeds (unless you implement further steps in counting Timer overflows). The maximum "time" the Timer can count is 65536 microseconds (without a prescaler). That means you wouldn't be able to measure speeds below 1000rpm without adding a prescaler (losing precision) or adding more code to cater for the overflow counting. The maximum RPM you could measure this way with a 4Mhz clock goes beyond the speeds any physical motor/fan could achieve, so that's not something to worry about :)
 
  • Like
Reactions: zeus38

    zeus38

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top