In/Out Instructions

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi! I am lost! I really tried but my code doesn't work properly, I tried to debug, but keeps an infinite loop, and to complicated more this i need send a bit to port b.

If port D pin is 1, a "walking bit" to the left is sent to port B.

Any help will be appreciated.

.NOLIST
.INCLUDE "8515def.inc"
.LIST

rjmp begin

begin:

    ser r16        ; r16 = 0xFF
    out DDRD,r16
    clr r16        ; r16 = 0x00
    out DDRB,r16

    sbi PORTD,0
    ldi r30,3
    retraso1:
        dec r30
        breq sigue1
        rjmp retraso1
    sigue1:
    cbi PORTD,0   
    ldi r30,3
    retraso2:
        dec r30
        breq sigue2
        rjmp retraso2
    sigue2:
    brne loop

loop:
    sbi PORTB,6
    ldi r30,3
    retraso3:
        dec r30
        breq sigue3
        rjmp retraso3
    sigue3:
    cbi PORTB,7   
    ldi r30,3
    retraso4:
        dec r30
        breq sigue4
        rjmp retraso4
    sigue4:
    sbi PORTB,6
    ldi r30,3
    retraso5:
        dec r30
        breq sigue5
        rjmp retraso5
    sigue5:
    cbi PORTB,6   
    ldi r30,3
    retraso6:
        dec r30
        breq sigue6
    rjmp retraso6
    sigue6:

    sbi PORTB,5
    ldi r30,3
    retraso7:
        dec r30
        breq sigue7
    rjmp retraso7
    sigue7:
    cbi PORTB,5   
    ldi r30,3
    retraso8:
        dec r30
        breq sigue8
       rjmp retraso8
    sigue8:
    sbi PORTB,4
    ldi r30,3
    retraso9:
        dec r30
        breq sigue9
        rjmp retraso9
    sigue9:
    cbi PORTD,5   
    ldi r30,3
    retraso10:
        dec r30
        breq sigue10
        rjmp retraso10
    sigue10:
    sbi PORTB,3
    ldi r30,3
    retraso11:
        dec r30
        breq sigue11
        rjmp retraso11
      sigue11:
    cbi PORTB,3   
    ldi r30,3
    retraso12:
        dec r30
        breq sigue12
        rjmp retraso12
    sigue12:
    sbi PORTB,2
    ldi r30,3
    retraso13:
        dec r30
        breq sigue13
        rjmp retraso13
    sigue13:
    cbi PORTB,2   
    ldi r30,3
    retraso14:
        dec r30
        breq sigue14
        rjmp retraso14
    sigue14:
    sbi PORTB,1
    ldi r30,3
    retraso15:
        dec r30
        breq sigue15
        rjmp retraso15
    sigue15:
    cbi PORTB,0   
    ldi r30,3
    retraso16:
        dec r30
        breq sigue16
        rjmp retraso16
    sigue16:
    sbi PORTB,0
    ldi r30,3
    retraso17:
        dec r30
        breq sigue17
        rjmp retraso17
    sigue17:
    cbi PORTB,0   
    ldi r30,3
    retraso18:
        dec r30
        breq sigue18
        rjmp retraso18

    sigue18:
    in r16,PIND
   andi r16,0b00000001

    breq round_loop_longjmp
    round_loop_longjmp:

    rjmp loop

 

 

Azul Garcia

Last Edited: Thu. Jun 10, 2021 - 11:26 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is the assignment

 

Read the status of a switch connected to one of the D port pins. If the pin is at 0, a "walking bit" to the right is sent to port B, that is, first activate for a certain time only pin 7 of the port, then deactivate pin 7 and activate by a time only pin 6, after pin 6 turns off and pin 5 turns on for a while, and so on until pin 0 turns on. After pin 0 turns on, the cycle repeats starting from pin 7 again. If port D pin is 1, a "walking bit" to the left is sent to port B.

 

 

Azul Garcia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your code is way way way way way tooo long, should be no more than maybe 30 lines


ser XL
out DDRB, XL ;all outputs
out PORTB, XL   ; turn on pullups
clr XL
out DDRD, XL  ;all input

ldi ZL, 1

loopy:     sbis PIND, 0  ;check PDO
	   rjmp go_right
           lsl ZL
           brne show_it
	   ldi ZL, 1
	   rjmp show_it

go_right:  lsr ZL
	   brne show_it
	   ldi ZL, 128
show_it:   out PORTB, ZL

	   ser XH
	   ser XL
waitup:	   dec XL
           nop
	   nop
           nop
           nop
           brne waitup
           dec XH
           brne waitup
           rjmp loopy 

 

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Fri. Jun 11, 2021 - 02:21 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Double check the initialize of the DDR registers. Which port should be input and which port should be output?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Double check the initialize of the DDR registers. Which port should be input and which port should be output?

Thanks, updated,--was setting DDRB twice, needed to set both DDRB & DDRD

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is the typical example why the subroutines MUST be used.

Do replace all instances of the code:

    ldi r30,3
    retrasoNN:
    dec r30
    breq sigueNN
    rjmp retrasoNN
    sigueNN:

with the code:

    rcall sDelay3
...
sDelay3:
    ldi r30,3
    retraso:
    dec r30
    breq sigue
    rjmp retraso
    sigue:
    ret

For the code insertion you have <>  in the tool-list. There are 4-5 other problems with the code, we may discuss.

/The code must not be the pain in the..., and when it is readable, it is also enjoying./

 

 

Last Edited: Fri. Jun 11, 2021 - 05:53 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Seems to me the "walking 1 bit" is a test to see if you know about shifts and rotates. What I don't think they are looking for is a load of:

 

    SBI 7
    RCALL delay
    SBI 6
    CBI 7
    RCALL delay
    SBI 5
    CBI 6
    RCALL delay
    ...

What I think he's looking for is a simple loop:

   LDI count, 8
   SBI PORTB, 7
loop:
   IN R16, PORTB
   LSR R16
   OUT PORTB, R16
   RCALL delay
   DEC count
   BRNE loop

where the very key thing in that is knowing that the instruction that will make 0x80 follow the sequence 0x80, 0x40, 0x20, 0x10... is the LSR in the middle.

Last Edited: Fri. Jun 11, 2021 - 08:23 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is the typical example why the subroutines MUST be used.

There is no need at all for a subroutine (saying it must be used gives a wrong impression)...the delay happens at all times (though calling it as a sub can allow it to be used in other parts of the program)...but then this chip, would prob also need the stack pointer set.. so it is more efficient to use as an inline delay.

 

Unfortunately the AVR doesn't have a rotate without carry, so it is just as easy to reinit when the LSL/LSR shifted value runs dry (which has the advantage that if some rouge value got in, it wouldn't just rotate around forever).

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Interesting observation.

 

Once I started my first design it was really necessary ("Just do subroutines"). Of course that the subs are not strictly necessary in the C-programming, and I wonder is it worth time to strip the code of them also in ASM.

 

The worse what can happen to the ASM code is the spaghetti-coding. As long as the code diagram can be kept on A4 size, anything legal is allowed to do with the ASM code.

 

The skill you produced the #3 code is admirable, how any beginner may think that way, me included.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks so much, for your commentary, i didn't have any idea how to make it short.

Azul Garcia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for your help, the input port is B, the outport is D

Azul Garcia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for your help, I am studing how to code but I am having a hard time to simplify it, the good news I am keep trying.

Azul Garcia

Last Edited: Fri. Jun 11, 2021 - 03:12 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wow! I really appreciate your help! I will keep trying to learn code.

 

 

Azul Garcia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

azugr@outlook.com wrote:

I will keep trying to learn code.

Just keep trying. The best code learn is by coding and testing.

Milan