Arm Cortex-M4 Sub Routines Unexpected Behavior

47 views Asked by At

        .include "Equates.s"

        .syntax unified
        .text
        .global main
        .global Delay
        .global Top

Delay:    mov     r1,r3
          ldr    r2,=#0x000AAE60
          mul    r1,r2
Dloop:    subs    r1, #1
          bne     Dloop
          bx      lr

BlinkLed:   push    {lr}

Blink:
            mov             r3,#2
            bl      LEDGreenOn
            bl      Delay
            bl      LEDGreenOff
            bl      Delay
            bl      CheckButton
            cmp     r0,#1
            bne             ButtonNotPressed

            bl      LEDOrangeOn
            bl      Delay
            bl      LEDOrangeOff
            bl      Delay
            bl      CheckButton
            cmp     r0,#1
            bne     ButtonNotPressed

            bl      LEDBlueOn
            bl      Delay
            bl      LEDBlueOff
            bl      Delay
            bl      CheckButton
            cmp     r0,#1
            bne     ButtonNotPressed

            bl      LEDRedOn
            bl      Delay
            bl      LEDRedOff
            bl      Delay
            bl      CheckButton
            cmp     r0,#1
            beq     Blink
                bne             ButtonNotPressed


ButtonNotPressed:
            bl      CheckButton
            cmp     r0,#1
            bne     ButtonNotPressed
                bl              BinaryCount
            pop     {lr}
            b       Top

main:
            bl      InitButton
            bl      InitLEDs

Top:                bl      CheckButton
            cmp     r0,#1
            bne     Top
            bl      BlinkLed

            .end

I am trying to blink one light for one second while the button is held down, if I stop pressing the button on my board, then I want the lights to be off and the code to start waiting on me to start pressing again. If I do, then I want it to go to BinaryCount to do its thing. However, I am getting unexpected results, sometimes it works but sometimes it doesn't. Sometimes it works just the way I want, but sometimes it never reaches BinaryCount. I am not sure what I am doing wrong. Can somebody point me in the right direction, please?

Here's the rest of my code:


        .include "Equates.s"

        .syntax unified
        .text
        .global InitButton
        .global CheckButton


InitButton:

            ldr     r0,=RCC
            ldr     r1,[r0,AHBENR]
            orr     r1,#GPIOAEN
            str     r1,[r0,AHBENR]
            ldr     r0,=GPIOA
            ldr     r1,[r0,#MODER]
            bic     r1,#0x03
            str     r1,[r0,#MODER]
            bx      lr


CheckButton:

            ldr     r0,=GPIOA
            ldrh    r0,[r0,#IDR]
            and     r0,#0x01
            bx      lr

            .end

        .include "Equates.s"

        .syntax unified
        .text
        .global BinaryCount

BinaryCount:
            mov     r5,#-1

CountLoop:
            add     r5,#1
            cmp     r5,#15
            bgt     ResetCount
            ldr     r0,=GPIOB
            mov     r1,r5
            lsl     r1,r1,#6
            ldrh    r2,[r0,#ODR]
            bic     r2,#0x0FC0
            orr     r2,r1
            strh    r2,[r0,#ODR]
            mov     r3,#1
            bl      Delay
            bl      CheckButton
            cmp     r0,#1
            beq     CountLoop
            bl      AllLEDsOff
            b       Top

ResetCount:

            mov     r5,#0
            b       CountLoop

            .end

        .include "Equates.s"

        .syntax unified
        .text
        .global InitLEDs
        .global LEDGreenOff
        .global LEDOrangeOff
        .global LEDBlueOff
        .global LEDRedOff
        .global LEDGreenOn
        .global LEDOrangeOn
        .global LEDBlueOn
        .global LEDRedOn
        .global AllLEDsOff

InitLEDs:

            ldr     r0,=RCC
            ldr     r1,[r0,AHBENR]
            orr     r1,#GPIOBEN
            str     r1,[r0,AHBENR]
            ldr     r0,=GPIOB
            ldr     r1,[r0,#MODER]
            bic     r1,#0x000FF000
            orr     r1,#0x00055000
            str     r1,[r0,#MODER]

            ldrh    r1,[r0,#ODR]
            bic     r1,#0x03C0
            strh    r1,[r0,#ODR]
            bx      lr

LEDGreenOn:
            ldr     r0,=GPIOB
            ldrh    r1,[r0,#ODR]
            orr     r1,#0x0200
            strh    r1,[r0,#ODR]
            bx      lr

LEDGreenOff:
            ldr     r0,=GPIOB
            ldrh    r1,[r0,#ODR]
            bic     r1,#0x0200
            strh    r1,[r0,#ODR]
            bx      lr

LEDOrangeOn:
            ldr     r0,=GPIOB
            ldrh    r1,[r0,#ODR]
            orr     r1,#0x0100
            strh    r1,[r0,#ODR]
            bx      lr

LEDOrangeOff:
            ldr     r0,=GPIOB
            ldrh    r1,[r0,#ODR]
            bic     r1,#0x0100
            strh    r1,[r0,#ODR]
            bx      lr

LEDBlueOn:
            ldr     r0,=GPIOB
            ldrh    r1,[r0,#ODR]
            orr     r1,#0x0080
            strh    r1,[r0,#ODR]
            bx      lr

LEDBlueOff:
            ldr     r0,=GPIOB
            ldrh    r1,[r0,#ODR]
            bic     r1,#0x0080
            strh    r1,[r0,#ODR]
            bx      lr

LEDRedOn:
            ldr     r0,=GPIOB
            ldrh    r1,[r0,#ODR]
            orr     r1,#0x0040
            strh    r1,[r0,#ODR]
            bx      lr

LEDRedOff:
            ldr     r0,=GPIOB
            ldrh    r1,[r0,#ODR]
            bic     r1,#0x0040
            strh    r1,[r0,#ODR]
            bx      lr

AllLEDsOff:
            ldr     r0,=GPIOB
            ldrh    r1,[r0,#ODR]
            bic     r1,#0x0FC0
            strh    r1,[r0,#ODR]
            bx      lr

            .end

The lights to work green, orange, blue, red and then jump to binary count.

0

There are 0 answers