【文件属性】:
文件名称:51单片机可调时钟
文件大小:219KB
文件格式:DOC
更新时间:2016-05-27 19:07:55
单片机
#include
#define uchar unsigned char
#define uint unsigned int;
uint aa,ee,ff,temp;
uchar miao,shi,fen,num,num1,flag;
uchar code table0[]={0x80,0x90,0xb0,0xc0,0xe0,0xf0};
uchar code table6[]={0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89};
uchar code table5[]={0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99};
uchar code table4[]={0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9};
uchar code table3[]={0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9};
uchar code table2[]={0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9};
uchar code table1[]={0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9};
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void init()
{
miao=58;
fen=58;
shi=23;
TMOD=0X11;
TH1=(65536-50000)/256;
TL1=(65536-50000)%6;
EA=1;
ET1=1;
TR1=1;
}
void displaymiao(uchar date)
{
uchar shiwei,ge;
shiwei=date/10;
ge=date;
P0=table2[shiwei];
delay(2);
P0=table1[ge];
delay(2);
}
void displayfen(uchar date)
{
uchar shiwei,ge;
shiwei=date/10;
ge=date;
P0=table4[shiwei];
delay(2);
P0=table3[ge];
delay(2);
}
void displayshi(uchar date)
{
uchar shiwei,ge;
shiwei=date/10;
ge=date;
P0=table6[shiwei];
delay(2);
P0=table5[ge];
delay(2);
}
void keyb()
{
P2=0xfe;
temp=P2;
temp=temp&0xf8;
while(temp!=0xf8)
{
delay(10);
temp=temp&0xf8;
switch(temp)
{
case 0xf0:num=1;
break;
case 0xe8:num=2;
break;
case 0xd8:num=3;
break;
}
while(temp!=0xf8)
{
temp=P2;
temp=temp&0xf8;
}
}
P2=0xfd;
temp=P2;
temp=temp&0xf8;
while(temp!=0xf8)
{
delay(10);
temp=temp&0xf8;
switch(temp)
{
case 0xf0:num=4;
break;
case 0xe8:num=5;
break;
case 0xd8:num=6;
break;
}
while(temp!=0xf8)
{
temp=P2;
temp=temp&0xf8;
}
}
P2=0xfb;
temp=P2;
temp=temp&0xf8;
while(temp!=0xf8)
{
delay(10);
temp=temp&0xf8;
switch(temp)
{
case 0xf0:num=7;
break;
case 0xe8:num=8;
break;
case 0xd8:num=9;
break;
}
while(temp!=0xf8)
{
temp=P2;
temp=temp&0xf8;
}
}
if(num==1)
{
num=0;
TR1=0;
}
if(num==2)
{
num=0;
TR1=1;
}
if(num==3)
{
num=0;
TR1=0;
{
fen++;
if(fen==60)
{
fen=0;
}
}
}
if(num==4)
{
num=0;
TR1=0;
delay(50000);
shi++;
if(shi==24)
{
shi=0;
}
}
}
void main()
{
init();
while(1)
{
keyb();
if(aa==20)
{
aa=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
}
}
}
displaymiao(miao);
displayfen(fen);
displayshi(shi);
}
}
void timer1() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%6;
aa++;
}