# Measuring RPM of a motor using a Pic?

#### spinnaker

Joined Oct 29, 2009
7,830
What is the best way to measure the RPM of a motor using a Pic?

I already have the sensor working and I can count the number of times the motor rotates. What I am trying to figure is the best way to turn that into RPM.

Best I can figure is to set a timer for a given period. When that period expires, determine the RPM and reset the counter.

Is there a better way?

Joined Jul 18, 2013
28,824
I have used a slot photo sensor and the retro-reflective sort.
I started with this principle and expanded it.
I used the CCP capture input.
I have the code for this version also.
Max.

#### Attachments

• 1 MB Views: 85

#### spinnaker

Joined Oct 29, 2009
7,830
Thanks Max. I was thinking CCP but I was not sure. Any chance that code is in C? If so could you post the relevant portion?

Or can you point me to what I should be reading in the datasheet (just in general).

#### Picbuster

Joined Dec 2, 2013
1,049
The simplest way: connect sensor to interrupt line and measure number of pulses between the int's.

if (INT1IF)

{
INT1IF=0;
Timerflag=!Timerflag;
}

// run tmro fast as possible

If (!Timerflag)
{
speed++;
}
Else
{
Rp=speed;
Speed=0;
}

In main
calculate from Rp and tmr0 the rpm. and take your time look once a second or so to calculate.

Joined Jul 18, 2013
28,824
Any chance that code is in C? If so could you post the relevant portion?

Or can you point me to what I should be reading in the datasheet (just in general).
Sorry, I pgm in Assembly and the code is in it.
The pic sheet usually gives an example of Capture, the advantage of using a 32khz xtal as in the example is that the timer roll over is exactly 1 sec. or fractions of.
As shown in the example code.
Max.
'

#### ScottWang

Joined Aug 23, 2012
7,413
The industrial will using the encoder to be the input sensor, you can set the timer counting to 3 seconds and the numbers of counter of encoder will be the base number Num_Encoder, the rpm = Num_Encoder * 20.

#### spinnaker

Joined Oct 29, 2009
7,830
Sorry, I pgm in Assembly and the code is in it.
The pic sheet usually gives an example of Capture, the advantage of using a 32khz xtal as in the example is that the timer roll over is exactly 1 sec. or fractions of.
As shown in the example code.
Max.
'
I have a 16MHZ xtal for the mcu clock. But I have a 32k xtal for the RTC. Guess I need to look at the datasheet to see if that can be leveraged.

Joined Jul 18, 2013
28,824
What is the pic type?
Max.

#### spinnaker

Joined Oct 29, 2009
7,830
18F26J53

Joined Jul 18, 2013
28,824
Typically the 32Khz is across T1OSO and T1OSI. (TMR1).
Max.