日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

《STM32从零开始学习历程》——I2C固件库

發布時間:2024/1/1 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 《STM32从零开始学习历程》——I2C固件库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

《STM32從零開始學習歷程》@EnzoReventon

STM32 I2C固件庫介紹

相關資料:

I2C物理層介紹
I2C協議層介紹
STM32的I2C特性及架構介紹

參考資料:
[野火EmbedFire]《STM32庫開發實戰指南——基于野火霸天虎開發板》
[正點原子]STM32F4開發指南-庫函數版本_V1.2
[ST]《STM32F4xx中文參考手冊》

I2C初始化函數介紹

typedef struct {uint32_t I2C_ClockSpeed; /*!< 設置SCL時鐘頻率,此值要低于40 0000 */uint16_t I2C_Mode; /*!< 指定工作模式,可以選擇I2C模式以及SMBUS */uint16_t I2C_DutyCycle; /*!< 指定時鐘占空比,可以選擇low/high = 2:1或16:9模式 */uint16_t I2C_OwnAddress1; /*!< 指定自身的I2C設備地址 */uint16_t I2C_Ack; /*!< 使能或關閉響應(一般都要使能)*/uint16_t I2C_AcknowledgedAddress; /*!< 指定地址的長度,可以為7位或者10位 */}I2C_InitTypeDef;
  • I2C_ClockSpeed: 設置I2C的傳輸速率,在調用初始化函數時,函數會根據我們輸入的數值經過運算后把時鐘因子寫入到I2C的時鐘控制寄存器CCR。而我們寫入的這個參數值不得高于400KHz。
    實際上由于CCR寄存器不能寫入小數類型的時鐘因子,影響到SCL的實際頻率可能會低于本成員設置的參數值,這時除了通訊稍慢一點以外,不會對I2C的標準通訊造成其它影響。
  • I2C_Mode: 選擇I2C的使用方式,有I2C模式(I2C_Mode_I2C )和SMBus主、從模式(I2C_Mode_SMBusHost、 I2C_Mode_SMBusDevice ) 。
    I2C不需要在此處區分主從模式,直接設置I2C_Mode_I2C即可。
  • I2C_DutyCycle: 設置I2C的SCL線時鐘的占空比。該配置有兩個選擇,分別為低電平時間比高電平時間為2:1 ( I2C_DutyCycle_2)和16:9(I2C_DutyCycle_16_9)。
    其實這兩個模式的比例差別并不大,一般要求都不會如此嚴格,這里隨便選就可以了。
  • I2C_OwnAddress1: 配置STM32的I2C設備自己的地址,每個連接到I2C總線上的設備都要有一個自己的地址,作為主機也不例外。地址可設置為7位或10位(受下面I2C_AcknowledgeAddress成員決定),只要該地址是I2C總線上唯一的即可。
    STM32的I2C外設可同時使用兩個地址,即同時對兩個地址作出響應,這個結構成員I2C_OwnAddress1配置的是默認的、OAR1寄存器存儲的地址,若需要設置第二個地址寄存器OAR2,可使用
    I2C_OwnAddress2Config函數來配置,OAR2不支持10位地址。
  • I2C_Ack_Enable: 配置I2C應答是否使能,設置為使能則可以發送響應信號。一般配置為允許應答(I2C_Ack_Enable),這是絕大多數遵循I2C標準的設備的通訊要求,改為禁止應答(I2C_Ack_Disable)往往會導致通訊錯誤。
  • I2C_AcknowledgeAddress: 選擇I2C的尋址模式是7位還是10位地址。這需要根據實際連接到I2C總線上設備的地址進行選擇,這個成員的配置也影響到I2C_OwnAddress1成員,只有這里設置成10位模式時,I2C_OwnAddress1才支持10位地址。

配置好I2C初始化函數之后,對I2C進行初始化,使用I2C_Init函數:

void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitSturct)

I2C固件庫的使用

