郭天祥的51TX-1c开发板上的做的
#include<reg52.h>
#define uchar unsigned char
uchar code table[]="2013-07-08";
sbit dual=P2^6;
sbit weal=P2^7;
sbit rd=P3^7;
sbit rs=P3^5;
sbit lcdn=P3^4;
sbit sda=P2^0;
sbit scl=P2^1;
sbit beep=P2^3;
sbit S1=P3^0;
sbit S2=P3^1;
sbit S3=P3^2;
uchar miao,fen,shi,snum,num;
void delay()
{;;}//24C02芯片初始化延时时间
void delayms(int xms)
{
uchar i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}//延时
void laba()
{
beep=0;
delayms(500);
beep=1;
}
void write_com(uchar com)
{
rs=0;
lcdn=0;
P0=com;
delayms(5);
lcdn=1;
delayms(5);
lcdn=0;
}//LCD指令初始化
void write_date(uchar date)
{
rs=1;
lcdn=0;
P0=date;
delayms(5);
lcdn=1;
delayms(5);
lcdn=0;
}//LCD数据初始化
void lcdxianshi(uchar wei,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+wei);
write_date(0x30+shi);
write_date(0x30+ge);
}//显示时间
void init24c02()
{
sda=1;
delay();
scl=1;
delay();
}
void start()
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}//启动24c02
void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void respons()
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<255))i++;
scl=0;
delay();
}
void write_1(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay();
sda=CY;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_1()
{
uchar i,k;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
k=((k<<1)|sda);
scl=0;
delay();
}
return k;
}
void write_2(uchar address,uchar date)
{
start();
write_1(0xa0);
respons();
write_1(address);
respons();
write_1(date);
respons();
stop();
}
uchar read_2(uchar address)
{
uchar date;
start();
write_1(0xa0);
respons();
write_1(address);
respons();
start();
write_1(0xa1);
respons();
date=read_1();
stop();
return date;
}
void init()
{
uchar i;
dual=0;
weal=0;
lcdn=0;
rd=0;
shi=20;
fen=40;
miao=9;
snum=0;
init24c02();
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(i=0;i<10;i++)
{
write_date(table[i]);
delayms(5);
}
write_com(0x80+0x40+6);
write_date(':');
delayms(5);
write_com(0x80+0x40+9);
write_date(':');
miao=read_2(1);
fen=read_2(2);
shi=read_2(3);
lcdxianshi(10,miao);
lcdxianshi(7,fen);
lcdxianshi(4,shi);
TMOD=0x01;
TH0=0x4C;
TL0=0x00;
EA=1;
ET0=1;
TR0=1;
}
void jianpanjiance()
{
if(S1==0)
{
delayms(5);
if(S1==0)
{
while(S1!=0);
snum++;
laba();
if(snum==1)
{
TR0=0;
write_com(0x80+0x40+10);
write_com(0x0f);
}
if(snum==2)
{
write_com(0x80+0x40+7);
}
if(snum==3)
{
write_com(0x80+0x40+4);
}
if(snum==4)
{
snum=0;
TR0=1;
write_com(0x0c);
}
}
}
if(snum!=0)
{
if(S2==0)
{
delayms(5);
if(S2==0)
{
while(S2!=0);
laba();
if(snum==1)
{
miao++;
if(miao==60)
{
miao=0;
}
lcdxianshi(10,miao);
write_2(1,miao);
}
if(snum==2)
{
fen++;
if(fen==60)
{
fen=0;
}
lcdxianshi(7,fen);
write_2(2,fen);
}
if(snum==3)
{
shi++;
if(shi==24)
{
shi=0;
}
lcdxianshi(4,shi);
write_2(3,shi);
}
}
}
}
if(snum!=0)
{
if(S3==0)
{
delayms(5);
if(S3==0)
{
while(S3!=0);
if(snum==1)
{
miao--;
if(miao==-1)
{
miao=59;
}
lcdxianshi(10,miao);
write_2(1,miao);
}
if(snum==2)
{
fen--;
if(fen==-1)
{
fen=59;
}
lcdxianshi(7,fen);
write_2(2,fen);
}
if(snum==3)
{
shi--;
if(shi==-1)
{
shi=0;
}
lcdxianshi(4,shi);
write_2(3,shi);
}
}
}
}
}
void main()
{
init();
while(1)
{
jianpanjiance();
}
}
void T0_time() interrupt 1
{
TH0=0x4c;
TL0=0x00;
num++;
if(num==20)
{
num=0;
miao++;
if(miao==60)
{
fen++;
miao=0;
if(fen==60)
{
shi++;
fen=0;
if(shi==24)
{
shi=0;
}
lcdxianshi(4,shi);
write_2(3,shi);
}
lcdxianshi(7,fen);
write_2(2,fen);
}
lcdxianshi(10,miao);
write_2(1,miao);
}
} |