I am trying to make a simple button-controlled LED, but I cannot seem to get any input from the button that would change the state of the LED. In other words, the program keeps branching to lighton, keeping the LED always on, regardless of the button state. I am using an Elegoo Uno R3.
Code:
.text .global setup .global loop setup: ser r16 sts DDRB, r16 ldi r16, 0b00000000 sts DDRD, r16 ldi r16, 0b00100000 sts PORTB, r16 clr r16 sts PORTD, r16 loop: lds r20, PORTD cpi r20, 0x00 ; PORTD always equal to 0x00 ? breq lighton clr r16 sts PORTB, r16 rjmp loop lighton: ldi r16, 0b00100000 sts PORTB, r16 rjmp loop
Edit: I also implemented the method in https://stackoverflow.com/questions/39361410/creating-a-toggle-switch-in-avr-assembly to create a short delay with r18
instead of del
, but it didn't help; see revised loop method below:
loop: cpi r18, 0 brne dec_jmp lds r20, PORTD cpi r20, 0x00 ; PORTD always equal to 0x00 ? breq lighton clr r16 sts PORTB, r16 ldi r18, 250 rjmp loop lighton: ldi r16, 0b00100000 sts PORTB, r16 ldi r18, 250 rjmp loop dec_jmp: dec r18 rjmp loop
With delay:
lighton: ldi r16, 0b00100000 sts PORTB, r16 call delay150 ldi r16, 0b00000000 sts PORTB, r16 call delay150 rjmp loop
lighton
is always happening and that's the problem. For example, I removed the line that sets PORTB in thesetup
method, and the LED remains on. So, Pin13 is constantly being set atlighton
. Also, I did try the LED by itself with a delay and it works normally. – Bgeo25 Dec 5 '18 at 23:07