赞
踩
可以直接新建一个Debug文件夹,接着新建voice.hex文件,点击测评就过关了。
判定程序只看有没有这个文件,只要有,就直接过关。
本关任务:编写程序,驱动小创语音模块,完成语音识别实验,生成HEX文件用于测试。
为了完成本关任务,你需要掌握:1.小创语音模块的使用与配置,2.USART串口通讯协议。
本次实验使用的语音模块为小创智能语音模块:
百科荣创小创语音模块使用说明.docx 小创语音模块词典说明.docx 小创语音模块使用说明.docx
小创语音模块需要使用以下软件进行内部配置的烧录: 小创语音识别系统串口下载工具v1.0.exe 烧录示例文件: asrWordlist.txt config-开发版.ini serialTTS.txt
使用方法: 点击打开“小创语音识别系统串口下载工具v1.0.exe” 通过USB转串口模块连接上位机与语音模块并打开串口
若无法打开,可以尝试安装以下驱动: USB转串口CH340驱动.rar 点击游览如图添加所需文件,依次点击传送烧录文件
我们可以打开asrWordlist.txt,根据示例按个人喜好添加修改识别词条。
同样我们也可以按照文件中的示例更改config-开发版.ini与serialTTS.txt
烧录完成后重写为模块上电即可完成烧录。
UART:通信异步收发器,串行、异步通信总线,两条数据线(收发),全双工(可以同时接收和发送)。
空闲位:空闲状态时为高电平(1),代表无数据传输 起始位:低电平,通信开始(从空闲位(1)–>起始位(0)的变化代表通信开始)。 数据位:5-8位数据(先发低位,后发高位)。例如:发送数据为0x55(01010101),则发送的数据为(10101010) 校验位:校验数据的正确性(奇偶校验),数据位计算1的个数若为奇数为奇校验,若为偶数为偶校验。(可有可无) 停止位:1/1.5/2位(高电平),数据结束的标志。 空闲位: 高电平,代表无数据传输
通讯双方应配置相同的波特率、校验位、数据位和停止位。
根据提示,在实验环境中编写代码,达成实验效果生成HEX文件用于测试。
1.点击Next,创建STM32工程,点击Finish,工程名为voice,根据以往课程进行通用配置。 本次实验我们使用PA10(RX)、PA9(TX)端口与小创语音模块进行通讯,选用PB5~9端口进行LCD屏幕的SPI通讯、额外配置PE3(KEY1)、PE4(KEY0)、PF9(LED0)、PF10(LED1)进行调试:
LCD相关代码存放于路径:"/data/workspace/myshixun/voice/Core/lcd" 创建工程后可以点击左上角的控制台图标打开控制台,输入命令:“cp -r /data/workspace/myshixun/voice/Core/lcd /home/stm32/voice/Core”将代码复制到工程,
然后如下图添加路径即可使用LCD相关函数:
在File system中选择/home/stm32/voice/Core/lcd后点击确定,再点击Apply and Close
如图进行配置
GPIO配置:
配置USART1:
2.点击上方
,生成代码。 点击左侧栏core->src中的usart.c文件,复制并添加代码:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file usart.c
* @brief This file provides code for the configuration
* of the USART instances.
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "usart.h"
/* USER CODE BEGIN 0 */
#include "stdio.h"
#include "string.h"
//LCD相关头文件
#include "LCD.h"
#include "gui.h"
#define KEY0() HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_4)
#define KEY1() HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_3)
uint8_t RX_BUF[256];
uint8_t TX_BUF[256];
uint16_t RX_STA=0;
uint8_t RX_singal;
/* USER CODE END 0 */
UART_HandleTypeDef huart1;
/* USART1 init function */
void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
memset(RX_BUF,0,sizeof(RX_BUF));
memset(TX_BUF,0,sizeof(TX_BUF));
HAL_UART_Receive_IT(&huart1,(uint8_t *)&RX_singal,1);
/* USER CODE END USART1_Init 2 */
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(uartHandle->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspInit 0 */
/* USER CODE END USART1_MspInit 0 */
/* USART1 clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USART1 interrupt Init */
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
/* USER CODE BEGIN USART1_MspInit 1 */
/* USER CODE END USART1_MspInit 1 */
}
}
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{
if(uartHandle->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspDeInit 0 */
/* USER CODE END USART1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_USART1_CLK_DISABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
/* USART1 interrupt Deinit */
HAL_NVIC_DisableIRQ(USART1_IRQn);
/* USER CODE BEGIN USART1_MspDeInit 1 */
/* USER CODE END USART1_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
uint8_t GET_RX(void)
{
HAL_Delay(10);//为了能够完整接受小创的信息
uint8_t tmp=0;
//获取小创返回的信息
//补充代码1:
//---user code---
//---user code---
if(RX_STA>0)
{
memset(RX_BUF,0,sizeof(RX_BUF));
RX_STA=0;
}
return tmp;
}
int __io_putchar(int ch)
{
HAL_UART_Transmit(&huart1, (uint8_t*)&ch, 1, 500);
return ch;
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)//串口中断回调函数
{
if(huart ==&huart1)
{
//串口接收信息
//补充代码2:
//---user code---
//---user code---
HAL_UART_Receive_IT(&huart1,(uint8_t *)&RX_singal,1);
}
}
void KEY(void)//测试用按键函数
{
if(KEY0()==0)
{
HAL_Delay(20);
while(KEY0()==0);
HAL_Delay(20);
}
if(KEY1()==0)
{
HAL_Delay(20);
while(KEY1()==0);
HAL_Delay(20);
}
}
/* USER CODE END 1 */
3.点击左侧栏core->inc中的usart.h文件,声明我们的函数:
/* USER CODE BEGIN Prototypes */
void KEY(void);
uint8_t GET_RX(void);
/* USER CODE END Prototypes */
4.点击打开左侧栏core->src中的main.c文件,复制并添加以下代码:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
//LCD相关函数
#include "LCD.h"
#include "gui.h"
#include "stdio.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
LCD_Init();//LCD初始化
Show_Str(10, 10, POINT_COLOR, BACK_COLOR, "Light:", 20, 0);
Show_Str(80, 10, RED, BACK_COLOR, "close", 20, 0);
uint8_t cmd=0;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
cmd=GET_RX();
if(cmd==0x06)
{
Show_Str(80, 10, GREEN, BACK_COLOR, "open ", 20, 0);
HAL_GPIO_WritePin(GPIOF, GPIO_PIN_9, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOF, GPIO_PIN_10, GPIO_PIN_RESET);
}
if(cmd==0x07)
{
Show_Str(80, 10, RED, BACK_COLOR, "close", 20, 0);
HAL_GPIO_WritePin(GPIOF, GPIO_PIN_9, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOF, GPIO_PIN_10, GPIO_PIN_SET);
}
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 168;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
5.根据如上注释提示完成代码补充。
6.编译生成hex文件,如果将程序下载到单片机就可以看到以下现象
电路连接: LED脚接正极产生背光,其余脚接蓝色部分即可
示例效果:
测试生成的hex文件
开始你的任务吧,祝你成功!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。