Mini2440学习笔记(二)——GPIO

 

硬件连接:

Mini2440有四个LED,使用的管脚是GPB5到GPB8,当管脚输出低电平时,led亮;六个按键使用的管脚是GPG0、GPG3、GPG5、GPG6、GPG7、GPG11,按下按键时,管脚是低电平状态。

 

GPIO寄存器:

GPXCON:每两位配置一个管脚,00为输入,01为输出;

GPXDAT:端口的数据寄存器,每一位表示对应管脚的状态,0为低电平,1为高电平;

GPXUP:设置管脚的上拉电阻,每一位对应一个管脚,0为使能,1为禁止。

 #ifndef S3c2440A_H #define S3C2440A_H /* * GPIO Register */ //GPB #define GPBCON (*(volatile unsigned long *)0x56000010) #define GPBDAT (*(volatile unsigned long *)0x56000014) #define GPBUP (*(volatile unsigned long *)0x56000018) //GPG #define GPGCON (*(volatile unsigned long *)0x56000060) #define GPGDAT (*(volatile unsigned long *)0x56000064) #define GPGUP (*(volatile unsigned long *)0x56000068) #endif

 

程序:

使用按键控制LED的亮灭,Key1~Key6非别表示数字1~6,每个LED代表一个二进制位,例如,当按下Key3时,LED1和LED2亮,表示二进制的0011。该程序可下载到内部4K-ROM中运行。

@****************************************************************************** @ File:init.S @ 功能:通过它转入C程序 @****************************************************************************** .text .global _start _start: ldr r0, =0x53000000 @ 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

 

/* * gpio.c * 用按键控制LED * 六个按键Key1~Key6,四个LED */ #include "s3c2440a.h" int main(void) { unsigned long key=0; unsigned long led=0; //led GPBCON &= (~(0xff<<10)); GPBCON |= (0x55<<10); //将GPB5~GPB8的管脚设为输出 GPBUP &= (~(0xf<<5)); GPBUP |= (0xf<<5); //禁止GPB5~GPB8的上拉电阻 //key GPGCON &= ~((3<<0)|(3<<6)|(3<<10)|(3<<12)|(3<<14)|(3<<22)); //将GPG0、3、5、6、7、11设为输入 GPGUP |= ((1<<0)|(1<<3)|(1<<5)|(1<<6)|(1<<7)|(1<<11)); //禁止上拉 while(1) { key = GPGDAT; //读取按键的状态 if(!(key & (1<<0))) //Key1 { GPBDAT &= (~(0xf<<5)); GPBDAT |= ((0xf-1)<<5); } if(!(key & (1<<3))) //Key2 { GPBDAT &= (~(0xf<<5)); GPBDAT |= ((0xf-2)<<5); } if(!(key & (1<<5))) //Key3 { GPBDAT &= (~(0xf<<5)); GPBDAT |= ((0xf-3)<<5); } if(!(key & (1<<6))) //Key4 { GPBDAT &= (~(0xf<<5)); GPBDAT |= ((0xf-4)<<5); } if(!(key & (1<<7))) //Key5 { GPBDAT &= (~(0xf<<5)); GPBDAT |= ((0xf-5)<<5); } if(!(key & (1<<11))) //Key6 { GPBDAT &= (~(0xf<<5)); GPBDAT |= ((0xf-6)<<5); } } return 0; }   

 

Makefile: 

all:init.S gpio.c arm-elf-gcc -g -c -o init.o init.S arm-elf-gcc -g -c -o gpio.o gpio.c arm-elf-ld -Tgpio.lds -g init.o gpio.o -o gpio_elf arm-elf-objcopy -O binary -S gpio_elf gpio.bin arm-elf-objdump -D -m arm gpio_elf > gpio.dis clean: rm -f gpio_elf gpio.bin gpio.dis *.o

你可能感兴趣的:(c,File,Flash,makefile)