
Originally Posted by
Bruce
With both PICs running at 20MHz, CCPR1L & CCPR1H should have the same values - since it's all done in hardware with the capture module capturing the value of timer1 after the
4 rising edges.
Yes the captured value is stored in CCPR1L:H, but the CCP module does not clear Timer1. You have to do that. And it's not being cleared until after the math.
Then it goes back up to Loop: and clears it again, without any reference to the incoming signal. (actually, it is referenced to the last capture plus math time).
If you only clear it once, immediately after the capture, then it will be ready for the next capture.
The first capture is always wrong, and must be discarded.
Code:
Loop:
TMR1H = 0 'Clear 8-bit register
TMR1L = 0 'Clear 8-bit register
capture = 0 'Clear Timer
Start:
If Capture = 0 then
Goto Start
endif
T1CON.0=1
Capture = 0
CaptureLoop:
If Capture = 0 then
Goto CaptureLoop
endif
T1Con.0=0
period.LowByte=CCPR1L
period.HighByte=CCPR1H
RPM = 10000
RPM = RPM * RPM ' 100,000,000
RPM = DIV32 period ' 100,000,000 / RevCount
RPM = RPM * 60 ' Per minute
RPM = DIV32 400
RPM = (RPM*5)
SEROUT2 LCD,84, [Prefix,CursorPS,20,"RPM ",dec5 RPM, " ", DEC CCPR1L, " ", dec CCPR1H]
Gosub ClearTimer1
Goto Loop
Added: I guess if Timer1 was Stopped in the ClearTimer1 routine (or at Loop: ), what you have would work better, but it will only capture every other 4-pulses. (8-pulses total)
Bookmarks