#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); //显示日
ReadValue = ReadSet1302(0x89);
//从分寄存器读
month=((ReadValue&0x70)>>4)*10 +
(ReadValue&0x0F); //将读出数据转化
DisplayMonth(month); //显示月
ReadValue = ReadSet1302(0x8d);
//从分寄存器读
year=((ReadValue&0xf0)>>4)*10 +
(ReadValue&0x0F); //将读出数据转化
DisplayYear(year); //显示年
}
}