Ayne
New Member
Microcontroller = PIC16F877A
Compiler = MikroBasic
Output on PC through SerialPort (cuz i have not LCD)
Crystal Frequency = 20MHz
Instruction Cycle Time = 200 nano Seconds
For lower Speed, I will do: Measuring the Period of a
Square Wave
For Higher Speed, I will do: Measuring the Period of a
Square Wave with Averaging
First About Hardware:-I am using a comparator LM393.
**broken link removed**
As light strike on PhotoDiode, comparator's output goes LOW to HIGH. We will count time periode b/w two Egdes and measure RPM according to it.
Second: About Method of Measuring RPM.
PIC will be in Capture Mode.
It will capture Time b/w two edges and convert this time into RPM.
I am using TIMER1 with 1:8 Prescale value.
Instruction Cycle Time = 200 nano Seconds.
TIMER1 value = 1 means 1.6 microSeconds has been elapsed because of 1:8 PreScaler. Am i right!!!
Now some math:
x is the time periode "T". x is the time b/w the two Edges and it's value in 1.6 microSeconds
**broken link removed**
Now Program
This Program has been compiled. Not Tested yet on PIC.
Am I Right!!!
Muhammad Ahmed.
Compiler = MikroBasic
Output on PC through SerialPort (cuz i have not LCD)
Crystal Frequency = 20MHz
Instruction Cycle Time = 200 nano Seconds
For lower Speed, I will do: Measuring the Period of a
Square Wave
For Higher Speed, I will do: Measuring the Period of a
Square Wave with Averaging
First About Hardware:-I am using a comparator LM393.
**broken link removed**
As light strike on PhotoDiode, comparator's output goes LOW to HIGH. We will count time periode b/w two Egdes and measure RPM according to it.
Second: About Method of Measuring RPM.
PIC will be in Capture Mode.
It will capture Time b/w two edges and convert this time into RPM.
I am using TIMER1 with 1:8 Prescale value.
Instruction Cycle Time = 200 nano Seconds.
TIMER1 value = 1 means 1.6 microSeconds has been elapsed because of 1:8 PreScaler. Am i right!!!
Now some math:
x is the time periode "T". x is the time b/w the two Edges and it's value in 1.6 microSeconds
**broken link removed**
Now Program
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[2]
Dim Capture As word absolute $15
Dim RPM, Period As Longint
'sub procedure procedure_name ' procedures declaration
sub procedure interrupt
if TestBit(PIR1,CCP1IF) = 1 then 'Capture mode Interrupt Flag
IF i > 1 Then
i = 0
End if
T[i] = ( (Over_Flow * 65535) + Capture )
Inc(i)
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_313
Delay_ms(313)
End Sub
'end sub
'sub function function_name ' functions declaration
'end sub
main: ' main program body
Delay_313 'For Stability in Supply
TRISC = 255 ' PORTC all Inputs
PORTC = 0 ' PORTC = 0
T1CON = %00110000 'TIMER1 CONTROL REGISTER
CCP1CON = %00000101
INTCON.GIE = 1
INTCON.PEIE = 1
Averaging = 0
USART_init(19200) ' initialize USART module
Reset_All:
PIE1.CCP1IE = 0 'CCP1 Interrupt Enable bit
T1CON.TMR1ON = 0 'Timer1 On bit
TMR1H = 0
TMR1L = 0
Over_Flow = 0
ReStart:
T1CON.TMR1ON = 1 'Timer1 On bit
PIE1.CCP1IE = 1 'CCP1 Interrupt Enable bit
Delay_313 'Aquire The values of T0 and T2
Convert_RPM:
Period = T[1] - T[0] 'Subtract saved captured time (T0) from
'captured time (T1) and store
IF Averaging = 1 Then 'if CCP1CON = every 16 edge the divide by 16
Period = Period / 16
End IF
IF Period < 75000 Then ' If RPM > 500 then next time
CCP1CON = %00000111 ' Caculate every 16th Egde
Averaging = 1 ' and Measure RPM with Averaging
else ' IF RPM < 500 then next time
CCP1CON = %00000101 ' calculate every rising edge
Averaging = 0 ' Measure RPM without Averaging
End If
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 Reset_All
end. ' end of program
Am I Right!!!
Muhammad Ahmed.