(1)MDK5中點擊Functions,找到stm32f4xx_i2c.c,雙擊點開,即可看到所有函數,點擊相應函數即可定位到函數定義區域查看相關注釋與參數。

(2)使用STM32F4xx_DSP_StdPeriph_Lib_V1.4.0固件庫chm查詢。STM32F4xx_DSP_StdPeriph_Lib_V1.4.0下載地址。

I2C相關函數介紹

本章節介紹一下常用的幾個函數。

  • I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState) 函數:用來產生I2C通訊開始信號的函數。共有兩個參數,一個是選擇I2Cx,可以選擇1~3,根據芯片提供I2C數量來選擇;第二個是狀態選擇,ENABLE與DISABLE。
/** 函數源碼* @brief Generates I2Cx communication START condition.* @param I2Cx: where x can be 1, 2 or 3 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 |= I2C_CR1_START;}else{/* Disable the START condition generation */I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_START);} }
  • I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState) 函數:與上一個開始信號類似,為發送通訊停止信號。參數與開始信號函數一樣。
/*** @brief Generates I2Cx communication STOP condition.* @param I2Cx: where x can be 1, 2 or 3 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 |= I2C_CR1_STOP;}else{/* Disable the STOP condition generation */I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_STOP);} }
  • void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction)函數:為發送設備地址函數。
    I2C_TypeDef* I2Cx:為選擇I2C。
    uint8_t Address:為八位設備地址。
    uint8_t I2C_Direction:方向,Transmitter mode發送器,Receiver mode接收器。
/*** @brief Transmits the address byte to select the slave device.* @param I2Cx: where x can be 1, 2 or 3 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 |= I2C_OAR1_ADD0;}else{/* Reset the address bit0 for write */Address &= (uint8_t)~((uint8_t)I2C_OAR1_ADD0);}/* Send the address */I2Cx->DR = Address; }
  • void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data):為發送數據函數。
  • uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx):為接收數據函數。
/*** @brief Sends a data byte through the I2Cx peripheral.* @param I2Cx: where x can be 1, 2 or 3 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; } ===================================================================================== /*** @brief Returns the most recent received data by the I2Cx peripheral.* @param I2Cx: where x can be 1, 2 or 3 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; }
  • FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)狀態標志位函數:用來判斷指定功能的完成完成狀態。
    uint32_t I2C_FLAG:可以選擇不同的狀態判定功能。
/*** @brief Checks whether the specified I2C flag is set or not.* @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.* @param I2C_FLAG: specifies the flag to check. * This parameter can be one of the following values:* @arg I2C_FLAG_DUALF: Dual flag (Slave mode)* @arg I2C_FLAG_SMBHOST: SMBus host header (Slave mode)* @arg I2C_FLAG_SMBDEFAULT: SMBus default header (Slave mode)* @arg I2C_FLAG_GENCALL: General call header flag (Slave mode)* @arg I2C_FLAG_TRA: Transmitter/Receiver flag* @arg I2C_FLAG_BUSY: Bus busy flag* @arg I2C_FLAG_MSL: Master/Slave flag* @arg I2C_FLAG_SMBALERT: SMBus Alert flag* @arg I2C_FLAG_TIMEOUT: Timeout or Tlow error flag* @arg I2C_FLAG_PECERR: PEC error in reception flag* @arg I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)* @arg I2C_FLAG_AF: Acknowledge failure flag* @arg I2C_FLAG_ARLO: Arbitration lost flag (Master mode)* @arg I2C_FLAG_BERR: Bus error flag* @arg I2C_FLAG_TXE: Data register empty flag (Transmitter)* @arg I2C_FLAG_RXNE: Data register not empty (Receiver) flag* @arg I2C_FLAG_STOPF: Stop detection flag (Slave mode)* @arg I2C_FLAG_ADD10: 10-bit header sent flag (Master mode)* @arg I2C_FLAG_BTF: Byte transfer finished flag* @arg I2C_FLAG_ADDR: Address sent flag (Master mode) "ADSL"如果該地址被發送出去了,那么標志位將會置1.* Address matched flag (Slave mode)"ENDAD"* @arg I2C_FLAG_SB: Start bit flag (Master mode)* @retval The new state of I2C_FLAG (SET or RESET). 判斷下標志狀態,SET為1,RESET為0.*/
  • ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT)中斷標志位函數:同樣與標志位判斷函數用法一樣。
