1. 程式人生 > >arm彙編按鍵控制led燈

arm彙編按鍵控制led燈

例項一:按下就亮個燈
.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