JQ8400语音模块-stm32f103c8t6(内含全代码)-亲测有效

一.模块介绍
1.常见的JQ8400-FL语音模块有两种,一种是带3W功放,有4M的存储空间,可通过USB拷贝声音文件,通过单片机串口控制;另外一种是附有SD卡槽,用SD卡存储文件的文件(MP3格式)进行语音播报,也是通过单片机串口控制
2.我用的是二线串口通信,一线的没试过
3.程序完整代码以及其他资料均已打包放在了我的个人资源里面,需要的可以自行下载(需要5积分)

二.主要引脚说明

JQ8400语音模块-stm32f103c8t6(内含全代码)-亲测有效
1 ONE LINE 一线串口脚
2 BUSY 忙信号脚,播放时为高,其它为低
3 RX 芯片串口接收脚,接MCU的TX脚(我用的是串口3)
4 TX 芯片的串口发送脚,接MCU的RX脚(我用的是串口3)
5 GND 芯片数字地
6 DC-5V 芯片供电脚,3.3-5.0V
9 SPK- 接喇叭
10 SPK+

; 三.注意事项

  1. 必须接喇叭才可以播放音乐,没有喇叭在上位机控制或者将程序下载进单片机,模块不会有任何反应!
    2.第一次使用该模块,建议建议使用配套的” 串口调试工具“,该软件可以对模块的所用功能进行调试,并可以获取相应功能指令
    3.上位机串口的 TX 和 RX 需要交叉后与模块相连,即上位机串口的 TX 对模块的 RX,上位机串口的 RX 对模块的 TX。 在本项目中,我使用的是串口3,其中串口1的配置也在z_uart.c有所体现,直接使用即可
    4.模块通过 USB 线连接电脑后,是 U 盘模式, 此时不接受任何控制指令。请改用其他电源供电即可; 如果购买的是SD卡的模块,需要用户自己往SD卡提前下载歌曲,之后通过上位机播放即可
    5.模块的程序基本没什么大的变化,如果发现程序下载进去无法使用, 大概率是配置的问题,请仔细检查自己的相关初始化配置(如串口的波特率是否是9600,是否已经正确打开,io的配置是否正确等等)

四.全代码

(注:因为这是一个完整的项目,因此包含头文件较多,用户可根据自己需求,删除部分头文件和其中代码)
——
—— ———— ———— (以下是全部相关程序)———— ———— ———— ——

main.c

#include "stm32f10x.h"
#include "jq8400.h"
#include "z_timer.h"
#include "z_beep.h"
#include "z_loop.h"
#include "z_setup.h"
#include "z_led.h"
#include "z_uart.h"
#include "stdio.h"
#include "main.h"
#include "led.h"

u8 i=0;
u8 uart_receive_buf[UART_RECEIVE_BUF_SIZE]={0}, uart_receive_buf_index, uart_get_ok;

int main(void)
{
    LED_Init();
    setup_uart3();
    delay_init();

    while(1)
    {
        LED=0;
        playMusic(1);
        delay_ms(100);
        playMusic(5);
        delay_ms(1000);
        LED=1;
        delay_ms(1000);
        break;
    }

    while (1)
    {

    }
}

main.h

#ifndef __MAIN_H__
#define __MAIN_H__

#include "z_setup.h"
#include "z_loop.h"

#define UART_RECEIVE_BUF_SIZE  100

extern u8 i;
extern u8 uart_receive_buf[UART_RECEIVE_BUF_SIZE], uart_receive_buf_index, uart_get_ok;

#endif

jq8400.c

#include "jq8400.h"
#include "z_uart.h"

void playMusic(int i)
{
    switch(i)
    {

        case 1:
        uart3_send_byte(0xAA);
        uart3_send_byte(0x02);
        uart3_send_byte(0x00);
        uart3_send_byte(0xAC);
            break;

        case 2:
        uart3_send_byte(0xAA);
        uart3_send_byte(0x03);
        uart3_send_byte(0x00);
        uart3_send_byte(0xAD);
            break;

        case 3:
        uart3_send_byte(0xAA);
        uart3_send_byte(0x06);
        uart3_send_byte(0x00);
        uart3_send_byte(0xB0);
            break;

        case 4:
        uart3_send_byte(0xAA);
        uart3_send_byte(0x05);
        uart3_send_byte(0x00);
        uart3_send_byte(0xAF);
            break;

        case 5:
        uart3_send_byte(0xAA);
        uart3_send_byte(0x13);
        uart3_send_byte(0x01);
        uart3_send_byte(0x1E);
        uart3_send_byte(0xDC);
            break;
        default:
        uart3_send_str((u8 *)"the order does not exist!\r\n");
            break;
    }
}

jq8400.h

#ifndef __JQ8400_H
#define __JQ8400_H

#include "stm32f10x.h"
#define USART_REC_LEN           200
#define EN_USART1_RX            1

