求一个基于AT89C51的一个C语言计时器程序
3个回答
展开全部
#include<reg51.h>
sbit RS=P2^0;
sbit RW=P2^1;
sbit E=P2^2;
sbit LED=P2^7;
char n[7][4]={"mon","Tue","Wed","Thu","Fri","Sat","sun"},(*p)[4],i=0;
char tt=0,g[2][3]={"am","pm"},*q;
unsigned char min=55 ,sec=0,hour=17,day=5,mon=12,age=10,a,b;
delay(unsigned char x) //延时;
{
int i,j;
for(i=0;i<=x;i++)
for(j=1;j<=150;j++);
}
void write_com(unsigned char com) //写指令;
{
RS=0;
RW=0;
P1=com;
delay(5);
E=1;
delay(5);
E=0;
}
void write_date(unsigned char date) //写数据;
{
RS=1;
RW=0;
P1=date;
delay(5);
E=1;
delay(5);
E=0;
}
void init(void) //初始化函数
{
write_com (0x01);
delay(5);
write_com (0x38);
delay(5);
write_com (0x06);
delay(5);
write_com (0x0c);
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
TR0=1;
EX0=1;
IT0=1;
EA=1;
}
void putstr(char *str) //写字符串;
{
while(*str)
{
write_date(*str);
delay(5);
str++;
}
}
void setxy(char i,char j) //指针定位;
{
if(i==1)
write_com(0x80+j-1);
else write_com(0xc0+j-1);
}
void put_str(char i,char j,unsigned char dat)
{
if(i==1)
write_com(0x80+j-1);
else write_com(0xc0+j-1);
write_date(dat);
}
void put_num(char i,char j,unsigned char dat)
{
if(i==1)
write_com(0x80+j-1);
else write_com(0xc0+j-1);
write_date(dat+'0');
}
void upnum()
{
if(sec>59){min++;sec=0;}
if(min>59){hour++;min=0;}
if(hour>12){q=g[1];}
if(hour>23){day++;hour=0;q=g[2];i++;}
if(day>30){mon++;day=0;}
if(mon>12){age++;mon=0;}
if(i>6)i=0;
}
void updata()
{
put_num(1,1,2);
put_num(1,2,0);
put_num(1,3,age/10);
put_num(1,4,age%10);
put_str(1,5,'-');
put_num(1,6,mon/10);
put_num(1,7,mon%10);
put_str(1,8,'-');
put_num(1,9,day/10);
put_num(1,10,day%10);
setxy(1,14);
putstr(p[i]);
put_num(2,1,hour/10);
put_num(2,2,hour%10);
put_str(2,3,':');
put_num(2,4,min/10);
put_num(2,5,min%10);
put_str(2,6,':');
put_num(2,7,sec/10);
put_num(2,8,sec%10);
setxy(2,15);
putstr(q);
}
void main()
{
init();
p=n;
while(1)
{
upnum();
updata();
}
}
void timer0 (void) interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
tt++;
if(tt>=20)
{
tt=0;
sec++;
}
}
void interr0(void) interrupt 0
{
}
没用数码管,而使用的1602液晶显示器。有用的程序你可以剪下来。
sbit RS=P2^0;
sbit RW=P2^1;
sbit E=P2^2;
sbit LED=P2^7;
char n[7][4]={"mon","Tue","Wed","Thu","Fri","Sat","sun"},(*p)[4],i=0;
char tt=0,g[2][3]={"am","pm"},*q;
unsigned char min=55 ,sec=0,hour=17,day=5,mon=12,age=10,a,b;
delay(unsigned char x) //延时;
{
int i,j;
for(i=0;i<=x;i++)
for(j=1;j<=150;j++);
}
void write_com(unsigned char com) //写指令;
{
RS=0;
RW=0;
P1=com;
delay(5);
E=1;
delay(5);
E=0;
}
void write_date(unsigned char date) //写数据;
{
RS=1;
RW=0;
P1=date;
delay(5);
E=1;
delay(5);
E=0;
}
void init(void) //初始化函数
{
write_com (0x01);
delay(5);
write_com (0x38);
delay(5);
write_com (0x06);
delay(5);
write_com (0x0c);
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
TR0=1;
EX0=1;
IT0=1;
EA=1;
}
void putstr(char *str) //写字符串;
{
while(*str)
{
write_date(*str);
delay(5);
str++;
}
}
void setxy(char i,char j) //指针定位;
{
if(i==1)
write_com(0x80+j-1);
else write_com(0xc0+j-1);
}
void put_str(char i,char j,unsigned char dat)
{
if(i==1)
write_com(0x80+j-1);
else write_com(0xc0+j-1);
write_date(dat);
}
void put_num(char i,char j,unsigned char dat)
{
if(i==1)
write_com(0x80+j-1);
else write_com(0xc0+j-1);
write_date(dat+'0');
}
void upnum()
{
if(sec>59){min++;sec=0;}
if(min>59){hour++;min=0;}
if(hour>12){q=g[1];}
if(hour>23){day++;hour=0;q=g[2];i++;}
if(day>30){mon++;day=0;}
if(mon>12){age++;mon=0;}
if(i>6)i=0;
}
void updata()
{
put_num(1,1,2);
put_num(1,2,0);
put_num(1,3,age/10);
put_num(1,4,age%10);
put_str(1,5,'-');
put_num(1,6,mon/10);
put_num(1,7,mon%10);
put_str(1,8,'-');
put_num(1,9,day/10);
put_num(1,10,day%10);
setxy(1,14);
putstr(p[i]);
put_num(2,1,hour/10);
put_num(2,2,hour%10);
put_str(2,3,':');
put_num(2,4,min/10);
put_num(2,5,min%10);
put_str(2,6,':');
put_num(2,7,sec/10);
put_num(2,8,sec%10);
setxy(2,15);
putstr(q);
}
void main()
{
init();
p=n;
while(1)
{
upnum();
updata();
}
}
void timer0 (void) interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
tt++;
if(tt>=20)
{
tt=0;
sec++;
}
}
void interr0(void) interrupt 0
{
}
没用数码管,而使用的1602液晶显示器。有用的程序你可以剪下来。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询