51单片机程序框架之矩阵按键

news/发布时间2024/5/19 5:45:05
/******************************************************************************
此程序是依据吴坚鸿程序框架,在普中51 A2单片机开发板上的程序练习
程序目标:4*4矩阵按键
*******************************************************************************/
#include<REG51.H>
#define Main_Fosc 12000000L //默认系统时钟12Mhz
#define T1MS  (65536-Main_Fosc/12/1000)  //12分频下1ms定时器的装载值,n=t/T=t/(12/f)=0.001*f/12=f/12/1000
#define Key_Debounce 40  //按键debounce time
sbit Key_Line1_Output=P1^7;
sbit Key_Line2_Output=P1^6;
sbit Key_Line3_Output=P1^5;
sbit Key_Line4_Output=P1^4;
sbit Key_Row1_Input=P1^3;
sbit Key_Row2_Input=P1^2;
sbit Key_Row3_Input=P1^1;
sbit Key_Row4_Input=P1^0;
sbit LED1=P2^0;
sbit LED2=P2^1;
sbit LED3=P2^2;
sbit LED4=P2^3;
sbit LED5=P2^4;
sbit LED6=P2^5;
sbit LED7=P2^6;
sbit LED8=P2^7;
unsigned char Key_Handle=0;  //按键值
void Sys_Init();    //系统初始化
void Delay_Long();   //长延时,等待系统稳定
void Perpherial_Init();  //端口初始化
void Key_Scan();  //按键扫描函数
void Key_Service(); //按键响应函数void main()
{Sys_Init(); Delay_Long();Perpherial_Init();while (1){Key_Service();}}void Sys_Init()
{TMOD=0X01; //定时器0模式1TL0=T1MS;TH0=T1MS>>8;
}void Delay_Long()
{unsigned char i,j;for(i=0;i++;i<220){for(j=0;j<220;j++);}
}void Perpherial_Init()
{ET0=1;TR0=1;EA=1;
}void Timer0_ISR() interrupt 1   //定时器0中断函数
{TL0=T1MS;TH0=T1MS>>8;Key_Scan();
}void Key_Scan()
{static unsigned int Key_CNT;static unsigned char Key_Lock=0;static unsigned char KeyScan_Step;static unsigned char KeyOutput_LineNum=1;switch (KeyScan_Step){case 0:if (1==KeyOutput_LineNum){Key_Line1_Output=0;Key_Line2_Output=1;Key_Line3_Output=1;Key_Line4_Output=1;}else if (2==KeyOutput_LineNum){Key_Line1_Output=1;Key_Line2_Output=0;Key_Line3_Output=1;Key_Line4_Output=1;}else if (3==KeyOutput_LineNum){Key_Line1_Output=1;Key_Line2_Output=1;Key_Line3_Output=0;Key_Line4_Output=1;}else if (4==KeyOutput_LineNum){Key_Line1_Output=1;Key_Line2_Output=1;Key_Line3_Output=1;Key_Line4_Output=0;}Key_CNT=0;KeyScan_Step++;break;case 1:Key_CNT++;if (Key_CNT>=2){Key_CNT=0;KeyScan_Step++;}break;case 2:if ((1==Key_Row1_Input)&&(1==Key_Row2_Input)&&(1==Key_Row3_Input)&&(1==Key_Row4_Input)){Key_CNT=0;Key_Lock=0;KeyScan_Step=0;KeyOutput_LineNum++;if(KeyOutput_LineNum>=5){KeyOutput_LineNum=1;}}else if (0==Key_Lock){if ((0==Key_Row1_Input)&&(1==Key_Row2_Input)&&(1==Key_Row3_Input)&&(1==Key_Row4_Input)){Key_CNT++;if (Key_CNT>Key_Debounce){Key_Lock=1;Key_CNT=0;if (1==KeyOutput_LineNum){Key_Handle=1;}else if (2==KeyOutput_LineNum){Key_Handle=5;}else if (3==KeyOutput_LineNum){Key_Handle=9;}else if(4==KeyOutput_LineNum){Key_Handle=13;}}}else if ((1==Key_Row1_Input)&&(0==Key_Row2_Input)&&(1==Key_Row3_Input)&&(1==Key_Row4_Input)){Key_CNT++;if (Key_CNT>Key_Debounce){Key_Lock=1;Key_CNT=0;if (1==KeyOutput_LineNum){Key_Handle=2;}else if (2==KeyOutput_LineNum){Key_Handle=6;}else if (3==KeyOutput_LineNum){Key_Handle=10;}else if (4==KeyOutput_LineNum){Key_Handle=14;}}              }else if ((1==Key_Row1_Input)&&(1==Key_Row2_Input)&&(0==Key_Row3_Input)&&(1==Key_Row4_Input)){Key_CNT++;if (Key_CNT>Key_Debounce){Key_Lock=1;Key_CNT=0;if (1==KeyOutput_LineNum){Key_Handle=3;}else if (2==KeyOutput_LineNum){Key_Handle=7;}else if (3==KeyOutput_LineNum){Key_Handle=11;}else if (4==KeyOutput_LineNum){Key_Handle=15;}  }   }else if ((1==Key_Row1_Input)&&(1==Key_Row2_Input)&&(1==Key_Row3_Input)&&(0==Key_Row4_Input)){Key_CNT++;if (Key_CNT>Key_Debounce){Key_Lock=1;Key_CNT=0;if (1==KeyOutput_LineNum){Key_Handle=4;}else if (2==KeyOutput_LineNum){Key_Handle=8;}else if (3==KeyOutput_LineNum){Key_Handle=12;}else if (4==KeyOutput_LineNum){Key_Handle=16;}              }}}break;}}void Key_Service()
{if (0==Key_Handle){return;}switch (Key_Handle){case 1:LED1=0;Key_Handle=0;break;case 2:LED2=0;Key_Handle=0;break;case 3:LED3=0;Key_Handle=0;break;  case 4:LED4=0;Key_Handle=0;break;case 5:LED5=0;Key_Handle=0;break;case 6:LED6=0;Key_Handle=0;break;case 7:LED7=0;Key_Handle=0;break;case 8:LED8=0;Key_Handle=0;break;case 9:LED1=1;Key_Handle=0;break;case 10:LED2=1;Key_Handle=0;break;case 11:LED3=1;Key_Handle=0;break;case 12:LED4=1;Key_Handle=0;break;case 13:LED5=1;Key_Handle=0;break;case 14:LED6=1;Key_Handle=0;break;case 15:LED7=1;Key_Handle=0;break;case 16:LED8=1;Key_Handle=0;break;}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ulsteruni.cn/article/51086722.html

如若内容造成侵权/违法违规/事实不符,请联系编程大学网进行投诉反馈email:xxxxxxxx@qq.com,一经查实,立即删除!

相关文章

[转]ptp(precision time protocol)时钟同步

一、介绍1:什么是ptpPTP(Precision Time Protocol) 是一个通过网络同步时钟的一个协议。当硬件支持时,PTP 精度能达到亚微秒,比 NTP(Network Time Protocol)精度更高。 2:ptp应用场景1)数据中心数据中心需要NTP/PTP同步,以确保集群的时域运行。同步对于虚拟机计算是必不…

