eecs-bsp-test-code-2/inc/DS1302.h

75 lines
3.5 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**********************************DS1302 V1.1 说明 ************************************************************************
DS1302模块用于控制“STC-B学习板”上DS1302芯片操作。
DS1302提供RTC实时时钟和NVM非易失存储器功能断电后RTC和NVM是依靠纽扣电池BAT维持工作的。其中
RTC提供年、月、日、星期、时、分、秒功能
NVM提供31 Bytes非易失存储器功能(地址为030。其中地址为30的单元被DS1302Init()函数用于检测DS1302是否掉电用户不能使用)
DS1302模块共提供1个驱动函数、4个应用函数
(1) void DS1302Init(struct_DS1302_RTC time)DS1302驱动函数。使用DS1302需用该函数初始化和驱动一次
函数参数结构struct_DS1302_RTC time
如果DS1302掉电初始化时检测RTC数据失效则以参数time定义的时间初始化RTC
函数返回值:无
(2) struct_DS1302_RTC RTC_Read(void)读取DS1302内部实时时钟RTC内容
函数参数:无
函数返回值结构struct_DS1302见结构struct_DS1302定义
(3) void RTC_Write(struct_DS1302_RTC time) 写DS1302内部实时时钟RTC内容
函数参数结构struct_DS1302 time见结构struct_DS1302定义
函数返回值:无
(4) unsigned char NVM_Read(unsigned char NVM_addr): 读取NVM一个指定地址内容
函数参数:
NVM_addr指定非易失存储单元地址有效值030共31个单元
函数返回值当函数参数正常时返回NVM中对应单元的存储数值1Byte
当函数参数错误时返回enumDS1302_error
(5) unsigned char NVM_Write(unsigned char NVM_addr, unsigned char NVM_data)向NVM一个指定地址写入新值
函数参数:
NVM_addr指定非易失存储单元地址有效值030共31个单元。其中第30单元被DS1302Init()函数用于检测DS1302是否掉电用户不能使用)
NVM_data待写入NVM单元的新值1Byte
函数返回值当函数参数正常时返回enumDS1302_OK
当函数参数错误时返回enumDS1302_error
结构struct_DS1302_RTC定义参见DS1302Z数据手册
typedef struct
{ unsigned char second; //秒BCD码以下均为BCD码
unsigned char minute; //分
unsigned char hour; //时
unsigned char day; //日
unsigned char month; //月
unsigned char week; //星期
unsigned char year; //年
} struct_DS1302_RTC;
关于DS1302内部非易失性存储器补充说明
DS1302提供的非易失性存储器为低功耗RAM结构靠纽扣电池保持掉电后其存储内容不变。
与M24C01区别容量小仅31字节但无”写“寿命问题且写周期很短可忽略即两次写操作之间无需等待
读、写DS1302内部NVM每一个字节均需要花费一定操作时间数十uS
仅在需要时使用以上读或写函数读写需要的特定字节内容,应避免对其进行无效、大量、重复操作!
编写徐成电话18008400450 2021年8月5日设计2021年8月15日改进
*/
#ifndef _DS1302_H_
#define _DS1302_H_
typedef struct
{ unsigned char second; //秒BCD码以下均为BCD码
unsigned char minute; //分
unsigned char hour; //时
unsigned char day; //日
unsigned char month; //月
unsigned char week; //星期
unsigned char year; //年
} struct_DS1302_RTC;
extern void DS1302Init(struct_DS1302_RTC time); //DS1302初始化
extern struct_DS1302_RTC RTC_Read(void); //读RTC读RTC时钟内容
extern void RTC_Write(struct_DS1302_RTC time); //写RTC校对RTC时钟
extern unsigned char NVM_Read(unsigned char NVM_addr); //读NVM读DS1302中的非易失存储单元内容
extern unsigned char NVM_Write(unsigned char NVM_addr, unsigned char NVM_data); //写NVM写DS1302中的非易失存储单元
enum DS1302name {enumDS1302_OK,enumDS1302_error};
#endif