#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit scl=P1^0;//设置P1-0为时钟
uchar s;
sbit sda=P1^1;//设置P1-1为数据
sbit dula=P2^6;//实验板关闭数码管
sbit wela=P2^7;//实验板关闭数码管
sbit rs=P3^5;//按键
sbit lcden=P3^4;
sbit s1=P3^0;//按键
sbit s2=P3^1;//按键
sbit s3=P3^2;//按键
sbit rd=P3^7;//按键
uchar count,s1num,num;
char miao,shi,fen;
uchar code table[]=" 2007-7-30 MON";
uchar code table1[]=" 00:00:00";//液晶显示开始值
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}//长延时
void delay1()
{
;;
}//短延时
/*********8563芯片操作********/
void start()//开始
{
sda=1;
delay1();
scl=1;
delay1();
sda=0;
delay1();
sda=1;
}
void stop()//结束
{
sda=0;
delay1();
scl=1;
delay1();
sda=1;
delay1();
}
void r_ask()//应答
{
uchar i;
scl=1;
delay1();
while((sda==1)&&(i<250))i++;
scl=0;
delay1();
}
void w_8563(uchar date)//写一个数
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay1();
sda=CY;
delay1();
scl=1;
delay1();
// scl=0;
// delay();
}
scl=0;
delay1();
sda=1;
delay1();
}
uchar r_8563()//读取一个数
{
uchar i,k;
scl=0;
delay1();
sda=1;
delay1();
for(i=0;i<8;i++)
{
scl=1;
delay1();
k=(k<<1)|sda;
scl=0;
delay1();
}
return k;
}
uchar r_d8563(uchar ad)//读取一个地址里数
{
uchar date;
start();
w_8563(0xa2);
r_ask();
w_8563(ad);
r_ask();
w_8563(0xa1);
r_ask();
date=r_8563();
stop();
return date;
}
void w_d8653(uchar dizhi,uchar shu)//在以一个地址里写数
{
start();
w_8563(0xa2);
r_ask();
w_8563(dizhi);
r_ask();
w_8563(0xa0);
r_ask();
w_8563(shu);
stop();
}
/*显示部分*/
void write_com(uchar com)//液晶写命令
{
rs=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date)//液晶写数据
{
rs=1;
lcden=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_sfm(uchar add,uchar date)//往液晶里定点写数
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
/************键盘扫描***********////
void keyscan()
{
rd=0;
if(s1==0)
{
delay(5);
if(s1==0)
{ s1num++;
while(!s1);
if(s1num==1)
{
TR0=0;
write_com(0x80+0x40+10);
write_com(0x0f);
}
}
if(s1num==2)
{
write_com(0x80+0x40+7);
}
if(s1num==3)
{
write_com(0x80+0x40+4);
}
if(s1num==4)
{
s1num=0;
write_com(0x0c);
TR0=1;
}
}
if(s1num!=0)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
if(s1num==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
if(s1num==1)
{
/* if(miao==0)
{
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}*/
miao--;
if(miao==-1)
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(s1num==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
}
}
}
}
/********初始化****///
void init()
{
start();
w_8563(0x00);
stop();
dula=0;
wela=0;
lcden=0;
// fen=59;
// miao=53;
// shi=23;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<15;num++)
{
write_date(table[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<12;num++)
{
write_date(table1[num]);
delay(5);
}
}
mian()
{
init();
while(1)
{
keyscan();
s=r_d8563(0x02);
write_sfm(0x04,s);//就读取一个秒 试试
}
} |