stm32f1与GY-53-L1测距代码
时间: 2023-08-12 22:07:18 浏览: 74
STM32超声波测距代码
5星 · 资源好评率100%
以下是使用 STM32F1 和 GY-53-L1 模块进行测距的示例代码:
```c
#include "stm32f10x.h"
#include "stdio.h"
#define I2C_SPEED 100000
#define GY53L1_ADDR 0x42
void I2C_Configuration(void);
void I2C_WriteByte(uint8_t addr, uint8_t reg, uint8_t val);
uint8_t I2C_ReadByte(uint8_t addr, uint8_t reg);
void GY53L1_Init(void);
uint16_t GY53L1_ReadDistance(void);
int main(void)
{
uint16_t distance;
char str[20];
I2C_Configuration();
GY53L1_Init();
while (1)
{
distance = GY53L1_ReadDistance();
sprintf(str, "distance: %d mm\r\n", distance);
printf(str);
delay_ms(500);
}
}
void I2C_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
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 = I2C_SPEED;
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd(I2C1, ENABLE);
}
void I2C_WriteByte(uint8_t addr, uint8_t reg, uint8_t val)
{
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY))
;
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
;
I2C_Send7bitAddress(I2C1, addr, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
;
I2C_SendData(I2C1, reg);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
;
I2C_SendData(I2C1, val);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
;
I2C_GenerateSTOP(I2C1, ENABLE);
}
uint8_t I2C_ReadByte(uint8_t addr, uint8_t reg)
{
uint8_t val;
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY))
;
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
;
I2C_Send7bitAddress(I2C1, addr, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
;
I2C_SendData(I2C1, reg);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
;
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
;
I2C_Send7bitAddress(I2C1, addr, I2C_Direction_Receiver);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
;
I2C_AcknowledgeConfig(I2C1, DISABLE);
I2C_GenerateSTOP(I2C1, ENABLE);
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) == RESET)
;
val = I2C_ReceiveData(I2C1);
return val;
}
void GY53L1_Init(void)
{
I2C_WriteByte(GY53L1_ADDR, 0x00, 0x51);
}
uint16_t GY53L1_ReadDistance(void)
{
uint8_t msb = I2C_ReadByte(GY53L1_ADDR, 0x8f);
uint8_t lsb = I2C_ReadByte(GY53L1_ADDR, 0x10);
return ((uint16_t) msb << 8) | lsb;
}
```
以上代码中,`I2C_Configuration` 函数用于配置 I2C 总线,`I2C_WriteByte` 和 `I2C_ReadByte` 函数分别用于向 GY-53-L1 模块写入和读取数据,`GY53L1_Init` 函数用于初始化 GY-53-L1 模块,`GY53L1_ReadDistance` 函数用于读取距离数据。在 `main` 函数中,循环读取距离数据并输出。注意,以上代码未包含延时函数的实现,需要自行实现或使用外部库。
阅读全文