The timer tick code with the use of TimerW works well. There is probably a boundry condition in the waitticks code that may have a problem, which I will try to explain later. However TimerW is used in the HSERVO sub-system, and so if choose to use HSERVO then you will probably need to convert to an 8 bit timer, such as timerA.
In my current project I am trying to measure the pulse widths for the 6 channels of an HITEC receiver, using WKP0-WKP5(P0-P5) on the BAP. When I was using TimerW I had the counter count with Clk/8 and the code looked like it was computing the pulses reasonably well. However when I used TimerA with an interrupt to process overflows to get a count greater than 256, the code was not generating overly consistently. It worked better when I used the counter of CLK/32.
The interrupt functions currently look something like:
[code]handle_intp0
if fProcessingPulses = FALSE then
If (IEGR2 & 0x01) then ; process rising edge
; need to reset timer
TMA = 0xc ; reset timer
TMA = 0x6 ; clk/32
iOverflowTime = 0
fPulseTimingsBusy = 1
IEGR2.bit0 = 0 ;interrupt on a falling edge.
else
aiPulseTimings(0) = iOverflowTime + TCA
IEGR2.bit0 = 1 ;interrupt on a falling edge.
endif
endif
resume
handle_intp1
if fPulseTimingsBusy then
aiPulseTimings(1) = iOverflowTime + TCA
endif
resume
handle_intp2
if fPulseTimingsBusy then
aiPulseTimings(2) = iOverflowTime + TCA
endif
resume
handle_intp3
if fPulseTimingsBusy then
aiPulseTimings(3) = iOverflowTime + TCA
endif
resume
handle_intp4
if fPulseTimingsBusy then
aiPulseTimings(4) = iOverflowTime + TCA
endif
resume
handle_intp5
if fPulseTimingsBusy then
aiPulseTimings(5) = iOverflowTime + TCA
fPulseDataValid = TRUE
fPulseTimingsBusy = FALSE
endif
resume
handle_timera
iOverflowTime = iOverflowTime + 256
resume[/code]
I believe the problem is that at CLK/8 there is a Timer A overflow interrupt every 2048 clocks and and CLK/32 every 8192 clocks. I believe that the overflow interrupt is at a lower priority than the WKP interrupt. So it is very possible that when in my code I am handling an WKP3 interrupt there may be a pending TimerA overflow and as such my Overflow time will be off by at least 256. It may also be possible where we could get multiple higher priority interrupts WKP INTP0… and possibly be off by multiples of 256? I am sorry if my explanation is not very good.
What I am trying to now to fix this is to try to see if there is a pending timer interrupt and process it during the WKP interrupt. Something like:
[code]handle_intp1
if fPulseTimingsBusy then
if (irr1.bit6) then ; pending interrupt?
irr1.bit6 = 0
iOverflowTime = iOverflowTime + 256
endif
aiPulseTimings(1) = iOverflowTime + TCA
endif
resume
[/code]
This shortens the window. The only problem could be that TCA wraps around just during or after the testing if irr1.bit6. This is small window. I suppose I could add in some additional tests. Like maybe save away TCA to use as the first instruction of the interrupt handler and if irr1.bit6 is set and the TCA is lower than the save value I know I probably wrapped within the handler.
Anyone else have some suggestions?
Thanks