基于stm32的i2c代码
时间: 2023-09-09 18:07:12 浏览: 160
I2C.zip_STM32 I2C_stm32 i2c 模拟_stm32 产品_stm32引脚_基于STM32的标准I2C接口
以下是基于STM32的I2C代码示例:
```c
#include "stm32f4xx.h"
#include "stm32f4xx_i2c.h"
#define I2C_TIMEOUT_MAX 0x3000 /* I2C Time out max value */
I2C_HandleTypeDef I2C_Handle;
void I2C_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable I2C and GPIO clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
/* Configure I2C pins */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Assign alternate functions to I2C pins */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);
/* Configure I2C */
I2C_Handle.Instance = I2C1;
I2C_Handle.Init.ClockSpeed = 100000;
I2C_Handle.Init.DutyCycle = I2C_DutyCycle_2;
I2C_Handle.Init.OwnAddress1 = 0x00;
I2C_Handle.Init.AddressingMode = I2C_AddressingMode_7bit;
I2C_Handle.Init.DualAddressMode = I2C_DualAddress_Disable;
I2C_Handle.Init.OwnAddress2 = 0x00;
I2C_Handle.Init.GeneralCallMode = I2C_GeneralCall_Disable;
I2C_Handle.Init.NoStretchMode = I2C_NoStretch_Disable;
I2C_Init(&I2C_Handle);
/* Enable I2C */
I2C_Cmd(I2C_Handle.Instance, ENABLE);
}
uint8_t I2C_Write(I2C_TypeDef* I2Cx, uint8_t addr, uint8_t* data, uint16_t len)
{
uint32_t timeout = I2C_TIMEOUT_MAX;
uint8_t status = 0;
/* Wait until I2C is ready */
while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY))
{
if((timeout--) == 0)
{
return 0xFF;
}
}
/* Send start condition */
I2C_GenerateSTART(I2Cx, ENABLE);
/* Wait until start condition is sent */
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))
{
if((timeout--) == 0)
{
return 0xFF;
}
}
/* Send slave address */
I2C_Send7bitAddress(I2Cx, addr, I2C_Direction_Transmitter);
/* Wait until slave acknowledges address */
timeout = I2C_TIMEOUT_MAX;
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
if((timeout--) == 0)
{
return 0xFF;
}
}
/* Send data */
while(len--)
{
I2C_SendData(I2Cx, *data);
/* Wait until data is sent */
timeout = I2C_TIMEOUT_MAX;
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
{
if((timeout--) == 0)
{
return 0xFF;
}
}
data++;
}
/* Send stop condition */
I2C_GenerateSTOP(I2Cx, ENABLE);
return status;
}
uint8_t I2C_Read(I2C_TypeDef* I2Cx, uint8_t addr, uint8_t* data, uint16_t len)
{
uint32_t timeout = I2C_TIMEOUT_MAX;
uint8_t status = 0;
/* Wait until I2C is ready */
while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY))
{
if((timeout--) == 0)
{
return 0xFF;
}
}
/* Send start condition */
I2C_GenerateSTART(I2Cx, ENABLE);
/* Wait until start condition is sent */
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))
{
if((timeout--) == 0)
{
return 0xFF;
}
}
/* Send slave address */
I2C_Send7bitAddress(I2Cx, addr, I2C_Direction_Receiver);
/* Wait until slave acknowledges address */
timeout = I2C_TIMEOUT_MAX;
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
{
if((timeout--) == 0)
{
return 0xFF;
}
}
/* Receive data */
while(len--)
{
/* Wait until data is received */
timeout = I2C_TIMEOUT_MAX;
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
if((timeout--) == 0)
{
return 0xFF;
}
}
*data = I2C_ReceiveData(I2Cx);
data++;
}
/* Send stop condition */
I2C_GenerateSTOP(I2Cx, ENABLE);
return status;
}
```
使用示例:
```c
uint8_t data[2];
I2C_Init();
data[0] = 0x01;
data[1] = 0x02;
I2C_Write(I2C1, 0x50, data, 2);
I2C_Read(I2C1, 0x50, data, 2);
```
以上是一个简单的I2C读写函数示例,可以根据需要进行修改和扩展。
阅读全文