Question about djnz
Trying to get this chuck of code to work, the idea is the aDAT pin cycles twice, then the aSER pin cycles 24 times then waits for the aDAT pin to cycle again.
There is more code before the posted chunk that works fine.
Any help would be appreciated.
:loop1 mov bitcount, #24
waitpeq aDAT, aDAT
waitpne aDAT, aDAT
waitpeq aDAT, aDAT
waitpne aDAT, aDAT
:loop2 xor outa, aSER
waitcnt cnt, delay
xor outa, aSER
waitcnt cnt, delay
djnz bitcount, #:loop2
jmp #:loop1
waitpeq $, #0
delay long 200
There is more code before the posted chunk that works fine.
Any help would be appreciated.
Comments
So, to wait 10 cycles, you do this:
mov waitTime, #10 add waitTime, cnt 'add the current value of the counter waitcnt waitTime, #0
If you wanted to wait 10 cycles, then run some code, then wait for exactly 100 cycles from when you started, you do this:
mov waitTime, #10 add waitTime, cnt waitcnt waitTime, #90 'add 90 cycles to the waitTime when it's finished nop nop ' real code that takes less than 90 cycles goes here nop waitcnt waitTime, #0 'wait for the 10 + 90 cycle time to elapse
So I think in your case, you want:
mov waitTimer, delay add waitTimer, cnt :loop2 xor outa, aSER waitcnt waitTimer, delay 'add more delay to the existing timer xor outa, aSER waitcnt waitTimer, delay 'and again... djnz bitcount, #:loop2 jmp #:loop1
It's also worth noting that the delay "begins" from the moment you read the previous count value. In my code, I'm adding the counter after setting the delay time, so the delay is counting from the instruction where the add takes place.
Some people prefer this:
mov waitTime, cnt add waitTime, delay
That's a rather odd example don't you think?
Why do you say that?
mov waitTime, #5{14} + 5 add waitTime, cnt 'add the current value of the counter waitcnt waitTime, #0
nop waitpne $, #0
@SSkalko: What about this:
:loop1 waitpeq aDAT, aDAT waitpne aDAT, aDAT waitpeq aDAT, aDAT waitpne aDAT, aDAT mov bitcount, #24*2 ' double loop count -> xor [COLOR="red"] mov cnt, cnt add cnt, #9 [/COLOR]:loop2 waitcnt cnt, delay xor outa, aSER djnz bitcount, #:loop2 jmp #:loop1 delay long 200
It makes sure that your initial waitcnt target is one which can be reached in a reasonable time frame, i.e. is known.Need some in-line comments here, this is as twisted as the
As for waitcnt, maybe you feel more comfortable with this version:
mov temp, cnt add temp, #9
The #9 makes sure that an immediately following waitcnt just falls through (6 cycles). Any other value (greater 8) will do. I prefer it this way. Using cnt in the dst slot refers to its shadow register (i.e. is as good as temp).I was just wondering how you got the posted lines of code to show in red.
Please walk me through this. Where does "5{14} + 5" come from?
(@Publison: Thanks! How did you know?)
mov cnt, #5{14} + 0 add cnt, cnt waitcnt cnt, #0
ormov cnt, cnt add cnt, #9{14} + 0 waitcnt cnt, #0
So 5{14} + 5 is just a way of saying that 5 cycles are used to guarantee the minimum 14 cyles ({14} being a comment) and the other 5 cycles are extra delay (14 -> 19).