PDA

View Full Version : WTF? Am I crazy?



RichardBowser
- 6th July 2006, 21:50
Hi everyone

I am really puzzled, though I suspect my difficulty has a TRIVIAL solution. I am trying to insert a parameterized iteration of do-nothings to allow simple tuning of application software to match target hardware. It SHOULD be easy: I declare “TtW” CON $n” for (Time to Wait). Then here’s what I try:

movlw _TtW,0
movwf _WaitCt,0
movlw _TtW
movwf _WaitCt,0
decf _WaitCt,F,0
btfss STATUS,Z ;normally WaitCt <> 0, so Z is clear
goto $-2

Only it doesn’t work. If TtW = 1, it immediately falls through. If TtW = 2, then it hangs infinitely. If I correct that by inserting a second “decf” then WaitCt = 0 when it reaches the bit test and then it just falls through. Yet I’ve seen similar code used in MANY known working routines. But it ONLY skips hanging if TtW = the nunber of "decf" instructions before the "btfss".

Why will the above not just keep on decrementing WaitCt until it reaches a terminal 0?

mister_e
- 6th July 2006, 22:20
for sure you use a 18F serie right? change your GOTO $-2 to GOTO $-4

what happen now?

RichardBowser
- 9th July 2006, 02:46
Thanks for your responsive suggestion. I am using an 18F8720. I tried a goto $-4 as you suggested, but it stall hangs infinitely. Evidently I need to understand relative relocation better. It has seemed to me that each count represented one instruction, and each instruction in the 18 series occopies one program word. So that made sense to me. I won't bother listing everything I tried, but I just downloaded MPLAB 7.40 and I would like to try it in single step mode in their SIM debugger. I'm sure I'm dong something wrong!

Thanks again for trying!

Darrel Taylor
- 9th July 2006, 03:59
Hey Bowser,

Try this...

CHK?RP _WaitCt
movlw _TtW
movwf _WaitCt
DelayLoop
decfsz _WaitCt, F
goto DelayLoop

RichardBowser
- 10th July 2006, 01:21
You answered my question quite succinctly: YES - I was crazy. Your solution solved the problem that was bugging me. And after getting that fixed, I took a better look at my code. I had several at least questionable lines in there. A cleaned up and working version is:

movlw _TtW
wloop
decfsz _Wct,F
goto wloop

That helped a BUNCH. (Sometimes it's good to know the truth.)

Darrel Taylor
- 10th July 2006, 02:49
Hi Richard,

Great! Glad it worked out for you.

I guess I was a little short there, but now that you've got that figured out, you might want to take a look at this routine.
http://www.picbasic.co.uk/forum/showthread.php?p=22098

With that you can just go...
@ DelayUS 6for a 6us delay.

It looks really big, but it actually compiles to almost exactly the same thing you have. Without having to worry about OSC freq.

Don't know if that helps or not, but thought I'd mention it.
<br>