Camunda 流程执行错误处理ERROR BOUNDARY EVENT

ERROR BOUNDARY EVENT:在任务发生异常时候会触发走,在代码中必须显式抛出throw new BpmnError("error.....");public void execute(DelegateExecution delegateExecution) throws Exception {System.out.println("进来了>>>>>>>>>…

关于diffusion model一些统计和数学的基础知识

likelihood-based models,通过(近似)最大似然直接学习分布的probability density(或mass)函数。典型的基于似然的模型包括自回归模型、归一化流模型、基于能量的模型(EBMs)和变分自编码器(VAEs)。 概率质量函数(Probability Mass Function,PMF):概率质量函数用于描述离散随…

AutoCAD C# 两不平行直线倒圆弧算法

参考的博客:https://www.cnblogs.com/JJBox/p/14300098.html 下面是计算示例主要计算代码:var peo = new PromptEntityOptions("选择直线1"){AllowNone = false,AllowObjectOnLockedLayer = false};peo.SetRejectMessage("请选择直线Line");peo.AddAllow…

2. 基础配置

1. 配置文件格式 1.1 配置文件自动提示功能消失解决方案 ​​ 1.2 SpringBoot配置文件加载顺序(了解) application.properties > application.yml > application.yaml 1.3 注意事项 SpringBoot核心配置文件名为application SpringBoot内置属性过多,且所有属性集中…

搭建自己的博客

基于github和Hexo 搭建自己的博客 【摘要】该教程基于个人的虚拟机和个人的GitHub,过程会详细注明对应的安装包的版本。 1、搭建hexo环境 环境配置 本地虚拟机:ubuntu 20.4(也可以基于对应的服务器) Hexo搭建步骤 1. 安装nodejs和npm 由于Ubuntu20通过apt安装nodejs默认只能…