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

如需转载请注明出处

本实验是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"
		    );
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值