STM32F103C8T6连接LD3320语音识别模块(USART)

Posted 一只小阿大:)

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STM32F103C8T6连接LD3320语音识别模块(USART)相关的知识,希望对你有一定的参考价值。

目录

吹水

写了快半年go语言吧,毕业后还是打算从事嵌入式,重新捡起来,调这个我犯了个很睿智的问题,调了两天才发现!就是配置串口相关的参数!一定要配置对!开始我通过串口和ld3320连接,有数据传输,32的串口也能接收到电脑发的数据,但是两个连起来就是拿不到数据,最后洗澡的时候才想起来ld3320给的波特率不是115200而是9600

ld3320那边的程序如何修改就不贴了,网上蛮多的。

硬件平台


工程代码

ld3320连接USART1,通过透传的方式将获取到的数据通过USART2打印出来

main.c

#include "stm32f10x.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "led.h"
#include "usart.h"
#include "delay.h"

int main()

		Led_init();
		usart1_init();
	    usart2_init();
        initSysTick();
	    printf("main start!");
	
	    GPIO_ResetBits(GPIOC, GPIO_Pin_13);
		delay_ms(300);
        GPIO_SetBits(GPIOC, GPIO_Pin_13);


//串口中断
void USART1_IRQHandler(void)

	int temp;
	while(USART_GetITStatus(USART1,USART_IT_RXNE) != RESET)
        temp = USART_ReceiveData(USART1);
        printf("%c",temp);
	    USART_ClearITPendingBit(USART1,USART_IT_RXNE);
	
    



delay.c

#include "stm32f10x.h"
#include "delay.h"

void initSysTick(void)

		SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //ÉèÖÃʱÖÓÔ´8·ÖÅä
		SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;            //ʹÄÜÖжÏ
		SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;             //¿ª¶¨Ê±Æ÷
		SysTick->LOAD = 9;                                    //ËæÒâÉèÖÃÒ»¸öÖØ×°ÔØÖµ

/**
 * ¹¦ÄÜ£ºÎ¢Ã뼶ÑÓʱº¯Êý
 * ²ÎÊý£ºxus £º¾ßÌåÑÓʱʱ¼ä (µ¥Î»£ºÎ¢Ãî)
 * ·µ»ØÖµ£ºNone
 */
void delay_us(u32 xus)

		SysTick->LOAD = 9 * xus;                              //¼Æ9´ÎΪ1us£¬xusÔòÖØ×°ÔØÖµÒª*9
		SysTick->VAL = 0;                                     //¼ÆÊýÆ÷¹éÁã
		while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk));//µÈ´ý¼ÆÊýÍê³É


/**
 * ¹¦ÄÜ£ººÁÃ뼶ÑÓʱº¯Êý
 * ²ÎÊý£ºxms £º¾ßÌåÑÓʱʱ¼ä (µ¥Î»£ººÁÃë)
 * ·µ»ØÖµ£ºNone
 */
void delay_ms(u32 xms)

		SysTick->LOAD = 9000;                                  //¼Æ9´ÎΪ1ms£¬1000´ÎΪ1s
		SysTick->VAL = 0;                                      //¼ÆÊýÆ÷¹éÁã
		while (xms--)
		
				while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk));//µÈ´ýµ¥´Î¼ÆÊýÍê³É
		


delay.h

#ifndef _DELAY_H_
#define _DELAY_H_

#include "stm32f10x.h"
void initSysTick(void);
void delay_us(u32 xus);
void delay_ms(u32 xms);

#endif

usart.c

#include "usart.h"
#include "stm32f10x.h"

void usart1_init(void)

	GPIO_InitTypeDef  gpio_init;
	USART_InitTypeDef usartStruct;
	NVIC_InitTypeDef  nvic_initStruct;
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	//1.ÅäÖÃʱÖÓ£ºGPIO¿ÚµÄʱÖÓ£¬Òý½Å¸´ÓõÄʱÖÓ£¬´®¿ÚµÄʱÖÓ
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
	
	//2.ÅäÖÃGPIOµÄ½á¹¹Ìå
	//USART1 TX --- PA9,RX --- PA10
	//2.1 TX
	gpio_init.GPIO_Mode = GPIO_Mode_AF_PP;
	gpio_init.GPIO_Pin = GPIO_Pin_9;
	gpio_init.GPIO_Speed = GPIO_Speed_10MHz;
	
	GPIO_Init(GPIOA,&gpio_init);
	
	//2.2 RX
	gpio_init.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	gpio_init.GPIO_Pin = GPIO_Pin_10;
	
	GPIO_Init(GPIOA,&gpio_init);
	
	//3.ÅäÖô®¿ÚµÄ½á¹¹Ìå
	usartStruct.USART_BaudRate = 9600;
	usartStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	usartStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	usartStruct.USART_Parity = USART_Parity_No;
	usartStruct.USART_StopBits = USART_StopBits_1;
	usartStruct.USART_WordLength = USART_WordLength_8b;
	
	USART_Init(USART1,&usartStruct);
	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//ÅäÖô®¿ÚÖжÏ
	USART_Cmd(USART1, ENABLE );
	
	nvic_initStruct.NVIC_IRQChannel = USART1_IRQn;
	nvic_initStruct.NVIC_IRQChannelPreemptionPriority = 1;
	nvic_initStruct.NVIC_IRQChannelSubPriority = 1;
	nvic_initStruct.NVIC_IRQChannelCmd = ENABLE;
	
	NVIC_Init(&nvic_initStruct);



