STM32驱动SYN6288语音合成模块实现语音播报

SYN6288语音合成模块使用说明

请跳转该专栏:SYN6288语音合成模块使用说明(MicroPython、STM32、Arduino)-CSDN博客

最终效果

未完待续

接线

|-------|---------|
| STM32 | SYN6288 |
| 3.3 | VDD |
| G | G |
| PA2 | RXD |

代码

工程文件:

https://download.csdn.net/download/qq_44955826/90131372?spm=1001.2014.3001.5503

main.c

cpp 复制代码
#include "stm32f10x.h"  
#include "serial.h"
#include "USART2.h"
#include "voice.h"
#include "Delay.h"


int main(void)
{	
	Serial_Init();

	USART2_Init();
	

	voice_output_fingerprint_input("请放手指");
	
	Delay_s(3);
	
	voice_output_fingerprint_input("指纹录入成功");
	
	Delay_s(3);
	
	voice_output_fingerprint_input("指纹录入失败");
	
	Delay_s(3);
	
	voice_output_fingerprint_input("手指接触不良");
	
	Delay_s(3);
	
	voice_output_fingerprint_identify("指纹识别成功");
	
	Delay_s(3);
	
	voice_output_fingerprint_identify("无效指纹");
	
	Delay_s(3);
	
	voice_output_QR("请提供二维码");
	
	Delay_s(3);
	
	voice_output_QR("二维码录入成功");
	
	Delay_s(3);
	
	voice_output_QR("二维码识别成功");
	
	Delay_s(3);
	
	voice_output_QR("无效二维码");
	
	Delay_s(3);
	
	voice_output_RFID("请放卡片");
	
	Delay_s(3);

	voice_output_RFID("射频卡录入成功");
	
	Delay_s(3);
	
	voice_output_RFID("射频卡识别成功");
	
	Delay_s(3);
	
	voice_output_RFID("无效卡片");

	
	
	while(1)
	{		
	
	}	
}

voice.c

cpp 复制代码
#include<stdio.h>
#include "serial.h"
#include "USART2.h"
#include <string.h>		//字符串处理

/*
指纹录入中,请放手指
指纹录入成功
指纹录入失败
手指接触不良
指纹识别成功 
无效指纹 
二维码扫描中,请提供二维码
二维码录入成功
二维码识别成功 
无效二维码 
射频卡录入成功 
射频卡识别成功 
无效卡片 
*/

//写4个函数是因为1个函数查找太慢
 
void voice_output_fingerprint_input(char s[])
{	
	if(strcmp(s,"指纹录入成功") == 0 )
	{
		uint8_t Array[18]={253, 0, 15, 1, 0, 214, 184, 206, 198, 194, 188, 200, 235, 179, 201, 185, 166, 173};
		USART2_SendArray(Array, 18);
	}
	if(strcmp(s,"指纹录入失败") == 0 )
	{
		uint8_t Array[18]={253, 0, 15, 1, 0, 214, 184, 206, 198, 194, 188, 200, 235, 202, 167, 176, 220, 201};
		USART2_SendArray(Array, 18);
	}
	if(strcmp(s,"手指接触不良") == 0 )
	{
		uint8_t Array[18]={253, 0, 15, 1, 0, 202, 214, 214, 184, 189, 211, 180, 165, 178, 187, 193, 188, 138};
		USART2_SendArray(Array, 18);
	}
	if(strcmp(s,"请放手指") == 0 )
	{
		uint8_t Array[14]={253, 0, 11, 1, 0, 199, 235, 183, 197, 202, 214, 214, 184, 219};
		USART2_SendArray(Array, 14);
	}
}
 
void voice_output_fingerprint_identify(char s[])
{	
	if(strcmp(s,"指纹识别成功") == 0 )
	{
		uint8_t Array[18]={253, 0, 15, 1, 0, 214, 184, 206, 198, 202, 182, 177, 240, 179, 201, 185, 166, 205};
		USART2_SendArray(Array, 18);
	}
	if(strcmp(s,"无效指纹") == 0 )
	{
		uint8_t Array[14]={253, 0, 11, 1, 0, 206, 222, 208, 167, 214, 184, 206, 198, 246};
		USART2_SendArray(Array, 14);
	}
}

void voice_output_QR(char s[])
{	
	if(strcmp(s,"请提供二维码") == 0 )
	{
		uint8_t Array[18]={253, 0, 15, 1, 0, 199, 235, 204, 225, 185, 169, 182, 254, 206, 172, 194, 235, 225};
		USART2_SendArray(Array, 18);
	}
	if(strcmp(s,"二维码录入成功") == 0 )
	{
		uint8_t Array[20]={253, 0, 17, 1, 0, 182, 254, 206, 172, 194, 235, 194, 188, 200, 235, 179, 201, 185, 166, 214};
		USART2_SendArray(Array, 20);
	}
	if(strcmp(s,"二维码识别成功") == 0 )
	{
		uint8_t Array[20]={253, 0, 17, 1, 0, 182, 254, 206, 172, 194, 235, 202, 182, 177, 240, 179, 201, 185, 166, 182};
		USART2_SendArray(Array, 20);
	}
	if(strcmp(s,"无效二维码") == 0 )
	{
		uint8_t Array[16]={253, 0, 13, 1, 0, 206, 222, 208, 167, 182, 254, 206, 172, 194, 235, 149};
		USART2_SendArray(Array, 16);
	}
}

void voice_output_RFID(char s[])
{	
	if(strcmp(s,"请放卡片") == 0 )
	{
		uint8_t Array[14]={253, 0, 11, 1, 0, 199, 235, 183, 197, 191, 168, 198, 172, 212};
		USART2_SendArray(Array, 14);
	}
	if(strcmp(s,"射频卡录入成功") == 0 )
	{
		uint8_t Array[20]={253, 0, 17, 1, 0, 201, 228, 198, 181, 191, 168, 194, 188, 200, 235, 179, 201, 185, 166, 156};
		USART2_SendArray(Array, 20);
	}
	if(strcmp(s,"射频卡识别成功") == 0 )
	{ 
		uint8_t Array[20]={253, 0, 17, 1, 0, 201, 228, 198, 181, 191, 168, 202, 182, 177, 240, 179, 201, 185, 166, 252};
		USART2_SendArray(Array, 20);
	}
	if(strcmp(s,"无效卡片") == 0 )
	{
		uint8_t Array[14]={253, 0, 11, 1, 0, 206, 222, 208, 167, 191, 168, 198, 172, 237};
		USART2_SendArray(Array, 14);
	}
}

voice.h

cpp 复制代码
#ifndef __VOICE_H
#define __VOICE_H

void voice_output_fingerprint_input(char s[]);
void voice_output_fingerprint_identify(char s[]);
void voice_output_QR(char s[]);
void voice_output_RFID(char s[]);

#endif

serial.c

cpp 复制代码
//串口1打印和输入

#include "stm32f10x.h"                  
#include <stdio.h>
#include <stdarg.h>

void Serial_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = 9600;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART1, &USART_InitStructure);
		
	USART_Cmd(USART1, ENABLE);
}


void Serial_SendByte(uint8_t Byte)
{
	USART_SendData(USART1, Byte);
	while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);	//当其置1(SET) 标志着串口1接收完成
}

//重定向c库函数printf到串口
int fputc(int ch, FILE *f)
{
	Serial_SendByte(ch);
	return ch;
}

uint8_t Serial_ReceiveByte(void)
{
	uint8_t Byte;
	Byte = USART_ReceiveData(USART1);
	while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);	//当其置1(SET) 标志着串口1发送完成
	return Byte;
}

//重定向c库函数scanf到串口,重写后可使用scanf、getchar等函数
int fgetc(FILE *f)
{
    uint8_t ch;
    ch = Serial_ReceiveByte();
    return ch;
}

serial.h

cpp 复制代码
//串口1打印和输入

#ifndef __SERIAL_H
#define __SERIAL_H

#include "stm32f10x.h" 
#include <stdio.h>

void Serial_Init(void);
void Serial_SendByte(uint8_t Byte);
uint8_t Serial_ReceiveByte(void);

#endif

USART2.c

cpp 复制代码
#include "stm32f10x.h"                  
#include <stdio.h>
#include <stdarg.h>

void USART2_Init(void)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;		
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;		
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
		
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = 9600;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART2, &USART_InitStructure);
	
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Cmd(USART2, ENABLE);
}

void USART2_SendByte(uint8_t Byte)
{
	USART_SendData(USART2, Byte);
	while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
}

void USART2_SendArray(uint8_t *Array, uint16_t Length)
{
	uint16_t i;
	for (i = 0; i < Length; i ++)
	{
		USART2_SendByte(Array[i]);
	}
}



char USART2_RxData[100];		//全局变量,用于储存USART2获得的文本数据,最多100个字符
uint8_t USART2_RxFlag;		//全局变量,串口接收标志位

/*
下面的中断服务函数只会向USART2_RxData[]数组中储存第1对{}之间的内容
如  fdgh{"lamp": 77,"curtain": 3},"airConditioner": {2}frgtr  发送至USART2后,USART2_RxData[]数组中的内容为  {"lamp": 77,"curtain": 3}
如果只有 { 而没有 } ,USART2_RxData[]数组的'\0'结束符的位置就未被重置,后续处理就会出错
*/

void USART2_IRQHandler(void)
{
	static uint8_t RxState = 0;
	static uint8_t count = 0;	//USART2_RxData[]数组的下标
	
	if (USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
	{
		uint8_t RxData = USART_ReceiveData(USART2);
		
		if (RxState == 0)
		{
			if (RxData == '{' && USART2_RxFlag == 0)
			{
				RxState = 1;
				count = 0;
			}
		}
		if (RxState == 1)
		{
			if (RxData == '}')
			{
				USART2_RxData[count] = '}';
				USART2_RxData[count + 1] = '\0';	//标志数组的结束,方便后续处理
				RxState = 0;
				USART2_RxFlag = 1;
				count = 0;
			}			
			else
			{
				USART2_RxData[count] = RxData;
				count ++;	
			}
		}
		
		USART_ClearITPendingBit(USART2, USART_IT_RXNE);
	}
}

USART2.h

cpp 复制代码
#ifndef __USART2_H
#define __USART2_H

#include <stdio.h>

extern char USART2_RxData[100];		
extern uint8_t USART2_RxFlag;		

void USART2_Init(void);
void USART2_SendByte(uint8_t Byte);
void USART2_SendArray(uint8_t *Array, uint16_t Length);

#endif

Delay.c

cpp 复制代码
#include "stm32f10x.h"

/**
  * @brief  微秒级延时
  * @param  xus 延时时长,范围:0~233015
  * @retval 无
  */
void Delay_us(uint32_t xus)
{
	SysTick->LOAD = 72 * xus;				//设置定时器重装值
	SysTick->VAL = 0x00;					//清空当前计数值
	SysTick->CTRL = 0x00000005;				//设置时钟源为HCLK,启动定时器
	while(!(SysTick->CTRL & 0x00010000));	//等待计数到0
	SysTick->CTRL = 0x00000004;				//关闭定时器
}

/**
  * @brief  毫秒级延时
  * @param  xms 延时时长,范围:0~4294967295
  * @retval 无
  */
void Delay_ms(uint32_t xms)
{
	while(xms--)
	{
		Delay_us(1000);
	}
}
 
/**
  * @brief  秒级延时
  * @param  xs 延时时长,范围:0~4294967295
  * @retval 无
  */
void Delay_s(uint32_t xs)
{
	while(xs--)
	{
		Delay_ms(1000);
	}
} 

Delay.h

cpp 复制代码
#ifndef __DELAY_H
#define __DELAY_H

void Delay_us(uint32_t us);
void Delay_ms(uint32_t ms);
void Delay_s(uint32_t s);

#endif
相关推荐
番茄老夫子2 小时前
STM32 USB通信知识与应用详解
stm32·单片机·嵌入式硬件
人才程序员3 小时前
LVGL9.2 鼠标悬停处理
c语言·c++·stm32·单片机·mcu·物联网·51单片机
TNT_TT4 小时前
二、STM32MP257安全启动流程简介
stm32·嵌入式硬件·安全·yocto
代码总长两年半13 小时前
ADC模数转换---STM32
stm32·单片机·嵌入式硬件
嵌入式大圣17 小时前
单片机锂电池电量电压检测
stm32·单片机·嵌入式硬件·mcu·物联网·51单片机·iot
Echo_cy_17 小时前
STM32 串口收发HEX数据包
stm32·单片机·嵌入式硬件
冷曦_sole19 小时前
linux-16 关于shell(十五)date,clock,hwclock,man,时间管理,命令帮助
linux·stm32·单片机
嵌入式@hxydj20 小时前
STM32F103单片机HAL库串口通信卡死问题解决方法
stm32·单片机·嵌入式硬件·串口·uart·hal·卡死
嵌入式阿萌20 小时前
STM32F407+LAN8720A +LWIP +FreeRTOS 实现TCP服务器
服务器·stm32·tcp/ip