使用stm32标准库函数写出iic超声波测距代码
时间: 2023-07-30 12:04:15 浏览: 111
以下是基于STM32标准库函数的IIC超声波测距代码实现:
```c
#include "stm32f10x.h"
#define I2C_SPEED 100000 // I2C速度,单位:Hz
#define I2C_TIMEOUT 1000 // I2C超时时间,单位:ms
#define ADDR_WR 0xE0 // 超声波I2C写地址
#define ADDR_RD 0xE1 // 超声波I2C读地址
#define CMD_RANGE 0x51 // 测距命令
I2C_InitTypeDef I2C_InitStruct;
// I2C总线初始化
void I2C_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // 使能GPIOB时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); // 使能I2C1时钟
// PB6-I2C1_SCL、PB7-I2C1_SDA
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
I2C_StructInit(&I2C_InitStruct);
I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStruct.I2C_OwnAddress1 = 0x00;
I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStruct.I2C_ClockSpeed = I2C_SPEED;
I2C_Init(I2C1, &I2C_InitStruct);
I2C_Cmd(I2C1, ENABLE);
}
// 向超声波发送命令
void Ultrasonic_SendCmd(uint8_t cmd)
{
uint32_t timeout = I2C_TIMEOUT;
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY))
{
if ((timeout--) == 0)
return;
}
I2C_GenerateSTART(I2C1, ENABLE);
timeout = I2C_TIMEOUT;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
{
if ((timeout--) == 0)
return;
}
I2C_Send7bitAddress(I2C1, ADDR_WR, I2C_Direction_Transmitter);
timeout = I2C_TIMEOUT;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
if ((timeout--) == 0)
return;
}
I2C_SendData(I2C1, cmd);
timeout = I2C_TIMEOUT;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
{
if ((timeout--) == 0)
return;
}
I2C_GenerateSTOP(I2C1, ENABLE);
}
// 读取超声波测距数据
uint16_t Ultrasonic_ReadData(void)
{
uint16_t data = 0;
uint8_t databuf[2];
uint32_t timeout = I2C_TIMEOUT;
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY))
{
if ((timeout--) == 0)
return data;
}
I2C_GenerateSTART(I2C1, ENABLE);
timeout = I2C_TIMEOUT;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
{
if ((timeout--) == 0)
return data;
}
I2C_Send7bitAddress(I2C1, ADDR_RD, I2C_Direction_Receiver);
timeout = I2C_TIMEOUT;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
{
if ((timeout--) == 0)
return data;
}
I2C_AcknowledgeConfig(I2C1, DISABLE);
I2C_GenerateSTOP(I2C1, ENABLE);
timeout = I2C_TIMEOUT;
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) == RESET)
{
if ((timeout--) == 0)
return data;
}
databuf[0] = I2C_ReceiveData(I2C1);
timeout = I2C_TIMEOUT;
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) == RESET)
{
if ((timeout--) == 0)
return data;
}
databuf[1] = I2C_ReceiveData(I2C1);
data = (databuf[0] << 8) + databuf[1];
return data;
}
int main(void)
{
uint16_t distance = 0;
I2C_Configuration(); // I2C总线初始化
while (1)
{
Ultrasonic_SendCmd(CMD_RANGE); // 发送测距命令
distance = Ultrasonic_ReadData(); // 读取测距数据
// 处理测距数据
// ...
}
}
```
其中,`I2C_Configuration()`函数用于初始化I2C总线,`Ultrasonic_SendCmd()`函数用于向超声波发送命令,`Ultrasonic_ReadData()`函数用于读取超声波测距数据。通过不断发送测距命令和读取测距数据,可实现超声波连续测距功能。
阅读全文