【问题标题】:How to check if the user button is pressed on the STM32F disco in Assembly如何在汇编中检查是否按下了STM32F迪斯科上的用户按钮
【发布时间】:2018-09-19 00:05:11
【问题描述】:

我是 stm32f 发现板上的汇编编程新手。我正在尝试在汇编中编写一个可以在 C 中调用的程序(.S 文件)。我希望汇编程序检查是否按下了用户按钮。

我做了一些研究,发现用户按钮位于 GPIOA 端口中,并且可以从 IDR 空间访问它的数据。具体来说,我相信当按下用户按钮时,GPIOA->IDR 的第一位会切换为 1。

这是我写的代码:

.global checkB1
.thumb_func
checkB1: 

@; accessing B1
ldr r3,=GPIOA_BASE

ldr r2, [r3,#IDR]
and r0, r2, #GPIO_IDR_IDR_0     @; check if 1 and put in r0

bx lr

我在 C 中调用函数没有问题,但是当按下用户按钮时 r0 永远不会变为 1?

我对自己做错了什么很迷茫,我研究的所有内容都在 C 中完成了整个过程,但它并没有真正帮助我。如果有人知道错误,将不胜感激。

编辑:

我还想包含配置 GPIOA 的初始化代码:

.global initB1
.thumb_func
initB1: @;configure B1 as an input

@; make sure GPIOA is enabled
ldr r3,=RCC_BASE
ldr r2,[r3,#RCC_AHB1ENR]
orr r2,#1   @; set enable bit
str r2,[r3,#RCC_AHB1ENR]

@; configuring B1
ldr r3,=GPIOA_BASE

@; configure B1 as an input
ldr r2,[r3,#MODER]
bic r2,#3   @;clear current value if any of A0 mode
            @; new value of A0 mode is general purpose input
str r2,[r3,#MODER]  @; ..

@; configure input of B1 as pulldown
ldr r2,[r3,#OPUPDR]
bic r2,#3   @;clear current value if any of control bits
orr r2,#2   @; pulldown mode (bit value: 10)
str r2,[r3,#OPUPDR] @; ..

bx lr

【问题讨论】:

    标签: assembly stm32


    【解决方案1】:

    我的代码的问题是我在初始化时没有启用正确的端口!

    @; make sure GPIOA is enabled
    ldr r3,=RCC_BASE
    ldr r2,[r3,#RCC_AHB1ENR]
    orr r2,#1   @; set enable bit <--------------
    str r2,[r3,#RCC_AHB1ENR]
    

    Port A对应#1 up Port F对应#(1

    学习新语法肯定会令人沮丧。我的问题中的代码已经过编辑,可以检测用户按钮。我相信我在这方面的挣扎总有一天会派上用场!

    【讨论】:

      猜你喜欢
      • 2015-10-30
      • 1970-01-01
      • 2018-01-25
      • 2020-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      相关资源
      最近更新 更多