Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
03/08/04 12:02
Read: times


 
#66237 - RE: Counting events
Responding to: ???'s previous message
DJNZ is Decrement and Jump if Not Zero.
It's used in constructing loops like

mov R1,#2
LABEL:
;(...do something here)
djnz R1,LABEL

If you want plain decrement, use DEC instead.

First off, please clarify if you want to set a breakpoint in Keil for debugging purposes or you mean just programming a 8051 in ASM (C?) and creating some kind of loop. Then, what do you want

-----What i'v done is loaded a value in the accumalator and then decrementing(DJNZ instruction) it and then jump to the sequence i want to repeat, but each time the program comes back to this it loads the accumalator again and puts it in an continuous loop. to do?-----

Make all the jumps go BEHIND the initialisation of the loop.

(normal program flow)
mov A,#init_value
JUMP_HERE_TO_REPEAT:
(something repeated)
DJNZ A,JUMP_HERE_TO_REPEAT

I can't make any sense of the 'ret' in your function. ret pulls 2 bytes from stack and jumps to address pointed by them. If you CALL without RET, you spam the stack. If you RET without CALL, you jump nobody-knows-where (unless you purposedly PUSHed some address first, but that's a rather advanced technique) so if you jump away from a subroutine through DJNZ, you leave junk on stack. That's a Bad Thing to do. Of course you can use DJNZ in a subroutine but its jump range must be within the subroutine. Say, like this:

(main program)
ACALL BLINK_LED
(more of main program)

BLINK_LED:
PUSH ACC ;
PUSH PSW ; usually a wise thing to do in subroutines
MOV ACC,#5 ; We want to blink 5 times.
BLINK_LOOP:
SETB LED
ACALL SLEEP_1500ms
CLR LED
ACALL SLEEP_500ms ; military standard blink, dutycycle 75%
DJNZ A,BLINK_LOOP ; Here make things repeat
POP PSW
POP ACC ; preparing to jump back
RET ; return wherever we were called from.



List of 9 messages in thread
TopicAuthorDate
Counting events            01/01/70 00:00      
   RE: Counting events            01/01/70 00:00      
   RE: Counting events            01/01/70 00:00      
      RE: Counting events            01/01/70 00:00      
         RE: Counting events            01/01/70 00:00      
            RE: Counting events            01/01/70 00:00      
               RE: Counting events            01/01/70 00:00      
         RE: Counting events            01/01/70 00:00      
   RE: Counting events            01/01/70 00:00      

Back to Subject List