51单片机里面用定时器实现倒计时,在LCD1602上显示倒计时,写完程序发现时间不能实现时间自减,求大神
#include<reg51.h>#include<intrins.h>sbitRS=P2^5;sbitRW=P2^6;sbitEP=P2^7;unsignedinta=...
#include<reg51.h>
#include<intrins.h>
sbit RS=P2^5;
sbit RW=P2^6;
sbit EP=P2^7;
unsigned int a=121;
int i;
unsigned char Disp_Tab[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A};
void timer_init()//定时器初始化函数
{
TMOD=0x11;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
TR0=1;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
TCON |=0X40;
IE|=0X08;
EA=1;
}
void delay_ms(unsigned int ms) //延时函数
{
unsigned char a,b,c;
while(ms--)
for(c=1;c>0;c--)
for(b=142;b>0;b--)
for(a=2;a>0;a--);
}
void write_com(char com) //输入命令函数
{
RS=0;
RW=0;
EP=0;
P0=com;
delay_ms(5);
EP=1;
delay_ms(5);
EP=0;
}
void write_date(char date)//输入数据函数
{
{
RS=1;
RW=0;
EP=0;
P0=date;
delay_ms(5);
EP=1;
delay_ms(5);
EP=0;
}
}
void write_init()//初始化函数
{
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void main()
{
timer_init();
while(1)
{
write_init();
delay_ms(10);
write_com(0x80+0x00);
write_date (Disp_Tab[a/600]);
write_date (Disp_Tab[a%600/60]);
write_date (Disp_Tab[10]);
write_date (Disp_Tab[a%600%60/10]);
write_date (Disp_Tab[a%600%60%10]);
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
i++;
if(i==20)
{
a--;
i=0;
}
} 展开
#include<intrins.h>
sbit RS=P2^5;
sbit RW=P2^6;
sbit EP=P2^7;
unsigned int a=121;
int i;
unsigned char Disp_Tab[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A};
void timer_init()//定时器初始化函数
{
TMOD=0x11;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
TR0=1;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
TCON |=0X40;
IE|=0X08;
EA=1;
}
void delay_ms(unsigned int ms) //延时函数
{
unsigned char a,b,c;
while(ms--)
for(c=1;c>0;c--)
for(b=142;b>0;b--)
for(a=2;a>0;a--);
}
void write_com(char com) //输入命令函数
{
RS=0;
RW=0;
EP=0;
P0=com;
delay_ms(5);
EP=1;
delay_ms(5);
EP=0;
}
void write_date(char date)//输入数据函数
{
{
RS=1;
RW=0;
EP=0;
P0=date;
delay_ms(5);
EP=1;
delay_ms(5);
EP=0;
}
}
void write_init()//初始化函数
{
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void main()
{
timer_init();
while(1)
{
write_init();
delay_ms(10);
write_com(0x80+0x00);
write_date (Disp_Tab[a/600]);
write_date (Disp_Tab[a%600/60]);
write_date (Disp_Tab[10]);
write_date (Disp_Tab[a%600%60/10]);
write_date (Disp_Tab[a%600%60%10]);
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
i++;
if(i==20)
{
a--;
i=0;
}
} 展开
展开全部
void main()
{
timer_init();
write_init();
while(1)
{
delay_ms(10);
write_com(0x80+0x00);
write_date (Disp_Tab[a/600]);
write_date (Disp_Tab[a%600/60]);
write_date (Disp_Tab[10]);
write_date (Disp_Tab[a/10]);
write_date (Disp_Tab[a%10]);
}
}
{
timer_init();
write_init();
while(1)
{
delay_ms(10);
write_com(0x80+0x00);
write_date (Disp_Tab[a/600]);
write_date (Disp_Tab[a%600/60]);
write_date (Disp_Tab[10]);
write_date (Disp_Tab[a/10]);
write_date (Disp_Tab[a%10]);
}
}
追问
这样还是不行啊
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询