#ifndef __DS3231_H__
#define __DS3231_H__
#include "stm32f10x.h"
#include "iic1.h"
//DS3231初始宏设置
#define DS3231_WriteAddress 0xD0 //器件写地址
#define DS3231_ReadAddress 0xD1 //器件读地址
#define DS3231_SECOND 0x00 //秒
#define DS3231_MINUTE 0x01 //分
#define DS3231_HOUR 0x02 //时
#define DS3231_WEEK 0x03 //星期
#define DS3231_DAY 0x04 //日
#define DS3231_MONTH 0x05 //月
#define DS3231_YEAR 0x06 //年
//闹铃1
#define DS3231_SALARM1ECOND 0x07 //秒
#define DS3231_ALARM1MINUTE 0x08 //分
#define DS3231_ALARM1HOUR 0x09 //时
#define DS3231_ALARM1WEEK 0x0A //星期/日
//闹铃2
#define DS3231_ALARM2MINUTE 0x0b //分
#define DS3231_ALARM2HOUR 0x0c //时
#define DS3231_ALARM2WEEK 0x0d //星期/日
#define DS3231_CONTROL 0x0e //控制寄存器
#define DS3231_STATUS 0x0f //状态寄存器
#define BSY 2 //忙
#define OSF 7 //振荡器停止标志
#define DS3231_XTAL 0x10 //晶体老化寄存器
#define DS3231_TEMPERATUREH 0x11 //温度寄存器高字节(8位)
#define DS3231_TEMPERATUREL 0x12 //温度寄存器低字节(高2位)
typedef struct
{
u8 hour;
u8 min;
u8 sec;
u8 year;
u8 month;
u8 date;
u8 week;
u8 temperature;
}_calendar_obj;
extern _calendar_obj calendar; //日历结构体
void DS3231_SetTime(u8 yea,u8 mon,u8 da,u8 we,u8 hou,u8 min,u8 sec);
void get_show_time(void);
void get_show_Temperature(void);
#endif
//###############################################
#include "ds3231.h"
_calendar_obj calendar;
void DS3231_SetTime(u8 yea,u8 mon,u8 da,u8 we,u8 hou,u8 min,u8 sec)
{
u8 temp=0;
temp=B_BCD(yea);
I2C1_Write_Byte(0x06,temp);
temp=B_BCD(mon);
I2C1_Write_Byte(0x05,temp);
temp=B_BCD(da);
I2C1_Write_Byte(0x04,temp);
temp=B_BCD(we);
I2C1_Write_Byte(0x03,temp);
temp=B_BCD(hou);
I2C1_Write_Byte(0x02,temp);
temp=B_BCD(min);
I2C1_Write_Byte(0x01,temp);
temp=B_BCD(sec);
I2C1_Write_Byte(0x00,temp);
}
void get_show_time(void)
{
calendar.year=I2C1_Read_Data(0x06);
calendar.year=BCD2HEX(calendar.year);
calendar.month=I2C1_Read_Data(0x05);
calendar.month=BCD2HEX(calendar.month);
calendar.date=I2C1_Read_Data(0x04);
calendar.date=BCD2HEX(calendar.date);
calendar.week=I2C1_Read_Data(0x03);
calendar.week=BCD2HEX(calendar.week);
calendar.hour=I2C1_Read_Data(0x02);
calendar.hour&=0x3f;
calendar.hour=BCD2HEX(calendar.hour);
calendar.min=I2C1_Read_Data(0x01);
calendar.min=BCD2HEX(calendar.min);
calendar.sec=I2C1_Read_Data(0x00);
calendar.sec=BCD2HEX(calendar.sec);
}
void get_show_Temperature()
{
I2C1_Write_Byte(DS3231_CONTROL,0x20|0x05);
calendar.temperature=I2C1_Read_Data(DS3231_TEMPERATUREH);
}