下面是原理图
下面是程序头文件
#ifndef DS1302_H
#define DS1302_H
#include "main.h"
#define Time_24_Hour 0x00 //24时制控制
#define Time_Start 0x00 //开始走时
#define Time_Stop 0x80 //停止走时
#define DS1302_SECOND 0x80 //DS1302各寄存器操作命令定义
#define DS1302_MINUTE 0x82
#define DS1302_HOUR 0x84
#define DS1302_WEEK 0x8A
#define DS1302_DAY 0x86
#define DS1302_MONTH 0x88
#define DS1302_YEAR 0x8C
#define DS1302_WRITE 0x8E
#define DS1302_POWER 0x90
#define BCD2DEC(X) (((X&0x70)>>4)*10 + (X&0x0F)) //用于将BCD码转成十进制的宏
#define DEC2BCD(X) ((X/10)<<4 | (X%10)) //用于将十进制转成BCD
#define RST RC0
#define DATA RC1
#define DS_CLK RC2
#define RST_DIR TRISC0
#define DATA_DIR TRISC1
#define SCLK_DIR TRISC2
typedef struct __SYSTEMTIME__ //定义的时间类型结构体,全部数为十进制
{
unsigned char Second;
unsigned char Minute;
unsigned char Hour;
unsigned char Week;
unsigned char Day;
unsigned char Month;
unsigned char Year;
unsigned char DateString[11];
unsigned char TimeString[9];
}systemetime;
void init_ds1302() ;
void writebyte(uchar data) ;
uchar readbyte() ;
void ds_writedata(uchar add,uchar data) ;
uchar ds_readdata(uchar add) ;
void get_time(systemetime *Time) ;
void set_time(systemetime *Time) ;
void data2str(systemetime *Time) ;
void time2str(systemetime *Time) ;
#endif
子程序
#include "ds1302.h"
void init_ds1302()
{
RST_DIR=0 ;
DATA_DIR=0 ;
SCLK_DIR=0 ;
ds_writedata(DS1302_WRITE,0x00); //关闭写保护
ds_writedata(DS1302_SECOND,Time_Stop);//停止计时
ds_writedata(DS1302_HOUR,Time_24_Hour);//24时制
// ds_writedata(DS1302_POWER,0xA6); //单二极管,4K电阻充电
ds_writedata(DS1302_WRITE,0x80); //打开写保护
}
void writebyte(uchar data)
{
uchar i;
DATA_DIR=0 ;
for(i=0;i<8;i++)
{
if(data&0x01)
DATA=1 ;
else
DATA=0 ;
DS_CLK=1 ;
DS_CLK=0 ;
data=data>>1 ;
}
}
uchar readbyte()
{
uchar i,temp ;
DATA_DIR=1 ;
for(i=0 ;i<8;i++)
{
temp=temp>>1 ;
if(DATA)
temp=temp|0x80 ;
DS_CLK=1 ;
DS_CLK=0 ;
}
DATA_DIR=0 ;
return temp ;
}
void ds_writedata(uchar add,uchar data)
{
RST=0 ;
DS_CLK=0 ;
RST=1 ;
writebyte(add) ;
writebyte(data) ;
DS_CLK=1 ;
RST=0 ;
}
uchar ds_readdata(uchar add)
{
uchar temp ;
RST=0 ;
DS_CLK=0 ;
RST=1 ;
writebyte(add|0x01) ;
temp=readbyte() ;
DS_CLK=1 ;
RST=0 ;
}
void get_time(systemetime *Time)
{
unsigned char ReadValue;
ReadValue = ds_readdata(DS1302_SECOND);
Time->Second = BCD2DEC(ReadValue&0x7F);
ReadValue = ds_readdata(DS1302_MINUTE);
Time->Minute = BCD2DEC(ReadValue);
ReadValue = ds_readdata(DS1302_HOUR);
Time->Hour = BCD2DEC(ReadValue&0x7F);
ReadValue = ds_readdata(DS1302_DAY);
Time->Day = BCD2DEC(ReadValue);
ReadValue = ds_readdata(DS1302_WEEK);
Time->Week = BCD2DEC(ReadValue);
ReadValue = ds_readdata(DS1302_MONTH);
Time->Month = BCD2DEC(ReadValue);
ReadValue = ds_readdata(DS1302_YEAR);
Time->Year = BCD2DEC(ReadValue);
}
void set_time(systemetime *Time)
{
uchar temp ;
ds_writedata(DS1302_WRITE,0x00); //关闭写保护
temp=DEC2BCD(Time->Year) ;
ds_writedata(DS1302_YEAR,temp);
temp=DEC2BCD(Time->Month) ;
ds_writedata(DS1302_MONTH,temp);
temp=DEC2BCD(Time->Week) ;
ds_writedata(DS1302_WEEK,temp);
temp=DEC2BCD(Time->Day) ;
ds_writedata(DS1302_DAY,temp);
temp=DEC2BCD(Time->Hour)|Time_24_Hour ;
ds_writedata(DS1302_HOUR,temp);
temp=DEC2BCD(Time->Minute) ;
ds_writedata(DS1302_MINUTE,temp);
temp=DEC2BCD(Time->Second)|Time_Start ;
ds_writedata(DS1302_SECOND,temp);
ds_writedata(DS1302_WRITE,0x80); //打开写保护
}
void data2str(systemetime *Time)
{
//Year
Time->DateString[0] = '2';
Time->DateString[1] = '0';
Time->DateString[2] = Time->Year/10 + '0';
Time->DateString[3] = Time->Year%10 + '0';
Time->DateString[4] = '/';
//Month
Time->DateString[5] = Time->Month/10 + '0';
Time->DateString[6] = Time->Month%10 + '0';
Time->DateString[7] = '/';
//Day
Time->DateString[8] = Time->Day/10 + '0';
Time->DateString[9] = Time->Day%10 + '0';
Time->DateString[10] = '/0'; //字符串末尾加 '/0' ,判断结束字符
}
void time2str(systemetime *Time)
{
//Hout
Time->TimeString[0] = Time->Hour/10 + '0';
Time->TimeString[1] = Time->Hour%10 + '0';
Time->TimeString[2] = ':';
//Minute
Time->TimeString[3] = Time->Minute/10 + '0';
Time->TimeString[4] = Time->Minute%10 + '0';
Time->TimeString[5] = ':';
//Second
Time->TimeString[6] = Time->Second/10 + '0';
Time->TimeString[7] = Time->Second%10 + '0';
//
Time->DateString[8] = '/0';
}
下面是测试程序
#include "main.h"
#include "ds1302.h"
void init_all()
{
init_ds1302() ;
}
void interrupt main_int()
{
;
}
systemetime tm ;
void main()
{
init_all() ;
tm.Day=19;
tm.Hour=13 ;
tm.Minute=4 ;
tm.Month=8 ;
tm.Year=9 ;
tm.Week=1 ;
set_time(&tm) ;
while(1)
{
get_time(&tm) ;
time2str(&tm) ;
data2str(&tm) ;
}
}
上一篇:Microchip PIC32系列再添新成员
下一篇:PIC16F877A驱动12864液晶
推荐阅读最新更新时间:2024-03-16 14:59