Ok,
Super big thanks Henrik for helping me get this working on a 16F648A!
It seems to do ok, but of course I can’t leave well enough alone.
The 16F648A code does what I need, but has some “quarks” I assume are due to the polling. Like if I run the same signal into both capture pins, the times are different by a bit, which I would think comes from the fact one pin is getting polled later than the other, which is fine, I can live with it.
But wanting to learn more about interrupts and timers, I tried taking the code for 18F chips using Darrel’s instant interrupts that Bruce did in the post here: http://www.picbasic.co.uk/forum/show...se+measurement and combining it with the code that Henrik provided.
This is what I have so far for an 18F2320, 20MHz HS, 2.60C, All configs set properly (off , input pin, etc.):
Code:
DEFINE NO_CLRWDT 1
DEFINE OSC 20
DEFINE HSER_BAUD 9600
DEFINE HSER_TXSTA 20h
TrigOut VAR PortB.3
Time1 VAR WORD ' Time between leading edge of Pulse1 and leading edge of Pulse2
Time2 VAR WORD ' Time between leading edge of Pulse1 and leading edge of Pulse3
Measure VAR BIT ' Semaphore to indicate when we want to start a measurment.
OverFlows VAR BYTE ' Timer1 overflow total
INCLUDE "DT_INTS-18.bas" ; Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ; Include if using PBP interrupts
;----[High Priority Interrupts]----------------------------------------
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR0_INT, _SixtyHz, PBP, yes
INT_Handler CCP1_INT, _Capture1, PBP, yes
INT_Handler CCP2_INT, _Capture2, PBP, yes
INT_Handler TMR1_INT, _Timer1, PBP, yes
endm
INT_CREATE ; Creates the High Priority interrupt processor
ENDASM
CCP1CON = %00000101 ' Capture1 mode, capture on rising edge
CCP2CON = %00000101 ' Capture2 mode, capture on rising edge
T0CON = %11000111 ' Prescaler assigned to TMR0, 1:256 ratio
T1CON = 0 ' TMR1 prescale=1, clock=Fosc/4, TMR1=off (200nS per count @20MHz)
@ INT_ENABLE TMR0_INT ; Enable the 120Hz interrupt for the square wave generator.
Main:
Gosub Capture
IF T1CON.0 = 0 THEN ' If done, show result
hserout ["Timer Overflows = ",DEC OverFlows]
hserout [" Remaining ticks = ",dec time1]
hserout [" Remaining ticks = ",dec time2,13,10]
ENDIF
PAUSE 2000
GOTO Main
'-------------------------------------------------------------------------------
Capture:
Measure = 1
While Measure = 1 : WEND ' Wait until ISR has created the rising edge and started the timer
@ INT_ENABLE CCP1_INT ; Start new capture
Measure = 1
While Measure = 1 : WEND ' Wait until ISR has created the rising edge and started the timer
@ INT_ENABLE CCP2_INT ; Start new capture
RETURN
'-------------------------------------------------------------------------------
'---[CCP1 - interrupt handler]------------------------------------------
Capture1:
Time1.LowByte = TMR1L ; Get remaining Timer1 counts
Time1.HighByte = TMR1H
'---[CCP2 - interrupt handler]------------------------------------------
Capture2:
Time2.LowByte = TMR1L ; Get remaining Timer1 counts
Time2.HighByte = TMR1H
@ INT_RETURN
'---[TMR1 - interrupt handler]---------------------------------------------
Timer1:
OverFlows = OverFlows + 1
@ INT_RETURN
'-------------------------------------------------------------------------------
SixtyHz:
If TrigOut = 0 THEN ' If trig output is low....
If Measure = 1 THEN ' ...and we want to start a measurment
T1CON.0 = 0 : PIR1.0 = 0 ' Stop timer and clear interrupt flag
TMR1H = 0 : TMR1L = 0 ' Clear the time
Time1 = 0 : Time2 = 0 ' Clear measurements
T1CON.0 = 1 ' And start the timer
Measure = 0 ' Let main program know we've set the ball in motion.
ENDIF
TrigOut = 1 ' Set trig output high
ELSE
TrigOut = 0 ' Trig output was high, set it low.
ENDIF
TMR0 = TMR0 + 94 ' Reload TMR0 for 120Hz interrupt rate
@ INT_RETURN
'-------------------------------------------------------------------------------
END
It compiled the first time with an error about TMR0 not being defined, so I edited the .INC file and got around that, but of course it still doesn’t work. No 60Hz square wave out and no serial output.
Anyone care to have a gander at my code and tell me what I’m doing wrong? Point and laugh?? Poke at me with a stick???
Huge thanks as always!
-Ryan
Bookmarks