void usart2_init(void)

	GPIO_InitTypeDef  gpio_init;
	USART_InitTypeDef usartStruct;
//	NVIC_InitTypeDef  nvic_initStruct;
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	//1.ÅäÖÃʱÖÓ£ºGPIO¿ÚµÄʱÖÓ£¬Òý½Å¸´ÓõÄʱÖÓ£¬´®¿ÚµÄʱÖÓ
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
	
	//2.ÅäÖÃGPIOµÄ½á¹¹Ìå	
	//USART2 TX --- PA2,RX --- PA3
	gpio_init.GPIO_Mode = GPIO_Mode_AF_PP;
	gpio_init.GPIO_Pin = GPIO_Pin_2;
	gpio_init.GPIO_Speed = GPIO_Speed_10MHz;
	GPIO_Init(GPIOA,&gpio_init);
	
	gpio_init.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	gpio_init.GPIO_Pin = GPIO_Pin_3;
	GPIO_Init(GPIOA,&gpio_init);
	
	//3.ÅäÖô®¿ÚµÄ½á¹¹Ìå
	usartStruct.USART_BaudRate = 115200;
	usartStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	usartStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	usartStruct.USART_Parity = USART_Parity_No;
	usartStruct.USART_StopBits = USART_StopBits_1;
	usartStruct.USART_WordLength = USART_WordLength_8b;
	
	USART_Init(USART2,&usartStruct);
//	USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//ÅäÖô®¿ÚÖжÏ
	USART_Cmd(USART2, ENABLE );
	
//	nvic_initStruct.NVIC_IRQChannel = USART2_IRQn;
//	nvic_initStruct.NVIC_IRQChannelPreemptionPriority = 1;
//	nvic_initStruct.NVIC_IRQChannelSubPriority = 2;
//	nvic_initStruct.NVIC_IRQChannelCmd = ENABLE;
//	
//	NVIC_Init(&nvic_initStruct);

	


//·â×°ÁËһϷ¢ËÍ×Ö·û
void usartSendByte(USART_TypeDef* USARTx, uint16_t Data)

	USART_SendData(USARTx,Data);
	while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);



//·¢ËÍ×Ö·û´®
void usartSendStr(USART_TypeDef* USARTx,char *str)

	uint16_t i = 0;
	do
		usartSendByte(USARTx,*(str+i));
		i++;
	while(*(str+i) != '\\0');

	//ÅжÏÊÇ·ñ·¢ËÍÍê
	while(USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET);


int fputc(int ch,FILE *f)

	USART_SendData(USART2,(uint8_t)ch);
	while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
	
	return (ch);


int fgetc(FILE *f)

	while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
	
	return (int) USART_ReceiveData(USART2);


usart.h

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

void usart1_init(void);
void usart2_init(void);
void usartSendByte(USART_TypeDef* USARTx, uint16_t Data);
void usartSendStr(USART_TypeDef* USARTx,char *str);

led.c

#include "stm32f10x.h"
#include "led.h"

void Led_init(void)

	GPIO_InitTypeDef Led_initStructer;
	
	//1.ʹÄÜAPB2µÄʱÖÓGPIO
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
	
	//2.½á¹¹ÌåÅäÖÃ
	Led_initStructer.GPIO_Mode 	= GPIO_Mode_Out_PP;
	Led_initStructer.GPIO_Pin	  = GPIO_Pin_13;
	Led_initStructer.GPIO_Speed = GPIO_Speed_10MHz;
	
	GPIO_Init(GPIOC, &Led_initStructer);


led.h

#include "stm32f10x.h"
void Led_init(void);

再次注意!ld3320波特率为9600

以上是关于STM32F103C8T6连接LD3320语音识别模块(USART)的主要内容,如果未能解决你的问题,请参考以下文章

STM32F103C8T6连接LD3320语音识别模块(USART)

STM32语音识别的智能蓝牙音箱

基于stm32F103C8T6的智能门禁(毕业设计)

点亮STM32F103C8T6上的LED

STM32f103C8T6 Bootloader设计(转)

stm32f103c8t6最小系统板可以调速吗