Back on January 4th, Mike (K8LH) was kind enough to provide this utility for delays:
Subroutine:
It worked well for serial transmission at 9600 baud. After much study, I felt was was getting enough of an understanding to test it in the MPLab simulator with this code. I used the stopwatch function to time between breakpoints set at bsf and bcf.
At 4 MHz, the timings were all 1 μsec too long, i.e., one instruction cycle. Assuming that most of my uses would include a return, such as used here, I changed the entry point in the uDelay subroutine by subtracting 13 instead of 12 (see bolded line in macro). Testing with several delays from 20 msecs to 13 μsec worked with no error. I only tested processors at 4 MHz and 8 MHz.
Now, 1 μsec is not going to make a huge difference for 10 bits at 9600 baud, but it is still appreciable. The question here is that while the delay macro is accurate in itself as originally written, the program or the method of measuring the delay (i.e., breakpoints and stopwatch) introduces an error of one instruction cycle.
Question: How accurate is the stopwatch function the way I used it?
Regards,
John
Edit: I guess bolding doesn't work in code. The line I am referencing has bolding tags on it.
Code:
clock equ 8 ;4, 8, 12, 16, or 20 MHz clock
usecs equ clock/4 ;cycles per microsecond multiplier
msecs equ clock/4*1000 ;cycles per millisecond multiplier
DelayCy macro delay ;12..327690 cycle range
movlw high((delay-12)/5)+1
movwf delayhi
movlw low((delay-12)/5)+1
movwf delaylo
[B]call uDelay-((delay-12)%5)[/B]
endm
Code:
nop ;entry for (delay-12)%5 == 4
nop ;entry for (delay-12)%5 == 3
nop ;entry for (delay-12)%5 == 2
nop ;entry for (delay-12)%5 == 1
uDelay decf delaylo,f ;5 cycle loop
skpnz
decfsz delayhi,f
goto uDelay
retlw 0
Code:
<snip>
nop
nop
bsf en ;"en" is an enable pin in a port
DelayCy (13*msecs)
bcf en
Now, 1 μsec is not going to make a huge difference for 10 bits at 9600 baud, but it is still appreciable. The question here is that while the delay macro is accurate in itself as originally written, the program or the method of measuring the delay (i.e., breakpoints and stopwatch) introduces an error of one instruction cycle.
Question: How accurate is the stopwatch function the way I used it?
Regards,
John
Edit: I guess bolding doesn't work in code. The line I am referencing has bolding tags on it.
Last edited: