跪求一个at89c51单片机的c语言程序
跪求一个at89c51单片机的c语言程序只需要时分秒的功能,3只两位共阳极数码管显示,用一个开关实现时分秒的切换。...
跪求一个at89c51单片机的c语言程序只需要时分秒的功能,3只两位共阳极数码管显示,用一个开关实现时分秒的切换。
展开
1个回答
展开全部
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit db=P2^7;
char i,sec,min,h,date,month,year,flag;
uchar j,k,m,n,o,p;
sbit dula=P2^0;
sbit wela1=P2^1;
sbit wela2=P2^2;
sbit key_ch=P3^5;
sbit key_add=P3^6;
sbit key_minus=P3^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void init();
void delay(uint);
void second_display();
void minute_display();
void hour_display();
void date_display();
void month_display();
void year_display();
void control();
void time();
void main()
{
init();
while(1)
{
second_display();
minute_display();
hour_display();
date_display();
month_display();
year_display();
control();
}
}
void init()
{
db=0;
i=0;
sec=0;
min=0;
h=0;
date=1;
month=1;
year=1;
flag=0;
wela1=0;
wela2=0;
EA=1;
ET0=1;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void second_display()
{
int sec_shi,sec_ge;
sec_shi=sec/10;
sec_ge=sec%10;
if(flag==1&&j!=1)
j++;
else
{
j=0;
dula=1;
P0=~table[sec_ge];
dula=0;
wela1=1;
P1=0x00;
wela1=0;
wela2=1;
P1=0x20;
wela2=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
dula=1;
P0=~table[sec_shi];
dula=0;
wela1=1;
P1=0x00;
wela1=0;
wela2=1;
P1=0x10;
wela2=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
}
}
void minute_display()
{
int min_shi,min_ge;
min_shi=min/10;
min_ge=min%10;
if(flag==2&&k!=1)
k++;
else
{
k=0;
dula=1;
P0=~table[min_ge];
dula=0;
wela1=1;
P1=0x00;
wela1=0;
wela2=1;
P1=0x08;
wela2=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
dula=1;
P0=~table[min_shi];
dula=0;
wela1=1;
P1=0x00;
wela1=0;
wela2=1;
P1=0x04;
wela2=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
}
}
void hour_display()
{
int h_shi,h_ge;
h_shi=h/10;
h_ge=h%10;
if(flag==3&&m!=1)
m++;
else
{
m=0;
dula=1;
P0=~table[h_ge];
dula=0;
wela1=1;
P1=0x00;
wela1=0;
wela2=1;
P1=0x02;
wela2=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
delay(1);
dula=1;
P0=~table[h_shi];
dula=0;
wela1=1;
P1=0x00;
wela1=1;
P1=0x00;
wela1=0;
wela2=1;
P1=0x01;
wela2=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
wela2=0;
delay(1);
}
}
void date_display()
{
int date_shi,date_ge;
date_shi=date/10;
date_ge=date%10;
if(flag==4&&n!=1)
n++;
else
{
n=0;
dula=1;
P0=~table[date_ge];
dula=0;
wela2=1;
P1=0x00;
wela2=0;
wela1=1;
P1=0x80;
wela1=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
dula=1;
P0=~table[date_shi];
dula=0;
wela2=1;
P1=0x00;
wela2=0;
wela1=1;
P1=0x40;
wela1=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
}
}
void month_display()
{
int month_shi,month_ge;
month_shi=month/10;
month_ge=month%10;
if(flag==5&&o!=1)
o++;
else
{
o=0;
dula=1;
P0=~table[month_ge];
dula=0;
wela2=1;
P1=0x00;
wela2=0;
wela1=1;
P1=0x20;
wela1=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
dula=1;
P0=~table[month_shi];
dula=0;
wela2=1;
P1=0x00;
wela2=0;
wela1=1;
P1=0x10;
wela1=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
}
}
void year_display()
{
int year_qian,year_bai,year_shi,year_ge;
year_qian=year/1000;
year_bai=year%1000/100;
year_shi=year%1000%100/10;
year_ge=year%10;
if(flag==6&&p!=1)
p++;
else
{
p=0;
dula=1;
P0=~table[year_ge];
dula=0;
wela2=1;
P1=0x00;
wela2=0;
wela1=1;
P1=0x08;
wela1=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
dula=1;
P0=~table[year_shi];
dula=0;
wela2=1;
P1=0x00;
wela2=0;
wela1=1;
P1=0x04;
wela1=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
dula=1;
P0=~table[year_bai];
dula=0;
wela2=1;
P1=0x00;
wela2=0;
wela1=1;
P1=0x02;
wela1=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
dula=1;
P0=~table[2];
dula=0;
wela2=1;
P1=0x00;
wela2=0;
wela1=1;
P1=0x01;
wela1=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
wela1=0;
}
}
void control()
{
if(!key_ch)
{
delay(5);
if(!key_ch)
{
flag++;
if(flag==7)
flag=0;
}
}
while(!key_ch);
if(flag==1&&key_add==0)
{
while(!key_add);
sec++;
if(sec==60)
sec=0;
}
if(flag==1&&key_minus==0)
{
while(!key_minus);
sec--;
if(sec==-1)
sec=59;
}
if(flag==2&&key_add==0)
{
while(!key_add);
min++;
if(min==60)
min=0;
}
if(flag==2&&key_minus==0)
{
while(!key_minus);
min--;
if(min==-1)
min=59;
}
if(flag==3&&key_add==0)
{
while(!key_add);
h++;
if(h==24)
h=0;
}
if(flag==3&&key_minus==0)
{
while(!key_minus);
h--;
if(h==-1)
h=23;
}
if(flag==4&&key_add==0)
{
while(!key_add);
date++;
if(date==29)
if((year%4!=0)&&(month==2))
date=1;
if(date==30)
if((year%4==0)&&(month==2))
date=1;
if(date==31)
if((month==4)||(month==6)||(month==9)||(month==11))
date=1;
if(date==32)
if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
date=1;
}
if(flag==5&&key_add==0)
{
while(!key_add);
month++;
if(month==13)
month=1;
}
if(flag==5&&key_minus==0)
{
while(!key_minus);
month--;
if(month==0)
month=12;
}
if(flag==6&&key_add==0)
{
while(!key_add);
year++;
if(year==99)
year=1;
}
if(flag==6&&key_minus==0)
{
while(!key_minus);
year--;
if(year==0)
year=99;
}
}
void T0_rpt() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
i++;
time();
}
void time()
{
if(i==20)
{
i=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
h++;
if(h==24)
{
h=0;
min=0;
sec=0;
date++;
if(date==29)
if((year%4!=0)&&(month==2))
{
date=1;
month++;
if(month==13)
{
month=1;
year++;
}
}
if(date==30)
if((year%4==0)&&(month==2))
{
date=1;
month++;
if(month==13)
{
month=1;
year++;
}
}
if(date==31)
if((month==4)||(month==6)||(month==9)||(month==11))
{
date=1;
month++;
if(month==13)
{
month=1;
year++;
}
}
if(date==32)
if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
{
date=1;
month++;
if(month==13)
{
month=1;
year++;
}
}
}
}
}
}
}
#define uchar unsigned char
#define uint unsigned int
sbit db=P2^7;
char i,sec,min,h,date,month,year,flag;
uchar j,k,m,n,o,p;
sbit dula=P2^0;
sbit wela1=P2^1;
sbit wela2=P2^2;
sbit key_ch=P3^5;
sbit key_add=P3^6;
sbit key_minus=P3^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void init();
void delay(uint);
void second_display();
void minute_display();
void hour_display();
void date_display();
void month_display();
void year_display();
void control();
void time();
void main()
{
init();
while(1)
{
second_display();
minute_display();
hour_display();
date_display();
month_display();
year_display();
control();
}
}
void init()
{
db=0;
i=0;
sec=0;
min=0;
h=0;
date=1;
month=1;
year=1;
flag=0;
wela1=0;
wela2=0;
EA=1;
ET0=1;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void second_display()
{
int sec_shi,sec_ge;
sec_shi=sec/10;
sec_ge=sec%10;
if(flag==1&&j!=1)
j++;
else
{
j=0;
dula=1;
P0=~table[sec_ge];
dula=0;
wela1=1;
P1=0x00;
wela1=0;
wela2=1;
P1=0x20;
wela2=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
dula=1;
P0=~table[sec_shi];
dula=0;
wela1=1;
P1=0x00;
wela1=0;
wela2=1;
P1=0x10;
wela2=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
}
}
void minute_display()
{
int min_shi,min_ge;
min_shi=min/10;
min_ge=min%10;
if(flag==2&&k!=1)
k++;
else
{
k=0;
dula=1;
P0=~table[min_ge];
dula=0;
wela1=1;
P1=0x00;
wela1=0;
wela2=1;
P1=0x08;
wela2=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
dula=1;
P0=~table[min_shi];
dula=0;
wela1=1;
P1=0x00;
wela1=0;
wela2=1;
P1=0x04;
wela2=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
}
}
void hour_display()
{
int h_shi,h_ge;
h_shi=h/10;
h_ge=h%10;
if(flag==3&&m!=1)
m++;
else
{
m=0;
dula=1;
P0=~table[h_ge];
dula=0;
wela1=1;
P1=0x00;
wela1=0;
wela2=1;
P1=0x02;
wela2=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
delay(1);
dula=1;
P0=~table[h_shi];
dula=0;
wela1=1;
P1=0x00;
wela1=1;
P1=0x00;
wela1=0;
wela2=1;
P1=0x01;
wela2=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
wela2=0;
delay(1);
}
}
void date_display()
{
int date_shi,date_ge;
date_shi=date/10;
date_ge=date%10;
if(flag==4&&n!=1)
n++;
else
{
n=0;
dula=1;
P0=~table[date_ge];
dula=0;
wela2=1;
P1=0x00;
wela2=0;
wela1=1;
P1=0x80;
wela1=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
dula=1;
P0=~table[date_shi];
dula=0;
wela2=1;
P1=0x00;
wela2=0;
wela1=1;
P1=0x40;
wela1=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
}
}
void month_display()
{
int month_shi,month_ge;
month_shi=month/10;
month_ge=month%10;
if(flag==5&&o!=1)
o++;
else
{
o=0;
dula=1;
P0=~table[month_ge];
dula=0;
wela2=1;
P1=0x00;
wela2=0;
wela1=1;
P1=0x20;
wela1=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
dula=1;
P0=~table[month_shi];
dula=0;
wela2=1;
P1=0x00;
wela2=0;
wela1=1;
P1=0x10;
wela1=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
}
}
void year_display()
{
int year_qian,year_bai,year_shi,year_ge;
year_qian=year/1000;
year_bai=year%1000/100;
year_shi=year%1000%100/10;
year_ge=year%10;
if(flag==6&&p!=1)
p++;
else
{
p=0;
dula=1;
P0=~table[year_ge];
dula=0;
wela2=1;
P1=0x00;
wela2=0;
wela1=1;
P1=0x08;
wela1=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
dula=1;
P0=~table[year_shi];
dula=0;
wela2=1;
P1=0x00;
wela2=0;
wela1=1;
P1=0x04;
wela1=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
dula=1;
P0=~table[year_bai];
dula=0;
wela2=1;
P1=0x00;
wela2=0;
wela1=1;
P1=0x02;
wela1=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
dula=1;
P0=~table[2];
dula=0;
wela2=1;
P1=0x00;
wela2=0;
wela1=1;
P1=0x01;
wela1=0;
delay(1);
dula=1;
P0=0xff;
dula=0;
wela1=0;
}
}
void control()
{
if(!key_ch)
{
delay(5);
if(!key_ch)
{
flag++;
if(flag==7)
flag=0;
}
}
while(!key_ch);
if(flag==1&&key_add==0)
{
while(!key_add);
sec++;
if(sec==60)
sec=0;
}
if(flag==1&&key_minus==0)
{
while(!key_minus);
sec--;
if(sec==-1)
sec=59;
}
if(flag==2&&key_add==0)
{
while(!key_add);
min++;
if(min==60)
min=0;
}
if(flag==2&&key_minus==0)
{
while(!key_minus);
min--;
if(min==-1)
min=59;
}
if(flag==3&&key_add==0)
{
while(!key_add);
h++;
if(h==24)
h=0;
}
if(flag==3&&key_minus==0)
{
while(!key_minus);
h--;
if(h==-1)
h=23;
}
if(flag==4&&key_add==0)
{
while(!key_add);
date++;
if(date==29)
if((year%4!=0)&&(month==2))
date=1;
if(date==30)
if((year%4==0)&&(month==2))
date=1;
if(date==31)
if((month==4)||(month==6)||(month==9)||(month==11))
date=1;
if(date==32)
if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
date=1;
}
if(flag==5&&key_add==0)
{
while(!key_add);
month++;
if(month==13)
month=1;
}
if(flag==5&&key_minus==0)
{
while(!key_minus);
month--;
if(month==0)
month=12;
}
if(flag==6&&key_add==0)
{
while(!key_add);
year++;
if(year==99)
year=1;
}
if(flag==6&&key_minus==0)
{
while(!key_minus);
year--;
if(year==0)
year=99;
}
}
void T0_rpt() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
i++;
time();
}
void time()
{
if(i==20)
{
i=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
h++;
if(h==24)
{
h=0;
min=0;
sec=0;
date++;
if(date==29)
if((year%4!=0)&&(month==2))
{
date=1;
month++;
if(month==13)
{
month=1;
year++;
}
}
if(date==30)
if((year%4==0)&&(month==2))
{
date=1;
month++;
if(month==13)
{
month=1;
year++;
}
}
if(date==31)
if((month==4)||(month==6)||(month==9)||(month==11))
{
date=1;
month++;
if(month==13)
{
month=1;
year++;
}
}
if(date==32)
if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
{
date=1;
month++;
if(month==13)
{
month=1;
year++;
}
}
}
}
}
}
}
追问
谢谢大神
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询