stm32f103c8t6的4×4矩阵键盘的实现代码
时间: 2023-04-03 15:04:07 浏览: 182
以下是 stm32f103c8t6 的 4×4 矩阵键盘的实现代码:
#include "stm32f10x.h"
#define ROW GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3
#define COL GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = ROW;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = COL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
uint8_t Key_Scan(void)
{
uint8_t i, j;
uint8_t key_value = 0;
for (i = 0; i < 4; i++)
{
GPIO_ResetBits(GPIOA, COL);
GPIO_SetBits(GPIOA, COL << i);
for (j = 0; j < 4; j++)
{
if (GPIO_ReadInputDataBit(GPIOA, ROW << j) == 0)
{
key_value = i * 4 + j + 1;
while (GPIO_ReadInputDataBit(GPIOA, ROW << j) == 0)
;
}
}
}
return key_value;
}
int main(void)
{
GPIO_Configuration();
while (1)
{
uint8_t key_value = Key_Scan();
if (key_value != 0)
{
// 处理按键事件
}
}
}
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)