GPIO实验之c语言
上一章节进行实验使用的是汇编进行编程的,本次实验是使用c语言进行编写的。
(1)点亮一个led灯
1)启动文件:
crt.S
.text
.global _start
_start:
ldr R0,=0x53000000
mov R1,#0x00000000
str R1,[R0]
ldr sp,=1024*4
bl main
halt_loop:
b halt_loop
2)led.c
#define GPFCON (*(volatile unsigned long*)0x56000050)
#define GPFDAT (*(volatile unsigned long*)0x56000054)
int main()
{
GPFCON= 0x00000100; //可能对其他位进行产生影响
GPFDAT= 0x00000000;
return 0;
}
3)Makefile
led.bin: crt.S led.c
arm-linux-gcc -g -c -o crt.o crt.S
arm-linux-gcc -g -c -o led.o led.c
arm-linux-ld -Ttext 0x00000000 -g led.o crt.o -o led_elf
arm-linux-objcopy -O binary -S led_elf led.bin
arm-linux-objdump -D -m arm led_elf > led.dis
clean:
rm -f led_elf led.dis led.bin *.o
(2)流水灯实验
启动文件:crt.S
源文件:leds.c
#define GPFCON (*(volatile unsigned long*)0x56000050)
#define GPFDAT (*(volatile unsigned long*)0x56000054)
#define GPF4_reset (3<<(4*2))
#define GPF5_reset (3<<(5*2))
#define GPF6_reset (3<<(6*2))
#define GPF4_out (1<<(4*2))
#define GPF5_out (1<<(5*2))
#define GPF6_out (1<<(6*2))
void Delay_ms(volatile unsigned long ms)//延时
{
for(; ms>0; ms--);
}
int main()
{
GPFCON &=~(GPF4_reset | GPF5_reset | GPF6_reset);
GPFCON |= GPF4_out | GPF5_out | GPF6_out; //输出
while(1)
{
GPFDAT &=~(1<<4);
Delay_ms(30000);
GPFDAT |=(1<<4);
GPFDAT &=~(1<<5);
Delay_ms(30000);
GPFDAT |=(1<<5);
GPFDAT &=~(1<<6);
Delay_ms(30000);
GPFDAT |=(1<<6);
}
}
Makefile:
leds.bin: crt.S leds.c
arm-linux-gcc -g -c -o crt.o crt.S
arm-linux-gcc -g -c -o leds.o leds.c
arm-linux-ld -Ttext -g crt.o leds.o -o leds_elf
arm-linux-objcopy -O binary -S leds_elf leds.bin
arm-linux-objdump -D -m arm leds_elf >leds.dis
clean:
rm -f leds_elf leds.bin leds.dis *.o
(3)按键控制led
按键采用外部上拉的形式连接,其中S2,S3,S4分别连接于2440的GPF0,GPF2,GPG3引脚。
启动文件:crt.S
源文件:led_key.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_reset (3<<(4*2))
#define GPF5_reset (3<<(5*2))
#define GPF6_reset (3<<(6*2))
#define GPF4_out (1<<(4*2))
#define GPF5_out (1<<(5*2))
#define GPF6_out (1<<(6*2))
#define GPF0_reset (3<<(0*2))
#define GPF2_reset (3<<(2*2))
#define GPG3_reset (3<<(3*3))
#define GPF0_in (0<<(0*2))
#define GPF2_in (0<<(2*2))
#define GPG3_in (0<<(3*2))
int main()
{
GPFCON &=~(GPF4_reset | GPF5_reset | GPF6_reset);//输出
GPFCON |= GPF_out4 | GPF_out5 | GPF_out6;
GPFCON &=~(GPF0_rest | GPF2_reset);
GPFCON |= GPF0_in | GPF2_in;//该处可要可不要,由于[x:x]=00,通过上面语句已经实现
GPGCON &=~GPG3_reset;
GPFCON |= GPG3_in; //该处可以省去
while(1)
{
if(GPFDAT & (1<<0)) //按键未按
GPFDAT |= (1<<4); //led1灭掉
else
GPFDAT &=~(1<<4); //led1点亮
if(GPFDAT & (1<<2))
GPFDAT |= (1<<5);
else
GPFDAT &=~(1<<5);
if(GPGDAT & (1<<3))
GPGDAT |=(1<<6);
else
GPGDAT &=~(1<<6);
}
}
Makefile:
led_key.bin: crt.S led_key.c
arm-linux-gcc -g -c -o crt.o crt.S
arm-linux-gcc -g -c -o led_key.o led_key.c
arm-linux-ld -Ttext 0x00000000 -g crt.o led_key.o led_key_elf
arm-linux-objcopy -O binary -S led_key_elf led_key.bin
arm-linux-objdump -D -m arm led_key_elf > led_key.dis
clean:
rm -f led_key_elf led_key.dis led_key.bin *.o
通过本次实验简单掌握了S3C244的gpio操作,gpio段落也算告一段落了。下面进行总结一下:
posted on 2017-11-27 14:53 Darren_pty 阅读(2122) 评论(0) 编辑 收藏 举报