欢迎来到天天文库
浏览记录
ID:46797889
大小:42.00 KB
页数:6页
时间:2019-11-27
《51单片机的键盘扫描程序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、一个51单片机的键盘扫描程序,算法简单有效/**************************************** 键盘_不采用定时器_不延时 特点: 按键在松手后有效,灵敏度高,消耗资源少,运行效率高 独立键盘为:K01=P2^4;K02=P2^5;K03=P2^6;K04=P2^7; 矩阵键盘为:行(上到下)_P2.3_P2.2_P2.1_P2.0 列(左到右)_P2.7_P2.6_P2.5_P2.4 提供的操作函数: //独立键盘.无按键动作时其返回值num_key=0,否则返回按键号num_key externunsignedcharkeyboa
2、rd_self(); //矩阵键盘.无按键动作时其返回值num_key=0,否则返回按键号num_key****检测高四位 externunsignedcharkeyboard_matrix(); ****************************************/先看独立键盘(和矩阵键盘的算法一样) ----------------------------------------------------------------------- #include #include //独立键盘.无按键动作时其返回值num_key
3、=0,否则返回按键号num_key externunsignedcharkeyboard_self() { unsignedcharnum_key=0;//按键号 unsignedchartemp=0;//用于读取P2线上按键值 staticunsignedchartemp_code=0;//保存按键值 staticunsignedcharnum_check=0;//低电平有效次数 staticunsignedcharkey_flag=0;//按键有效标识 temp=P2&0xF0;//读取P2线数据
4、 if(temp!=0xF0)//低电平判断 { num_check++; if(num_check==10)//连续10次(10ms)低电平有效,则认为按键有效 { key_flag=1;//使能按键有效标识 temp_code=temp;//保存按键值 } } else//松手时判断 { num_check=0; if(key_flag==1)
5、//按键有效 { key_flag=0; switch(temp_code)//读取按键号 { case0xE0:num_key=1; break; case0xD0:num_key=2; break; case0xB0:nu
6、m_key=3; break; case0x70:num_key=4; break; } } } return(num_key); } 现在是矩阵键盘的 ----------------------------------------------------------------------- #include #include<
7、intrins.h> //矩阵键盘.无按键动作时其返回值num_key=0,否则返回按键号num_key****检测高四位 externunsignedcharkeyboard_matrix() { unsignedcharnum_key=0;//按键号 unsignedchartemp=0;//读取P2口线数据 staticunsignedchartemp_code=0;//用于保存按键值 staticunsignedchart
此文档下载收益归作者所有