写一段程序使数码管显示(静态+动态),按下k1k2数 码管数字加一,按下k3k4数码管数字减一,K1k3 控制数码管左四位,k2k4控制数码管右四位。 (加需要能加到100,减需要能减到-10)。
时间: 2023-04-02 20:02:12 浏览: 132
b027智慧社区居家养老健康管理系统-springboot+vue+elementui.zip(可运行源码+sql文件+文档)
很高兴回答你的问题。以下是程序代码:
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit k1 = P1^;
sbit k2 = P1^1;
sbit k3 = P1^2;
sbit k4 = P1^3;
uchar code table[] = {x3f, x06, x5b, x4f, x66, x6d, x7d, x07, x7f, x6f};
uchar num[8] = {, , , , , , , };
uchar flag = ;
void delay(uint xms)
{
uint i, j;
for(i=xms; i>; i--)
for(j=110; j>; j--);
}
void display()
{
uchar i;
for(i=; i<8; i++)
{
switch(i)
{
case : P2 = xfe; break;
case 1: P2 = xfd; break;
case 2: P2 = xfb; break;
case 3: P2 = xf7; break;
case 4: P2 = xef; break;
case 5: P2 = xdf; break;
case 6: P2 = xbf; break;
case 7: P2 = x7f; break;
}
P = table[num[i]];
delay(1);
P = x00;
}
}
void keyscan()
{
if(k1 == )
{
delay(10);
if(k1 == )
{
num[]++;
if(num[] > 9)
num[] = ;
flag = 1;
}
while(!k1);
}
if(k2 == )
{
delay(10);
if(k2 == )
{
num[1]++;
if(num[1] > 9)
num[1] = ;
flag = 1;
}
while(!k2);
}
if(k3 == )
{
delay(10);
if(k3 == )
{
num[2]--;
if(num[2] < )
num[2] = 9;
flag = 1;
}
while(!k3);
}
if(k4 == )
{
delay(10);
if(k4 == )
{
num[3]--;
if(num[3] < )
num[3] = 9;
flag = 1;
}
while(!k4);
}
}
void main()
{
while(1)
{
keyscan();
if(flag)
{
flag = ;
if(num[] == 1 && num[1] == && num[2] == && num[3] == )
{
num[] = ;
num[1] = ;
num[2] = ;
num[3] = ;
}
if(num[] > 1 || (num[] == 1 && num[1] > ))
{
num[] = 1;
num[1] = ;
num[2] = ;
num[3] = ;
}
if(num[] < || (num[] == && num[1] == && num[2] == && num[3] < -1))
{
num[] = ;
num[1] = ;
num[2] = ;
num[3] = -1;
}
display();
}
}
}
阅读全文