STM32 I2C硬件配置库函数
单片机学习!
目录
前言
一、I2C_DeInit函数
二、I2C_Init函数
三、I2C_StructInit函数
四、I2C_Cmd函数
五、I2C_GenerateSTART函数
六、I2C_GenerateSTOP函数
七、I2C_AcknowledgeConfig函数
八、I2C_SendData函数
九、I2C_ReceiveData函数
十、I2C_Send7bitAddress函数
十一、I2C状态监控功能
十二、标志位函数
12.1 I2C_GetFlagStatus函数
12.2 I2C_ClearFlag函数
12.3 I2C_GetITStatus函数
12.4 I2C_ClearITPendingBit函数
前言
本文介绍了I2C硬件配置常用的一些库函数。
一、I2C_DeInit函数
I2C_DeInit函数将外设 I2Cx 寄存器重设为缺省值。
二、I2C_Init函数
I2C_Init函数用于I2C的初始化。函数第二个参数是初始化的结构体。
I2C_Mode 用以设置 I2C 的模式。下表给出了该参数可取的值:
I2C_DutyCycle 用以设置 I2C 的占空比。下表给出了该参数可取的值:
注意:该参数只有在 I2C 工作在快速模式(时钟工作频率高于 100KHz)下才有意义。
I2C_OwnAddress1 该参数用来设置第一个设备自身地址,它可以是一个 7 位地址或者一个 10 位地址。
I2C_Ack 使能或者失能应答(ACK),下表给出了该参数可取的值:
I2C_AcknowledgedAddress 定义了应答 7 位地址还是 10 位地址。下表给出了该参数可取的值:
I2C_ClockSpeed 该参数用来设置时钟频率,这个值不能高于 400KHz。
三、I2C_StructInit函数
下表给出了 I2C_InitStruct 各个成员的缺省值:
四、I2C_Cmd函数
使能或失能I2C外设用I2C_Cmd函数来完成。
五、I2C_GenerateSTART函数
调用I2C_GenerateSTART函数,就可以生成起始条件。
程序源码:
/*** @brief Generates I2Cx communication START condition.* @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.* @param NewState: new state of the I2C START condition generation.* This parameter can be: ENABLE or DISABLE.* @retval None.*/
void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)
{/* Check the parameters */assert_param(IS_I2C_ALL_PERIPH(I2Cx));assert_param(IS_FUNCTIONAL_STATE(NewState));if (NewState != DISABLE){/* Generate a START condition */I2Cx->CR1 |= CR1_START_Set;}else{/* Disable the START condition generation */I2Cx->CR1 &= CR1_START_Reset;}
}
如果 NewState 不等于 DISABLE,就把CR1寄存器的 START 位置1;否则,把 START 位清0.
START 位意义可以从手册里的寄存器描述中来了解:
START 置1
- 在从模式下是产生起始条件。
- 在主模式下是产生重复起始条件。
就是 START 这一位置1,产生起始条件。
六、I2C_GenerateSTOP函数
调用I2C_GenerateSTOP函数,生成终止条件。
程序源码:
/*** @brief Generates I2Cx communication STOP condition.* @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.* @param NewState: new state of the I2C STOP condition generation.* This parameter can be: ENABLE or DISABLE.* @retval None.*/
void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)
{/* Check the parameters */assert_param(IS_I2C_ALL_PERIPH(I2Cx));assert_param(IS_FUNCTIONAL_STATE(NewState));if (NewState != DISABLE){/* Generate a STOP condition */I2Cx->CR1 |= CR1_STOP_Set;}else{/* Disable the STOP condition generation */I2Cx->CR1 &= CR1_STOP_Reset;}
}
I2C_GenerateSTOP函数其实就是操作 CR1 的 STOP 位,查询手册中I2C寄存器的内容:
STOP 是产生停止条件的。STOP位置1,产生停止条件。
七、I2C_AcknowledgeConfig函数
I2C_AcknowledgeConfig函数是用来配置 CR1 的 ACK 这一位。就是配置,在收到一个字节后是否给从机应答。
程序源码:
/*** @brief Enables or disables the specified I2C acknowledge feature.* @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.* @param NewState: new state of the I2C Acknowledgement.* This parameter can be: ENABLE or DISABLE.* @retval None.*/
void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState)
{/* Check the parameters */assert_param(IS_I2C_ALL_PERIPH(I2Cx));assert_param(IS_FUNCTIONAL_STATE(NewState));if (NewState != DISABLE){/* Enable the acknowledgement */I2Cx->CR1 |= CR1_ACK_Set;}else{/* Disable the acknowledgement */I2Cx->CR1 &= CR1_ACK_Reset;}
}
手册ACK寄存器描述:
ACK就是应答使能。STM32作为主机,在接收到一个字节后,是给从机应答还是非应答,就取决于ACK这一位。
在应答的时候:
- 如果ACK是1,就给从机应答。
- 如果ACK是0,就不给从机应答。
八、I2C_SendData函数
I2C_SendData函数用于写数据到数据寄存器DR。
程序源码:
/*** @brief Sends a data byte through the I2Cx peripheral.* @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.* @param Data: Byte to be transmitted..* @retval None*/
void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data)
{/* Check the parameters */assert_param(IS_I2C_ALL_PERIPH(I2Cx));/* Write in the DR register the data to be sent */I2Cx->DR = Data;
}
分析源码,I2C_SendData函数实际就是把Data这个数据直接写入到DR寄存器。
手册DR寄存器描述:
DR数据寄存器用于存放接收到的数据或放置用于发送到总线的数据。在发送器模式下,当写一个字节至DR寄存器时,自动启动数据传输。一旦传输开始,也就是TxE=1,发送寄存器空,如果能及时把下一个需传输的数据写入DR寄存器,I2C模块将保持连续的数据流。
从上面可以看出两个信息:
- 一个是,写入DR,自动启动数据传输。也就是产生发送一个字节的波形。
- 另一个是,在上一个数据移位传输过程中,如果及时把下一个数据放在DR里等着,这样就能保持连续的数据流。
九、I2C_ReceiveData函数
I2C_ReceiveData函数用于读取DR数据,作为返回值。
程序源码:
/*** @brief Returns the most recent received data by the I2Cx peripheral.* @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.* @retval The value of the received data.*/
uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx)
{/* Check the parameters */assert_param(IS_I2C_ALL_PERIPH(I2Cx));/* Return the data in the DR register */return (uint8_t)I2Cx->DR;
}
手册DR寄存器描述:
在接收器模式下,接收到的字节被拷贝到DR寄存器,这时就是RxNE=1,接收寄存器非空。在接收到下一个字节(RxNE=1)之前读出数据寄存器,即可实现连续的数据传送。
这里也可以看出两个信息:
- 一个是,接收移位完成时,收到的一个字节由移位寄存器转到数据寄存器。读取数据寄存器就能接收一个字节了。
- 另一个是,要在下一个字节收到之前,及时把上一个字节取走,防止数据覆盖。这样才能实现连续的数据流。
十、I2C_Send7bitAddress函数
I2C_Send7bitAddress函数是发送7位地址的专用函数。
程序源码:
/*** @brief Transmits the address byte to select the slave device.* @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.* @param Address: specifies the slave address which will be transmitted* @param I2C_Direction: specifies whether the I2C device will be a* Transmitter or a Receiver. This parameter can be one of the following values* @arg I2C_Direction_Transmitter: Transmitter mode* @arg I2C_Direction_Receiver: Receiver mode* @retval None.*/
void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction)
{/* Check the parameters */assert_param(IS_I2C_ALL_PERIPH(I2Cx));assert_param(IS_I2C_DIRECTION(I2C_Direction));/* Test on the direction to set/reset the read/write bit */if (I2C_Direction != I2C_Direction_Transmitter){/* Set the address bit0 for read */Address |= OAR1_ADD0_Set;}else{/* Reset the address bit0 for write */Address &= OAR1_ADD0_Reset;}/* Send the address */I2Cx->DR = Address;
}
从源码可以看出,Address这个参数实际上也是通过DR发送的。只不过是Address参数在发送之前设置了Address最低位的读写位。
代码意思是:如果I2C_Direction不是发送,就把Address的最低位置1,也就是读;否则就把Address的最低位清0,也就是写。
在发送地址的时候,可以用一下这个函数。当然也可以手动设置最低位,调用I2C_SendData函数来发送地址。
十一、I2C状态监控功能
源码说明:
/*** @brief****************************************************************************************** I2C State Monitoring Functions* **************************************************************************************** * This I2C driver provides three different ways for I2C state monitoring* depending on the application requirements and constraints:* * * 1) Basic state monitoring:* Using I2C_CheckEvent() function:* It compares the status registers (SR1 and SR2) content to a given event* (can be the combination of one or more flags).* It returns SUCCESS if the current status includes the given flags * and returns ERROR if one or more flags are missing in the current status.* - When to use:* - This function is suitable for most applications as well as for startup * activity since the events are fully described in the product reference manual * (RM0008).* - It is also suitable for users who need to define their own events.* - Limitations:* - If an error occurs (ie. error flags are set besides to the monitored flags),* the I2C_CheckEvent() function may return SUCCESS despite the communication* hold or corrupted real state. * In this case, it is advised to use error interrupts to monitor the error* events and handle them in the interrupt IRQ handler.* * @note * For error management, it is advised to use the following functions:* - I2C_ITConfig() to configure and enable the error interrupts (I2C_IT_ERR).* - I2Cx_ER_IRQHandler() which is called when the error interrupt occurs.* Where x is the peripheral instance (I2C1, I2C2 ...)* - I2C_GetFlagStatus() or I2C_GetITStatus() to be called into I2Cx_ER_IRQHandler()* in order to determine which error occurred.* - I2C_ClearFlag() or I2C_ClearITPendingBit() and/or I2C_SoftwareResetCmd()* and/or I2C_GenerateStop() in order to clear the error flag and source,* and return to correct communication status.* ** 2) Advanced state monitoring:* Using the function I2C_GetLastEvent() which returns the image of both status * registers in a single word (uint32_t) (Status Register 2 value is shifted left * by 16 bits and concatenated to Status Register 1).* - When to use:* - This function is suitable for the same applications above but it allows to* overcome the limitations of I2C_GetFlagStatus() function (see below).* The returned value could be compared to events already defined in the * library (stm32f10x_i2c.h) or to custom values defined by user.* - This function is suitable when multiple flags are monitored at the same time.* - At the opposite of I2C_CheckEvent() function, this function allows user to* choose when an event is accepted (when all events flags are set and no * other flags are set or just when the needed flags are set like * I2C_CheckEvent() function).* - Limitations:* - User may need to define his own events.* - Same remark concerning the error management is applicable for this * function if user decides to check only regular communication flags (and * ignores error flags).* ** 3) Flag-based state monitoring:* Using the function I2C_GetFlagStatus() which simply returns the status of * one single flag (ie. I2C_FLAG_RXNE ...). * - When to use:* - This function could be used for specific applications or in debug phase.* - It is suitable when only one flag checking is needed (most I2C events * are monitored through multiple flags).* - Limitations: * - When calling this function, the Status register is accessed. Some flags are* cleared when the status register is accessed. So checking the status* of one Flag, may clear other ones.* - Function may need to be called twice or more in order to monitor one * single event.* */
以上描述的是I2C的状态监控函数。
STM32有的状态可能会同时置多个标志位,
- 如果只检查某一个标志位就认为这个状态已经发生了,就不太严谨。
- 如果用I2C_GetFlagStatus函数读多次,再进行判断,又可能比较麻烦。
所以这里库函数就给了多种监控标志位的方案:
第一种,基本状态监控,使用I2C_CheckEvent函数。这种方式就是同时判断一个或多个标志位,来确定几个EVx,从而判断某个状态是否发生。和下图的流程是对应的。
第二种,高级状态监控,使用I2C_GetLastEvent函数,是直接把SR1和SR2这两个状态寄存器拼接成16位的数据。
第三种,基于标志位的状态监控,使用I2C_GetFlagStatus函数判断某一个标志位是否置1了。
十二、标志位函数
12.1 I2C_GetFlagStatus函数
I2C_GetFlagStatus函数用于读取标志位。
下表给出了所有可以被函数 I2C_ GetFlagStatus 检查的标志位列表:
12.2 I2C_ClearFlag函数
I2C_ClearFlag函数用于清除标志位。
下表给出了所有可以被函数 I2C_ ClearFlag 清除的标志位列表:
12.3 I2C_GetITStatus函数
I2C_GetITStatus函数用于读取中断标志位。
下表给出了所有可以被函数 I2C_ GetITStatus 检查的中断标志位列表:
12.4 I2C_ClearITPendingBit函数
I2C_ClearITPendingBit函数用于清除中断标志位。
下表给出了所有可以被函数 I2C_ ClearITPendingBit 清除的中断待处理位列表:
相关文章:

