转自《嵌入式Linux应用开发完全手册》
1. 先看电路图
2. 配置按键为输入,LED为输入
(1)通过crt0.S转入C程序
.text
.global _start
_start:
ldr r0, =0x53000000 @ WATCHDOG寄存器地址
mov r1, #0x0
str r1, [r0] @ 写入0,禁止WATCHDOG,否则CPU会不断重启
ldr sp, =1024*4 @ 设置堆栈,注意:不能大于4K,因为可用的内存只有4K
@ nand flash中的代码在复位后会移到内部ram中,此ram只有4K
bl main @ 调用C程序中的main函数
halt_loop:
b halt_loop
(2)key_led.c
#define GPFCON (*(volatile unsigned long *) 0x56000050)
#define GPFDAT (*(volatile unsigned long *) 0x56000054)
#define GPGCON (*(volatile unsigned long *) 0x56000060)
#define GPGDAT (*(volatile unsigned long *) 0x56000064)
#define GPF4_out (1<<(4*2))
#define GPF5_out (1<<(5*2))
#define GPF6_out (1<<(6*2))
#define GPF4_msk (3<<(4*2))
#define GPF5_msk (3<<(5*2))
#define GPF6_msk (3<<(6*2))
#define GPF0_in (0<<(0*2))
#define GPF2_in (0<<(2*2))
#define GPG3_in (0<<(3*2))
#define GPF0_msk (3<<(0*2))
#define GPF2_msk (3<<(2*2))
#define GPG3_msk (3<<(3*2))
int main()
{
unsigned long dwDat;
GPFCON &= ~(GPF4_msk | GPF5_msk | GPF6_msk);
GPFCON |= GPF4_out | GPF5_out | GPF_out;
GPFCON &= ~(GPF0_msk | GPF2_msk);
GPFCPN |= GPF0_in | GPF2_in;
GPGCON &= ~GPG3_msk;
GPGCON != GPG3_in;
while (1) {
dwDat = GPFDAT;
if (dwDat & (1<<0))
GPFDAT |= (1<<4);
else
GPFDAT &= ~(1<<4);
if (dwDat & (1<<2))
GPFDAT |= (1<<5);
else
GPFDAT &= ~(1<<5);
dwDat = GPGDAT;
if (dwDat & (1<<3))
GPFDAT |= (1<<6);
else
GPFDAT &= ~(1<<6);
}
return 0;
}
3. 总结
清0使用 &= ~
置1使用 |=