c51单片机定时器30秒倒计时程序,一个按键控制开始,另一个按键控制暂停和继续
1个回答
展开全部
#include<reg51.h>
unsigned char code ledtab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};//0-9
unsigned char sec=30,scanled;
unsigned char disdat[2];
sbit keys=P1^0;
sbit keyc=P1^1;
sbit fmq=P3^0;
void delay(unsigned int x)
{
unsigned int i,j;
for(i=0;i<x;i++)
for(j=0;j<120;j++);
}
void dischg()
{
disdat[0]=sec/10;
disdat[1]=sec%10;
}
void t0isr() interrupt 1 //秒计时
{
TH0=0x3c;
TL0=0xb0;
time++;
if(time==20)
{
time=0;
if(sec>0 && flag==0)sec--;
}
dischg();
}
void t1isr() interrupt 3 //显示
{
TH1=0xec;
TL1=0x78;
switch(scanled)
{
case 0:
P2=0xfe;
P0=ledtab[disdat[1]];
break;
case 1:
P2=0xfd;
P0=ledtab[disdat[0]];
break;
default:break;
}
scanled++;
scanled%=2;
}
main()
{
TMOD=0x11;
TH0=0x3c;
TL0=0xb0;
TH1=0xec;
TL1=0x78;
TR1=1;
TR0=0;
ET0=1;
ET1=1;
EA=1;
fmq=0;
scanled=0;
while(1)
{
if(keys==0)
{
while(keys==0);
TR0=1;
}
if(keyc==0)
{
while(keyc==0);
TR0=~TR0;
}
}
}
追问
你好,你上面的那个程序有毛病,编译错误,顺便问一下在AT89c51芯片,2.2开发板上该怎样接线
追答
少许错误,你自己应该可以发现和排除的。
#include<reg51.h>
unsigned char code ledtab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};//0-9
unsigned char time,sec=30,scanled;
unsigned char disdat[2];
sbit keys=P1^0;
sbit keyc=P1^1;
sbit fmq=P3^0;
void delay(unsigned int x)
{
unsigned int i,j;
for(i=0;i<x;i++)
for(j=0;j<120;j++);
}
void dischg()
{
disdat[0]=sec/10;
disdat[1]=sec%10;
}
void t0isr() interrupt 1 //秒计时
{
TH0=0x3c;
TL0=0xb0;
time++;
if(time==20)
{
time=0;
if(sec>0)sec--;
if(sec==0)
{
TR0=0;
fmq=0;
delay(200);
}
}
dischg();
}
void t1isr() interrupt 3 //显示
{
TH1=0xec;
TL1=0x78;
switch(scanled)
{
case 0:
P2=0xfe;
P0=ledtab[disdat[1]];
break;
case 1:
P2=0xfd;
P0=ledtab[disdat[0]];
break;
default:break;
}
scanled++;
scanled%=2;
}
......主程序不变
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询