Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

RPM Counter (Project Started PIC16F877A)

Status
Not open for further replies.
Personally I wouldn’t consider using a RPM meter in the manner you have. It is by far too open for errors from noise etc.

Sampling over a time period produces a rather accurate average of the signal. You can calculate the period and frequency quite easily afterwards, knowing that you are dealing with an average over a time frame - containing more than 1 sample.

A single pulse could be slightly deformed, or noise on the circuit could make it appear to go longer, giving a result that could be a couple of 100% out when dealing with fast signals. Circuits near or a part of motor circuits will be subject to a lot of noise thanks to the continuous EMF/EMI produced by the motor.

Sampling from PORTC on a PIC would be another great advantage, especially if signals are not square pulse, but rather ac/sine types. PORTC has built Schmitt triggers on every pin
 
Last edited:
Code:
program RPM_Counter


' variables declaration
Dim i, j, UART_Byte, Over_Flow, Averaging As Byte
Dim Digit as Byte[5]
Dim T As Longint
Dim Capture As word absolute $15
Dim RPM, Period As Longint

'sub procedure procedure_name ' procedures declaration
sub procedure interrupt               'On

 if TestBit(PIR1,CCP1IF) = 1 then     'Capture mode Interrupt Flag

 Inc(i)
 Select case i
 Case 1
   T1CON.TMR1ON = 1

 Case 2
  PIE1.CCP1IE  = 0
  T1CON.TMR1ON = 0
  T            = (Over_Flow * 65535) + Capture
  i            = 0
  TMR1H        = 0
  TMR1L        = 0
  Over_Flow    = 0

  End Select

  PIR1.CCP1IF = 0
 end if

 if TestBit(PIR1,TMR1IF) = 1 then     'TMR1 Interrupt Flag
  Inc(Over_Flow)
  PIR1.TMR1IF = 0
 end if

end sub

Sub procedure Delay_10
 Delay_ms(10)
End Sub
'end sub


'sub function function_name ' functions declaration

'end sub
main:                       ' main program body
 Delay_10                   'Delay 10 milisecond For Stability in Supply
 Delay_10                   'Delay 10 milisecond For Stability in Supply
 TRISC   = 255              ' PORTC all Inputs
 PORTC   = 0                ' PORTC = 0
 PIR1    = 0                'individual flag bits for the peripheral interrupts
 T1CON   = %00110000        '11 = 1:8 Prescale value, Timer1 On bit Disabled
 CCP1CON = %00000101        '0101 = Capture mode, every rising edge
                            '0111 = Capture mode, every 16th rising edge
 INTCON  = %11000000        'GIE, PEIE Enabled
 PIE1    = %00000001        'CCP1 Interrupt Disabled & TMR1 Overflow Interrupt Enabled
 Averaging    = 0

 USART_init(19200) ' initialize USART module

ReStart:
 PIE1.CCP1IE  = 1           'CCP1 Interrupt Enable bit

 do
 Delay_10

 if TestBit(PIE1,CCP1IE) = 0 then
 Goto Convert_RPM
 End if

 Inc(j)
 loop until j = 27

 Goto ReStart

Convert_RPM:
 Period = T

 RPM = 37500000 / Period   'Convert Time Period in Round/Minute

Into_Digit:
 Digit[0] = RPM Div 10000 mod 10
 Digit[1] = RPM Div 1000  mod 10
 Digit[2] = RPM Div 100   mod 10
 Digit[3] = RPM Div 10    mod 10
 Digit[4] = RPM           mod 10

UsartWrite:
   Usart_Write(0x52)                   ' "R" For indication of RPM
   For j = 0 To 4 step 1
   UART_Byte = Digit[j] + 48           ' Add 48 for converting into ASCII
   Usart_Write(UART_Byte)
   Next j
   Usart_Write(0x0D)                   'CR
   Usart_Write(0x0A)                   'LF

Goto ReStart
end. ' end of program

Above Program is runing
Complier MikroBasic.
Output on Serial Port (PORTC.6, PORTC.7)
Sensor Input on PORTC.2
_______________
Muhammad Ahmed
 
Poling.

Plz vote..

U Prefer??? for RPM Count and why.

1. Sampling over a time period

OR

2. Measuring the Period of a Square Wave



If i use No.1(Sampling over a time period) method then what should be the time for sampling???
 
Ayne said:
If I use No.1(Sampling over a time period) method then what should be the time for sampling???

The time period depends on the RPM your measuring, and a comfortable error factor if you miss one or two cycles each sample.

For example, your input is 100Hz, and you sample for 100mS, so you should receive 10 "pulses". But what if you started sampling mid-way through, then you could miss a pulse, or read an 'extra' pulse, for this example you "miss" a pulse - ie - you sampled 9 Pulses. Now you have to scale your result up by 10, and then by 60 to give RPM, as you only sampled 1/10th of a second.

Your result would be 5940 RPM (1% Error), But the correct answer should be 6000RPM. Notice that a single error at 100Hz with a 100mS sample rate will scale accordingly to your sample time.

The work around - use a longer sample time. In this case 500mS. Now the same thing happened - you missed a cycle for some reason (you read 499 samples instead of 500). Now your RPM will read 5988 (0.2% Error).

With a single sample, you could be out by much much more than 0.2% - 1% error, infact depending on the application, you could be out by near 100%, if not more on some samples.

By all means, if the signal is a clean square wave, and you know there is not going to be any noise or read errors, then single sample. But that’s not the case for most applications :(
 
Well done, to get steady readings like that where your error is less than 0.1% is fantastic.

Mike.
 
Is there anywhere I can get an example of this code in ass.? I'm trying to wrap my head around the "time between pulses/ tmr overun" subject, but its kind of difficult when its in an unfamiliar format. For now I don't really need any lcd or display, just how to get the result. I don't want a gimme, just a point in the right direction with a good description if at all possible. I haven't started implementing it any but it is the next step in my project.
 
Hi hotrodhed120.
Above described method is measuring the Time Periode B/W Two edges.
and doing some math with that time and knowing the RPM.

Where r u confusing on which step??

OR

What r u trying to do may i help u.
 
basically the same thing for RPM but I am writing in assembly.
I'm a bit fuzzy on the counting the tmr overrun to calculate frequency.
Or maybe I just need an overall description of what the program is doing step by step in plain engrish.? (assm.?)
I found microchips source code but I would like to understand what is actually going on and why as the program progresses, as I am a self taught newbie. Any help would be great.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top