frequency counter pic basic
HERE IS THE CODE THAT MEASURES FREQUNCY AND RPM AS WELL
'****************************************************************
'* Name : UNTITLED.BAS *
'* Author : SYED AYAZ AHMED *
'* Notice : Copyright (c) 2009 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 2/7/2009 *
'* Version : 1.0 *
'* Notes : *
'* : *
'****************************************************************
Device 16F870
'BOOTLOADER On
XTAL 20
Declare WATCHDOG TRUE
'Config CPD_ON, CP_ALL
Declare LCD_TYPE 0
Declare LCD_DTPIN PORTC.0
Declare LCD_RSPIN PORTC.4
Declare LCD_ENPIN PORTC.5
Declare LCD_LINES 2
' ** Declare the flags of the INTCON register **
Symbol RBIF = INTCON.0 ' RB Port Interrupt Flag
Symbol INT0IF = INTCON.1 ' RB0 External Interrupt Flag
Symbol TMR0IF = INTCON.2 ' TMR0 Overflow Interrupt Flag
Symbol RBIE = INTCON.3 ' RB Port Change Interrupt Enable
Symbol INT0IE = INTCON.4 ' RB0 External Interrupt Enable
Symbol PEIE = INTCON.6 ' Peripheral Interrupt Enable
Symbol GIE = INTCON.7 ' Global Interrupt Enable
Symbol T0IF = INTCON.2 ' Timer0 Overflow Interrupt Flag
Symbol LED_PORT = PORTB.7
'
DelayMS 200
Cls
ALL_DIGITAL=true
PORTB_PULLUPS=true
TRISB = %00000001 ' Set bit 2-7 to outputs, bit 0 input
Symbol Tmr0ReloadVal = 100 ;256 - 200
Symbol RPM_In = PORTB.0
Dim DISPVAL As DWord
Dim Hz2Counter As DWord
Dim FREQ As DWord
Dim RPMRaw As DWord
Dim DLY As Byte
Dim ONES As Byte
Dim TENS As Byte
Dim HUNDREDS As Byte
Dim THOUSANDS As Byte
Dim TIMESHARE As Byte ' Used by the interrupt for time sharing
Dim RefreshReq As Bit
' Set TMR0 to interrupt on rollover
OPTION_REG = %00000100 ' Set the prescaler to 1:32
INTCON = %11110000 ' Enable TMR0 overflow interrupt and ' Assign TMR0 clock to internal source and interrupt on RB0
TMR0 = Tmr0ReloadVal
; While GIE = 1 : GIE = 0 : Wend ' Make sure global interrupts are off
Clear
Input RPM_In
'
GoTo OVER_SUBS ' Jump over the subroutine
'
On_Interrupt GoTo MULT_INT ' Point to the interrupt handler and ENABLE global interrupts
'---------------------------------------------------------------------------------------
' INTERRUPT HANDLER
MULT_INT:
If INT0IF = 1 Then ' If we have an interrupt from INT0 then we have an RPM pulse
Inc RPMRaw ' so up our interrupt counter
Clear INT0IF ' Clear the interrupt flag now
GoTo IntExit ' exit
EndIf
If T0IF = 1 Then
TMR0 = TMR0 + Tmr0ReloadVal ' reload with the nearest we can get to 1ms interrupts
Else
GoTo IntExit ' not a INT0 interrupt? Bad thing but better exit anycase
EndIf
Inc Hz2Counter ' Every 1 of a second we will read the FREQUENCY
If Hz2Counter = 1000 Then
LED_PORT = LED_PORT ^ 1
FREQ = RPMRaw
RPMRaw = 0
Hz2Counter = 0
RefreshReq = 1
EndIf
Clear T0IF ' Reset TMR0 interrupt flag
IntExit:
Context Restore ' Exit the interrupt
'---------------------------------------------------
OVER_SUBS:
While 1 = 1
If RefreshReq >= 1 Then
DISPVAL = FREQ * 60 ' Do the maths to convert it to RPM
'GoSub Build_Digits
Toggle LED_PORT 'TO MAKE ENSURE PIC IS RUNING
Print At 1,1,Dec DISPVAL," RPM "
Print At 2,1,Dec FREQ," Hz "
RefreshReq = 0
EndIf
Wend