[STM32] 4 * 4矩阵键盘扫描程序(中断模式)

电脑杂谈  发布时间:2020-11-29 22:04:25  来源:网络整理

4*4矩阵键盘扫描程序_4 4矩阵键盘扫描程序_4 4矩阵键盘扫描程序

点击下载源代码

在上一篇文章中,我以扫描模式编写了一个键盘程序,但是最近我正在制作一个需要CPU响应速度的小型游戏,所以让我们编写一个简单的键盘中断检测程序。

4*4矩阵键盘扫描程序_4 4矩阵键盘扫描程序_4 4矩阵键盘扫描程序

4 4矩阵键盘扫描程序

一般思路:中断线是4行连接的GPIO端口。矩阵的4行初始化为输出低电平,而4列初始化为输出高电平。当按下一个按钮时,上升沿触发中断。在中断处理功能中,键值是通过查询获得的(请参阅上一篇文章)。

4 4矩阵键盘扫描程序_4 4矩阵键盘扫描程序_4*4矩阵键盘扫描程序

程序的一部分

void EXTI9_5_IRQHandler(void)                           //外部中断中断函数
{
		if(EXTI_GetITStatus(EXTI_Line7) != RESET)           //检测中断标志位
		{			
			//行置高,列置低,扫描列
			GPIO_SetBits ( ROW1_GPIO_PORT, ROW1_GPIO_PIN );
			
			GPIO_ResetBits ( LINE1_GPIO_PORT, LINE1_GPIO_PIN );
		    GPIO_ResetBits ( LINE2_GPIO_PORT, LINE2_GPIO_PIN );
		    GPIO_ResetBits ( LINE3_GPIO_PORT, LINE3_GPIO_PIN );
			GPIO_ResetBits ( LINE4_GPIO_PORT, LINE4_GPIO_PIN );
			//取4列的状态
			if(GPIO_ReadInputDataBit ( LINE1_GPIO_PORT, LINE1_GPIO_PIN ))
			{
				while(GPIO_ReadInputDataBit ( LINE1_GPIO_PORT, LINE1_GPIO_PIN )); //等待按键松开,不然出现连续进入中断的错误
				KeyValue = 1;
									
			}
			if(GPIO_ReadInputDataBit ( LINE2_GPIO_PORT, LINE2_GPIO_PIN ))
			{
				while(GPIO_ReadInputDataBit ( LINE2_GPIO_PORT, LINE2_GPIO_PIN ));
				KeyValue = 2;
			}
			if(GPIO_ReadInputDataBit ( LINE3_GPIO_PORT, LINE3_GPIO_PIN ))
			{
				while(GPIO_ReadInputDataBit ( LINE3_GPIO_PORT, LINE3_GPIO_PIN ));
				KeyValue = 3;
			}
           if(GPIO_ReadInputDataBit ( LINE4_GPIO_PORT, LINE4_GPIO_PIN ))
			{
				while(GPIO_ReadInputDataBit ( LINE4_GPIO_PORT, LINE4_GPIO_PIN ));
				KeyValue = 4;
			}				
			EXTI_ClearITPendingBit(EXTI_Line7);                         //清除标志位	
		}		
		
		
		if(EXTI_GetITStatus(EXTI_Line6) != RESET)           //检测中断标志位
		{
		
			GPIO_SetBits ( ROW2_GPIO_PORT, ROW2_GPIO_PIN );
			
			GPIO_ResetBits ( LINE1_GPIO_PORT, LINE1_GPIO_PIN );
		    GPIO_ResetBits ( LINE2_GPIO_PORT, LINE2_GPIO_PIN );
		    GPIO_ResetBits ( LINE3_GPIO_PORT, LINE3_GPIO_PIN );
			GPIO_ResetBits ( LINE4_GPIO_PORT, LINE4_GPIO_PIN );
			//取4列的状态
			if(GPIO_ReadInputDataBit ( LINE1_GPIO_PORT, LINE1_GPIO_PIN ))
			{
				while(GPIO_ReadInputDataBit ( LINE1_GPIO_PORT, LINE1_GPIO_PIN ));
				KeyValue = 5;			
			}
			if(GPIO_ReadInputDataBit ( LINE2_GPIO_PORT, LINE2_GPIO_PIN ))
			{
				while(GPIO_ReadInputDataBit ( LINE2_GPIO_PORT, LINE2_GPIO_PIN ));
				KeyValue = 6;
			}
			if(GPIO_ReadInputDataBit ( LINE3_GPIO_PORT, LINE3_GPIO_PIN ))
			{
				while(GPIO_ReadInputDataBit ( LINE3_GPIO_PORT, LINE3_GPIO_PIN ));
				KeyValue = 7;
			}
			if(GPIO_ReadInputDataBit ( LINE4_GPIO_PORT, LINE4_GPIO_PIN ))
			{
				while(GPIO_ReadInputDataBit ( LINE4_GPIO_PORT, LINE4_GPIO_PIN ));
				KeyValue = 8;
			}
			EXTI_ClearITPendingBit(EXTI_Line6);                         //清除标志位
		}
		
		if(EXTI_GetITStatus(EXTI_Line5) != RESET)           //检测中断标志位
		{
	
			GPIO_SetBits ( ROW3_GPIO_PORT, ROW3_GPIO_PIN );
			
			GPIO_ResetBits ( LINE1_GPIO_PORT, LINE1_GPIO_PIN );
		    GPIO_ResetBits ( LINE2_GPIO_PORT, LINE2_GPIO_PIN );
		    GPIO_ResetBits ( LINE3_GPIO_PORT, LINE3_GPIO_PIN );
			GPIO_ResetBits ( LINE4_GPIO_PORT, LINE4_GPIO_PIN );
			//取4列的状态
			if(GPIO_ReadInputDataBit ( LINE1_GPIO_PORT, LINE1_GPIO_PIN ))
			{
				while(GPIO_ReadInputDataBit ( LINE1_GPIO_PORT, LINE1_GPIO_PIN ));
				KeyValue = 9;			
			}
			if(GPIO_ReadInputDataBit ( LINE2_GPIO_PORT, LINE2_GPIO_PIN ))
			{
				while(GPIO_ReadInputDataBit ( LINE2_GPIO_PORT, LINE2_GPIO_PIN ));
				KeyValue = 10;
			}
			if(GPIO_ReadInputDataBit ( LINE3_GPIO_PORT, LINE3_GPIO_PIN ))
			{
				while(GPIO_ReadInputDataBit ( LINE3_GPIO_PORT, LINE3_GPIO_PIN ));
				KeyValue = 11;
			}
			if(GPIO_ReadInputDataBit ( LINE4_GPIO_PORT, LINE4_GPIO_PIN ))
			{
				while(GPIO_ReadInputDataBit ( LINE4_GPIO_PORT, LINE4_GPIO_PIN ));
				KeyValue = 12;
			}
			EXTI_ClearITPendingBit(EXTI_Line5);                         //清除标志位
		}
				
		//重置GPIO,等待下次中断
	    //行置高
		GPIO_ResetBits ( ROW1_GPIO_PORT, ROW1_GPIO_PIN );
		GPIO_ResetBits ( ROW2_GPIO_PORT, ROW2_GPIO_PIN );
		GPIO_ResetBits ( ROW3_GPIO_PORT, ROW3_GPIO_PIN );
	    GPIO_ResetBits ( ROW4_GPIO_PORT, ROW4_GPIO_PIN );
    	//列置高
		GPIO_SetBits ( LINE1_GPIO_PORT, LINE1_GPIO_PIN );
		GPIO_SetBits ( LINE2_GPIO_PORT, LINE2_GPIO_PIN );
		GPIO_SetBits ( LINE3_GPIO_PORT, LINE3_GPIO_PIN );	
		GPIO_SetBits ( LINE4_GPIO_PORT, LINE4_GPIO_PIN );	
	}
