【STM32激光测距】STP-23、cubemx、HAL库、串口中断、测距模块、激光测距、单点激光、

本文介绍了如何使用STM32CubeMX配置STM32F401CCU6芯片的USART中断,以便从STP-23激光传感器接收数据。作者提供了代码修改步骤,包括普通USART的打印验证和中断服务函数的修改,以及数据处理函数的实现,用于计算并打印平均距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


前言

在这里插入图片描述
在一个项目中使用到了一个激光模块,使用的是轮趣科技的STP-23激光传感器,这个模块采用的usart串口,不过只需要收就可以 了,不需要给它发送数据。
例程中使用的芯片是stm32f401ccu6
本文使用的cubemx生成的HAL库
本文并未对各个函数进行讲解,只是如何应用,小白勿喷!!

一、CUBEMX的配置

首先最基础的配置这里就不进行演示了,直接是usart的配置。
要将波特率改为921600,
在这里插入图片描述

然后就是开启中断

在这里插入图片描述
还需要再开一个usart把数据通过串口模块打印在电脑上。

在这里插入图片描述

二、代码修改

配置完成以后使用cubemx生成代码随后我们进行代码的修改

1.普通usart的打印验证(这段要是确保无误的话可以跳过)

在usart.c文件中需要添加

/* USER CODE BEGIN 0 */
#include <stdio.h>
/* USER CODE END 0 */

随后在此文件的文末添加

/* USER CODE BEGIN 1 */
int fputc(int ch,FILE *f)
{
	/*发送一个字节到串口*/
	
	HAL_UART_Transmit(&huart6,(uint8_t*)&ch,1,1000);

	return (ch);

}
int fgetc(FILE *f) 
{
	uint8_t ch;
	HAL_UART_Receive(&huart6,(uint8_t *)&ch,sizeof(ch),0xFFFF);
	return ch;
}
/* USER CODE END 1 */

还需要勾选微库

在这里插入图片描述
随后在主函数 main.c中去验证
先包含微库

/* USER CODE BEGIN Includes */
#include <stdio.h>
/* USER CODE END Includes */

随后在主循环中进行打印测试

		printf("hello world!\n");
		HAL_Delay(100);

2.中断服务函数的修改

首先是在main.c 中添加两个全局变量。

/* USER CODE BEGIN PM */
uint16_t  receive_cnt;
uint16_t distance;
/* USER CODE END PM */

随后在初始化的时候打开中断(这里很重要,不打开没有值~!!!!)

  /* USER CODE BEGIN 2 */
	__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);
  /* USER CODE END 2 */

最后我们去修改It.c和.h文件
先改.h文件
最开始要定义几个变量和常量以及宏定义:

/* USER CODE BEGIN EC */
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef signed char int8_t;
typedef signed short int16_t;
typedef signed int int32_t;
/* USER CODE END EC */

/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */
#define ANGLE_PER_FRAME 12
#define HEADER 0x54
#define POINT_PER_PACK 12
#define VERLEN  0x2C 	//低五位是一帧数据接收到的点数,目前固定是12,高三位固定为1

typedef struct __attribute__((packed)) Point_Data
{
	uint16_t distance;//距离
	uint8_t intensity;//置信度
	
}LidarPointStructDef;

typedef struct __attribute__((packed)) Pack_Data
{
	uint8_t header;
	uint8_t ver_len;
	uint16_t temperature;
	uint16_t start_angle;
	LidarPointStructDef point[POINT_PER_PACK];
	uint16_t end_angle;
	uint16_t timestamp;
	uint8_t crc8;
}LiDARFrameTypeDef;
/* USER CODE END EM */

然后是申明函数:

/* USER CODE BEGIN EFP */
void data_process(void);
/* USER CODE END EFP */

现在去改.c文件
先定义变量和常量以及申明外部变量

