#include<reg52.h>
#include<intrins.h>
#define nop _nop_()
#define uchar unsigned char
#define uint unsigned int
uchar k ;
sbit ACC7= ACC^7;
sbit ACC0= ACC^0;
sbit rs=P3^5;//1602 引脚
sbit wr=P3^6;
sbit e=P3^4;
sbit rst=P3^3; //1302引脚
sbit sclk=P3^2;
sbit io=P2^4;
uchar code table1[] = {0x00,0x00,0x00,0x05,0x00,0x00,0x00};
uchar code week[][3] = {{"SUN"},{"MON"},{"TUE"},{"WED"},{"THU"},{"FRI"},{"SAT"}};
uchar data line1[] = {"2012/07/30"};
uchar code table2[] = {"0123456789"};
uchar data line2[] = {"00:00:00"};
void lcd_busy_wait() //CD1602 忙等待
{
rs = 0;
wr = 1;
e = 1;
P0 = 0xff;
while (P0&0x80);
P0 = 0;
}
void delay(uint z) //延时函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void writecom(uchar b)
{
lcd_busy_wait();
rs=0; //根据时序图
wr=0;
P0=b;
delay(1);
e=1;
delay(1);
e=0;
}
void writedate(uchar b)
{
rs=1; //根据时序图
wr=0;
P0=b;
delay(1);
e=1;
delay(1);
e=0;
}
void init() //初始函数1602
{
wr=0; //写
e=0;
delay(15);
writecom(0x38); //设置16x2显示,5x7点阵,8位数据口
delay(5);
writecom(0x38);
delay(5);
writecom(0x38);
delay(5);
writecom(0x38);
writecom(0x08);
delay(5);
writecom(0x0c); //设置开显示,显光标,光标闪烁
writecom(0x06); // 写一个字符地址指针加1
writecom(0x01); //显示清0;数据指针清0
}
void writebit(uchar q)//1302写一字节
{
uchar i;
ACC=q;
for(i=8;i>0;i--)
{
sclk=0;
io=ACC0;
sclk=1;
ACC =ACC>>1;
}
}
uchar readbit(void)//读一字节
{
uchar i;
for(i=8;i>0;i--)
{
ACC7=io;
sclk=0;
ACC7=ACC>>1;
sclk=1;
}
return(ACC);
}
/*uchar readbit()//读一字节
{
uchar i,date;
delay(2);
for(i=0;i>8;i++);
{
date>>=1;
if(io==1)
date|=0x80;
sclk=1;
nop;
nop;
nop;
sclk=0;
delay(2);
}
return(date);
}
void writebit (uchar q)//写一字节
{
uchar i,num;
for(i=8;i>0;i--)
{
sclk=1;
nop;
nop;
nop;
num=q;
io=num&0x01;
q>>=1;
sclk=0;
}
}*/
void write1302(uchar add,uchar date)//往1302写入数据
{
rst=0;
nop;
sclk=0;
nop;
rst=1;
nop;
writebit(add);
writebit(date);
sclk=1;
rst=0;
}
uchar read1302(uchar add)//从1302读取数据
{
uchar date;
rst=0;
nop;
sclk=0;
nop;
rst=1;
writebit(add);
date=readbit();
sclk=1;
rst=0;
return(date);
}
void set1302(uchar *a)//1302初始化
{
uchar i,add=0x80;
write1302(0x8e,0x00);//禁止写 保护
for(i=7;i>0;i--)
{
write1302(add,*a);
a++;
add+=2;
}
write1302(0x8e,0x80); //允许写保护
}
void get1302(uchar table[])//将1302读取的数据存到数组中
{
uchar i,ad=0x81;
for(i=0;i<7;i++)
{
table[i]=read1302(ad);
ad+=2;
}
}
void display() //1602显示
{
uchar a,i,j;
wr=0;
e=0;
writecom(0x81);
lcd_busy_wait();
for(a=0;a<10;a++)
{
writedate(line1[a]);
delay(5);
}
writecom(0x80+0x0c);
lcd_busy_wait();
for(i=0;i<3;i++)
{
writedate(week[k][i]);
delay(5);
}
writecom(0x80+0x43);
lcd_busy_wait();
for(j=0;j<8;j++)
{
writedate(line2[j]);
delay(5);
}
}
main()
{
init() ;
set1302(table1);
while(1)
{
get1302(table1);
line1[2]=table2[table1[6]/0x10];
line1[3]=table2[table1[6]%0x10];
line1[5]=table2[table1[5]/0x10];
line1[6]=table2[table1[5]%0x10];
line1[8]=table2[table1[4]/0x10];
line1[9]=table2[table1[4]%0x10];
k=table2[table1[3]%0x10]-'0';
line2[0]=table2[table1[2]/0x10];
line2[1]=table2[table1[2]%0x10];
line2[3]=table2[table1[1]/0x10];
line2[4]=table2[table1[1]%0x10];
line2[6]=table2[table1[0]/0x10];
line2[7]=table2[table1[0]%0x10];
display();
}
} |