您的位置:首页 > 其它

arm9 点亮led程序优化导致全亮问题

2015-04-11 23:27 387 查看
如需转载请注明出处

本实验是arm9 裸板程序,主要功能是循环点亮4个led。

参考伟山东的《嵌入式linux应用开发》点亮led节。

电路图如下:



代码如下:

head.s
.text
.global _start
_start:
ldr     r0, =0x56000010     @ 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


main.c
#define	GPBCON		(*(volatile unsigned long *)0x56000010)
#define	GPBDAT		(*(volatile unsigned long *)0x56000014)
#define GPBUP 		(*(volatile unsigned long *)0x56000018)

#define	GPB5_out	(1<<(5*2))
#define	GPB6_out	(1<<(6*2))
#define	GPB8_out	(1<<(8*2))
#define	GPB10_out	(1<<(10*2))

void  wait(unsigned long dly)
{
for(; dly > 0; dly--);
}

int main(void)
{
unsigned long i = 0;
GPBUP = 0x00;
GPBCON = GPB5_out|GPB6_out|GPB8_out|GPB10_out;
while(1){
if (i > 3 && i < 8){
GPBDAT = (~((i + 4) << 5));
} else if (i > 7 && i < 12) {
GPBDAT = (~((i + 24) << 5));
} else if (i > 11 && i < 16) {
GPBDAT = (~((i + 28) << 5));
} else {
GPBDAT = (~(i << 5));
}

if(++i == 16)
i = 0;
wait(30000);
}
return 0;
}


Makefile
led_rotate.bin : head.S  main.c
arm-elf-gcc -g -c -O2 -o head.o head.S
arm-elf-gcc -g -c -O2 -o main.o main.c
arm-elf-ld -Ttext 0x0000000 -g  head.o main.o -o led_rotate_elf
arm-elf-objcopy -O binary -S led_rotate_elf led_rotate.bin
arm-elf-objdump -D -m arm  led_rotate_elf > led_rotate.dis
clean:
rm -f led_rotate.dis led_rotate.bin led_rotate_elf *.o *.bin


编译、烧录、观察结果是:已开启电影4个等全部亮了。
理论结果是:4个灯循环模拟1-15的递增过程。

为什么会出现四个灯全亮的情况呢?

答案是编译器优化了wait函数(可以查看led_rotate.dis),导致亮灯间隔时间不能被肉眼所感觉。

wait函数优化后的汇编代码如下:

0000001c <wait>:
1c:	e12fff1e 	bx	lr


解决这个问题有两种办法:

1、去掉Makfile中gcc编译选项-O2,不让gcc优化。

2、修改wait函数,在wait函数中增加nop指令,如下:

void  wait(unsigned long dly)
{
for(; dly > 0; dly--){
__asm__(
"NOP\n"
"NOP\n"
);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: