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