STM32 I2C硬件配置库函数
单片机学习! 目录 前言 一、I2C_DeInit函数 二、I2C_Init函数 三、I2C_StructInit函数 四、I2C_Cmd函数 五、I2C_GenerateSTART函数 六、I2C_GenerateSTOP函数 七、I2C_AcknowledgeConfig函数 八、I2C_SendData函数 九、I2C_ReceiveData函数 十、I2C_Sen…...

特制一个自己的UI库,只用CSS、图标、emoji图 第二版
图: 代码: index.html <!DOCTYPE html> <html lang"zh-CN"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>M…...
Hologres 介绍
Hologres 是 阿里云 提供的一款 实时数据分析平台,它结合了数据仓库(Data Warehouse)和流式计算(Stream Processing)的优势,专为大规模数据分析和实时数据处理而设计。Hologres 基于 PostgreSQL 构建&#…...

oracle闪回表
文章目录 闪回表案例1:(未清理回收站时的闪回表--成功)案例2(清理回收站时的闪回表--失败)案例3:彻底删除表(不经过回收站--失败)案例4:闪回表之后重新命名新表总结1、删…...

蓝桥与力扣刷题(283 移动零)
题目:给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 请注意 ,必须在不复制数组的情况下原地对数组进行操作。 示例 1: 输入: nums [0,1,0,3,12] 输出: [1,3,12,0,0]示例 2: 输入: …...

