您的位置 首页 应用

PIC16F877驱动DS1302芯片

这个程序改网上的程序只用了一上午的时间,看来是用修改别人程序比自个从头开始编快的的多呀!下面是原理图下面是程序头文件#ifndefDS130…

这个程序改网上的程序只用了一上午的时刻,看来是用修正他人程序比自个从头开端编快的的多呀!

下面是原理图

下面是程序头文件

#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码转成十进制的宏
#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) ;
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/yingyong/276647.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部