如何在组装中检查STM32F迪斯科上是否按下了用户按钮

时间:2018-09-19 00:05:12

标签: assembly stm32

我是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

1 个答案:

答案 0 :(得分: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]

对应于#1的端口A向上对应于#(1 << 7)的端口F

学习新语法肯定会令人沮丧。我问题中的代码已被编辑,可以检测用户按钮。我敢肯定,有一天我可以为此付出很多努力!