#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcdrs=P0^7;
sbit lcdrw=P0^6;
sbit lcde=P0^5;
sbit lcdpsb=P0^4;
sbit rst=P1^0;
sbit sclk=P1^2;
sbit io=P1^1;
uchar writedat[]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80};
uchar read_dat[]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81};
uchar time_dat[7]={14,1,4,21,11,58,30}; //年周月日时分秒
uchar code table[]="0123456789";
uchar code table1[]="一二三四五六日";
uchar str0[16]="2014/04/21";
uchar str1[16]="09:46:30 星期一";
uchar str2[16]="fffff";
uchar str3[16]="dddd";
void write1302_byte(uchar dat);//写数据
void write1302(uchar add,uchar dat);//写地址和写数据
read1302(uchar add);//读数据
void set_rtc(void);//初始化
void lcd12864display(uchar *p,uchar line);
void lcd12864display(uchar *p,uchar line)
{
uchar add;
uchar i;
if(line==1)
{add=0x80;}
if(line==2)
{add=0x90;}
if(line==3)
{add=0x88;}
if(line==4)
{add=0x98;}
i=*p+line;
// write_com(i);
}
void delayms(uint x)
{
uchar i,j;
for(j=0;j<x;j++)
for(i=0;i<110;i++); //延时x秒
}
void write_com(uchar com) //写指令
{
lcdrs=0;
lcdrw=0;
lcde=0;
P2=com;
delayms(5);
lcde=1;
delayms(5);
lcde=0;
}
void write_dat(uchar dat) //写数据
{
lcdrs=1;
lcdrw=0;
lcde=0;
P2=dat;
delayms(5);
lcde=1;
delayms(5);
lcde=0;
}
void lcd_init() //初始化
{
lcdpsb=1;
write_com(0x30);
delayms(5);
write_com(0x0c);
delayms(5);
write_com(0x01);
delayms(5);
}
void write1302_byte(uchar dat)//写数据
{
uchar i;
rst=0;
_nop_();
rst=1;
_nop_();
for(i=0;i<8;i++)
{
sclk=0;
io=dat;
dat=dat&&0x01;
dat=dat>>1;
sclk=1;
}
rst=0;
_nop_();
}
void write1302(uchar add,uchar dat)
{
rst=0;
_nop_();
rst=1;
_nop_();
write1302_byte(add);//写地址
write1302_byte(dat);//写数据
rst=0;
_nop_();
sclk=1;
_nop_();
io=1;
_nop_();
}
read1302(uchar add)
{
uchar i,value;
rst=0;
_nop_();
sclk=0;
_nop_();
rst=1;
_nop_();
write1302_byte(add);
for(i=0;i<8;i++)
{
value=value>>1;
sclk=0;
if(io)
value=value|0x80;
sclk=1;
_nop_();
}
rst=0;
_nop_();
sclk=0;
_nop_();
sclk=1;
_nop_();
io=1;
_nop_();
return value;//返回
}
void set_rtc() //初始化
{
uchar i,j;
for(i=0;i<7;i++)
{
j=time_dat[i]/10;
time_dat[i]=time_dat[i]%10;
time_dat[i]=time_dat[i]+j*16;
}
write1302(0x8e,0x00); //去除写保护
for(i=0;i<7;i++)
{
write1302(writedat[i],time_dat[i]);
}
write1302(0x8e,0x80);//写保护
}
void read_str(void)
{
uchar i;
for(i=0;i<7;i++)
{
time_dat[i]=read1302(read_dat[i]);
}
}
struct Time
{
uchar second;
uchar minute;
uchar hour;
uchar day;
uchar week;
uchar month;
uchar year;
}time;
//extern struct Time time;
void mian()
{
time.second=59;
time.minute=30;
time.hour=11;
time.day =21;
time.week=1;
time.month=4;
time.year=14;
set_rtc();
delayms(50);
lcd_init();
delayms(50);
while(1)
{
read_str();
str0[2]=table[time.year/10];
str0[3]=table[time.year%10];
str0[6]=table[time.month/10];
str0[7]=table[time.month%10];
str0[9]=table[time.day/10];
str0[10]=table[time.day%10];
str1[1]=table[time.hour/10];
str1[2]=table[time.hour%10];
str1[4]=table[time.minute/10];
str1[5]=table[time.minute%10];
str1[7]=table[time.second/10];
str1[8]=table[time.second%10];
str1[14]=table1[(time.week-1)*2-1];
str1[15]=table1[(time.week-1)*2] ;
lcd12864display(str0,1);
lcd12864display(str1,2);
lcd12864display(str2,3);
lcd12864display(str3,4);
}
}
|