各位同志好,这一篇我们使用IIC通信来设置与读取DS3231的数据,我们采用第一篇的程序模板。IIC分为硬件IIC和软件IIC。我们先来使用STM32内部的硬件IIC来实现对DS3231的数据读取工作,接着在下方我们使用GPIO口来模拟软件IIC再次实现一遍。
这里简单介绍一下本次试验用到的模组,这个是我去年自制的一个RTC时钟模块,准度非常可以。这块DS3231模组的板子开源了,链接如下:https://oshwhub.com/c70e/ds3231-real-time-clock-rtc-modul

既然测试IIC,所以我们打开IIC1外设,并且将控制IIC的GPIO引脚设置成开漏模式,并不上拉因为我自制的DS3231模组具备IIC通信用的上拉电阻。由于采用了第一篇文章的程序模板,这里仅仅对IIC的配置进行记录。基本配置如下:


程序基本原理是:MCU上电后先初始化一堆东西(开启IIC1外设)并使用串口打印相关作者信息,接着进入主函数中,每间隔1s打印一次串口信息,读取DS3231内部的数据信息并且翻转LED2表示它工作正常,如下图所示:

串口打印如图所示:

程序实现如下:
STM32C092_DS3231.c的库文件
//STM32C092_DS3231.c 库文件
#include "STM32C092_DS3231.h"
#define DS3231_ADDRESS (0x68 << 1) //DS3231 IIC通信地址 7-bit地址左移1位
//DS3231寄存器地址
#define DS3231ADD_Sec 0x00
#define DS3231ADD_Min 0x01
#define DS3231ADD_Hour 0x02
#define DS3231ADD_Week 0x03
#define DS3231ADD_Date 0x04
#define DS3231ADD_Mouth 0x05
#define DS3231ADD_Year 0x06
#define DS3231ADD_Control 0x0E
#define DS3231ADD_TempH 0x11
#define DS3231ADD_TempL 0x12
uint8_t DS3231_Data[7];
// 十进制转BCD
static uint8_t DEC_to_BCD(uint8_t dec) {
return ((dec / 10) << 4) | (dec % 10);
}
// BCD转十进制
static uint8_t BCD_to_DEC(uint8_t bcd) {
return (bcd >> 4) * 10 + (bcd & 0x0F);
}
// 初始化函数
void DS3231_Init(void) {
// 禁用32kHz输出,设置控制寄存器
uint8_t ctrl_reg [2]= {DS3231ADD_Control,0x04};
HAL_I2C_Master_Transmit(&hi2c1, DS3231_ADDRESS, ctrl_reg, 2, 100);
// 清除状态寄存器
uint8_t status_reg [2]= {0x0F,0x00};
HAL_I2C_Master_Transmit(&hi2c1, DS3231_ADDRESS, status_reg, 2, 100);
}
/******************************************************************
* 函 数 名 称:DS3231_SQW
* 函 数 说 明:DS3231设置SQW频率值
* 函 数 形 参:DS3231_SQWState(枚举变量,支持DS3231_SQW_1S、DS3231_SQW_NONE)
* 函 数 返 回:无
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
void DS3231_SQW(DS3231_SQWState SQW_SOURCE){
if(SQW_SOURCE==DS3231_SQW_1S){
uint8_t ctrl_reg [2]= {DS3231ADD_Control,0x00};
HAL_I2C_Master_Transmit(&hi2c1, DS3231_ADDRESS, ctrl_reg, 2, 100);
}
else{
uint8_t ctrl_reg [2]= {DS3231ADD_Control,0x04};
HAL_I2C_Master_Transmit(&hi2c1, DS3231_ADDRESS, ctrl_reg, 2, 100);
}
}
/******************************************************************
* 函 数 名 称:DS3231_SetTime1
* 函 数 说 明:DS3231设置时间1
* 函 数 形 参:Hour:设置小时 Min:设置分钟 Sec:设置秒
* 函 数 返 回:无
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
void DS3231_SetTime1(uint8_t Hour,uint8_t Min,uint8_t Sec){
uint8_t SetTime1[4]={DS3231ADD_Sec,DEC_to_BCD(Sec),DEC_to_BCD(Min),DEC_to_BCD(Hour)};
HAL_I2C_Master_Transmit(&hi2c1, DS3231_ADDRESS, SetTime1, 4, 100);
//HAL_I2C_Mem_Write(&hi2c1, DS3231_ADDRESS, 0x00, I2C_MEMADD_SIZE_8BIT, data, 7, 100)
}
/******************************************************************
* 函 数 名 称:DS3231_SetTime2
* 函 数 说 明:DS3231设置时间2
* 函 数 形 参:Year:设置年 Mouth:设置月 Date:设置日期 Week:设置周
* 函 数 返 回:无
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
void DS3231_SetTime2(int Year,uint8_t Mouth,uint8_t Date,uint8_t Week){
uint8_t SetTime2[5]={DS3231ADD_Week,DEC_to_BCD(Week),DEC_to_BCD(Date),DEC_to_BCD(Mouth),DEC_to_BCD(Year-2000)};
HAL_I2C_Master_Transmit(&hi2c1, DS3231_ADDRESS, SetTime2, 5, 100);
//HAL_I2C_Mem_Write(&hi2c1, DS3231_ADDRESS, 0x00, I2C_MEMADD_SIZE_8BIT, data, 7, 100)
}
/******************************************************************
* 函 数 名 称:DS3231_RaedTime
* 函 数 说 明:DS3231读出时间
* 函 数 形 参:无
* 函 数 返 回:Sec
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
void DS3231_ReadTime(void){
HAL_I2C_Mem_Read(&hi2c1, DS3231_ADDRESS, DS3231ADD_Sec, I2C_MEMADD_SIZE_8BIT, DS3231_Data, 7, 0xFFFFF);
}
/******************************************************************
* 函 数 名 称:DS3231_RaedSec
* 函 数 说 明:DS3231读出时间(秒数)
* 函 数 形 参:无
* 函 数 返 回:Sec
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
uint8_t DS3231_RaedSec(void){
uint8_t Sec=BCD_to_DEC(DS3231_Data[0]);
return Sec;
}
/******************************************************************
* 函 数 名 称:DS3231_RaedMin
* 函 数 说 明:DS3231读出分钟
* 函 数 形 参:无
* 函 数 返 回:Min
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
uint8_t DS3231_RaedMin(void){
uint8_t Min=BCD_to_DEC(DS3231_Data[1]);
return Min;
}
/******************************************************************
* 函 数 名 称:DS3231_RaedHour
* 函 数 说 明:DS3231读出小时
* 函 数 形 参:无
* 函 数 返 回:Min
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
uint8_t DS3231_RaedHour(void){
uint8_t Hour=0;
Hour=BCD_to_DEC(DS3231_Data[2]);
return Hour;
}
/******************************************************************
* 函 数 名 称:DS3231_RaedWeek
* 函 数 说 明:DS3231读出星期
* 函 数 形 参:无
* 函 数 返 回:Week
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
uint8_t DS3231_RaedWeek(void){
uint8_t Week=0;
Week=BCD_to_DEC(DS3231_Data[3]);
return Week;
}
/******************************************************************
* 函 数 名 称:DS3231_RaedDate
* 函 数 说 明:DS3231读出日期
* 函 数 形 参:无
* 函 数 返 回:Date
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
uint8_t DS3231_RaedDate(void){
uint8_t Date=0;
Date=BCD_to_DEC(DS3231_Data[4]);
return Date;
}
/******************************************************************
* 函 数 名 称:DS3231_RaedMouth
* 函 数 说 明:DS3231读出月份
* 函 数 形 参:无
* 函 数 返 回:Mouth
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
uint8_t DS3231_RaedMouth(void){
uint8_t Mouth=0;
Mouth=BCD_to_DEC(DS3231_Data[5]);
return Mouth;
}
/******************************************************************
* 函 数 名 称:DS3231_RaedYear
* 函 数 说 明:DS3231读出年
* 函 数 形 参:无
* 函 数 返 回:Mouth
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
int DS3231_RaedYear(void){
int Year=0;
Year=BCD_to_DEC(DS3231_Data[6])+2000;
return Year;
}
// 读取温度(精度0.25°C)
float DS3231_RaedTemp(void) {
uint8_t temp[2];
if(HAL_I2C_Mem_Read(&hi2c1, DS3231_ADDRESS, DS3231ADD_TempH, I2C_MEMADD_SIZE_8BIT, temp, 2, 100) != HAL_OK) {
return -99.9; // 错误值
}
return (float)temp[0] + ((temp[1] >> 6) * 0.25f);
}
STM32C092_DS3231.h的库文件
#ifndef __STM32C092_DS3231_H
#define __STM32C092_DS3231_H
#include "main.h"
#include "i2c.h"
typedef enum
{
DS3231_SQW_1S=0,
DS3231_SQW_NONE
} DS3231_SQWState;
//枚举变量 DS3231_SQW枚举变量
// 函数声明
void DS3231_Init(void);
void DS3231_SQW(DS3231_SQWState SQW_SOURCE);
void DS3231_SetTime1(uint8_t Hour,uint8_t Min,uint8_t Sec);
void DS3231_SetTime2(int Year,uint8_t Mouth,uint8_t Date,uint8_t Week);
void DS3231_ReadTime(void);
uint8_t DS3231_RaedSec(void);//DS3231读出时间(秒数)
uint8_t DS3231_RaedMin(void);//DS3231读出分钟
uint8_t DS3231_RaedHour(void);//DS3231读出小时
uint8_t DS3231_RaedWeek(void);//DS3231读出星期
uint8_t DS3231_RaedDate(void);//DS3231读出日期
uint8_t DS3231_RaedMouth(void);//DS3231读出月份
int DS3231_RaedYear(void);//DS3231读出年
float DS3231_RaedTemp(void);//DS3231读出温度
#endif
主函数
/*
*作者:下行路轨上的C70E 论坛ID钟详
*Nucleo-C092 demo程序任务6:使用I2C1读取DS3231获得时间,将收到的时间发出给串口助手,同时控制LED2灯及DS3231 SQW脚LED闪烁。
*使用意法半导体Nucleo板卡,配置外置晶体48M,波特率9600
*光泽,光泽,到光泽的下车了~~光泽,光泽......
*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
#include "stdio.h"
#include "STM32C092_DS3231.h"
/*函数声明*/
void SystemClock_Config(void);
/*主函数*/
int main(void) {
HAL_Init();
SystemClock_Config();
MX_I2C1_Init(); // 确保I2C已初始化
MX_GPIO_Init();
MX_USART2_UART_Init();
/*DS3231初始化*/
DS3231_Init();
/*DS3231写入初始时间*/
DS3231_SQW(DS3231_SQW_1S);
DS3231_SetTime1(23,59,30);
DS3231_SetTime2(2025,4,21,1);
/*打印作者信息*/
printf("\r\nST Chinese Forum Evaluation Plan\r\n");
printf("\r\nBoard Mode:Nucleo-C092\r\n");
printf("\r\nDemo6:DS3231 HardwareIIC Test\r\n");
printf("\r\nReviewer:Xiang Zhong Bli:C70E\r\n");
while(1) {
DS3231_ReadTime();//使用DS3231读取一个总的时间放到.c文件数组里面
/*打印从DS3231读取到的信息*/
printf("Time2=%d:%d:%d:%d\n",DS3231_RaedYear(),DS3231_RaedMouth(),DS3231_RaedDate(),DS3231_RaedWeek());
printf("Time1=%d:%d:%d\n",DS3231_RaedHour(),DS3231_RaedMin(),DS3231_RaedSec());
printf("Temp=%2f\n",DS3231_RaedTemp());
HAL_GPIO_TogglePin(User_LED2_GPIO_Port,User_LED2_Pin);
HAL_Delay(1000);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_FLASH_SET_LATENCY(FLASH_LATENCY_1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
/*printf函数支持(阻塞法打印数据)*/
int fputc(int ch,FILE *f)
{
HAL_UART_Transmit(&huart2,(uint8_t *)&ch,1,0xFFFF);//阻塞方式打印
return ch;
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
接着我们介绍软件IIC,以下并不是ST官方要求的评测内容,但是我觉得这个软件IIC也是挺重要的。软件IIC则是并没有启用IIC模块,而是配置两个GPIO口当作IIC数据线来模拟IIC通信的时序,最后读取到DS3231中的信息。值得注意的是,由于我们模拟IIC的时序,这里不需要开芯片的IIC外设,相关配置如下:

程序基本原理是:MCU上电后先初始化一堆东西,并使用串口打印相关作者信息,接着进入主函数中,每间隔1s打印一次串口信息,读取DS3231内部的数据信息并且翻转LED2表示它工作正常,如下图所示:

串口打印如下:

程序实现如下:
STM32C092_SoftwareIIC.c的库文件(模拟时序用的库)
#include "STM32C092_SoftwareIIC.h"
uint8_t Freq=0;
//GPIO口宏定义中配置,方便更改
#define SoftWareIIC_PORT GPIOB //使用GPIOB
#define SDA_PIN SoftwareIIC_SDA_Pin//配置第9号脚
#define SCL_PIN SoftwareIIC_SCL_Pin//配置第8号脚
void STM32C092_SoftwareIIC_us (unsigned long __us);
/******************************************************************
* 函 数 名 称:STM32C092_SoftwareIIC_Init
* 函 数 说 明:初始化软件IIC的引脚并将两个引脚置高电平
* 函 数 形 参:uint8_t __Freq 写入加入项目中的RCC频率(例如配置64000000hz则写入64即可)
* 函 数 返 回:无
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
void STM32C092_SoftwareIIC_Init(uint8_t __Freq)
{
Freq=__Freq;
HAL_GPIO_WritePin(SoftWareIIC_PORT,SDA_PIN|SCL_PIN,GPIO_PIN_SET);//将SDA线,SCL线置高电平,待后续处理
}
/******************************************************************
* 函 数 名 称:IIC_DigitalWrite_SDA
* 函 数 说 明:对SDA线进行操作(1或者0)
* 函 数 形 参:uint8_t Value
* 函 数 返 回:无
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
void IIC_DigitalWrite_SDA(uint8_t Value)
{
if(Value==0){
HAL_GPIO_WritePin(SoftWareIIC_PORT,SDA_PIN,GPIO_PIN_RESET);
}
else{
HAL_GPIO_WritePin(SoftWareIIC_PORT,SDA_PIN,GPIO_PIN_SET);
}
STM32C092_SoftwareIIC_us(10);
}
/******************************************************************
* 函 数 名 称:IIC_DigitalWrite_SCL
* 函 数 说 明:对SCL线进行操作(1或者0)
* 函 数 形 参:uint8_t Value
* 函 数 返 回:无
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
void IIC_DigitalWrite_SCL(uint8_t Value)
{
if(Value==0){
HAL_GPIO_WritePin(SoftWareIIC_PORT,SCL_PIN,GPIO_PIN_RESET);
}
else{
HAL_GPIO_WritePin(SoftWareIIC_PORT,SCL_PIN,GPIO_PIN_SET);
}
STM32C092_SoftwareIIC_us(10);
}
/******************************************************************
* 函 数 名 称:IIC_DigitalRead_SDA
* 函 数 说 明:对SDA线进行读操作
* 函 数 形 参:无
* 函 数 返 回:Value
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
uint8_t IIC_DigitalRead_SDA(void){
uint8_t Value;
Value=HAL_GPIO_ReadPin(SoftWareIIC_PORT,SDA_PIN);
STM32C092_SoftwareIIC_us(10);
return Value;
}
/******************************************************************
* 函 数 名 称:STM32C092_SoftwareIIC_us
* 函 数 说 明:使用nop()产生IIC通信时序需要的延时
* 函 数 形 参:unsigned long __us
* 函 数 返 回:无
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
void STM32C092_SoftwareIIC_us (unsigned long __us)
{
__us*=Freq/4;
while(__us){
__NOP();
__us--;
}
}
/******************************************************************
* 函 数 名 称:STM32C092_SoftwareIIC_Start
* 函 数 说 明:软件IIC启动
* 函 数 形 参:无
* 函 数 返 回:无
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
void STM32C092_SoftwareIIC_Start(void){
IIC_DigitalWrite_SDA(1);
IIC_DigitalWrite_SCL(1);
IIC_DigitalWrite_SDA(0);
IIC_DigitalWrite_SCL(0);
}
/******************************************************************
* 函 数 名 称:STM32C092_SoftWareIIC_Stop
* 函 数 说 明:软件IIC停止
* 函 数 形 参:无
* 函 数 返 回:无
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
void STM32C092_SoftWareIIC_Stop(void){
IIC_DigitalWrite_SDA(0);
IIC_DigitalWrite_SCL(1);
IIC_DigitalWrite_SDA(1);
}
/******************************************************************
* 函 数 名 称:STM32C092_SoftWareIIC_SendByte
* 函 数 说 明:软件IIC发送一个字节
* 函 数 形 参:uint8_t Byte
* 函 数 返 回:无
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
void STM32C092_SoftWareIIC_SendByte(uint8_t Byte){
uint8_t i;
for(i=0;i<8;i++){
IIC_DigitalWrite_SDA(Byte&(0x80>>i));
IIC_DigitalWrite_SCL(1);
IIC_DigitalWrite_SCL(0);
}
}
/******************************************************************
* 函 数 名 称:STM32C092_SoftWareIIC_ReadByte
* 函 数 说 明:软件IIC接收一个字节
* 函 数 形 参:无
* 函 数 返 回:Byte
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
uint8_t STM32C092_SoftWareIIC_ReadByte(void){
uint8_t i,Byte=0;
IIC_DigitalWrite_SDA(1);
for(i=0;i<8;i++){
IIC_DigitalWrite_SCL(1);
if(IIC_DigitalRead_SDA()==1){
Byte|=(0x80>>i);
}
IIC_DigitalWrite_SCL(0);
}
return Byte;
}
/******************************************************************
* 函 数 名 称:STM32C092_SoftWareIIC_SendACK
* 函 数 说 明:软件IIC发送一个应答信号
* 函 数 形 参:uint8_t ACK
* 函 数 返 回:无
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
void STM32C092_SoftWareIIC_SendACK(uint8_t ACK){
IIC_DigitalWrite_SDA(ACK);
IIC_DigitalWrite_SCL(1);
IIC_DigitalWrite_SCL(0);
}
/******************************************************************
* 函 数 名 称:STM32C092_SoftWareIIC_ReadACK
* 函 数 说 明:软件IIC接收一个应答信号用来监测从设备
* 函 数 形 参:无
* 函 数 返 回:Byte
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
uint8_t STM32C092_SoftWareIIC_ReadACK(void){
uint8_t ACK=0;
IIC_DigitalWrite_SDA(1);
IIC_DigitalWrite_SCL(1);
ACK=IIC_DigitalRead_SDA();
IIC_DigitalWrite_SCL(0);
return ACK;
}
STM32C092_SoftwareIIC.h的库文件(模拟时序用的库)
#ifndef __STM32C092_SoftwareIIC_H
#define __STM32C092_SoftwareIIC_H
#include "main.h"
void STM32C092_SoftwareIIC_Init(uint8_t __Freq);
void IIC_DigitalWrite_SDA(uint8_t Value);
void IIC_DigitalWrite_SCL(uint8_t Value);
uint8_t IIC_DigitalRead_SDA(void);
void STM32C092_SoftwareIIC_Start(void);
void STM32C092_SoftWareIIC_Stop(void);
void STM32C092_SoftWareIIC_SendByte(uint8_t Byte);
uint8_t STM32C092_SoftWareIIC_ReadByte(void);
void STM32C092_SoftWareIIC_SendACK(uint8_t ACK);
uint8_t STM32C092_SoftWareIIC_ReadACK(void);
#endif
STM32C092_DS3231.c的库文件
#include "STM32C092_DS3231.h"
#define DS3231_ADDRESS 0xD0 //DS3231 IIC通信地址
//DS3231寄存器地址
#define DS3231ADD_Sec 0x00
#define DS3231ADD_Min 0x01
#define DS3231ADD_Hour 0x02
#define DS3231ADD_Week 0x03
#define DS3231ADD_Date 0x04
#define DS3231ADD_Mouth 0x05
#define DS3231ADD_Year 0x06
#define DS3231ADD_Control 0x0E
#define DS3231ADD_TempH 0x11
#define DS3231ADD_TempL 0x12
/******************************************************************
* 函 数 名 称:BCD_DEC
* 函 数 说 明:BCD码转换DEC数字
* 函 数 形 参:Value
* 函 数 返 回:(Value / 16 * 10) + (Value % 16)
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
uint8_t BCD_DEC(uint8_t Value){ //BCD码转换DEC数字
return ( (Value / 16 * 10) + (Value % 16) );
}
/******************************************************************
* 函 数 名 称:DEC_BCD
* 函 数 说 明:DEC码转换BCD数字
* 函 数 形 参:Value
* 函 数 返 回:(Value / 10 * 16) + (Value % 10)
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
uint8_t DEC_BCD(uint8_t Value){ //DEC码转换BCD数字
return ( (Value / 10 * 16) + (Value % 10) );
}
/******************************************************************
* 函 数 名 称:DS3231_Init
* 函 数 说 明:DS3231初始化,将RCC频率写入软件IIC库初始化,写入项目中的RCC频率(例如配置64000000hz则写入64即可)
* 函 数 形 参:uint8_t __Freq
* 函 数 返 回:无
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
void DS3231_Init(uint8_t __Freq){
STM32C092_SoftwareIIC_Init(__Freq);
}
/******************************************************************
* 函 数 名 称:DS3231_WriteByte
* 函 数 说 明:DS3231写入一个字节
* 函 数 形 参:寄存器地址Address 写入数据Data
* 函 数 返 回:无
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
void DS3231_WriteByte(uint8_t Address,uint8_t Data){
STM32C092_SoftwareIIC_Start();
STM32C092_SoftWareIIC_SendByte(DS3231_ADDRESS);
STM32C092_SoftWareIIC_ReadACK();
STM32C092_SoftWareIIC_SendByte(Address);
STM32C092_SoftWareIIC_ReadACK();
STM32C092_SoftWareIIC_SendByte(DEC_BCD(Data));
STM32C092_SoftWareIIC_ReadACK();
STM32C092_SoftWareIIC_Stop();
}
/******************************************************************
* 函 数 名 称:DS3231_ReadByte
* 函 数 说 明:DS3231读取一个字节
* 函 数 形 参:寄存器地址Address
* 函 数 返 回:读出数据Data
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
uint8_t DS3231_ReadByte(uint8_t Address){
uint8_t Data;
STM32C092_SoftwareIIC_Start();
STM32C092_SoftWareIIC_SendByte(DS3231_ADDRESS);
STM32C092_SoftWareIIC_ReadACK();
STM32C092_SoftWareIIC_SendByte(Address);
STM32C092_SoftWareIIC_ReadACK();
STM32C092_SoftwareIIC_Start();
STM32C092_SoftWareIIC_SendByte(DS3231_ADDRESS | 0x01);
STM32C092_SoftWareIIC_ReadACK();
Data=STM32C092_SoftWareIIC_ReadByte();
STM32C092_SoftWareIIC_SendACK(1);
STM32C092_SoftWareIIC_Stop();
return Data;
}
/******************************************************************
* 函 数 名 称:DS3231_SQW
* 函 数 说 明:DS3231设置SQW频率值
* 函 数 形 参:DS3231_SQWState(枚举变量,支持DS3231_SQW_1S、DS3231_SQW_NONE)
* 函 数 返 回:无
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
void DS3231_SQW(DS3231_SQWState SQW_SOURCE){
if(SQW_SOURCE==DS3231_SQW_1S){
DS3231_WriteByte(DS3231ADD_Control,0x00);
}
else{
DS3231_WriteByte(DS3231ADD_Control,0x04);
}
}
/******************************************************************
* 函 数 名 称:DS3231_SetTime1
* 函 数 说 明:DS3231设置时间1
* 函 数 形 参:Hour:设置小时 Min:设置分钟 Sec:设置秒
* 函 数 返 回:无
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
void DS3231_SetTime1(uint8_t Hour,uint8_t Min,uint8_t Sec){
DS3231_WriteByte(DS3231ADD_Sec,Sec);
DS3231_WriteByte(DS3231ADD_Min,Min);
DS3231_WriteByte(DS3231ADD_Hour,Hour);
}
/******************************************************************
* 函 数 名 称:DS3231_SetTime2
* 函 数 说 明:DS3231设置时间2
* 函 数 形 参:Year:设置年 Mouth:设置月 Date:设置日期 Week:设置周
* 函 数 返 回:无
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
void DS3231_SetTime2(int Year,uint8_t Mouth,uint8_t Date,uint8_t Week){
DS3231_WriteByte(DS3231ADD_Week,Week);
DS3231_WriteByte(DS3231ADD_Date,Date);
DS3231_WriteByte(DS3231ADD_Mouth,Mouth);
DS3231_WriteByte(DS3231ADD_Year,Year-2000);
}
/******************************************************************
* 函 数 名 称:DS3231_RaedSec
* 函 数 说 明:DS3231读出时间(秒数)
* 函 数 形 参:无
* 函 数 返 回:Sec
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
uint8_t DS3231_RaedSec(void){
uint8_t Sec=BCD_DEC(DS3231_ReadByte(DS3231ADD_Sec));
return Sec;
}
/******************************************************************
* 函 数 名 称:DS3231_RaedMin
* 函 数 说 明:DS3231读出分钟
* 函 数 形 参:无
* 函 数 返 回:Min
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
uint8_t DS3231_RaedMin(void){
uint8_t Min=0;
Min=BCD_DEC(DS3231_ReadByte(DS3231ADD_Min));
return Min;
}
/******************************************************************
* 函 数 名 称:DS3231_RaedHour
* 函 数 说 明:DS3231读出小时
* 函 数 形 参:无
* 函 数 返 回:Min
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
uint8_t DS3231_RaedHour(void){
uint8_t Hour=0;
Hour=BCD_DEC(DS3231_ReadByte(DS3231ADD_Hour));
return Hour;
}
/******************************************************************
* 函 数 名 称:DS3231_RaedWeek
* 函 数 说 明:DS3231读出星期
* 函 数 形 参:无
* 函 数 返 回:Week
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
uint8_t DS3231_RaedWeek(void){
uint8_t Week=0;
Week=BCD_DEC(DS3231_ReadByte(DS3231ADD_Week));
return Week;
}
/******************************************************************
* 函 数 名 称:DS3231_RaedDate
* 函 数 说 明:DS3231读出日期
* 函 数 形 参:无
* 函 数 返 回:Date
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
uint8_t DS3231_RaedDate(void){
uint8_t Date=0;
Date=BCD_DEC(DS3231_ReadByte(DS3231ADD_Date));
return Date;
}
/******************************************************************
* 函 数 名 称:DS3231_RaedMouth
* 函 数 说 明:DS3231读出月份
* 函 数 形 参:无
* 函 数 返 回:Mouth
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
uint8_t DS3231_RaedMouth(void){
uint8_t Mouth=0;
Mouth=BCD_DEC(DS3231_ReadByte(DS3231ADD_Mouth));
return Mouth;
}
/******************************************************************
* 函 数 名 称:DS3231_RaedYear
* 函 数 说 明:DS3231读出年
* 函 数 形 参:无
* 函 数 返 回:Mouth
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
int DS3231_RaedYear(void){
int Year=0;
Year=BCD_DEC(DS3231_ReadByte(DS3231ADD_Year))+2000;
return Year;
}
/******************************************************************
* 函 数 名 称:DS3231_RaedTemp
* 函 数 说 明:DS3231读出温度
* 函 数 形 参:无
* 函 数 返 回:Temp
* 作 者:C70E 1720223
* 备 注:无
******************************************************************/
float DS3231_RaedTemp(void){
float Temp=BCD_DEC(DS3231_ReadByte(DS3231ADD_TempH))+(DS3231_ReadByte(DS3231ADD_TempL)/2.56)*0.01;
return Temp;
}
STM32C092_DS3231.h的库文件
#ifndef __STM32C092_DS3231_H
#define __STM32C092_DS3231_H
#include "STM32C092_SoftWareIIC.h"
typedef enum
{
DS3231_SQW_1S=0,
DS3231_SQW_NONE
} DS3231_SQWState;
//枚举变量 DS3231_SQW枚举变量
/******************************************************************************/
/* Include files */
/******************************************************************************/
uint8_t BCD_DEC(uint8_t Value);//BCD码转换DEC数字
uint8_t DEC_BCD(uint8_t Value);//DEC码转换BCD数字
void DS3231_Init(uint8_t __Freq);//DS3231初始化,将RCC频率写入软件IIC库初始化,写入项目中的RCC频率(例如配置64000000hz则写入64即可)
void DS3231_WriteByte(uint8_t Address,uint8_t Data);//DS3231写入一个字节
uint8_t DS3231_ReadByte(uint8_t Address);//DS3231读取一个字节
void DS3231_SQW(DS3231_SQWState SQW_SOURCE);//DS3231设置SQW频率值
void DS3231_SetTime1(uint8_t Hour,uint8_t Min,uint8_t Sec);//DS3231设置时间1
void DS3231_SetTime2(int Year,uint8_t Mouth,uint8_t Date,uint8_t Week);//DS3231设置时间2
uint8_t DS3231_RaedSec(void);//DS3231读出时间(秒数)
uint8_t DS3231_RaedMin(void);//DS3231读出分钟
uint8_t DS3231_RaedHour(void);//DS3231读出小时
uint8_t DS3231_RaedWeek(void);//DS3231读出星期
uint8_t DS3231_RaedDate(void);//DS3231读出日期
uint8_t DS3231_RaedMouth(void);//DS3231读出月份
int DS3231_RaedYear(void);//DS3231读出年
float DS3231_RaedTemp(void);//DS3231读出温度
#endif
主函数
/*
*作者:下行路轨上的C70E 论坛ID钟详
*Nucleo-C092 demo程序任务5:使用I/O模拟IIC时序,并读取DS3231获得时间,将收到的时间发出给串口助手,同时控制LED2灯闪烁。
*使用意法半导体Nucleo板卡,配置外置晶体48M,波特率9600
*本次列车开往萧山国际机场,下一站,港城大道,开左边门
*/
/*包含所需头文件*/
#include "main.h"
#include "usart.h"
#include "gpio.h"
#include "stdio.h"
#include "stm32c0xx_it.h"
#include "STM32C092_DS3231.h"
/*函数声明*/
void SystemClock_Config(void);
/*定义变量*/
float Temp;
/*主函数*/
int main(void){
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
/*DS3231写入初始时间*/
DS3231_Init(48);
DS3231_SetTime1(23,59,30);
DS3231_SetTime2(2025,4,21,1);
DS3231_SQW(DS3231_SQW_1S);
/*打印作者信息*/
printf("\r\nST Chinese Forum Evaluation Plan\r\n");
printf("\r\nBoard Mode:Nucleo-C092\r\n");
printf("\r\nDemo5:DS3231 SoftwareIIC Test\r\n");
printf("\r\nReviewer:Xiang Zhong Bli:C70E\r\n");
/*进入循环体*/
while (1){
/*打印从DS3231读取到的信息*/
printf("Time2=%d:%d:%d:%d\n",DS3231_RaedYear(),DS3231_RaedMouth(),DS3231_RaedDate(),DS3231_RaedWeek());
printf("Time1=%d:%d:%d\n",DS3231_RaedHour(),DS3231_RaedMin(),DS3231_RaedSec());
printf("Temp=%f\n",DS3231_RaedTemp());
HAL_GPIO_TogglePin(User_LED2_GPIO_Port,User_LED2_Pin);
HAL_Delay(1000);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_FLASH_SET_LATENCY(FLASH_LATENCY_1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
/*printf函数支持(阻塞法打印数据)*/
int fputc(int ch,FILE *f)
{
HAL_UART_Transmit(&huart2,(uint8_t *)&ch,1,0xFFFF);//阻塞方式打印
return ch;
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
好的,感谢各位的观看,这样我们就实现了IIC通信测试。