ds3231stm32例程
时间: 2023-10-22 16:09:13 浏览: 70
以下是 DS3231 在 STM32 上的例程:
```c
#include "stm32f10x.h"
#include "i2c.h"
#define DS3231_ADDR 0xD0
void DS3231_Init(void)
{
I2C_Start();
I2C_SendByte(DS3231_ADDR);
I2C_SendByte(0x0E);
I2C_SendByte(0x00);
I2C_Stop();
}
void DS3231_SetTime(uint8_t hour, uint8_t min, uint8_t sec)
{
I2C_Start();
I2C_SendByte(DS3231_ADDR);
I2C_SendByte(0x00);
I2C_SendByte(((hour / 10) << 4) | (hour % 10));
I2C_SendByte(((min / 10) << 4) | (min % 10));
I2C_SendByte(((sec / 10) << 4) | (sec % 10));
I2C_Stop();
}
void DS3231_GetTime(uint8_t *hour, uint8_t *min, uint8_t *sec)
{
I2C_Start();
I2C_SendByte(DS3231_ADDR);
I2C_SendByte(0x00);
I2C_Start();
I2C_SendByte(DS3231_ADDR | 0x01);
*sec = ((I2C_ReadByte() & 0x0F) + ((I2C_ReadByte() >> 4) * 10));
*min = ((I2C_ReadByte() & 0x0F) + ((I2C_ReadByte() >> 4) * 10));
*hour = ((I2C_ReadByte() & 0x0F) + ((I2C_ReadByte() >> 4) * 10));
I2C_Stop();
}
相关问题: