微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 51单片机做的数码管电子日历

51单片机做的数码管电子日历

时间:11-30 来源:互联网 点击:



电路图及仿真文件下载:http://www.51hei.com/bbs/dpj-20404-1.html
下面是程序源码:
#include
#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;
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top