#include //定义52单片机
#include
#define uchar unsigned char //宏定义 无符号字符型
#define uint unsigend int //宏定义 无符号字符型
uchar code able1[]="week2 2012:11:09";
uchar code able2[]=" 28.0C 12:05:00";
uchar code able3[]="0123456789";
***it rs=P2^0; //单片机位定义 1602液晶
***it rw=P2^1;
***it e=P2^2;
***it rst=P2^3; //ds1302时钟芯片的复位端
***it sclk=P2^4; //ds1302时钟芯片的时钟端
***it io=P2^5; //ds1302时钟芯片的数据端
***it dq=P2^6; //ds18b20数据端
***it ACC0=ACC^0;
***it ACC7=ACC^7;
uchar t_sec,t_min,t_hour,t_day,t_mon,t_year,t_week;
uchar sec1,sec2,min1,min2,hour1,hour2,mon1,mon2,year1,year2;
uchar week,day1,day2;
void delay(uchar xms) //延时子程序控制
{
uchar x,y;
for(x=0;x
for(y=0;y<110;y++);
}
void write_in(uchar into) //写入指令 1602液晶
{
delay(5);
rs=0;
rw=0;
e=0;
P0=into;
e=1;
delay(5);
e=0;
}
void write_data(uchar date) //写入数据 1602液晶
{
delay(5);
rs=1;
rw=0;
e=0;
P0=date;
e=1;
delay(5);
e=0;
}
void init() //初始化程序 1602液晶
{
delay(15); //复位的过程
write_in(0x38); //不查忙信号
delay(5); //延时5ms
write_in(0x38); //不查忙信号
delay(5); //延时5ms
write_in(0x38); //不查忙信号
write_in(0x38);
write_in(0x08);
write_in(0x06);
write_in(0x01);
write_in(0x0c);
write_in(0x80);
}
//向1302写入一个字节的数据
void write_bit(uchar dat)
{
uchar i;
ACC=dat;
for(i=0;i<8;i++)
{
io=ACC0;
sclk=1;
sclk=0;
ACC=(ACC>>1);
}
}
//ds1302读出一个字节
void write_read()
{
uchar i;
for(i=0;i<8;i++)
{
ACC=(ACC>>1);
ACC7=io;
sclk=1;
sclk=0;
}
return (ACC);
}
//写数据
void write_1302(uchar add,uchar dat)
{
rst=0;
sclk=0;
rst=0;
write_bit(add);
write_bit(dat);
sclk=1;
rst=0;
}
//读数据
void read_1302(uchar add)
{
uchar inf;
rst=0;
sclk=0;
rst=1;
write_bit(add);
inf=write_read();
sclk=1;
rst=0;
return (inf);
}
void init_1302()
{
write_1302(0x8e,0x00);//关闭写保护;
// write_1302(0x90,0xaa);//设置充电方式;
write_1302(0x80,0x00);//秒寄存器初始化;
write_1302(0x82,0x29);//分.......
write_1302(0x84,0x08);//时.......
write_1302(0x86,0x03);//日........
write_1302(0x88,0x06);//月.......
write_1302(0x8a,0x05);//星期...
write_1302(0x8c,0x11);//年......
write_1302(0x8e,0x80);//打开写保护;
}
void lcd1602() //lcd1602显示程序
{
uchar a,b;
write_in(0x80);
for(a=0;a<16;a++)
{
write_data(able1[a]);
delay(100);
}
write_in(0x80+0x40);
for(b=0;b<16;b++)
{
write_data(able2[b]);
delay(100);
}
}
void display() //ds1302显示程序
{
while(1)
{
t_sec=read_1302(0x81);//读秒 ;
sec1=t_sec&0x0f;
sec2=(t_sec>>4);
t_min=read_1302(0x83);//读分
min1=t_min&0x0f;
min2=(t_min>>4);
t_hour=read_1302(0x85); //读小时
hour1=t_hour&0x0f;
hour2=(t_hour>>4);
t_day=read_1302(0x87);//读日
day1=t_day&0x0f;
day2=(t_day>>4);
t_mon=read_1302(0x89);//读月
mon1=t_mon&0x0f;
mon2=(t_mon>>4);
t_year=read_1302(0x8d);//读年
year1=t_year&0x0f;
year2=(t_year>>4);
t_week=read_1302(0x8b);//读星期
week=t_week&0x0f;
write_in(0x80+0x06); //显示2
write_data(able3[3]);
write_in(0x80+0x07);
write_data(able3[1]); //显示0
write_in(0x80+0x08); //显示年
write_data(year2);
write_in(0x80+0x09);
write_data(year1);
write_in(0x80+0x11);
write_data(mon2);
write_in(0x80+0x12);
write_data(mon1);
//第二行显示
write_in(0x80+0x40+0x9);
write_data(hour2);
write_in(0x80+0x40+0x10);
write_data(hour1);
write_in(0x80+0x40+0x12);
write_data(min2);
write_in(0x80+0x40+0x13);
write_data(min1);
write_in(0x80+0x40+0x15);
write_Data(sec2);
write_in(0x80+0x40+0x16);
write_data(sec1);
}
}
void main()
{
uchar a,b;
init();
init_1302();
lcd1602();
while(1)
{
display();
}
}
|