求一个单片机程序:三个按键,一个启动设置,一个加,一个减。

三个按键配合实现程序中的某个变量大小的设置。
设置过程在LCD1602上显示

第1个回答  推荐于2018-04-10
#include<reg52.h>

#define uchar unsigned char
#define uint unsigned int

sbit aj1=P3^4;
sbit aj2=P3^5;
sbit aj3=P3^6;
sbit aj4=P3^7;

sbit dula=P2^6;
sbit wela=P2^7;

uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,
0x00};

void delay(uint z);
void init();
void shumaguan(uchar shu);
void anjian();
uchar aa,shu;

void main()
{
init();
while(1)
{
anjian();
shumaguan(shu);

}
}

void init()
{
shu=0;
TMOD=0x01;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
EA=1;
ET0=1;
// TR0=1;

}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}

void dingshiqi0() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
aa++;
if(aa==20)
{
aa=0;
shu++;
if(shu==60)
shu=0;
}
}
void shumaguan(uchar shu)
{
uchar shi,ge;
dula=1;
P0=table[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay(5);

dula=1;
P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(5);

shi=shu/10;
ge=shu%10;
}

void anjian()
{
if(aj1==0)
{
delay(10);
if(aj1==0)
{
shu++;
if(shu==60)
shu=0;
while(!aj1)
shumaguan(shu);
while(!aj1);
}
}
if(aj2==0)
{
delay(10);
if(aj2==0)
{
if(shu==0)
shu=60;
shu--;
while(!aj2)
shumaguan(shu);
while(!aj2);
}
}
if(aj3==0)
{
delay(10);
if(aj3==0)
{
shu=0;
while(!aj3)
shumaguan(shu);
while(!aj3);
}
}
if(aj4==0)
{
delay(10);

if(aj4==0)
{
TR0=~TR0;

while(!aj4)
shumaguan(shu);
while(!aj4);
}
}

}

这是我以前写得4个按键的 要不用不上 你就把aj3去掉那个是清零的 你这问题补充的 先前怎么不一起加上 不知道你的IO口的定义 你这样吧 你把液晶的 地址 数据 设置好 然后用定时器 控制1秒++;在按键里面 和你要显示的液晶的 指令和数据 分解加上 就可以了本回答被网友采纳
相似回答