这里思路跟点亮led灯是一样的,在main函数中不断查询按键的状态,然后点亮led灯,需要注意的是需要配置gpf相应引脚为输入引脚。
.text
.global _start
_start:
ldr r0, =0x53000000
mov r1, #0
str r1, [r0]
ldr sp, =4096
bl main
halt_hoop:
b halt_hoop
#define LEDCON (*(volatile unsigned long *)0x56000050)
#define LEDDAT (*(volatile unsigned long *)0x56000054)
#define KEY2CON (*(volatile unsigned long *)0x56000050)
#define KEY2DAT (*(volatile unsigned long *)0x56000054)
#define KEY2_PRESSED (0x1 << 2)
void allLedEnable()
{
LEDCON |= 0x00005500;
}
void key2Enable()
{
KEY2CON &= ~0x00000030;
}
void ledOn(int i)
{
LEDDAT &= ~(0x1 << (i+3));
}
void ledOff(int i)
{
LEDDAT |= (0x1 << (i+3));
}
int main()
{
allLedEnable();
key2Enable();
ledOff(1);
ledOff(2);
ledOff(3);
while(1)
{
if(KEY2DAT & KEY2_PRESSED)
{
ledOff(1);
}
else
{
ledOn(1);
}
}
return 0;
}
hello.bin:hello.c crt.S
arm-linux-gcc -c hello.c crt.S
arm-linux-ld -Ttext 0x00000000 crt.o hello.o -o hello_elf
arm-linux-objcopy -O binary -S hello_elf hello.bin
clean:
rm *.bak *.bin *.o