- 积分
- 368
贡献137
飞刀6 FD
注册时间2010-5-27
在线时间304 小时
|
发表于 2011-9-21 12:08:50
|
显示全部楼层
回复 1# cmz86121361
谁说一定要汇编的啊?下面我写的一个项目就是用纯C来控制LED的,因为是下载到S3C2440的内核SRAM中运行,不需要初始化外部SDRAM。因为只是对GPIO进行操作,所以也没有对CPU进行一些初始化。这个程序下载到S3C2440的内部SRAM中就能够正常运行了。
如果你是一些复杂的C代码,如需要设置堆栈等,或需要其他一些外设,如USB等,就需要用汇编对CPU,内存,外设等进行初始化了。
[guowenxue@localhost c_led]$ ls
led.c led.lds makefile
[guowenxue@localhost c_led]$ cat led.c
/***********************************************************************
* File: led.c
* Version: 1.0.0
* Copyright: 2011 (c) Guo Wenxue <guowenxue@gmail.com>
* Description: This C code used to turn LED0~LED4 on on FL2440 board
* ChangeLog: 1, Release initial version on "Mon Mar 21 21:09:52 CST 2011"
*
***********************************************************************/
#define GPBCON (*(unsigned long volatile *)0x56000010)
#define GPBDAT (*(unsigned long volatile *)0x56000014)
#define GPBUP (*(unsigned long volatile *)0x56000018)
#define LED0 5 /*LED0 use GPB5*/
#define LED1 6 /*LED1 use GPB6*/
#define LED2 8 /*LED2 use GPB8*/
#define LED3 10 /*LED3 use GPB10*/
#define DELAY_TIME 20000000
static inline void delay (unsigned long loops)
{
__asm__ volatile ("1:\n"
"subs %0, %1, #1\n"
"bne 1b":"=r" (loops):"0" (loops));
}
int main(void)
{
/* Set GPB5,GPB6,GPB8,GPB10 as GPIO mode(0x01) */
GPBCON = (GPBCON|0x333C00)&0x111400;
GPBUP = (GPBUP | 0x560);
while(1)
{
/* Set GPB5,GPB6,GPB8,GPB10 as high level, to turn LED0,LED1,LED2,LED3 off */
GPBDAT = (GPBDAT | 0x560);
delay(DELAY_TIME);
/* Turn LED0 on */
GPBDAT = (GPBDAT & (~(1<<LED0)) );
delay(DELAY_TIME);
/* Turn LED1 on */
GPBDAT = (GPBDAT & (~(1<<LED1)) );
delay(DELAY_TIME);
/* Turn LED2 on */
GPBDAT = (GPBDAT & (~(1<<LED2)) );
delay(DELAY_TIME);
/* Turn LED3 on */
GPBDAT = (GPBDAT & (~(1<<LED3)) );
delay(DELAY_TIME);
}
}
[guowenxue@localhost c_led]$ make
/opt/buildroot-2011.02/arm920t/usr/bin/arm-linux-gcc -c -o led.o led.c
/opt/buildroot-2011.02/arm920t/usr/bin/arm-linux-ld -Tled.lds led.o -o led.elf
/opt/buildroot-2011.02/arm920t/usr/bin/arm-linux-objcopy -O binary -S led.elf led.bin
rm -f *.elf *.o |
|