基于单片机的,用C如何编程,让没按一下按键,数码管自动加一。另外一个按键没按一下,数码管自动减一。
2个回答
展开全部
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit wela=P2^4;
sbit dula=P2^3;
sbit key1=P2^1;
sbit key2=P2^0;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f
};
void display();
uint tt,bai,shi,ge;
void initial()
{
tt=100;
dula=1;
P0=0x3f;
dula=0;
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void main()
{
initial();
while(1)
{
bai=tt/100;
shi=tt%100/10;
ge=tt%10;
display();
if(key1==0) //加1
{
delay(5); //去抖
if(key1==0)
{
while(!key1);
delay(5); //去抖
while(!key1);
tt+=1;
}
}
if(key2==0) //减1
{
delay(5); //去抖
if(key2==0)
{
while(!key2);
delay(5); //去抖
while(!key2);
tt-=1;
}
}
}
}
void display()
{
wela=1;
P0=0xfe;
wela=0;
dula=1;
P0=table[bai];
dula=0;
delay(5);
wela=1;
P0=0xfd;
wela=0;
P0=0xff;
dula=1;
P0=table[shi];
dula=0;
delay(5);
wela=1;
P0=0xfb;
wela=0;
dula=1;
P0=table[ge];
dula=0;
delay(5);
}
#define uint unsigned int
#define uchar unsigned char
sbit wela=P2^4;
sbit dula=P2^3;
sbit key1=P2^1;
sbit key2=P2^0;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f
};
void display();
uint tt,bai,shi,ge;
void initial()
{
tt=100;
dula=1;
P0=0x3f;
dula=0;
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void main()
{
initial();
while(1)
{
bai=tt/100;
shi=tt%100/10;
ge=tt%10;
display();
if(key1==0) //加1
{
delay(5); //去抖
if(key1==0)
{
while(!key1);
delay(5); //去抖
while(!key1);
tt+=1;
}
}
if(key2==0) //减1
{
delay(5); //去抖
if(key2==0)
{
while(!key2);
delay(5); //去抖
while(!key2);
tt-=1;
}
}
}
}
void display()
{
wela=1;
P0=0xfe;
wela=0;
dula=1;
P0=table[bai];
dula=0;
delay(5);
wela=1;
P0=0xfd;
wela=0;
P0=0xff;
dula=1;
P0=table[shi];
dula=0;
delay(5);
wela=1;
P0=0xfb;
wela=0;
dula=1;
P0=table[ge];
dula=0;
delay(5);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询