2,嵌入式Linux之GPIO
1,点灯
1)看懂你所用的开发板的原理图 产看led和GPIO管脚是怎么接线的如图:
2) 通过s3c2440芯片控制寄存器配置GPF4 GPF5 GPF6为输入引脚 来点亮 上图的led灯
查看s3c2440芯片手册:
3) 配置GPIO 使得GPF4 GPF5 GPF6 为输出引脚并输出低电平
用c语言先点亮GPF4 程序如下:
#define GPFCON (*(volatile unsigned long *) 0x56000050)
#define GPFDAT (*(volatile unsigned long *) 0x56000054)
Int main (){
GPFCON = 0x100;
Gpfdat = 0x0;
Return 0;
} //点亮 gpf4引脚的所在的那盏灯。
循环点灯 GPF 4 5 6:
#define gpfcon (*(volatile unsigned long *) 0x56000050)
#define gpfdat (*(volatile unsigned long *) 0x56000054)
#define gpf4_out (1<<(4*2))
#define gpf5_out (1<<(5*2))
#define gpf6_out (1<<(6*2))
Void wait(volatile unsigned dly){
For(;dly>0;dly--);
}
Int main(void){
unsigned long I=0;
Gpfcon = gpf4_out|gpf5_out|gpf6_out;
While(1){
Wait(3000);
Gpfdat = (~(i<<4));
If(++i==8){
I=0;
}
}
Return 0;
}
4),按键点灯原理图:
①配置按键为输入引脚 led为输出引脚
②读按键的值 根据值设置led的引脚值
#define GPFCON (*(volatile unsigned long *)0x56000050)
#define GPFDAT (*(volatile unsigned long *)0x56000054)
#define GPGCON (*(volatile unsigned long *)0x56000060)
#define GPGDAT (*(volatile unsigned long *)0x56000064)
/*
* LED1,LED2,LED4对应GPF4、GPF5、GPF6
*/
#define GPF4_out (1<<(4*2)) // 01 00 00 00 00 bit[9:8] 01
#define GPF5_out (1<<(5*2)) // 01 00 00 00 00 00 bit[11:10] 01
#define GPF6_out (1<<(6*2)) // 01 00 00 00 00 00 00 bit[13:12] 01
#define GPF4_msk (3<<(4*2)) // 11 00 00 00 00 bit[9:8] 11
#define GPF5_msk (3<<(5*2)) // 11 00 00 00 00 00 bit[11:10] 11
#define GPF6_msk (3<<(6*2)) // 11 00 00 00 00 00 00 bit[13:12] 11
/*
* S2,S3,S4对应GPF0、GPF2、GPG3
*/
#define GPF0_in (0<<(0*2))
#define GPF2_in (0<<(2*2))
#define GPG3_in (0<<(3*2))
#define GPF0_msk (3<<(0*2))
#define GPF2_msk (3<<(2*2))
#define GPG3_msk (3<<(3*2))
int main()
{
unsigned long dwDat;
// LED1,LED2,LED4对应的3根引脚设为输出
/*
GPF4_msk 11 00 00 00 00
GPF5_msk 11 00 00 00 00 00
GPF6_msk 11 00 00 00 00 00 00
GPF4_msk | GPF5_msk | GPF6_msk = 11 11 11 00 00 00 00
~(GPF4_msk | GPF5_msk | GPF6_msk) = 00 00 00 11 11 11 11
*/
GPFCON &= ~(GPF4_msk | GPF5_msk | GPF6_msk);
/*
GPF4_out | GPF5_out | GPF6_out = 01 01 01 00 00 00 00
*/
GPFCON |= GPF4_out | GPF5_out | GPF6_out;
// S2,S3对应的2根引脚设为输入
GPFCON &= ~(GPF0_msk | GPF2_msk);
GPFCON |= GPF0_in | GPF2_in;
// S4对应的引脚设为输入
GPGCON &= ~GPG3_msk;
GPGCON |= GPG3_in;
while(1){
//若Kn为0(表示按下),则令LEDn为0(表示点亮)
dwDat = GPFDAT; // 读取GPF管脚电平状态
if (dwDat & (1<<0)) // S2没有按下
GPFDAT |= (1<<4); // LED1熄灭
else
GPFDAT &= ~(1<<4); // LED1点亮
if (dwDat & (1<<2)) // S3没有按下
GPFDAT |= (1<<5); // LED2熄灭
else
GPFDAT &= ~(1<<5); // LED2点亮
dwDat = GPGDAT; // 读取GPG管脚电平状态
if (dwDat & (1<<3)) // S4没有按下
GPFDAT |= (1<<6); // LED3熄灭
else
GPFDAT &= ~(1<<6); // LED3点亮
}
return 0;
}