/* USER CODE BEGIN PD */
LiDARFrameTypeDef Pack_Data;//雷达接收的数据储存在这个变量之中
extern uint16_t receive_cnt;
extern uint16_t distance;
static const uint8_t CrcTable[256] =
{
0x00, 0x4d, 0x9a, 0xd7, 0x79, 0x34, 0xe3,
0xae, 0xf2, 0xbf, 0x68, 0x25, 0x8b, 0xc6, 0x11, 0x5c, 0xa9, 0xe4, 0x33,
0x7e, 0xd0, 0x9d, 0x4a, 0x07, 0x5b, 0x16, 0xc1, 0x8c, 0x22, 0x6f, 0xb8,
0xf5, 0x1f, 0x52, 0x85, 0xc8, 0x66, 0x2b, 0xfc, 0xb1, 0xed, 0xa0, 0x77,
0x3a, 0x94, 0xd9, 0x0e, 0x43, 0xb6, 0xfb, 0x2c, 0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x9b, 0x4c, 0x01, 0xf4, 0xb9, 0x6e, 0x23, 0x8d, 0xc0, 0x17,
0x5a, 0x06, 0x4b, 0x9c, 0xd1, 0x7f, 0x32, 0xe5, 0xa8
};//用于crc校验的数组
/* USER CODE END PD */

然后我们需要对USART1_IRQHandler()函数进行修改,这里使用回调函数!!!而是直接修改的中断服务函数。

void USART1_IRQHandler(void)
{
  /* USER CODE BEGIN USART1_IRQn 0 */
	static uint8_t state = 0;//状态位	
	static uint8_t crc = 0;//校验和
	static uint8_t cnt = 0;//用于一帧12个点的计数
	uint8_t temp_data;
  /* USER CODE END USART1_IRQn 0 */
  HAL_UART_IRQHandler(&huart1);
  /* USER CODE BEGIN USART1_IRQn 1 */
  if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE) != RESET) //接收到数据
  {
    temp_data = (uint8_t)(huart1.Instance->DR & 0xFF);

    //if(state == 1)receive_cnt = temp_data;
    __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
		if (state > 5)
		{
			if(state < 42)
			{
				if(state%3 == 0)//一帧数据中的序号为6,9.....39的数据,距离值低8位
				{
					Pack_Data.point[cnt].distance = (uint16_t)temp_data;
					state++;
					crc = CrcTable[(crc^temp_data) & 0xff];
				}
				else if(state%3 == 1)//一帧数据中的序号为7,10.....40的数据,距离值高8位
				{
					Pack_Data.point[cnt].distance = ((uint16_t)temp_data<<8)+Pack_Data.point[cnt].distance;
					state++;
					crc = CrcTable[(crc^temp_data) & 0xff];
				}
				else//一帧数据中的序号为8,11.....41的数据,置信度
				{
					Pack_Data.point[cnt].intensity = temp_data;
					cnt++;	
					state++;
					crc = CrcTable[(crc^temp_data) & 0xff];
				}
			}
			else 
			{
				switch(state)
				{
					case 42:
						Pack_Data.end_angle = (uint16_t)temp_data;//结束角度低8位
						state++;
						crc = CrcTable[(crc^temp_data) & 0xff];
						break;
					case 43:
						Pack_Data.end_angle = ((uint16_t)temp_data<<8)+Pack_Data.end_angle;//结束角度高8位
						state++;
						crc = CrcTable[(crc^temp_data) & 0xff];
						break;
					case 44:
						Pack_Data.timestamp = (uint16_t)temp_data;//时间戳低8位
						state++;
						crc = CrcTable[(crc^temp_data) & 0xff];
						break;
					case 45:
						Pack_Data.timestamp = ((uint16_t)temp_data<<8)+Pack_Data.timestamp;//时间戳高8位
						state++;
						crc = CrcTable[(crc^temp_data) & 0xff];
						break;
					case 46:
						Pack_Data.crc8 = temp_data;//雷达传来的校验和
						if(Pack_Data.crc8 == crc)//校验正确
						{
							data_process();//接收到一帧且校验正确可以进行数据处理
							receive_cnt++;//输出接收到正确数据的次数
						}
						else
						{
						//校验不正确
						}
							//memset(&Pack_Data,0,sizeof(Pack_Data)*);//清零
						crc = 0;
						state = 0;
						cnt = 0;//复位
					default: break;
				}
			}
		}
		else 
		{
			switch(state)
			{
				case 0:
					if(temp_data == HEADER)//头固定
					{
						Pack_Data.header = temp_data;
						state++;
						crc = CrcTable[(crc^temp_data) & 0xff];//开始进行校验
					} else state = 0,crc = 0;
					break;
				case 1:
					//receive_cnt = temp_data;
					if(temp_data == VERLEN)//测量的点数,目前固定
					{
						//receive_cnt++;
						Pack_Data.ver_len = temp_data;
						state++;
						crc = CrcTable[(crc^temp_data) & 0xff];
					} else state = 0,crc = 0;
					break;
				case 2:
					Pack_Data.temperature = (uint16_t)temp_data;//温度低8位,一共16位ADC,0--4096,无量纲
					state++;
					crc = CrcTable[(crc^temp_data) & 0xff];
					break;
				case 3:
					Pack_Data.temperature = ((uint16_t)temp_data<<8)+Pack_Data.temperature;//温度高8位
					state++;
					crc = CrcTable[(crc^temp_data) & 0xff];
					break;
				case 4:
					Pack_Data.start_angle = (uint16_t)temp_data;//开始角度低8位,放大了100倍
					state++;
					crc = CrcTable[(crc^temp_data) & 0xff];
					break;
				case 5:
					Pack_Data.start_angle = ((uint16_t)temp_data<<8)+Pack_Data.start_angle;
					state++;
					crc = CrcTable[(crc^temp_data) & 0xff];
					break;
				default: break;
			}
		}
	}				
  /* USER CODE END USART1_IRQn 1 */
}

