#include
#include
#define uchar unsigned char
#define uint unsigned int
uchar code smg_du[]="0123456789-"; //12864显示定数字,不在写成16进制的形式
uchar code smg_we[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
uchar time_csh[7]={10,5,5,8,10,38,12}; //年周月日时分秒
uchar write_add[7]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80}; //写地址
uchar read_add[7]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81}; //读地址
uchar disp[8]; //显示缓存区
uchar table[]="ds1302实时时钟";
***it scl=P1^0;
***it io=P1^1;
***it rst=P1^2;
***it dula=P1^3;
***it wela=P1^4;
***it PCB=P2^1;
// ***it rst=P2^3;
***it rs=P2^4;
***it rw=P2^5;
***it e=P2^6;
/*************50us********/
void delay_50us(uint t)
{
uint i,j;
for(j=t;j>0;j--)
for(i=19;i>0;i--);
}
/*************50ms********/
void delay50ms(uint t)
{
uint i,j;
for(j=t;j>0;j--)
for(i=6245;i>0;i--);
}
void ds1302_write_byte(uchar date) //单字节写
{
uchar i;
for(i=0;i<8;i++)
{
scl=0; //低电平时数据才允许变化,
io=date&0x01;
date=date>>1;
scl=1;
} //高电平写入数据
}
void ds1302_write(uchar add,uchar date) //多字节写
{
rst=0; //模拟时序
_nop_();
scl=0;
_nop_();
rst=1;
_nop_();
ds1302_write_byte(add); //写入地址
ds1302_write_byte(date); //写入数据
rst=0;
_nop_();
io=1; //将数据线释放
rst=1;
}
uchar read_ds1302(add) //读ds1302,须先写个地址,在读
{
uchar i,j;
rst=0;
_nop_();
scl=0;
_nop_();
rst=1;
_nop_();
ds1302_write_byte(add);
for(i=0;i<8;i++)
{
scl=0;
j=j>>1;
if(io)
j=j|0x80;
scl=1;
}
rst=0;
_nop_();
scl=0;
_nop_();
rst=1;
scl=1;
return j;
}
void set_time() //设置时间,对时间初始化
{
uchar i,j;
for(i=0;i<7;i++)
{
j=time_csh[i]/10;
time_csh[i]=time_csh[i]%10;
time_csh[i]=time_csh[i]+j*16;
}
ds1302_write(0x8e,0x00); //去除写保护
for(i=0;i<7;i++)
{
ds1302_write(write_add[i],time_csh[i]);
}
ds1302_write(0x8e,0x80); //加上写保护
}
void read_time()
{
uchar i;
for(i=0;i<7;i++)
{
time_csh[i]=read_ds1302(read_add[i]);
}
}
void chuli_time() //数据 处理,对BCD码进行分离
{
disp[1]=time_csh[4]%16;
disp[0]=time_csh[4]/16;
disp[2]=10;
disp[4]=time_csh[5]%16;
disp[3]=time_csh[5]/16;
disp[5]=10;
disp[7]=time_csh[6]%16;
disp[6]=time_csh[6]/16;
}
/******************12864*********/
void write_com(uchar com)
{
rs=0;
rw=0;
delay_50us(1);
P0=com;
e=1;
delay_50us(10);
e=0;
delay_50us(1);
}
void write_date(uchar dat)
{
rs=1;
rw=0;
delay_50us(1);
P0=dat;
e=1;
delay_50us(10);
e=0;
delay_50us(1);
}
void init_12864()
{
delay50ms(2);
write_com(0x30);
delay_50us(3);
write_com(0x30);
delay_50us(1);
write_com(0x0f);
delay_50us(3);
write_com(0x01);
delay50ms(1);
write_com(0x06);
delay50ms(1);
}
void display() //刷新显示时间
{
uchar i;
write_com(0x90);
for(i=0;i<8;i++)
{
write_date(smg_du[disp[i]]);
_nop_();_nop_();
}
}
void main()
{ uchar i;
init_12864();
write_com(0x80);
for(i=0;i<16;i++)
{
write_date(table[i]);
delay50ms(1);
}
// set_time(); //对时,开始写入时间初值
while(1)
{
read_time();
chuli_time();
display();
}
}
|