改进代码:实现每按键一次,显示的对应数字右移1位。#include<reg51.h> #include<intrins.h> #define uchar unsigned char #define uint unsigned int uchar code led []={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff}; uchar find[]={11,11,11,11,11,11,11,11}; void delayms(uint xms) { uchar i; while(xms--) for(i=0;i<120;i++); } void dir() {uchar i,k,aa=0x01; for(i=0;i<8;i++) {P2=aa; k=find[i]; P0=led[k]; aa=_crol_(aa,1); delayms(1); P0=0xff; } } uchar keychuli() { uchar key; P1=0xff; key=P1; key=~key; key=key&0xff; return key; } uchar keyscan() { uchar keys,keyzhi; keys=keychuli(); if(keys!=0) { dir(); dir(); keys=keychuli(); if(keys!=0) { dir(); dir(); keys=keychuli(); if(keys!=0) { keyzhi=keys; while(keys!=0) { dir(); keys=keychuli(); } } } } else keyzhi=0; return keyzhi; } void keyaa() { find[0]=1; } void keybb() { find[1]=2; } void keycc() { find[2]=3; } void keydd() { find[3]=4; } void keyee() { find[4]=5; } void keyff() { find[5]=6; } void keygg() { find[6]=7; } void keyhh() { find[7]=8; } void dischuli() { find[1]=find[0]; find[2]=find[1]; find[3]=find[2]; find[4]=find[3]; find[5]=find[4]; find[6]=find[5]; find[7]=find[6]; find[8]=find[7]; } void main() { uchar key11; while(1) { dir(); key11=keyscan(); if(key11!=0) switch(key11) { case 0x00:break; case 0x01:keyaa();break; case 0x02:keybb();break; case 0x04:keycc();break; case 0x08:keydd();break; case 0x10:keyee();break; case 0x20:keyff();break; case 0x40:keygg();break; case 0x80:keyhh();break; default:break; } } }
时间: 2023-12-01 12:02:42 浏览: 99
改进后的代码如下:
```
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar code led []={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
uchar find[]={11,11,11,11,11,11,11,11};
void delayms(uint xms)
{
uchar i;
while(xms--)
for(i=0;i<120;i++);
}
void dir()
{
uchar i,k,aa=0x01;
for(i=0;i<8;i++)
{
P2=aa;
k=find[i];
P0=led[k];
aa=_crol_(aa,1);
delayms(1);
P0=0xff;
}
}
uchar keychuli()
{
uchar key;
P1=0xff;
key=P1;
key=~key;
key=key&0xff;
return key;
}
uchar keyscan()
{
static uchar prev_key = 0; // 用静态变量记录上一个按键值
uchar keys,keyzhi;
keys=keychuli();
if(keys!=0)
{
dir();
dir();
keys=keychuli();
if(keys!=0)
{
dir();
dir();
keys=keychuli();
if(keys!=0)
{
keyzhi=keys;
while(keys!=0)
{
dir();
keys=keychuli();
}
}
}
}
else keyzhi=0;
if (keyzhi != 0 && keyzhi != prev_key) { // 如果检测到按键按下且与上一次按键不同
prev_key = keyzhi;
return keyzhi;
} else {
return 0;
}
}
void keyaa() {} // 不需要改动
void keybb() {
find[0] = find[0] >> 1; // 右移一位
}
void keycc() {
find[1] = find[1] >> 1;
}
void keydd() {
find[2] = find[2] >> 1;
}
void keyee() {
find[3] = find[3] >> 1;
}
void keyff() {
find[4] = find[4] >> 1;
}
void keygg() {
find[5] = find[5] >> 1;
}
void keyhh() {
find[6] = find[6] >> 1;
}
void dischuli()
{
// 不需要改动
}
void main()
{
uchar key11;
while(1)
{
dir();
key11=keyscan();
if(key11!=0)
switch(key11)
{
case 0x00:break;
case 0x01:keyaa();break;
case 0x02:keybb();break;
case 0x04:keycc();break;
case 0x08:keydd();break;
case 0x10:keyee();break;
case 0x20:keyff();break;
case 0x40:keygg();break;
case 0x80:keyhh();break;
default:break;
}
}
}
```
修改思路是在 `keyscan` 函数中添加静态变量 `prev_key`,记录上一个按键值。如果检测到的按键值与上一个按键值不同,才返回该按键值并更新 `prev_key`。同时在对应的按键处理函数中将 `find` 数组的对应元素右移一位即可。
阅读全文