最后我们只需要定义数据处理函数就可以:

/* USER CODE BEGIN 1 */
void data_process(void)//数据处理函数,完成一帧之后可进行数据处理
{
	//计算距离
	static uint8_t cnt = 0;
	uint8_t i;
	static uint16_t count = 0;
	static uint32_t sum = 0;
	for(i=0;i<12;i++)//12个点取平均
	{
		if(Pack_Data.point[i].distance != 0)//去除0的点
		{
			count++;
			sum += Pack_Data.point[i].distance;
		}
	}
	if(++cnt == 100)//100个数据帧计算一次距离
	{
		distance = sum/count;
		sum = 0;
		count = 0;
		cnt = 0;
	}
}
/* USER CODE END 1 */

回到主循环进行距离的打印即可:

  while (1)
  {
		printf("1--%d次\n",receive_cnt);
		printf("1--%dmm\n",distance);
		HAL_Delay(100);
  }

总结

本文只简述了配置和代码修改并未对具体函数进行讲解。其官方提供的是标准库,需要标准库的直接找官方资料就可。

### 小爱音箱接入DEEPSEEK AI大模型的方法 为了实现小爱音箱与DEEPSEEK AI大模型的集成,可以采用一种类似于已有的方法来构建接口服务。此过程涉及创建一个中间服务器作为桥梁,在这个方案里,该服务器负责接收来自小爱音箱的请求并转发给DEEPSEEK API获取响应数据再返回给设备。 #### 构建Node.js环境下的API网关 考虑到简易性和兼容性,推荐选用Node.js平台搭建这样的网关应用。通过安装必要的依赖如`express`框架用于处理HTTP请求以及`axios`来进行外部API调用,能够快速建立起稳定的服务端逻辑[^1]。 ```javascript // 安装所需模块 npm install express axios body-parser ``` 接着定义基本的应用结构: ```javascript const express = require('express'); const bodyParser = require('body-parser'); const axios = require('axios'); const app = express(); app.use(bodyParser.json()); // 设置监听端口 const PORT = process.env.PORT || 3000; app.listen(PORT, () => console.log(`Server running on port ${PORT}`)); ``` #### 配置路由以对接DEEPSEEK API 针对特定命令或查询语句设计相应的路由规则,使得当从小爱音箱传来指令时能正确解析并向DEEPSEEK发送恰当格式的数据包求解答案。 ```javascript app.post('/deepseek', async (req, res) => { try { const { query } = req.body; // 假设传入参数名为query let response = await axios({ method: 'post', url: 'https://api.deepseek.com/v1/query', // 替换成实际的DeepSeek API地址 headers: {'Authorization': `Bearer YOUR_API_KEY_HERE`, 'Content-Type': 'application/json'}, data: JSON.stringify({text: query}) }); res.status(200).send(response.data); } catch(error){ console.error("Error occurred while processing request:", error.message); res.status(500).send({"error": "An unexpected error has occurred."}); } }); ``` 请注意替换上述代码中的`YOUR_API_KEY_HERE`为真实的DEEPSEEK API密钥,并确认官方文档给出的确切URL路径和服务条款说明。 #### 连接至小米IoT开发平台 最后一步是要让小爱音箱知道如何访问新设立好的API网关。这通常涉及到注册成为开发者账户、创建自定义技能项目并将之前部署完毕的服务链接配置进去等一系列操作步骤。 完成以上设置之后,理论上讲只要一切正常工作的话,就可以享受到由DEEPSEEK提供支持的强大自然语言理解能力所带来的智能化体验提升效果了。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值