初级会员
- 积分
- 101
- 金钱
- 101
- 注册时间
- 2021-1-11
- 在线时间
- 19 小时
|
楼主 |
发表于 2023-3-14 10:34:42
|
显示全部楼层
ds1302.c
#include "ds1302.h"
#include "delay.h"
struct TIMEData TimeData;
char read_time[7];
char DS1302_data_1[10];
char DS1302_data_2[8];
void ds1302_gpio_init()//CE,SCLK端口初始化
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //PC.1 CE
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化GPIOC.0
GPIO_ResetBits(GPIOC,GPIO_Pin_1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PC.2 SCLK
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化GPIOC.2
GPIO_ResetBits(GPIOC,GPIO_Pin_2);
}
void ds1302_DATAOUT_init()//配置双向I/O端口为输出态
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //PC.0 DATC
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化GPIOC.0
GPIO_ResetBits(GPIOC,GPIO_Pin_0);
}
void ds1302_DATAINPUT_init()//配置双向I/O端口为输入态
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //PC.10 DATA
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化GPIOC.10
}
void ds1302_write_onebyte(unsigned char data)//向DS1302发送一字节数据
{
unsigned char count=0;
ds1302_DATAOUT_init();
SCLK_L;
for(count=0;count<8;count++)
{ SCLK_L;
if(data&0x01)
{DATA_H;}
else{DATA_L;}//先准备好数据再发送
SCLK_H;//拉高时钟线,发送数据
data>>=1;
}
}
void ds1302_wirte_rig(unsigned char address,unsigned char data)//向指定寄存器地址发送数据
{
unsigned char temp1=address;
unsigned char temp2=data;
CE_L;SCLK_L;delay_us(1);
CE_H;delay_us(2);
ds1302_write_onebyte(temp1);
ds1302_write_onebyte(temp2);
CE_L;SCLK_L;delay_us(2);
}
unsigned char ds1302_read_rig(unsigned char address)//从指定地址读取一字节数据
{
unsigned char temp3=address;
unsigned char count=0;
unsigned char return_data=0x00;
CE_L;SCLK_L;delay_us(3);
CE_H;delay_us(3);
ds1302_write_onebyte(temp3);
ds1302_DATAINPUT_init();//配置I/O口为输入
delay_us(2);
for(count=0;count<8;count++)
{
delay_us(2);//使电平持续一段时间
return_data>>=1;
SCLK_H;delay_us(4);//使高电平持续一段时间
SCLK_L;delay_us(14);//延时14us后再去读取电压,更加准确
if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_0))
{return_data=return_data|0x80;}
}
delay_us(2);
CE_L;DATA_L;
return return_data;
}
void ds1302_init()
{
ds1302_wirte_rig(0x8e,0x00);//关闭写保护
ds1302_wirte_rig(0x80,0x00);//seconds 0秒
ds1302_wirte_rig(0x82,0x038);//minutes 03分
ds1302_wirte_rig(0x84,0x11);//hours 时
ds1302_wirte_rig(0x86,0x13);//date 13日
ds1302_wirte_rig(0x88,0x03);//months 3月
ds1302_wirte_rig(0x8a,0x01);//days 星期一
ds1302_wirte_rig(0x8c,0x23);//year 2023年
ds1302_wirte_rig(0x8e,0x80);//关闭写保护
}
void ds1302_read_time()
{
read_time[0]=ds1302_read_rig(0x81);//读秒
read_time[1]=ds1302_read_rig(0x83);//读分
read_time[2]=ds1302_read_rig(0x85);//读时
read_time[3]=ds1302_read_rig(0x87);//读日
read_time[4]=ds1302_read_rig(0x89);//读月
read_time[5]=ds1302_read_rig(0x8B);//读星期
read_time[6]=ds1302_read_rig(0x8D);//读年
}
void ds1302_read_realTime()
{
ds1302_read_time(); //BCD码转换为10进制
TimeData.second=(read_time[0]>>4)*10+(read_time[0]&0x0f);
TimeData.minute=((read_time[1]>>4)&(0x07))*10+(read_time[1]&0x0f);
TimeData.hour=(read_time[2]>>4)*10+(read_time[2]&0x0f);
TimeData.day=(read_time[3]>>4)*10+(read_time[3]&0x0f);
TimeData.month=(read_time[4]>>4)*10+(read_time[4]&0x0f);
TimeData.week=read_time[5];
TimeData.year=(read_time[6]>>4)*10+(read_time[6]&0x0f)+2000;
DS1302_data_1[0]='2';
DS1302_data_1[1]='0';
DS1302_data_1[2]='0'+(TimeData.year-2000)/10;
DS1302_data_1[3]='0'+(TimeData.year-2000)%10;
DS1302_data_1[4]='-';
DS1302_data_1[5]='0'+TimeData.month/10;
DS1302_data_1[6]='0'+TimeData.month%10;
DS1302_data_1[7]='-';
DS1302_data_1[8]='0'+TimeData.day/10;
DS1302_data_1[9]='0'+TimeData.day%10;
DS1302_data_2[0]='0'+TimeData.hour/10;
DS1302_data_2[1]='0'+TimeData.hour%10;
DS1302_data_2[2]=':';
DS1302_data_2[3]='0'+TimeData.minute/10;
DS1302_data_2[4]='0'+TimeData.minute%10;
DS1302_data_2[5]=':';
DS1302_data_2[6]='0'+TimeData.second/10;
DS1302_data_2[7]='0'+TimeData.second%10;
}
|
|