写一段STM32 IIC的C语言
时间: 2023-12-19 13:09:06 浏览: 141
STM32 模拟I2C IIC从机demo代码 C语言
以下是一个简单的STM32 IIC的C语言例程,可以通过IIC总线连接到其他IIC设备进行数据传输。
```c
#include "stm32f10x.h"
#define SLAVE_ADDRESS 0x50
void IIC_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
// Enable GPIOB and I2C1 clocks
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
// Configure GPIOB pins as I2C alternate function
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// Configure I2C1
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd(I2C1, ENABLE);
}
void IIC_write(uint8_t addr, uint8_t* buffer, uint16_t length)
{
uint16_t i = 0;
// Wait for I2C ready
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
// Generate start condition
I2C_GenerateSTART(I2C1, ENABLE);
// Wait for start condition generated
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
// Send slave address with write bit
I2C_Send7bitAddress(I2C1, addr, I2C_Direction_Transmitter);
// Wait for address sent
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
// Send data
for(i=0; i<length; i++)
{
I2C_SendData(I2C1, buffer[i]);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
// Generate stop condition
I2C_GenerateSTOP(I2C1, ENABLE);
}
void IIC_read(uint8_t addr, uint8_t* buffer, uint16_t length)
{
uint16_t i = 0;
// Wait for I2C ready
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
// Generate start condition
I2C_GenerateSTART(I2C1, ENABLE);
// Wait for start condition generated
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
// Send slave address with read bit
I2C_Send7bitAddress(I2C1, addr, I2C_Direction_Receiver);
// Wait for address sent
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
// Receive data
for(i=0; i<length-1; i++)
{
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
buffer[i] = I2C_ReceiveData(I2C1);
}
// Disable ACK on last byte
I2C_AcknowledgeConfig(I2C1, DISABLE);
// Receive last byte and generate stop condition
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
buffer[i] = I2C_ReceiveData(I2C1);
I2C_GenerateSTOP(I2C1, ENABLE);
// Enable ACK for next transaction
I2C_AcknowledgeConfig(I2C1, ENABLE);
}
int main(void)
{
uint8_t tx_buffer[10] = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x11, 0x22};
uint8_t rx_buffer[10];
IIC_init();
// Write data to slave device
IIC_write(SLAVE_ADDRESS, tx_buffer, 10);
// Read data from slave device
IIC_read(SLAVE_ADDRESS, rx_buffer, 10);
while(1);
}
```
阅读全文