赞
踩
(1)管脚配置方案:
/* 配置列col GPIO:输入下拉模式 */
GPIO_InitStruct.Pin = Matrx_keyCol1 |Matrx_keyCol2| Matrx_keyCol3;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;//GPIO_PULLUP
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(Matrx_key_GPIO, &GPIO_InitStruct);
/* 配置行row GPIO:输出推挽 模式 */
GPIO_InitStruct.Pin = Matrx_keyRow1| Matrx_keyRow2| Matrx_keyRow3 |Matrx_keyRow4;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(Matrx_key_GPIO, &GPIO_InitStruct);
(2)头文件名称是拷贝的,后来报错发现是中文输入法不对引起的。
(3)配置文件也是拷贝的,程序运行始终不能得到预想结果,单独设置管脚,点亮led都ok,于是重新写了一遍配置,如下所示。
//__HAL_RCC_GPIOF_CLK_ENABLE();
//GPIO_InitStruct.Pin = GPIO_PIN_0;
//GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
//GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
//GPIO_InitStruct.Pull = GPIO_NOPULL;
//HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
//
//GPIO_InitStruct.Pin = GPIO_PIN_1;
//GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
//GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
//GPIO_InitStruct.Pull = GPIO_NOPULL;
//HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
//
//GPIO_InitStruct.Pin = GPIO_PIN_2;
//GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
//GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
//GPIO_InitStruct.Pull = GPIO_NOPULL;
//HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
//
//GPIO_InitStruct.Pin = GPIO_PIN_3;
//GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
//GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
//GPIO_InitStruct.Pull = GPIO_NOPULL;
//HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
//
// GPIO_InitStruct.Pin = GPIO_PIN_4;
// GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
// GPIO_InitStruct.Pull = GPIO_PULLDOWN;//GPIO_PULLUP
// HAL_GPIO_Init(keys_GPIO, &GPIO_InitStruct);
//
// GPIO_InitStruct.Pin = GPIO_PIN_5;
// GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
// GPIO_InitStruct.Pull = GPIO_PULLDOWN;//GPIO_PULLUP
// HAL_GPIO_Init(keys_GPIO, &GPIO_InitStruct);
//
// GPIO_InitStruct.Pin = GPIO_PIN_6;
// GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
// GPIO_InitStruct.Pull = GPIO_PULLDOWN;//GPIO_PULLUP
// HAL_GPIO_Init(keys_GPIO, &GPIO_InitStruct);
下面是测试代码。
//HAL_GPIO_WritePin(GPIOF,GPIO_PIN_0,GPIO_PIN_SET) ;
//HAL_GPIO_WritePin(GPIOF,GPIO_PIN_0,GPIO_PIN_RESET) ;
测试通过,然后把最上面的配置代码自己写了一遍,通过。窝靠。
(4)我的程序中,IDR寄存器得不到预期效果,(Matrx_key_GPIO->IDR&0x00f0)!=0x0000 舍弃。转而使用读引脚函数。
//while((Matrx_key_GPIO->IDR&0x00f0)!=0x0000);//等待松开的函数也被舍弃,转而使用读引脚函数
while(Matrx_Read_col());
#define Matrx_Read_col()(Matrx_Readcol1()|Matrx_Readcol2()|Matrx_Readcol3())
在第二行扫描代码中,直接拷贝了第一行代码,如下。
Matrx_keyRow1_H(); //1
Matrx_keyRow2_L();Matrx_keyRow3_L();Matrx_keyRow4_L();//0
后来经过测试,修改如下。
Matrx_keyRow2_H(); //1
Matrx_keyRow1_L();Matrx_keyRow3_L();Matrx_keyRow4_L();//0
都是低级错误。
第二行扫描完整代码如下。
Matrx_keyRow2_H(); //1
Matrx_keyRow1_L();Matrx_keyRow3_L();Matrx_keyRow4_L();//0
if(Matrx_Read_col())
{
HAL_Delay(10);
if(Matrx_Read_col())
{
temp=Matrx_Readcol1();
temp=temp|(Matrx_Readcol2()<<1);
temp=temp|(Matrx_Readcol3()<<2);
switch(temp)
{
case 0x01:KeyValue='4';break;//不同端口及定义的值修改参数即可
case 0x02:KeyValue='5';break;
case 0x04:KeyValue='6';break;
//case 0x0800:KeyValue='E';break;
}
}
while(Matrx_Read_col());//while((Matrx_key_GPIO->IDR&0x00f0)!=0x0000);
//GPIO_SetBits(keys_GPIO,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15); //1
return KeyValue;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。