初级会员

- 积分
- 75
- 金钱
- 75
- 注册时间
- 2014-12-13
- 在线时间
- 0 小时
|
5金钱
void key_init()//GPIOB接的4*4矩阵键盘,第四位设置为推挽输出,高四位为上拉输入
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
int keyscan()//键盘扫描
{ u8 i,j,val ;
u16 temp;
GPIO_Write(GPIOB, 0xff0f);
temp=GPIO_ReadInputData(GPIOB);
if(temp!=0xff0f)//去抖动
{
delay_us(10);
if(temp!=0xff0f)
{
for(i=0;i<4;i++)//将低四位逐位置低
{
GPIO_Write(GPIOB, ~(1<<i));
temp=GPIO_ReadInputData(GPIOB);
for(j=4;j<8;j++)//检测高四位电平,为低电平则扫描成功,计算键值
{
if((1<<j)&&temp==0)
{
val=(j-4)*4+i;
return val;
}
}
}
}
}
}
|
最佳答案
查看完整内容[请看2#楼]
学会搜索找答案。
让别人看你代码,肯定不如你自己看别人代码。
http://www.openedv.com/posts/list/0/43776.htm
|