读GPIO的引脚电平?

时间:2014-04-01 00:56:11

标签: assembly io arm

我想读取GPIO引脚的电平。以下内容可在芯片文档中找到:

GPIO Pin Level Registers (GPLEVn)

SYNOPSIS    The pin level registers return the actual value of the pin.
            The LEV{n} field gives the value of the respective GPIO pin.

Bit(s)    Field Name        Description              Type    Reset
21-0      LEVn (n=32..53)   0 = GPIO pin n is low    R/W     0
                            1 = GPIO pin n is high

这是我写的代码:

ldr r0,=0x20200038    @ Load address of GPLEV into r0
mov r1,#1             @ Load value 1 into r1
lsl r1,#18            @ Left shift value in r1 eighteen places. This corresponds to GPIO50
str r1,[r0]           @ Store the content of r1 at the address in r0

我现在希望在 r0 中找到引脚电平,但它似乎不起作用。以上代码是否是读取GPIO引脚电平的正确方法?

1 个答案:

答案 0 :(得分:0)

在您的代码中,r0保存GPLEV寄存器的地址,但不保存GPLEV寄存器的值。如果要将GPLEV寄存器中的值读入另一个寄存器,则需要以下内容:

ldr r0,=0x20200038
ldr r2,[r0]

您的代码将值0x00040000放在r1中,然后将该值存储在GPLEV中。