Thanks, when I insert the RPM part of code I get an error. Except for when I comment out the DIV32.
Code:
' Measuring signal pulse widths with capture module & interrupt
' Procedure for high-going pulse:
' 1. Configure CCP to capture on rising edge
' 2. Setup Timer1 so it will not overflow during max pulse width time
' 3. Enable ccp capture
' 4. Once capture flag bit is set, save captured value as T1
' 5. Reconfigure CCP to capture on falling edge
' 6. On 2nd capture, save 2nd value as PW
' 7. Subtract T1 from PW for the pulse width value
Prefix con $FE ' needed before each command
LcdCls CON $51 ' clear LCD (use PAUSE 5 after)
LcdLine1 CON $00 ' move cursor to line 1
LcdLine2 con $40 ' move curson to line 2
LcdLine3 con $14 ' move curson to line 3
lcdLine4 con $54 ' move curson to line 4
LcdCol1 con $00 ' move cursor to column 1
LcdCol2 con $07 ' move cursor to column 7
Backlight con $53 ' Backlighting 1-8
CursorPS con $45 'Cursor Position
CursorOn con $47 'Cursof On
CursorOff con $48 'Cursor Off
LCD VAR PortC.6 'LCD output
DEFINE OSC 20 ' 4MHz for 1uS resolution TMR1 counts
DEFINE INTHAND CCP_INT ' declare high-pri interrupt handler
Symbol Capture = PIR1.2 ' CCP1 capture flag
SYMBOL CapIE = PIE1.2 ' CCP1 interrupt enable bit
SYMBOL CapPriEn = IPR1.2 ' priority enable bit for CCP1 interrupt
SYMBOL PriEnable = RCON.7 ' set to enable priority levels on interrupts
T1 VAR WORD BANKA SYSTEM ' 1st capture value
PW VAR WORD BANKA SYSTEM ' 2nd capture value & ultimately final pulse width
CF VAR BYTE BANKA SYSTEM ' indicates when last capture is ready
RPM var word
Period var word
CLEAR ' clear RAM on POR
TRISC.2 = 1 ' CCP1 input pin (Capture input on 18F242)
INTCON = 0 ' Interrupts off for now
GOTO Init ' jump over interrupt handler
ASM
CCP_INT
BTFSS CCP1CON,0 ; capture from rising edge?
BRA Fall ; no .. goto falling edge
MOVFF CCPR1L, T1 ; get low capture byte into T1
MOVFF CCPR1H, T1+1 ; get high capture byte into T1
BRA IntExit ; outta here
Fall
MOVFF CCPR1L, PW ; get low capture byte into PW
MOVFF CCPR1H, PW+1 ; get high capture byte into PW
BSF CF,0 ; indicate last capture
IntExit
BTG CCP1CON,0 ; toggle between rising/falling edge captures
BCF PIR1,2 ; clear capture interrupt flag bit
RETFIE FAST ; return/restore W, STATUS and BSR
ENDASM
Init: ' initialize a few things first
CCP1CON = %00000110 ' Capture mode, capture on rising edge
'T1CON = 0 ' TMR1 prescale=1, clock=Fosc/4, TMR1=off
T1CON.7=1 'enable timer 16 bit `
T1CON.6=1 'Timer1 OSC
T1CON.5=1 '1:4 prescaler
T1CON.4=0 '1:4 prescaler
T1CON.3=0 'Timer1 OSC off
T1CON.2=0 'sychro clock
T1CON.1=0 'internal clock
TMR1H = 0 ' Clear high byte of TMR1 counter
TMR1L = 0 ' Clear low byte
PriEnable = 1 ' enable priority levels on interrupts
Capture = 0 ' clear capture flag bit
CapPriEn = 1 ' set CCP1 int to high priority
CapIE = 1 ' enable the CCP1 capture interrupt
INTCON = %11000000 ' global + peripheral ints enabled
T1CON.0 = 1 ' Turn TMR1 on here
Main:
' do other stuff here as required
IF CF.0 THEN ' figure out & print result only after last capture
PW = PW-T1 ' High pulse width = PW-T1
Period = PW
CF.0 = 0 ' clear flag bit
RPM = 10000
'RPM = RPM * RPM ' 100,000,000
'RPM = DIV32 period ' 100,000,000 / RevCount
RPM = RPM * 60 ' Per minute
'RPM = DIV32 1600
RPM = (RPM*5)
SEROUT2 LCD,84, [Prefix,CursorPS,0,#PW]
ENDIF
GOTO Main
END
Bookmarks