1

I wrote an assembly code for my Raspberry Pi to read an I/O with a button and switch an LED according to the state of the button. It seems that the I/O with the LED is toggling all the time and I don´t understand why.

My idea is to use conditional execution for setting the LED state, according to the state of the I/O with the button (pull up with button connecting to ground).

Where is the mistake?

_start:
@ Base adress for gpio controller
ldr r0, =0x20200000

@ Set GPIO 24 as output
mov r2, #0x01
lsl r2, #0x0C
str r2, [r0, #0x08]

@ Set GPIO 13 as input
mov r1, #0x00
str r1, [r0, #0x04]

mov r2, #0x01
lsl r2, #0x0D

Loop:
    @ Read GPIO 13
    ldr r1, [r0, #0x34]

    @ Check if the button is pressed
    tst r1, r2

    @ Clear the GPIO if the button is not pressed (GPIO 13 = 1)
    streq r2, [r0, #0x28]

    @ Set the GPIO if the button is pressed (GPIO 13 = 0)
    strne r2, [r0, #0x1C]

    b Loop
  • With physical buttons, you will probably need to implement some debouncing. – goldilocks Feb 15 at 19:41
  • The LED pin is toggeling all the time without pressing the button. – Kampi Feb 15 at 19:51
  • please provide the schematic diagram of your connections – jsotola Feb 15 at 23:24
1

Ok found the solution. There were some coding issues.

This code is working now:

.section .init
    .globl _start

_start:
@ Base adress for gpio controller
ldr r0, =0x20200000

@ Set GPIO 24 as output
mov r2, #0x01
lsl r2, #0x0C
str r2, [r0, #0x08]

@ Set GPIO 13 as input
mov r1, #0x00
str r1, [r0, #0x04]

@ I/O masks
mov r2, #0x01
lsl r2, #0x0D

mov r3, #0x01
lsl r3, #0x18

Loop:
    @ Read GPIO 13
    ldr r1, [r0, #0x34]

    @ Check if the button is pressed
    tst r1, r2

    @ Clear the GPIO if the button is not pressed (GPIO 13 = 1)
    streq r3, [r0, #0x28]

    @ Set the GPIO if the button is pressed (GPIO 13 = 0)
    strne r3, [r0, #0x1C]

    b Loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.