实例一:按下就亮个灯
.text
.global _start
_start:
mov r7,lr
@初始化按键
ldr r0,=0xe0200c40
ldr r1,=0x0
str r1,[r0]
@让按键都是输入模式
ldr r0,=0xe0200c44
loop:
ldr r1, [r0]
tst r1,#0x1
@让0xe0200c44地址获取到的值,0x1比较,如果是0就跳转到led
beq led
bl loop
@循环监听按键
led:
ldr r0,=0xe0200280
ldr r1,=0x111
str r1,[r0]
ldr r0,=0xE0200284
mov r1,#0x1
str r1, [r0]
mov lr,r7
bx lr
.end
实例二:用到2个按键,1个按键切换led灯亮灭,1个跳出程序
.text
.extern delay
.global buttun
buttun:
stmfd sp!,{lr}
ldr r2,=0xe0200280
ldr r4,=0xffff
bic r1,r1,r4
@让4个灯先为0
ldr r4,=0x1111
orr r1,r1,r4
@再置1为输出模式
str r1,[r2]
ldr r2,=0xe0200c40
ldr r4,=0xffff
bic r1,r1,r4
@让按键都为输入模式
str r1,[r2]
ldr r2,=0xe0200c44
loop:
ldr r1,[r2]
and r1,#0x1
cmp r1,#0x0
beq con1
ldr r1,[r2]
and r1,#0x2
cmp r1,#0x0
beq con2
b loop
con1:
bl delay
ldr r2,=0xe0200c44
ldr r1,[r2]
@延时去抖,再判定是否是
and r1,#0x1
cmp r1,#0x0
beq led
b loop
con2:
bl delay
ldr r2,=0xe0200c44
ldr r1,[r2]
and r1,#0x2
cmp r1,#0x0
beq out
b loop
led:
ldr r2,=0xe0200284
ldr r1,[r2]
eor r1,r1,#0xf
str r1,[r2]
b loop
out:
ldmfd sp!,{r7}
bx r7
.end
delay.S
.text
.global delay
delay:
mov r5,#0x100000
temp:
sub r5,r5,#1
cmp r5,#0
bne temp
bx lr
.end
makefile
all:start.bin clean
start.bin:start.o delay.o
arm-linux-ld -Ttext=0x40000000 start.o delay.o -o start.elf
arm-linux-objcopy -O binary -S start.elf start.bin
arm-linux-objdump -d start.elf > debug
delay.o:delay.S
arm-linux-gcc -c -nostdlib -nostdinc delay.S -o delay.o
start.o:start.S
arm-linux-gcc -c -nostdlib -nostdinc start.S -o start.o
clean:
rm -rf start.o start.elf