Question

I have 8 LEDs and 2 buttons, initially the first and the last LED are lit, if i press the first button, the first led shifts right, if i press the second button the last led shifts left. If the two lights "meet", by shifting upon each other, they switch off.

I did not know how to update the leds on a single row after pressing the buttons, so I made 2 rows, P1 and P2, for each button. The first row seems to shift properly, but the second is really messed up. What am I doing wrong?

Below is my code:

$TITLE(5)
$MOD51
ORG 100H
START: 
    ; 8 leds, P1.0-P1.7
    ; 2 buttons, P0.1 P0.2
    MOV P1, #00h
    MOV P2, #00h

    ; left LED positions
    MOV 30H, #00000001b ; initial position
    MOV 31H, #00000010b  
    MOV 32H, #00000100b  
    MOV 33H, #00001000b  
    MOV 34H, #00010000b             
    MOV 35H, #00100000b  
    MOV 36H, #01000000b  
    MOV 37H, #10000000b 

    MOV 38H, #00000000b ; leds meet

    ; right led positions
    MOV 39H, #10000000b ; initial position
    MOV 40H, #01000000b  
    MOV 41H, #00100000b  
    MOV 42H, #00010000b  
    MOV 43H, #00001000b             
    MOV 44H, #00000100b  
    MOV 45H, #00000010b  
    MOV 46H, #00000001b  

    MOV R1, #0
    MOV R2, #0

 LOOP:  
    JNB P0.1, INCREMENT_L ; left button pressed, led shifts right
    JNB P0.2, INCREMENT_R ; right button pressed, led shifts left

    CALL DISP_L ; display left led
    CALL DISP_R ; display right led

    JMP LOOP

INCREMENT_L:
    SETB P0.1
    CJNE R1, #7, INC_L
    MOV R1, #0
    JMP LOOP

INCREMENT_R:
    SETB P0.2
    CJNE R2, #7, INC_R
    MOV R2, #0
    JMP LOOP

INC_L:
    MOV A, R2
    ADD A, #39H
    MOV B, A
    MOV A, R1
    ADD A, #30H
    CJNE A, B, INCL
    JMP RESET
    JMP LOOP

INC_R:
    MOV A, R1
    ADD A, #30H
    MOV B, A
    MOV A, R2
    ADD A, #39H
    CJNE A, B, INCR
    JMP RESET
    JMP LOOP

INCL:
    INC R1
    JMP LOOP

INCR:
    INC R2
    JMP LOOP

DISP_L:
    MOV A, R1
    ADD A, #30H
    MOV R0, A
    MOV P1, @R0
    RET

DISP_R: 
    MOV A, R2
    ADD A, #39H
    MOV R0, A
    MOV P2, @R0
    RET

RESET:
    MOV R1, #0
    MOV R2, #0
    JMP LOOP

END
Was it helpful?

Solution

This sounds an awful lot like a signed/unsigned issue. Is your add unsigned, or does it assume the high bit is a sign bit and do the 2s compliment stuff for you. Any carry bits left over could also affect things if not careful as well. Do you have a straight shift instruction instead of using add?

Looked online real quick and see you do have a SHL/SHR instruction you could use to move your bits around. Then, you just xor against your location to turn the bit off if they match.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top