stm32语音播报_TTS语音模块:XFS5152CE语音合成模块详解代码
时间: 2023-08-16 17:03:45 浏览: 380
XFS5152CE是一款TTS语音模块,可以通过串口与STM32等微控制器连接,实现文字转语音的功能。以下是一个简单的STM32语音播报代码,可以作为参考:
```c
#include "stm32f10x.h"
#include <string.h>
#define USARTx USART1
#define USARTx_IRQn USART1_IRQn
#define USARTx_IRQHandler USART1_IRQHandler
#define XFS5152CE_BAUDRATE 9600
void USART_Config(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
// USART1_TX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// USART1_RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = XFS5152CE_BAUDRATE;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USARTx, &USART_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USARTx_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE);
USART_Cmd(USARTx, ENABLE);
}
void send_string(char *str)
{
while (*str) {
USART_SendData(USARTx, (uint8_t) *str++);
while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
}
}
void XFS5152CE_play(char *text)
{
char cmd[50];
strcpy(cmd, "SPEAK \"");
strcat(cmd, text);
strcat(cmd, "\"\r\n");
send_string(cmd);
}
void XFS5152CE_init(void)
{
send_string("\r\n\r\n"); // 重启模块
send_string("AT+CLR\r\n"); // 清空缓存
send_string("AT+V\r\n"); // 查询版本号
send_string("AT+L=3\r\n"); // 设置语音播报速度
send_string("AT+T=0\r\n"); // 设置语音播报音调
}
int main(void)
{
USART_Config();
XFS5152CE_init();
XFS5152CE_play("Hello, world!");
while (1);
}
void USARTx_IRQHandler(void)
{
if (USART_GetITStatus(USARTx, USART_IT_RXNE) != RESET) {
USART_ClearITPendingBit(USARTx, USART_IT_RXNE);
}
}
```
该代码通过串口与XFS5152CE语音模块通信,实现了文字转语音的功能。在`XFS5152CE_init()`函数中,可以通过AT指令设置语音播报速度和音调。在`XFS5152CE_play()`函数中,将要播报的文本转换成特定格式的字符串,并通过串口发送给模块。
阅读全文