#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit en=P2^5;
sbit rs=P1^0;
sbit rw=P1^1;
sbit psb=P1^2;
sbit dula=P2^6;
sbit wela=P2^7;
sbit sclk=P2^1;
sbit sda=P2^0;
sbit rst=P2^4;
//int c;
uchar lin1[]="年月日";
uchar lin2[]="时分秒";
uchar lin3[]="星期";
//uchar lin4[]="一二三四五六日";
uchar lin5[]={0,1,2,3,4,5,6,7,8,9};
void delay(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void write_comdate(uint n,uchar date)
{
rw=0;
rs=(0|n);
en=0;
P0=date;
en=1;
delay(5);
en=0;
delay(5);
}
void LCD_chinese( char *p )
{
write_comdate(1,*p);
p++;
write_comdate(1,*p);
}
void lcd_init()
{ psb=1;
dula=0;
wela=0;
delay(10);
write_comdate(0,0x38);
delay(5);
write_comdate(0,0x30);
delay(5);
write_comdate(0,0x06);
delay(5);
write_comdate(0,0x0c);
delay(5);
write_comdate(0,0x01);
delay(5);
}
void write_ds(uchar date)
{
uchar i;
sclk=0;
for(i=0;i<8;i++)
{
sda=date&0x01;
sclk=1;
delay(1);
sclk=0;
delay(1);
date>>=1;
}
}
void write_pos(uchar add,uchar date)
{
rst=0;
sclk=0;
rst=1;
write_comdate(0,add);
write_comdate(1,date);
rst=0;
sclk=1;
delay(5);
}
uchar read_comdate()
{
uint i;
uchar dat;
sclk=0;
for(i=0;i<8;i++)
{
dat=dat>>1;
if(sda==1)
dat|=0x80;
sclk=1;
delay(1);
sclk=0;
}
return dat;
}
uchar read_pos(uchar add)
{
uchar temp;
rst=0;
sclk=0;
rst=1;
write_comdate(0,add);
temp=(read_comdate());
sclk=1;
rst=0;
return temp;
}
void ds1302_init()
{
write_pos(0x8e,0x00);
write_pos(0x80,((55/10)<<4)|(55%10));
delay(1);
write_pos(0x8a,((3/10)<<4)|(3%10));
delay(1);
write_pos(0x82,((20/10)<<4)|(20%10));
delay(1);
write_pos(0x84,((23/10)<<4)|(23%10));
delay(1);
write_pos(0x86,((16/10)<<4)|(16%10));
delay(1);
write_pos(0x88,((3/10)<<4)|(3%10));
delay(1);
write_pos(0x8c,((12/10)<<4)|(12%10));
delay(1);
//write_pos(0x90,0xa6);
//delay(1);
write_pos(0x8e,0x80);
}
uchar read_value(uchar add)
{
uchar bcdvalue,dcevalue;
bcdvalue=read_pos(add);
dcevalue=((bcdvalue&0x70)>>4)*10+(bcdvalue&0x0f);
return dcevalue;
}
uchar read_year()
{
uchar bcdvalue,dcevalue;
bcdvalue=read_pos(0x8d);
dcevalue=((bcdvalue&0xf0)>>4)*10+(bcdvalue&0x0f);
return dcevalue;
}
void display(uchar add,uchar x)
{
uint i,j;
i=x/10;
j=x%10;
write_comdate(0,add);
write_comdate(0,lin5[i]+0x30);
write_comdate(0,lin5[j]+0x30);
delay(5);
i=j=0;
}
void init_devise()
{
lcd_init();
delay(5);
ds1302_init();
delay(5);
}
void display_init()
{
write_comdate(0,0x81);
write_comdate(1,'2');
write_comdate(1,'0');
write_comdate(0,0x83);
LCD_chinese(&lin1[0]);
write_comdate(0,0x85);
LCD_chinese(&lin1[2]);
write_comdate(0,0x87);
LCD_chinese(&lin1[4]);
write_comdate(0,0x92);
LCD_chinese(&lin2[0]);
write_comdate(0,0x94);
LCD_chinese(&lin2[2]);
write_comdate(0,0x96);
LCD_chinese(&lin2[4]);
write_comdate(0,0x8a);
LCD_chinese(&lin3[0]);
write_comdate(0,0x8b);
LCD_chinese(&lin3[2]);
}
void main()
{
init_devise();
delay(100);
display_init();
delay(20);
while(1)
{
display(0x82,read_year());
display(0x84,read_value(0x89));
display(0x86,read_value(0x87));
display(0x95,read_value(0x81));
display(0x93,read_value(0x83));
display(0x91,read_value(0x85));
display(0x8c,read_value(0x8b));
}
}
求大神指教
|