STM32F103 单片机RTC芯片DS3231驱动程序

#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);
}

  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GJZGRB

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值