Ok, success (of sorts)
By keeping it simple, this code works (many thanks to Paul, whose code sample I used, albeit in McSpank-esque form here)...
ie the C2 count decrements nice & smoothly (& below 336 - yay!) & totally in line with the amount of phase lag....Code:PIR2.5 = 0 'Clear the Comp1 interrupt flag Comp1Time = 0 Main_Loop: WHILE PIR2.5 = 0 GOTO Main_Loop WEND TMR1H = 0 TMR1L = 0 PIR2.5 = 0 Comp1_count: WHILE PIR2.5 = 0 goto Comp1_count wend Comp1Time.Highbyte = TMR1H Comp1Time.Lowbyte = TMR1L TMR1H = 0 TMR1L = 0 PIR2.6 = 0 'HSEROUT [dec Comp1Time,13, 10] Comp2_count: WHILE PIR2.6 = 0 goto Comp2_count wend Comp2Time.Highbyte = TMR1H Comp2Time.Lowbyte = TMR1L TMR1H = 0 TMR1L = 0 HSEROUT ["C1=",dec Comp1Time,9,"C2=", dec comp2time, 13, 10] PIR2.5 = 0 goto Main_Loop
(green trace is comp1 - non lagged, yellow trace is into comp2 & lagged a miniscule amout)
.....I can get all the way down to 13-14(ish) below which the C2 count gets very jittery (ie successive counts eg 5, 11, 6, 12, 5, - you can see some 19s in that stream above)....but a comp2 count of 13 @1kHz is granular enough for me (accuracy to within 0.936 degrees @1kHz). I'm elated...*BUT*
Quite clearly my original way is causing some issues (p& almost certainly to do with my poor sequence &/or coding), so if this way of doing it works, how can I integrate it into my main program?
My first attempt just now failed...
Here's the screen output...
C1=4909 C2=13
C1=4903 C2=13
C1=4909 C2=13
C1=4909 C2=13
hello
C1=4903 C2=13
C1=4909 C2=13
ie that C1 count has dropped?!! (even though the incoming frequency has remained at 1Khz)
here's what I did (disclaimer: I didn't know how to get the main body of program to 'jump' to to the interrupt handler when Comp1 interrupted, so I deferred back to DT's method as the initial 'trigger')...
Code:Comp1Time var word ' used to amalgamate TMR1 High & Low Bytes. Comp2Time var word INCLUDE "DT_INTS-14.bas" ' Base Interrupt System PO90OOO9 INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts ASM INT_LIST macro ; IntSource, Label, Type, ResetFlag? INT_Handler CMP1_INT, _Comp1_Int, PBP, yes endm INT_CREATE ; Creates the interrupt processor ENDASM T1CON.0= 1 'start timer Comp1Time = 0 'clear down Comp1Time, prior to starting. comp2Time = 0 'clear down Comp2Time, prior to starting @ INT_ENABLE CMP1_INT ; enable Comparator 1 interrupts PIR2.5 = 0 'Clear the Comp1 interrupt flag Comp1Time = 0 Comp2Time = 0 'Main body of Code**************************************************************** Main: hserout ["hello", 13, 10] pause 20 goto Main END '**********************Comp1 interrupt handler********************************************************* Comp1_Int: Comp1_Loop: WHILE PIR2.5 = 0 GOTO Comp1_Loop WEND TMR1H = 0 TMR1L = 0 PIR2.5 = 0 Comp1_count: WHILE PIR2.5 = 0 goto Comp1_count wend Comp1Time.Highbyte = TMR1H Comp1Time.Lowbyte = TMR1L TMR1H = 0 TMR1L = 0 PIR2.6 = 0 'HSEROUT [dec Comp1Time,13, 10] Comp2_count: WHILE PIR2.6 = 0 goto Comp2_count wend Comp2Time.Highbyte = TMR1H Comp2Time.Lowbyte = TMR1L TMR1H = 0 TMR1L = 0 HSEROUT ["C1=",dec Comp1Time,9,"C2=", dec comp2time, 13, 10] PIR2.5 = 0 @ INT_RETURN





Bookmarks