新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > PIC16F877驅(qū)動DS1302芯片

PIC16F877驅(qū)動DS1302芯片

作者: 時間:2016-11-09 來源:網(wǎng)絡 收藏
這個程序改網(wǎng)上的程序只用了一上午的時間,看來是用修改別人程序比自個從頭開始編快的的多呀!

下面是原理圖

本文引用地址:http://2s4d.com/article/201611/317661.htm

下面是程序頭文件

#ifndef DS1302_H
#define DS1302_H

#include "main.h"

#define Time_24_Hour0x00//24時制控制
#define Time_Start0x00//開始走時
#define Time_Stop0x80//停止走時

#define DS1302_SECOND0x80//DS1302各寄存器操作命令定義
#define DS1302_MINUTE0x82
#define DS1302_HOUR0x84
#define DS1302_WEEK0x8A
#define DS1302_DAY0x86
#define DS1302_MONTH0x88
#define DS1302_YEAR0x8C
#define DS1302_WRITE0x8E
#define DS1302_POWER0x90

#define BCD2DEC(X)(((X&0x70)>>4)*10 + (X&0x0F))//用于將BCD碼轉(zhuǎn)成十進制的宏
#define DEC2BCD(X)((X/10)<<4 | (X%10))//用于將十進制轉(zhuǎn)成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__//定義的時間類型結(jié)構(gòu)體,全部數(shù)為十進制
{
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 ,判斷結(jié)束字符
}
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) ;
}
}




評論


技術(shù)專區(qū)

關閉