每日学习30分轻松掌握CursorAI:Cursor AI自然语言编程入门
Cursor AI自然语言编程入门 1. 自然语言编程概述 自然语言编程是一种革命性的编程方式,让开发者能够使用日常语言描述需求,由AI将其转换为可执行的代码。让我们通过系统化的学习来掌握这项技能。 2. 自然语言编程基础 2.1 工作原理流程图 2.2 指令模…...
Ubuntu22.04 离线安装:gcc、make、dkms、build-essential
挂载启动U盘 查看U盘对应的设备名称 sudo fdisk -l 1 # 以下就是需要挂载的U盘 Disk /dev/sdc: 14.9 GiB, 15938355200 bytes, 31129600 sectors Units: sectors of 1 * 512 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): …...

【竞技宝】CS2:HLTV2024选手排名TOP4-NiKo
北京时间2025年1月11日,HLTV年度选手排名正在持续公布中,今日凌晨正式公布了今年的TOP4选手为G2(目前已转为至Falcons)战队的NiKo。 选手简介 NiKo是一名来自波黑的CS职业选手,现年26岁。作为DOTA2饱负盛名的职业选手,NiKo在CS1.6时代就已经开始征战职业赛场。2012年,年仅15岁…...

Kali系统(Debian 10.3) 遇到的问题
目录 问题一:非问题 kali 基础官网与安装 问题二: 问题三: Kali系统 MySQL问题Cant connect to local MySQL server through socket /run/mysqld/mysqld.sock (2) 问题四:重新安装MySQL 也就是MariaDB(MariaDB 含 MySQL相关…...

【2025最新计算机毕业设计】基于SpringBoot+Vue奶茶点单系统(高质量源码,提供文档,免费部署到本地)
作者简介:✌CSDN新星计划导师、Java领域优质创作者、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和学生毕业项目实战,高校老师/讲师/同行前辈交流。✌ 主要内容:🌟Java项目、Python项目、前端项目、PHP、ASP.NET、人工智能…...

洛谷 P1873 [COCI 2011/2012 #5] EKO / 砍树 c语言
题目: P1873 [COCI 2011/2012 #5] EKO / 砍树 - 洛谷 | 计算机科学教育新生态 题目描述 伐木工人 Mirko 需要砍 M 米长的木材。对 Mirko 来说这是很简单的工作,因为他有一个漂亮的新伐木机,可以如野火一般砍伐森林。不过,Mirko…...
【神经网络中的激活函数如何选择?】
在神经网络中,激活函数的选择对于模型的性能和学习效率至关重要。以下是一些关于如何选择激活函数的建议: 一、隐藏层中的激活函数选择 ReLU及其变体: ReLU(Rectified Linear Unit,修正线性单元)ÿ…...
服务器多节点 Grafana、Prometheus 和 Node-Exporter Docker版本部署指南
要在多台服务器上部署 Grafana、Prometheus 和 Node-Exporter,并且其中一台服务器专门用于 Grafana 和 Prometheus 的部署 1. 准备工作 服务器信息: Server 1:用于部署 Grafana 和 Prometheus。 Server 2-n:用于部署 Node-Export…...
<C++学习>C++ Boost 算法集合操作教程
C Boost 算法集合操作教程 Boost 提供了一些非常强大的算法库,用于对集合进行高效的操作。这些集合算法库主要提供了便捷的接口,支持常见的集合运算(如并集、交集、差集等)、排列组合和更高级的容器操作。 1. Boost 算法库简介 …...

Jaeger UI使用、采集应用API排除特定路径
Jaeger使用 注: Jaeger服务端版本为:jaegertracing/all-in-one-1.6.0 OpenTracing版本为:0.33.0,最后一个版本,停留在May 06, 2019。最好升级到OpenTelemetry。 Jaeger客户端版本为:jaeger-client-1.3.2。…...
设计一个利用事务特性可以阻塞线程的排他锁,并且通过注解和 AOP 来实现
设计思路: 利用数据库表记录锁标识:通过唯一标识符(如方法名 参数),我们可以在数据库中插入一条记录,表示当前方法正在执行。这条记录需要记录插入时间。 注解:通过注解标识哪些方法需要加锁&a…...

【2024年华为OD机试】 (A卷,100分)- 对称美学(Java JS PythonC/C++)
一、问题描述 题目描述 对称就是最大的美学,现有一道关于对称字符串的美学。已知: 第1个字符串:R第2个字符串:BR第3个字符串:RBBR第4个字符串:BRRBRBBR第5个字符串:RBBRBRRBBRRBRBBR 相信你…...

【教程】数据可视化处理之2024年各省GDP排名预测!
过去的一年里,我国的综合实力显著提升,在新能源汽车、新一代战机、两栖攻击舰、航空航天、芯片电子、装备制造等领域位居全球前列。虽然全国各省市全年的经济数据公布还需要一段时间,但各地的工业发展数据,财政收入数据已大概揭晓…...

Java 将RTF文档转换为Word、PDF、HTML、图片
RTF文档因其跨平台兼容性而广泛使用,但有时在不同的应用场景可能需要特定的文档格式。例如,Word文档适合编辑和协作,PDF文档适合打印和分发,HTML文档适合在线展示,图片格式则适合社交媒体分享。因此我们可能会需要将RT…...

深度学习的原理和应用
一、深度学习的原理 深度学习是机器学习领域的一个重要分支,其原理基于多层神经网络结构和优化算法。以下是深度学习的核心原理: 多层神经网络结构:深度学习模型通常由多层神经元组成,这些神经元通过权重和偏置相互连接。输入数据…...

C++实现分布式网络通信框架RPC(3)--rpc调用端
目录 一、前言 二、UserServiceRpc_Stub 三、 CallMethod方法的重写 头文件 实现 四、rpc调用端的调用 实现 五、 google::protobuf::RpcController *controller 头文件 实现 六、总结 一、前言 在前边的文章中,我们已经大致实现了rpc服务端的各项功能代…...

TDengine 快速体验(Docker 镜像方式)
简介 TDengine 可以通过安装包、Docker 镜像 及云服务快速体验 TDengine 的功能,本节首先介绍如何通过 Docker 快速体验 TDengine,然后介绍如何在 Docker 环境下体验 TDengine 的写入和查询功能。如果你不熟悉 Docker,请使用 安装包的方式快…...

CMake基础:构建流程详解
目录 1.CMake构建过程的基本流程 2.CMake构建的具体步骤 2.1.创建构建目录 2.2.使用 CMake 生成构建文件 2.3.编译和构建 2.4.清理构建文件 2.5.重新配置和构建 3.跨平台构建示例 4.工具链与交叉编译 5.CMake构建后的项目结构解析 5.1.CMake构建后的目录结构 5.2.构…...
测试markdown--肇兴
day1: 1、去程:7:04 --11:32高铁 高铁右转上售票大厅2楼,穿过候车厅下一楼,上大巴车 ¥10/人 **2、到达:**12点多到达寨子,买门票,美团/抖音:¥78人 3、中饭&a…...
python如何将word的doc另存为docx
将 DOCX 文件另存为 DOCX 格式(Python 实现) 在 Python 中,你可以使用 python-docx 库来操作 Word 文档。不过需要注意的是,.doc 是旧的 Word 格式,而 .docx 是新的基于 XML 的格式。python-docx 只能处理 .docx 格式…...

SpringTask-03.入门案例
一.入门案例 启动类: package com.sky;import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCach…...
是否存在路径(FIFOBB算法)
题目描述 一个具有 n 个顶点e条边的无向图,该图顶点的编号依次为0到n-1且不存在顶点与自身相连的边。请使用FIFOBB算法编写程序,确定是否存在从顶点 source到顶点 destination的路径。 输入 第一行两个整数,分别表示n 和 e 的值(1…...
Java线上CPU飙高问题排查全指南
一、引言 在Java应用的线上运行环境中,CPU飙高是一个常见且棘手的性能问题。当系统出现CPU飙高时,通常会导致应用响应缓慢,甚至服务不可用,严重影响用户体验和业务运行。因此,掌握一套科学有效的CPU飙高问题排查方法&…...
C语言中提供的第三方库之哈希表实现
一. 简介 前面一篇文章简单学习了C语言中第三方库(uthash库)提供对哈希表的操作,文章如下: C语言中提供的第三方库uthash常用接口-CSDN博客 本文简单学习一下第三方库 uthash库对哈希表的操作。 二. uthash库哈希表操作示例 u…...

AxureRP-Pro-Beta-Setup_114413.exe (6.0.0.2887)
Name:3ddown Serial:FiCGEezgdGoYILo8U/2MFyCWj0jZoJc/sziRRj2/ENvtEq7w1RH97k5MWctqVHA 注册用户名:Axure 序列号:8t3Yk/zu4cX601/seX6wBZgYRVj/lkC2PICCdO4sFKCCLx8mcCnccoylVb40lP...