程序中红 {MOD}部分是一种参数设置方法,按SET键切换参数,按UP键+,按DOWN键-。可是每设置一个参数都要调用按键扫描函数keyscan(),各位高人有没有更方便的办法实现参数设置呢?
#include<reg51.h>
unsigned char code LedChar[] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e
};
unsigned char LedBuff[4] = {0xff,0xff,0xff,0xff};
sbit gewei = P1^3;
sbit shiwei = P1^2;
sbit baiwei = P1^1;
unsigned char i = 0;
unsigned char iset = 0;
signed int disp0 = 15;
signed int disp1 = 21;
void delayms(unsigned int k)
{
unsigned int i,j;
for(i=k;i>0;i--)
for(j=110;j>0;j--);
}
unsigned char keyscan(void)
{
unsigned char keynum=0;
unsigned char temp=0;
P3=0xff; //P3口置oxff,准备读取输入值
if(P3!=0xff) //判断是否有键按下
{
delayms(10); //延时10ms消抖动
P3=0xff; //P3口置oxff,准备2次读取输入值
if(P3!=0xff) //2次判断是否有键按下
{
temp=P3; //读取P3口值至temp中
while(P3!=0xff); //按键释放检测语句
switch(temp) //进入开关多分支语句判断
{
case 0xfb:
keynum=1; //SET键
break;
case 0xf7:
keynum=2; //LEFT键
break;
case 0xef:
keynum=3; //UP键
break;
case 0xdf:
keynum=4; //DOWN键
break;
case 0xbf:
keynum=5; //模拟SET键长按
break;
case 0x7f:
keynum=6; //模拟LEFT键长按
break;
default:
break;
}
}
}
return keynum;
}
void main()
{
unsigned long sec = 0;
unsigned char key;
EA = 1;
TMOD = 0x01;
TH0 = 0xfc;
TL0 = 0x67;
ET0 = 1;
TR0 = 1;
while(1)
{
if(iset == 0)
{
key = keyscan();
if(key == 1) //SET键
{
iset++;
}
if(key == 3) //UP键
{
disp0++;
}
if(key == 4) //DOWN键
{
disp0--;
}
LedBuff[0] = LedChar[disp0 % 10];
LedBuff[1] = LedChar[disp0 / 10 % 10];
LedBuff[2] = LedChar[disp0 / 100 % 10];
}
else if(iset == 1)
{
key = keyscan();
if(key == 1) //SET键
{
iset++;
}
if(key == 3) //UP键
{
disp1++;
}
if(key == 4) //DOWN键
{
disp1--;
}
LedBuff[0] = LedChar[disp1 % 10];
LedBuff[1] = LedChar[disp1 / 10 % 10];
LedBuff[2] = LedChar[disp1 / 100 % 10];
}
}
}
void InterruptTimer0() interrupt 1
{
TH0 = 0xfc;
TL0 = 0x67;
P0 = 0xff;
if(i == 0)
{
baiwei = 1;
shiwei = 1;
gewei = 0;
i++;
P0 = LedBuff[0];
}
else if(i == 1)
{
baiwei = 1;
shiwei = 0;
gewei = 1;
i++;
P0 = LedBuff[1];
}
else if(i == 2)
{
baiwei = 0;
shiwei = 1;
gewei = 1;
i = 0;
P0 = LedBuff[2];
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
效率比较低
我一般的方法是,参数放在数组里面,DIS平时就显示参数【0】
然后
- switch(key){
- case 1:
- set++;
- break;
- case 2:
- 参数[set]++;
- break;
- case 3:
- 参数[set]--;
- break;
- }
复制代码在主循环里面判断set大于0则把参数[set]复制到参数【0】
一周热门 更多>