/*** @brief Checks whether the specified I2C interrupt has occurred or not.* @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.* @param I2C_IT: specifies the interrupt source to check. * This parameter can be one of the following values:* @arg I2C_IT_SMBALERT: SMBus Alert flag* @arg I2C_IT_TIMEOUT: Timeout or Tlow error flag* @arg I2C_IT_PECERR: PEC error in reception flag* @arg I2C_IT_OVR: Overrun/Underrun flag (Slave mode)* @arg I2C_IT_AF: Acknowledge failure flag* @arg I2C_IT_ARLO: Arbitration lost flag (Master mode)* @arg I2C_IT_BERR: Bus error flag* @arg I2C_IT_TXE: Data register empty flag (Transmitter)* @arg I2C_IT_RXNE: Data register not empty (Receiver) flag* @arg I2C_IT_STOPF: Stop detection flag (Slave mode)* @arg I2C_IT_ADD10: 10-bit header sent flag (Master mode)* @arg I2C_IT_BTF: Byte transfer finished flag* @arg I2C_IT_ADDR: Address sent flag (Master mode) "ADSL"* Address matched flag (Slave mode)"ENDAD"* @arg I2C_IT_SB: Start bit flag (Master mode)* @retval The new state of I2C_IT (SET or RESET).*/
  • ★ 比起I2C_GetFlagStatus & I2C_GetITStatus更好用的一個標志位判斷函數為:ErrorStatus I2C_CheckEvent(I2C_TypeDef I2Cx, uint32_t I2C_EVENT) 函數,這個函數是專門用來檢查EVx事件的。
    下面是可以供選擇的EV事件,在寫代碼時會用到。
/*** @brief Checks whether the last I2Cx Event is equal to the one passed* as parameter.* @param I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.* @param I2C_EVENT: specifies the event to be checked. * This parameter can be one of the following values:* @arg I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED: EV1* @arg I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED: EV1* @arg I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED: EV1* @arg I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED: EV1* @arg I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED: EV1* @arg I2C_EVENT_SLAVE_BYTE_RECEIVED: EV2* @arg (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_DUALF): EV2* @arg (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_GENCALL): EV2* @arg I2C_EVENT_SLAVE_BYTE_TRANSMITTED: EV3* @arg (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_DUALF): EV3* @arg (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_GENCALL): EV3* @arg I2C_EVENT_SLAVE_ACK_FAILURE: EV3_2* @arg I2C_EVENT_SLAVE_STOP_DETECTED: EV4* @arg I2C_EVENT_MASTER_MODE_SELECT: EV5* @arg I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED: EV6 * @arg I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED: EV6* @arg I2C_EVENT_MASTER_BYTE_RECEIVED: EV7* @arg I2C_EVENT_MASTER_BYTE_TRANSMITTING: EV8* @arg I2C_EVENT_MASTER_BYTE_TRANSMITTED: EV8_2* @arg I2C_EVENT_MASTER_MODE_ADDRESS10: EV9* * @note For detailed description of Events, please refer to section I2C_Events* in stm32f4xx_i2c.h file.* * @retval An ErrorStatus enumeration value:* - SUCCESS: Last event is equal to the I2C_EVENT* - ERROR: Last event is different from the I2C_EVENT*/
  • void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState)響應函數:配置成ENABLE時,接收到數據時會響應。
/*** @brief Enables or disables the specified I2C acknowledge feature.* @param I2Cx: where x can be 1, 2 or 3 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 |= I2C_CR1_ACK;}else{/* Disable the acknowledgement */I2Cx->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ACK);} }

總結

以上是生活随笔為你收集整理的《STM32从零开始学习历程》——I2C固件库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。