定义:DS1302为时钟芯片。
配置特点:1.定位到地址 2.写入数据。
如你要写59秒,那便先确定他的地址0x80,在编写它的数据0x59。若要写9月,那便先写0x88(它的地址,再写数据0x09)。
( 手册中的详细地址)
它的编写时序(规则)代码如下
#include "ds1302.h"
//写字节
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK = 0;
SDA = temp&0x01;
temp>>=1;
SCK=1;
}
}
//向DS1302寄存器写入数据
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
Write_Ds1302(dat);
RST=0;
}
//从DS1302寄存器读出数据
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0; _nop_();
SCK=0; _nop_();
SCK=1; _nop_();
SDA=0; _nop_();
SDA=1; _nop_();
return (temp);
}
void Set_Rtc(unsigned char*ucRtc)//写函数
{
Write_Ds1302_Byte(0x8e,0x00);//关闭保护位
Write_Ds1302_Byte(0x84,ucRtc[0]);//时
Write_Ds1302_Byte(0x82,ucRtc[1]);//分
Write_Ds1302_Byte(0x80,ucRtc[2]);//秒
Write_Ds1302_Byte(0x8e,0x80);//打开保护位
}
void Read_Rtc(unsigned char*ucRtc)//读函数
{
ucRtc[0]=Read_Ds1302_Byte(0x85);//时
ucRtc[1]=Read_Ds1302_Byte(0x83);//分
ucRtc[2]=Read_Ds1302_Byte(0x81);//秒
}
小细节,需将.h中的一些已经声明的头文件复制到.c当中去
记得在.h文件中声明这两个变量,带分号。
主函数需要做的:
数码管设置:
最后改个0xbf.