ds1302程序stm.docx
- 文档编号:18493645
- 上传时间:2023-08-18
- 格式:DOCX
- 页数:32
- 大小:21.31KB
ds1302程序stm.docx
《ds1302程序stm.docx》由会员分享,可在线阅读,更多相关《ds1302程序stm.docx(32页珍藏版)》请在冰点文库上搜索。
ds1302程序stm
/******************************************************************************
*Filename-->
*Author -->BySamChan
*Version -->V1.0
*Date -->08-23-2012
*Brief -->时钟芯片DS1302驱动
*
*Copyright(C)20**
*Allrightsreserved
*******************************************************************************
*
* FileUpdate
*Version -->V1.0.1
*Author -->BySamChan
*Date -->10-20-2013
*Revise -->A、增加对STM32控制的移植支持
* -->B、增加对内置的RAM操作相关函数
* -->C、增加检测DS1302是否存在,是否第一次上电检测函数
* -->D、增加对DS1302内置的锂电池充电寄存器参数设置函数和结构体
*
******************************************************************************/
#include"ds1302.h"
/******************************************************************************
定义变量
******************************************************************************/
#defineDateTimeValue.date
#defineMin TimeValue.minute
#defineSec TimeValue.second
#defineHourTimeValue.hour
#defineWeekTimeValue.week
#defineMonthTimeValue.month
#defineYearTimeValue.year
Time_TypedefTimeValue; //定义时间数据指针
voidTime_Init()
{
TimeValue.date=24;
TimeValue.hour=6;
TimeValue.minute=12;
TimeValue.month=5;
TimeValue.second=0;
TimeValue.week=2;
TimeValue.year=16;
}
Charge_TypedefChargeValue; //定义充电寄存器
/******************************************************************************
*FunctionName-->GPIO初始化
*Description -->none
*Input -->none
*Output -->none
*Reaturn -->none
******************************************************************************/
voidDS1302_GPIOInit(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); //开启GPIOC外设时钟
/* 初始化GPIOC */
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
}
/******************************************************************************
*FunctionName-->DS1302写入一个字节数据
*Description -->none
*Input -->dat:
要写入的数据
*Output -->none
*Reaturn -->none
******************************************************************************/
voidDS1302_Write_Byte(u8dat)
{
u8i;
DS1302_SDA_OUT(); //设置为输出口
for(i=0;i<8;i++)
{
DS1302_SCL=0; //时钟线拉低
if(dat&0x01) DS1302_SDA=1; //数据线放上数据,先发低位
else DS1302_SDA=0;
dat>>=1; //数据右移一位
DS1302_SCL=1; //发送数据,上升沿有效
}
}
/******************************************************************************
*FunctionName-->DS1302读出一个字节数据
*Description -->none
*Input -->none
*Output -->none
*Reaturn -->读到的数据
******************************************************************************/
unsignedDS1302_Read_Byte()
{
u8ReData=0x00;
u8i;
DS1302_SDA_IN(); //设置为输入口
for(i=0;i<8;i++)
{
if(DS1302_IN_SDA==1)
{ ReData|=0x80; } //读出一位数据是“1”
DS1302_SCL=0; //时钟线拉低
ReData>>=1; //数据右移一位,先读取低位,数据补“0”
DS1302_SCL=1; //上升沿读取数据
}
return(ReData); //返回读取到的数据
}
/******************************************************************************
*FunctionName-->向DS1302某地址写入数据
*Description -->none
*Input -->add:
要操作的地址
* dat:
要写入的数据
*Output -->none
*Reaturn -->none
******************************************************************************/
voidDS1302_Write_Data(u8add,u8dat)
{
DS1302_RST=0; //复位脚拉低
DS1302_SCL=0; //时钟线拉低
DS1302_RST=1; //复位脚拉高
DS1302_Write_Byte(add); //写入要操作地址
DS1302_Write_Byte(dat); //写入数据
//delay_us(5);
DS1302_RST=0;
DS1302_SCL=0;
}
/******************************************************************************
*FunctionName-->从DS1302某地址读取数据
*Description -->none
*Input -->add:
要操作的地址
*Output -->none
*Reaturn -->要读取的寄存器的数值
******************************************************************************/
unsignedDS1302_Read_Data(u8add)
{
u8Temp;
DS1302_RST=1;
DS1302_Write_Byte(add); //写入要操作地址
Temp=DS1302_Read_Byte(); //开始读取数据
DS1302_RST=0;
return(Temp); //返回读取到的数据
}
/******************************************************************************
*FunctionName-->主电源对备用电池充电设置
*Description -->如果备用电池接的是可充电的锂电池或者其他可充电电池的时候,
* 可以打开DS1302的充电电路,利用主供电对电池进行充电,免的换电池的麻烦
*Input -->*CHG_dat:
寄存器控制指针
*Output -->none
*Reaturn -->none
******************************************************************************/
voidDS1302_Charge_Manage(Charge_Typedef*CHG_dat)
{
u8CHG_Value;
CHG_Value=(CHG_dat->TCSx<<4)|(CHG_dat->DSx<<2)|CHG_dat->RSx;
DS1302_WP_Disable(); //取消写保护
DS1302_Write_Data(Trickle_Charger_Address,CHG_Value);
DS1302_WP_Enable(); //打开写保护
}
/******************************************************************************
*FunctionName-->DS1302内置的RAM读写操作
*Description -->none
*Input -->*pBuff:
读写数据存放区
* WRadd:
读写起始地址,范围在RAM_Address0~RAM_Address28之间,最后一位地址有其他用途
* num:
读写字节数据的数量,范围在1~28之间
* RW:
读写判断位。
0x00为写操作,0x01为读操作
*Output -->none
*Reaturn -->none
******************************************************************************/
voidDS1302_RAM_WriteRead_Data(u8*pBuff,u8WRadd,u8num,u8RW)
{
u8i;
if(WRadd==RAM_Address29) return; //要写入数据的RAM地址是最后一个,直接退出
//因为最后一个字节是用来检测DS1302的
if(RW==0x00) //写数据操作
{
for(i=0;i { DS1302_WP_Disable(); //取消写保护 DS1302_Write_Data(WRadd+(i<<1),pBuff[i]); DS1302_WP_Enable(); //打开写保护 } } else { for(i=0;i { DS1302_WP_Disable(); //取消写保护 pBuff[i]=DS1302_Read_Data(WRadd+1+(i<<1)); DS1302_WP_Enable(); //打开写保护 } } } /****************************************************************************** *FunctionName-->测试设备好坏 *Description -->在DS1302芯片的RAM的最后一个地址写入一个数据并读出来判断 * 与上次写入的值相等,不是第一次上电,否则则初始化时间 *Input -->none *Output -->none *Reaturn -->0: 设备正常并不是第一次上电 * 1: 设备错误或者已损坏 ******************************************************************************/ u8DS1302_Check(void) { u8test_value; DS1302_WP_Disable(); //取消写保护 test_value=DS1302_Read_Data(RAM_Address29+1); //读出最后一个RAM地址里的数据 DS1302_WP_Enable(); //打开写保护 if(test_value==test_data) //判断数据是否对 { return0; //设备正常,不是第一次上电 } else { return1; //不是第一次上电或者设备异常 // DS1302_WP_Disable(); //取消写保护 // DS1302_Write_Data(RAM_Address29,0x55); //向RAM最后一个地址里写入数据 // DS1302_WP_Enable(); //打开写保护 // // _nop_();_nop_(); // // DS1302_WP_Disable(); //取消写保护 // test_value=DS1302_Read_Data(RAM_Address29+1); //读出最后一个RAM地址里的数据 // DS1302_WP_Enable(); //打开写保护 // // if(test_value==test_data) return0; //设备正常,不是第一次上电 } // return1; //设备不在线或者已损坏 } /****************************************************************************** *FunctionName-->DS1302初始化 *Description -->none *Input -->*Time: 时间结构体指针 *Output -->none *Reaturn -->none ******************************************************************************/ voidDS1302_Init(Time_Typedef*Time) { DS1302_RST=0; DS1302_SCL=0; DS1302_WP_Disable(); //取消写保护 DS1302_Write_Data(Address_year,(((Time->year)/10)<<4|(Time->year)%10)); //写年数据 delay_us(3); DS1302_Write_Data(Address_week,(((Time->week)/10)<<4|(Time->week)%10)); //写星期数据 delay_us(3); DS1302_Write_Data(Address_month,(((Time->month)/10)<<4|(Time->month)%10));//写月数据 delay_us(3); DS1302_Write_Data(Address_date,(((Time->date)/10)<<4|(Time->date)%10)); //写日数据 delay_us(3); DS1302_Write_Data(Address_hour,(((Time->hour)/10)<<4|(Time->hour)%10)); //写小时数据 delay_us(3); DS1302_Write_Data(Address_minute,(((Time->minute)/10)<<4|(Time->minute)%10)); //写分钟数据 delay_us(3); DS1302_Write_Data(Address_second,(((Time->second)/10)<<4|(Time->s
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ds1302 程序 stm