void EXTI4_IRQHandler(void) 
{
if(EXTI_GetITStatus(EXTI_Line4) != RESET)           //检测中断标志位
		{
		
			
			GPIO_SetBits ( ROW4_GPIO_PORT, ROW4_GPIO_PIN );
			
			GPIO_ResetBits ( LINE1_GPIO_PORT, LINE1_GPIO_PIN );
		    GPIO_ResetBits ( LINE2_GPIO_PORT, LINE2_GPIO_PIN );
		    GPIO_ResetBits ( LINE3_GPIO_PORT, LINE3_GPIO_PIN );
			GPIO_ResetBits ( LINE4_GPIO_PORT, LINE4_GPIO_PIN );
			//取4列的状态
			if(GPIO_ReadInputDataBit ( LINE1_GPIO_PORT, LINE1_GPIO_PIN ))
			{
				while(GPIO_ReadInputDataBit ( LINE1_GPIO_PORT, LINE1_GPIO_PIN ));
				KeyValue = 13;		
			}
			if(GPIO_ReadInputDataBit ( LINE2_GPIO_PORT, LINE2_GPIO_PIN ))
			{
				while(GPIO_ReadInputDataBit ( LINE2_GPIO_PORT, LINE2_GPIO_PIN ));
				KeyValue = 14;
			}
			if(GPIO_ReadInputDataBit ( LINE3_GPIO_PORT, LINE3_GPIO_PIN ))
			{
				while(GPIO_ReadInputDataBit ( LINE3_GPIO_PORT, LINE3_GPIO_PIN ));
				KeyValue = 15;   
			}
			if(GPIO_ReadInputDataBit ( LINE4_GPIO_PORT, LINE4_GPIO_PIN ))
			{
				while(GPIO_ReadInputDataBit ( LINE4_GPIO_PORT, LINE4_GPIO_PIN ));
				KeyValue = 16;   
			}
			EXTI_ClearITPendingBit(EXTI_Line4);                         //清除标志位
		}
		//重置GPIO,等待下次中断
	    //行置高
		GPIO_ResetBits ( ROW1_GPIO_PORT, ROW1_GPIO_PIN );
		GPIO_ResetBits ( ROW2_GPIO_PORT, ROW2_GPIO_PIN );
		GPIO_ResetBits ( ROW3_GPIO_PORT, ROW3_GPIO_PIN );
	    GPIO_ResetBits ( ROW4_GPIO_PORT, ROW4_GPIO_PIN );
    	//列置高
		GPIO_SetBits ( LINE1_GPIO_PORT, LINE1_GPIO_PIN );
		GPIO_SetBits ( LINE2_GPIO_PORT, LINE2_GPIO_PIN );
		GPIO_SetBits ( LINE3_GPIO_PORT, LINE3_GPIO_PIN );	
		GPIO_SetBits ( LINE4_GPIO_PORT, LINE4_GPIO_PIN );	
}

4 4矩阵键盘扫描程序_4*4矩阵键盘扫描程序_4 4矩阵键盘扫描程序

注意:

①选择开发板的IO端口时,请务必查看原理图或数据表,以确保这8个IO端口可用。

4*4矩阵键盘扫描程序_4 4矩阵键盘扫描程序_4 4矩阵键盘扫描程序

②使用外部中断时,打开多路IO时钟AFIO

③中断程序中必须有一个按钮释放检测功能,否则它将始终进入中断服务功能。

如果要下载源代码,可以在邮箱中留言(设置0C几天后价格会自动上涨)。还有其他建议,欢迎交流! ! !


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/bofangqi/article-336928-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...