extern u8  USART_RX_BUF[USART_REC_LEN];
extern u16 USART_RX_STA;

void uart_init(u32 bound);
void playMusic(int i);
#endif

z_uart.c

#include "z_uart.h"

void uart1_init(u32 baud) {
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    USART_ClockInitTypeDef USART_ClockInitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
    USART_DeInit(USART1);

    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);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
    USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
    USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
    USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
    USART_ClockInit(USART1, &USART_ClockInitStructure);

    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 );

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

    USART_Cmd(USART1, ENABLE);
    uart1_open();
}

void uart3_init(u32 baud) {
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    USART_ClockInitTypeDef USART_ClockInitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
    USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
    USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
    USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
    USART_ClockInit(USART3, &USART_ClockInitStructure);

    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(USART3, &USART_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);

    USART_Cmd(USART3, ENABLE);
    uart3_open();
}

void uart1_send_byte(u8 dat) {
    USART_SendData(USART1, dat);
    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
    return;
}

void uart3_send_byte(u8 dat) {
    USART_SendData(USART3, dat);
    while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
    return;
}

void uart1_send_str(u8 *s) {
    uart1_close();
    while (*s) {
        uart1_send_byte(*s++);
    }
    uart1_open();
}

void uart3_send_str(u8 *s) {
    uart3_close();
    while (*s) {
        uart3_send_byte(*s++);
    }
    uart3_open();
}

void USART1_IRQHandler(void) {
    static u8 sbuf_bak;

    if(USART_GetFlagStatus(USART1,USART_IT_RXNE)==SET) {
        USART_ClearITPendingBit(USART1, USART_IT_RXNE);
        sbuf_bak = USART_ReceiveData(USART1);

        uart1_send_byte(sbuf_bak);

        if(uart_get_ok) return;

        if(sbuf_bak == '$') {
            uart_receive_buf_index = 0;
        }

        else if(sbuf_bak == '!'){
            uart_receive_buf[uart_receive_buf_index] = sbuf_bak;
            uart1_send_str((u8 *)"\r\n");
            uart_get_ok = 1;
            return;
        }
        uart_receive_buf[uart_receive_buf_index++] = sbuf_bak;

        if(uart_receive_buf_index >= UART_RECEIVE_BUF_SIZE) {
            uart_receive_buf_index = 0;
        }
    }
    return;
}

void USART3_IRQHandler(void) {
    static u8 sbuf_bak;
    if(USART_GetFlagStatus(USART3,USART_IT_RXNE)==SET) {
        USART_ClearITPendingBit(USART3, USART_IT_RXNE);
        sbuf_bak = USART_ReceiveData(USART3);

        uart1_send_byte(sbuf_bak);

        if(uart_get_ok) return;

        if(sbuf_bak == '$') {
            uart_receive_buf_index = 0;
        }

        else if(sbuf_bak == '!'){
            uart_receive_buf[uart_receive_buf_index] = sbuf_bak;
            uart1_send_str((u8 *)"\r\n");
            uart_get_ok = 1;
            return;
        }
        uart_receive_buf[uart_receive_buf_index++] = sbuf_bak;

        if(uart_receive_buf_index >= UART_RECEIVE_BUF_SIZE) {
            uart_receive_buf_index = 0;
        }
    }
    return;
}

z_uart.h

#ifndef __UART_H__
#define __UART_H__

#include
#include "stm32f10x.h"
#include "main.h"

#define uart1_open()  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE)
#define uart3_open()  USART_ITConfig(USART3, USART_IT_RXNE, ENABLE)
#define uart1_close() USART_ITConfig(USART1, USART_IT_RXNE, DISABLE)
#define uart3_close() USART_ITConfig(USART3, USART_IT_RXNE, DISABLE)

void uart1_init(u32 baud);
void uart1_send_byte(u8 dat);
void uart1_send_str(u8 *s);

void uart3_init(u32 baud);
void uart3_send_byte(u8 dat);
void uart3_send_str(u8 *s);

#endif

五.其他说明

答辩人能力有限,可能在文章中有一些写作错误,或者读者不太理解,请原谅。如果你什么都不懂,欢迎你留言或私下谈论。最后,程序的源代码已经上传到我的个人主页。需要完整程序的读者可以自己下载!

[En]

The respondent’s ability is limited, there may be some writing mistakes in the article or the reader does not quite understand, please forgive me. If you don’t understand anything, you are welcome to leave a message or talk about it in private. Finally, the source code of the program has been uploaded to my personal home page. Readers who need a complete program can download it by themselves!

Original: https://blog.csdn.net/weixin_45460330/article/details/123553563
Author: 时间管理者
Title: JQ8400语音模块-stm32f103c8t6(内含全代码)-亲测有效

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/45217/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

发表回复

登录后才能评论
免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部