📄 1302.c
字号:
#define uchar unsigned char
#define uint unsigned int
#include "def.h"
#include <reg52.h>
#include <stdio.h>
#include <math.h>
#include <intrins.h>
#include <public.h>
void Write_Ds1302_byte(unsigned char temp);
void Write_Ds1302( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302 ( unsigned char address );
void Read_RTC(void);//read RTC
void Set_RTC(void);//set RTC
/***************************************************************************************/
sbit SCL2=P1^3; //SCL2定义为P1口的第3位脚,连接DS1302SCL和ADC0831SCL脚
sbit SDA2=P1^4; //SDA2定义为P1口的第4位脚,连接DS1302SCL和ADC0831SDA脚
//sbit CS2=P1^6; //CS2定义为P1口的第4位脚,连接ADC0831CS脚
sbit RST = P1^5; // DS1302片选脚
/*********************************************************************/
void Write_Ds1302_Byte(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++) //循环8次 写入数据
{
SCL2=0;
SDA2=temp&0x01; //每次传输低字节
temp>>=1; //右移一位
SCL2=1;
}
}
/****************************************************************************/
void Write_Ds1302( unsigned char address,unsigned char dat )
{
RST=0;
_nop_();
SCL2=0;
_nop_();
RST=1;
_nop_(); //启动
Write_Ds1302_Byte(address); //发送地址
Write_Ds1302_Byte(dat); //发送数据
RST=0; //恢复
}
/****************************************************************************/
unsigned char Read_Ds1302 ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0;
_nop_();
SCL2=0;
_nop_();
RST=1;
_nop_();
Write_Ds1302_Byte(address);
for (i=0;i<8;i++) //循环8次 读取数据
{
if(SDA2)
temp|=0x80; //每次传输低字节
SCL2=0;
temp>>=1; //右移一位
SCL2=1;
}
RST=0;
_nop_(); //以下为DS1302复位的稳定时间
RST=0;
SCL2=0;
_nop_();
SCL2=1;
_nop_();
SDA2=0;
_nop_();
SDA2=1;
_nop_();
return (temp); //返回
}
/****************************************************************************/
void Read_RTC(void) //读取 日历
{
unsigned char i,*p;
p=read_rtc_address; //地址传递
for(i=0;i<7;i++) //分7次读取 年月日时分秒星期
{
l_tmpdate[i]=Read_Ds1302(*p);
// l_tmpdate[i]=i;
p++;
}
}
/***********************************************************************/
void Set_RTC(void) //设定 日历
{
unsigned char i,*p,tmp;
for(i=0;i<7;i++){
tmp=l_tmpdate[i]/10;
l_tmpdate[i]=l_tmpdate[i]%10;
l_tmpdate[i]=l_tmpdate[i]+tmp*16;
}
Write_Ds1302(0x8E,0X00);
p=write_rtc_address; //传地址
for(i=0;i<7;i++) //7次写入 年月日时分秒星期
{
Write_Ds1302(*p,l_tmpdate[i]);
p++;
}
Write_Ds1302(0x8E,0x80);
}
/*********************************************************************/
//1302读出16进制数的转化程序
void chang()
{
l_tmpdate1[0]=(l_tmpdate[6]/16) + 0x30;
l_tmpdate1[1]=(l_tmpdate[6]&0x0f) + 0x30;
l_tmpdate1[2]='/';
l_tmpdate1[3]=(l_tmpdate[4]/16)+0x30;
l_tmpdate1[4]=(l_tmpdate[4]&0x0f) +0x30;
l_tmpdate1[5]='/';
l_tmpdate1[6]=(l_tmpdate[3]/16)+0x30;
l_tmpdate1[7]=(l_tmpdate[3]&0x0f)+ 0x30;
l_tmpdate1[8]=((l_tmpdate[2]/16)& 0x03)+0x30;
l_tmpdate1[9]=(l_tmpdate[2]&0x0f) + 0x30;
l_tmpdate1[10]=':';
l_tmpdate1[11]=(l_tmpdate[1]/16)+0x30;
l_tmpdate1[12]=(l_tmpdate[1]&0x0f) +0x30;
l_tmpdate1[13]=':';
l_tmpdate1[14]=((l_tmpdate[0]/16) & 0x07)+0x30;
l_tmpdate1[15]=(l_tmpdate[0]&0x0f) +0x30;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -