慢吞云雾缓吐愁 发表于 2025-1-4 05:27:36

基于STM32F103的语音控制模块的应用(实现语音控制小灯开关)

一,硬件接线
https://i-blog.csdnimg.cn/direct/ecf0ee2411ed4e64a5fff9152a09918a.png
把语音模块的5v 和  GND  跟主控芯片的5v 和 GND 连接起来,就能一起供电了

二,语音模块图形化代码实现
我这边直接用了1.0版本的了,后面的陀机和风扇后面硬件接上就好了,现在主要看控小灯
https://i-blog.csdnimg.cn/direct/d26c4372831c45d79632e93c7f03591c.png
https://i-blog.csdnimg.cn/direct/d36c2393cada4d0f9f6e43da9786f41c.png
https://i-blog.csdnimg.cn/direct/f5a11c36b236446ebb8e1738168d54cf.png
https://i-blog.csdnimg.cn/direct/c7ac396ae7d34c1ead04a2b7422ede6c.png

三,代码实现
/*
******************************************************************************
* @file    main.c
* @author
* @version V1.0
* @date    2024/12/30
* @brief   本代码通过检测PA15的电平高低,来做出相应的动作,(目前)实现通过
                       来控制小灯的开关
                       往后        PB3                --->        负责检测SG90
                                        PB4                --->        负责检测风扇
******************************************************************************
*/

#include "stm32f10x.h"
#include "delay_us.h"
#include "delay_ms.h"
#include "string.h"

/* Private typedef   用于记录用户自定义的一些数据类型的别名-------------------*/


/* Private define    用于记录用户自定义的类型,比如结构体、共用体、枚举-------*/


/* Private macro   用于记录用户自定义的宏定义-------------------------------*/


/* Private variables 用于记录用户自定义的全局变量-----------------------------*/
uint8_tu1_rxbuf = {0}; //作为UART1的接收缓冲区
uint32_t u1_rxcnt = 0;      //作为UART1的接收计数器


/* Private function prototypes 用于记录用户自定义的函数声明-------------------*/



/* Private functions 用于记录用户自定义的函数原型-----------------------------*/


/**
* @brief该函数初始化IO引脚,用于LED的点亮
* @paramNone
* @retval None
*/
void LED_Config(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;

        //打开时钟
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

        //配置引脚参数
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 ;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_Init(GPIOC, &GPIO_InitStructure);
       
        //输出高电平(点亮小灯)
        GPIO_SetBits(GPIOC, GPIO_Pin_14);
}

/**
* @brief该函数初始化IO引脚,用于检测PA15的电平变化
* @paramNone
* @retval None
*/
void GPIOA_Pin15_Config(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;

        //打开时钟
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

        //配置引脚参数,配置为浮空输入模式
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 ;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOC, &GPIO_InitStructure);
}



/**
* @brief该函数初始化USART1,用于BLE通信
* @paramNone
* @retval None
*/
void USART1_Config(u32 baud)
{
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDefNVIC_InitStructure;
        GPIO_InitTypeDefGPIO_InitStructure;

        //打开GPIOA的时钟
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

        //打开USART2的时钟
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

        //重映射IO引脚为串口功能
        GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);


        //配置GPIO的引脚
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        //配置UART1的参数    最常用的格式: 1bit停止位8bit数据位No校验位9600bps
        USART_InitStructure.USART_BaudRate = baud;                                                                                        //波特率
        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(USART1, &USART_InitStructure);

        //配置USART的中断
        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);

        //选择UART1的中断源   接收到数据则触发中断请求
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

        //打开UART1串口
        USART_Cmd(USART1, ENABLE);
       
}



/**
* @brief程序的入口
* @paramNone
* @retval None
*/
int main(void)
{
        LED_Config();
        USART1_Config(9600);
        GPIOA_Pin15_Config();

        while (1)
        {
/*--------------------------------------------当语言模块信号来时------------------------------------------------------------------*/
                //检测LED,当检测为高电平时开灯
                if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_15) == 1)
                {
                        GPIO_SetBits(GPIOC,GPIO_Pin_14);         //输出高电平,开灯
                       
                }
                else
                {
                        GPIO_ResetBits(GPIOC,GPIO_Pin_14);         //输出低电平,关灯
                  
                }
               
               
               
/*--------------------------------------------当蓝牙信号来时------------------------------------------------------------------*/
                //判断UART2是否接收到数据假设接收到 "led_on",则让LED点亮
                if( u1_rxcnt > 0 && strstr((char *)u1_rxbuf,"led_on"))
                {
                       
                        GPIO_SetBits(GPIOC,GPIO_Pin_14); //输出高电平       
                        u1_rxcnt = 0; //计数器复位
                        memset((char *)u1_rxbuf,0,512); //清空数组
                }
               
                //判断UART2是否接收到数据假设接收到 "led_off",则让LED熄灭
                if( u1_rxcnt > 0 && strstr((char *)u1_rxbuf,"led_off"))
                {
                       
                        GPIO_ResetBits(GPIOC,GPIO_Pin_14); //输出低电平       
                        u1_rxcnt = 0; //计数器复位
                        memset((char *)u1_rxbuf,0,512); //清空数组
                }


        }
}


/**
* @briefThis function handles USRAT1 interrupt request.
* @paramNone
* @retval None
*/
void USART1_IRQHandler(void)
{

//判断是否接收到数据
if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
{   
      //把串口收到的字节存储到变量data中
      u1_rxbuf = USART_ReceiveData(USART1);
                        if( u1_rxcnt >= 512 )
                        {
                                u1_rxcnt = 0;
                        }
                  
}
}





/********************** (C) COPYRIGHT Your Name xxxx@126.com***END OF FILE****/

核心代码:
/**
* @brief该函数初始化IO引脚,用于检测PA15的电平变化
* @paramNone
* @retval None
*/
void GPIOA_Pin15_Config(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;

        //打开时钟
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

        //配置引脚参数,配置为浮空输入模式
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 ;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOC, &GPIO_InitStructure);
}

/*--------------------------------------------当语言模块信号来时------------------------------------------------------------------*/
                //检测LED,当检测为高电平时开灯
                if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_15) == 1)
                {
                        GPIO_SetBits(GPIOC,GPIO_Pin_14);         //输出高电平,开灯
                       
                }
                else
                {
                        GPIO_ResetBits(GPIOC,GPIO_Pin_14);         //输出低电平,关灯
                  
                }

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 基于STM32F103的语音控制模块的应用(实现语音控制小灯开关)