单片机C语言源程序如下:
#include "key.h"
unsigned char key_scan(unsigned char mode,unsigned int ti) //带返回值的按键扫描函数
{
static unsigned char key_up=1; //按键按松开标志,key_up=1为无按键按下,key_up=0为有按键按下
static unsigned int times;
if(mode)key_up=1; //mode为1,支持连按
if(key_up && (up==0||unit==0||down==0||md==0||led==0)) //只要在key_up=1时,任意一个按键按下
{
times++; //记录进入低电平的时间
if(times>=ti) //抖动的时间已过
{
times=0;
key_up=0; //有按键按下
if(up==0)return 1;
else if(unit==0)return 2;
else if(down==0)return 3;
else if(md==0)return 4;
else if(led==0)return 5;
}
}
else if(up==1&&unit==1&&down==1&&md==1&&led==1)
key_up=1; //还是默认电平,说明无按键按下
return 0;
}
这个要么5个键单按有效, 要么5个键连按有效,能不能分开或加进去一个定时器实现两个键单按,剩下三个键连按功能?
|