stay hungry stay foolish
实验二——流水灯
一。原理图等
GPF寄存器地址:
二。源码
Makefile:
1 led.bin : crt0.S led.c
2 arm-linux-gcc -Wall -O2 -c -o crt0.o crt0.S
3 arm-linux-gcc -Wall -O2 -c -o led.o led.c
4 arm-linux-ld -Ttext 0x0000000 crt0.o led.o -o led_elf
5 arm-linux-objcopy -O binary -S led_elf led.bin
6 arm-linux-objdump -D -m arm led_elf > led.dis
7 clean:
8 rm -f led.dis led.bin led_elf *.o
crt0.S:
1 .text
2 .global _start
3 _start:
4 ldr r0,=0x53000000
5 mov r1,#0x00000000
6 str r1,[r0]
7
8 ldr sp,=1024*4
9
10 bl main
11 halt_loop:
12 b halt_loop
然后是led.c
:
1 /*************************************************************************
2 > File Name: led.c
3 > Author: hulig
4 > Mail: fang682@oki.com
5 > Created Time: 2014年11月06日 星期四 20时38分59秒
6 >tested ok
7 ************************************************************************/
//寄存器地址
8 #define GPFCON (*(volatile unsigned long*)0x56000050)
9 #define GPFDAT (*(volatile unsigned long*)0x56000054)
10 //引脚定义
11 #define GPF5_out (1<<5*2)
12 #define GPF6_out (1<13 #define GPF4_out (1<14 //延时函数
15 void delay()
16 {
17 unsigned long cnt;
18 for(cnt=0;cnt<100000;cnt++);
19 }
20 //主函数
21 int main(void)
22 {
23 GPFCON=GPF5_out|GPF6_out|GPF4_out;
24 while(1){
25 GPFDAT |= (1<<5);
26 delay();
27 GPFDAT &=~(1<<5);
28 delay();
29 GPFDAT |= (1<<6);
30 delay();
31 GPFDAT &=~(1<<6);
32 delay();
33 GPFDAT |=(1<<4);
34 delay();
35 GPFDAT &=~(1<<4);
36 delay();
37 }
38 return 0;
39 }
代码很简单就不作解释啦。当然还有另一个写法:
1 /*************************************************************************
2 > File Name: led.c
3 > Author: hulig
4 > Mail: fang682@oki.com
5 > Created Time: 2014年11月07日 星期四 18时36分29秒
6 >tested ok
7 ************************************************************************/
8
9
10 #define GPFCON (*(volatile unsigned long*)0x56000050)
11 #define GPFDAT (*(volatile unsigned long*)0x56000054)
12
13 #define GPF5_out (1<<5*2)
14 #define GPF6_out (1<15 #define GPF4_out (1<16
17
18 #define led1_on (~(1<<4))
19 #define led2_on (~(1<<5))
20 #define led3_on (~(1<<6))
21
22 #define led1_off (1<<4)
23 #define led2_off (1<<5)
24 #define led3_off (1<<6)
25
26
27 void delay()
28 {
29 unsigned long cnt;
30 for(cnt=0;cnt<100000;cnt++);
31 }
32
33
34
35 int main(void)
36 {
37
38 GPFCON=GPF5_out|GPF6_out|GPF4_out;
39 while(1)
40 {
41 GPFDAT &=led1_on;
42 delay();
43 GPFDAT |=led1_off;
44 delay();
45
46 GPFDAT &=led2_on;
47 delay();
48 GPFDAT |=led2_off;
49 delay();
50
51 GPFDAT &=led3_on;
52 delay();
53 GPFDAT |=led3_off;
54 delay();
55 }
56 return 0;
57 }
其实就是换一种写法而已。