0

So I am creating a program that will cause an LED to blink on and off when a button is being pressed and held down, however, when I run the program that I have written, pressing the button will not initiate the LED's "blnkr" loop, are there any things I can fix with my program so that the LED's "blnkr" loop will run while the button's (BTN) input is returning HIGH?

// to compile: g++ redLed.s -lwiringPi -g -o redLed
// Program will power an LED when button is pressed, and when button is not pressed, LED will remain off

.equ INPUT, 0
.equ OUTPUT, 1
.equ LOW, 0
.equ HIGH, 1
.equ LED, 29    // wiringPi 29 (bcm 21 / physical 40)
.equ BTN, 28   // wiringPi 28 (bcm 20)

.text
.global main
main:
// int main()
    push {lr} // {
    bl wiringPiSetup // wiringPiSetup(); // initialize the wiringPi library
    
    mov r0, #LED // pinMode(29,OUTPUT); // set the wpi 29 pin for output
    mov r1, #OUTPUT
    bl pinMode
    
    mov r2, #BTN
    mov r1, #INPUT
    bl pinMode    // initiate r2 as input for wpi pin 28
    
do_while:
    mov r0, #LED
    mov r1, #LOW
    bl digitalWrite // turn off LED when button is not pressed
    
    mov r2, #BTN
    bl digitalRead    // r2 = digitalRead(BTN);

    cmp r2, #HIGH
    bleq blnkr       // if BTN is HIGH then branch with link to the blnkr loop
    b do_while

blnkr:
    cmp r2, #HIGH
    beq do_while
    
    mov r0, #LED // digitalWrite(29,HIGH); // write high volt signal to pin 21 to turn rgb led to red
    mov r1, #HIGH
    bl digitalWrite

    ldr r0, =#350 // delay(500); // delay for 500 milliseconds or 0.5 seconds
    bl delay

    mov r0, #LED // digitalWrite(29,LOW); // write low voltage to wpi 21 to turn off the rgb led
    mov r1, #LOW
    bl digitalWrite
    
    ldr r0, =#350
    bl delay
    b blnkr

    mov r0, #0 // return 0;
    pop {pc} // }

When I first tried to write this code, I attempted to make use of a while and do while loop, so the do while loop is supposed to run over and over again until the exit condition (BTN = HIGH) is met, then it should have branched into the blnkr loop where as long as BTN = HIGH the loop will repeat itself over and over while constantly checking after each iteration whether BTN = HIGH, however, when I run the program, the LED will remain off even when I press the button.

0andriy
  • 4,183
  • 1
  • 24
  • 37
BeefCake
  • 3
  • 2
  • 1
    Does `digitalRead` really take an argument and return a value in r2? The usual AAPCS calling conventions would have it in r0. I'd start by carefully reviewing all your function calls, making sure you fully understand the calling conventions and are complying with them. – Nate Eldredge Dec 10 '22 at 04:28
  • 1
    The WiringPi documentation describes its functions in C syntax, which strongly suggests that they follow the usual C calling conventions. – Nate Eldredge Dec 10 '22 at 04:31
  • @NateEldredge Thank you for reminding me to review the AAPCS, it seems like the issue was indeed the register not being r0 – BeefCake Dec 10 '22 at 18:08
  • 1
    you should probably use `r0` instead of `r2` at various places to follow ARM EABI calling conventions. You should also push 2 values on stack (instead of `push {lr}`) because stack is expected to be 8-byte aligned – ensc Dec 11 '22 at 16:52

0 Answers0