#include‘reg52.h’ //包括单片机寄存器的头文件
#include‘intrins.h’ //包括_nop_()的头文件
sbit RS=P2^0; //LCD读写挑选位
sbit RW=P2^1; //LCD读写挑选位
sbit E=P2^2; //LCD使能端
sbit BF=P0^7; //忙信号
sbit SCLK=P1^0; //1302时钟输出端
sbit DATA=P1^1; //1302数据端
sbit RST=P1^2; //1302复位端
unsigned char code digit[]=“0123456789”;
void delay1ms(unsigned int n)
{
unsigned char i;
while(n–)
for(i=0;i《115;i++);
}
void Write1302(unsigned char dat)
{
unsigned char i;
SCLK=0; //保证写数据前SCLK为低电平
_nop_();
_nop_(); //给硬件反应时间
for(i=0;i《8;i++){
DATA=dat&0x01; //取数据的最低位 低位在前,高位在后
_nop_();
_nop_(); //给硬件反应时间
SCLK=1; //正跳变写数据
_nop_();
_nop_(); //给硬件反应时间
SCLK=0; //从头拉低SCLK,构成脉冲
dat》》=1; //dat右移,为写入下位数据做准备
}
}
void WriteSet1302(unsigned char cmd, unsigned char dat)
{
RST=0; //制止数据传输 。!!!这条很重要
SCLK=0; //保证写数据前SCLK为低电平
RST=1; //敞开数据传输
_nop_();
_nop_();
Write1302(cmd);
Write1302(dat);
SCLK=1; // 将时钟电平置于高电平状况 ,处于已知状况
RST=0;
}
unsigned char Read1302()
{
unsigned char i,dat=0;
for(i=0;i《8;i++){
dat》》=1;
if(DATA==1) //假如读出的数据是1
dat|=0x80; //将1取出,写在dat的最高位
SCLK=1; //将SCLK置于高电平,为下降沿读出
_nop_();
_nop_();
SCLK=0; //拉低SCLK,构成脉冲下降沿
}
return dat;
}
unsigned char ReadSet1302(unsigned char cmd)
{
unsigned char dat;
RST=0; //这条很重要
SCLK=0; //保证写数居前SCLK被拉低
RST=1; //发动数据传输
_nop_();
_nop_();
Write1302(cmd); //写入命令字
dat=Read1302(); //读出数据
SCLK=1; //将时钟电平置于已知状况
RST=0; //制止数据传递
return dat; //将读出的数据回来
}
void DS1302_Int()
{
WriteSet1302(0x8E,0x00); //依据写状况寄存器命令字,写入不维护指令
WriteSet1302(0x80,((55/10)《《4|(55))); //依据写秒寄存器命令字,写入秒的初始值
WriteSet1302(0x82,((59/10)《《4|(59))); //依据写分寄存器命令字,写入分的初始值
WriteSet1302(0x84,((23/10)《《4|(23))); //依据写小时寄存器命令字,写入小时的初始值
WriteSet1302(0x86,((18/10)《《4|(18))); //依据写日寄存器命令字,写入日的初始值
WriteSet1302(0x88,((6/10)《《4|(6))); //依据写月寄存器命令字,写入月的初始值
WriteSet1302(0x8c,((9/10)《《4|(9))); //依据写年寄存器命令字,写入年的初始值
WriteSet1302(0x90,0xa5); //翻开充电功用 挑选2K电阻充电办法
WriteSet1302(0x8E,0x80); //依据写状况寄存器命令字,写入维护指令
}
bit BusyTest()
{
bit result;
RS=0; //依据规定,RS为低电平,RW为高电平时,能够读状况
RW=1;
E=1;
_nop_();
_nop_();
result=BF; //将繁忙标志电平赋给result
E=0; //将E康复低电平
_nop_();
_nop_();
return result;
}
void WriteInstrucTIon(unsigned char dictate)
{
while(BusyTest()==1); //假如忙就等候
RS=0; //依据规定,RS和R/W一起为低电平时,能够写入指令
RW=0;
E=0; //E置低电平(写指令时,E为高脉冲,
// 便是让E从0到1产生正跳变,所以应先置“0”
_nop_();
_nop_();
P0=dictate;
_nop_();
_nop_();
_nop_();
_nop_();
E=1; //E置高电平
_nop_();
_nop_();
_nop_();
_nop_();
E=0; //当E由高电平跳变成低电平时,液晶模块开端执行命令
}
void WriteAddress(unsigned char x)
{
WriteInstrucTIon(x|0x80); //显现方位的确认办法规定为“80H+地址码x”
}
void WriteData(unsigned char dat)
{
while(BusyTest()==1); //假如忙就等候
RS=1; //RS为高电平,RW为低电平时,能够写入数据
RW=0;
E=0; //E置低电平(写指令时,E为高脉冲,
// 便是让E从0到1产生正跳变,所以应先置“0”
_nop_();
_nop_();
P0=dat; //将数据送入P0口,行将数据写入液晶模块
_nop_();
_nop_();
_nop_();
_nop_();
E=1; //E置高电平
_nop_();
_nop_();
_nop_();
_nop_();
E=0; //当E由高电平跳变成低电平时,液晶模块开端执行命令
}
void LCD_Int()
{
delay1ms(15); //延时15ms,初次写指令时应给LCD一段较长的反应时间
WriteInstrucTIon(0x38); //显现形式设置:16×2显现,5×7点阵,8位数据接口
delay1ms(5);
WriteInstrucTIon(0x38);
delay1ms(5);
WriteInstruction(0x38);
delay1ms(5); //接连三次,保证初始化成功
WriteInstruction(0x0c); //显现形式设置:显现开,无光标,光标不闪耀
delay1ms(5);
WriteInstruction(0x06); //显现形式设置:光标右移,字符不移
delay1ms(5);
WriteInstruction(0x01); //清屏幕指令,将曾经的显现内容铲除
delay1ms(5);
}
void DisplaySecond(unsigned char x)
{
unsigned char i,j; //i,j别离贮存十位和个位
i=x/10;
j=x;
WriteAddress(0x49);
WriteData(digit[i]);
WriteData(digit[j]);
delay1ms(15);
}
void DisplayMinute(unsigned char x)
{
unsigned char i,j;
i=x/10;
j=x;
WriteAddress(0x46);
WriteData(digit[i]);
WriteData(digit[j]);
delay1ms(15);
}
void DisplayHour(unsigned char x)
{
unsigned char i,j;
i=x/10;
j=x;
WriteAddress(0x43);
WriteData(digit[i]);
WriteData(digit[j]);
delay1ms(15);
}
void DisplayDay(unsigned char x)
{
unsigned char i,j;
i=x/10;
j=x;
WriteAddress(0x0c);
WriteData(digit[i]);
WriteData(digit[j]);
delay1ms(15);
}
void DisplayMonth(unsigned char x)
{
unsigned char i,j;
i=x/10;
j=x;
WriteAddress(0x09);
WriteData(digit[i]);
WriteData(digit[j]);
delay1ms(15);
}
void DisplayYear(unsigned char x)
{
unsigned char i,j;
i=x/10;
j=x;
WriteAddress(0x06);
WriteData(digit[i]);
WriteData(digit[j]);
delay1ms(15);
}
void main()
{
unsigned char second,minute,hour,day,month,year; //别离贮存苗、分、小时,日,月,年
unsigned char ReadValue; //贮存从1302读取的数据
LCD_Int(); //将液晶初始化
WriteAddress(0x01); //写Date的显现地址,将在第1行第2列开端显现
WriteData(‘D’);
WriteData(‘a’);
WriteData(‘t’);
WriteData(‘e’);
WriteData(‘:’);
WriteAddress(0x08);
WriteData(‘-’);
WriteAddress(0x0b);
WriteData(‘-’);
WriteAddress(0x45);
WriteData(‘:’);
WriteAddress(0x48);
WriteData(‘:’);
DS1302_Int(); //将1302初始化
while(1){
ReadValue = ReadSet1302(0x81); //从秒寄存器读数据
second=((ReadValue&0x70)》》4)*10 + (ReadValue&0x0F);//将读出数据转化
DisplaySecond(second); //显现秒
ReadValue = ReadSet1302(0x83); //从分寄存器读
minute=((ReadValue&0x70)》》4)*10 + (ReadValue&0x0F); //将读出数据转化
DisplayMinute(minute); //显现分
ReadValue = ReadSet1302(0x85); //从分寄存器读
hour=((ReadValue&0x70)》》4)*10 + (ReadValue&0x0F); //将读出数据转化
DisplayHour(hour); //显现小时
ReadValue = ReadSet1302(0x87); //从分寄存器读
day=((ReadValue&0x70)》》4)*10 + (ReadValue&0x0F); //将读出数据转化
DisplayDay(day); //显现日