当前位置:   article > 正文

STM32传感器外设集--语音识别模块(LD3320)_ld3320官方

ld3320官方

目录

前言

本节主要贴上以前写的语音识别外设LD3320,希望对大家有用,本人已经测试过有用

如何使用,将下面这段函数添加到功能函数中

LD3320.h

LD3320.c

参考


前言

LD3320是一款由icroute公司生产的语音识别芯片。它具有高性能的语音识别能力,可以广泛应用于各种智能控制、语音交互等场景。以下是关于LD3320语音识别模块的详细介绍:

1. 基本原理:LD3320语音识别模块通过识别预先内置的指令来实现对语音的控制。它的工作模式主要有三种:普通模式、按键模式和口令模式。在口令模式下,设备需要一级唤醒词(口令)来启动ASR进程,从而避免误动作。

2. 应用场景:LD3320语音识别模块可以广泛应用于家居智能控制、智能小车DIY设计、毕业设计创新功能、个人DIY设计以及语音控制嵌入式产品设计等领域。

3. 性能特点:LD3320具有较高的识别准确率和稳定性,能够在各种环境下实现精确的语音识别。此外,它还具有较低的功耗和较小的体积,便于集成到各种设备中。

4. 技术参数:LD3320语音识别模块具备50条预设指令的识别能力。在接收到语音指令后,模块会根据指令执行相应的操作。其工作电压范围为3.3V-5V,适合多种电源供应方案。

5. 接口方式:LD3320模块采用标准的TTL电平接口,易于与微控制器、单片机等设备连接。同时,它还具备完善的开发文档和示例代码,便于开发者进行二次开发。

6. 市场前景:随着智能家居、智能硬件等领域的快速发展,对语音识别技术的需求日益增长。LD3320作为一种具有较高性能和实用性的语音识别模块,具有广阔的市场前景。

本节主要贴上以前写的语音识别外设LD3320,希望对大家有用,本人已经测试过有用

如何使用,将下面这段函数添加到功能函数中

  1. switch(nAsrStatus)
  2. {
  3. case LD_ASR_RUNING:
  4. case LD_ASR_ERROR:
  5. break;
  6. case LD_ASR_NONE:
  7. nAsrStatus=LD_ASR_RUNING;
  8. if (RunASR()==0)//启动一次ASR识别流程:ASR初始化,ASR添加关键词语,启动ASR运算
  9. {
  10. nAsrStatus = LD_ASR_ERROR;
  11. }
  12. break;
  13. case LD_ASR_FOUNDOK:
  14. nAsrRes = LD_GetResult( );//一次ASR识别流程结束,去取ASR识别结果
  15. printf("\r\n识别码:%d", nAsrRes);
  16. switch(nAsrRes) //对结果执行相关操作,客户修改
  17. {
  18. case CODE_LSD: //命令“流水灯”
  19. {
  20. printf(" 流水灯 指令识别成功\r\n");
  21. //SYN6288_Speech(DEBUG_USARTx,"现在展示流水灯"); /*播放声音,播放声音时候LED会灭掉,就是BY接口用来检测是否繁忙的*/
  22. Delay_ms(200);
  23. break;
  24. }
  25. case CODE_SS: //命令“闪烁”
  26. {
  27. printf(" 名字 指令识别成功\r\n");
  28. // SYN6288_Speech(DEBUG_USARTx,"现在展示闪烁"); /*播放声音,播放声音时候LED会灭掉,就是BY接口用来检测是否繁忙的*/
  29. Delay_ms(200);
  30. break;
  31. }
  32. case LDE_1:
  33. {
  34. printf(" 红灯打开\r\n");
  35. //SYN6288_Speech(DEBUG_USARTx,"按键触发"); /*播放声音,播放声音时候LED会灭掉,就是BY接口用来检测是否繁忙的*/
  36. Delay_ms(200);
  37. break;
  38. }
  39. case LDE_2:
  40. {
  41. printf(" 绿灯打开\r\n");
  42. //SYN6288_Speech(DEBUG_USARTx,"全灭 指令识别成功"); /*播放声音,播放声音时候LED会灭掉,就是BY接口用来检测是否繁忙的*/
  43. Delay_ms(200);
  44. break;
  45. }
  46. case LDE_3:
  47. {
  48. printf(" 蓝灯打开\r\n");
  49. // SYN6288_Speech(DEBUG_USARTx,"状态 指令识别成功"); /*播放声音,播放声音时候LED会灭掉,就是BY接口用来检测是否繁忙的*/
  50. Delay_ms(200);
  51. break;
  52. }
  53. case LDE_1_1:
  54. {
  55. printf(" 红灯关闭\r\n");
  56. //SYN6288_Speech(DEBUG_USARTx,"按键触发"); /*播放声音,播放声音时候LED会灭掉,就是BY接口用来检测是否繁忙的*/
  57. Delay_ms(200);
  58. break;
  59. }
  60. case LDE_2_1:
  61. {
  62. printf(" 绿灯关闭\r\n");
  63. //SYN6288_Speech(DEBUG_USARTx,"全灭 指令识别成功"); /*播放声音,播放声音时候LED会灭掉,就是BY接口用来检测是否繁忙的*/
  64. Delay_ms(200);
  65. break;
  66. }
  67. case LDE_3_1:
  68. {
  69. printf(" 蓝灯关闭\r\n");
  70. // SYN6288_Speech(DEBUG_USARTx,"状态 指令识别成功"); /*播放声音,播放声音时候LED会灭掉,就是BY接口用来检测是否繁忙的*/
  71. Delay_ms(200);
  72. break;
  73. }
  74. case LDE_All_OFF: //命令“状态”
  75. {
  76. printf(" 全灯关闭打开\r\n");
  77. // SYN6288_Speech(DEBUG_USARTx,"状态 指令识别成功"); /*播放声音,播放声音时候LED会灭掉,就是BY接口用来检测是否繁忙的*/
  78. Delay_ms(200);
  79. break;
  80. }
  81. case chen:
  82. {
  83. // SYN6288_Speech(DEBUG_USARTx,"状态 指令识别成功"); /*播放声音,播放声音时候LED会灭掉,就是BY接口用来检测是否繁忙的*/
  84. Delay_ms(200);
  85. break;
  86. }
  87. case chen_1:
  88. {
  89. // SYN6288_Speech(DEBUG_USARTx,"状态 指令识别成功"); /*播放声音,播放声音时候LED会灭掉,就是BY接口用来检测是否繁忙的*/
  90. Delay_ms(200);
  91. break;
  92. }
  93. case voice_J_0:
  94. {
  95. printf(" 音量提高\r\n");
  96. //SYN6288_Speech(DEBUG_USARTx,"全灭 指令识别成功"); /*播放声音,播放声音时候LED会灭掉,就是BY接口用来检测是否繁忙的*/
  97. Delay_ms(200);
  98. break;
  99. }
  100. case voice_J_1:
  101. {
  102. printf(" 音量降低\r\n");
  103. // SYN6288_Speech(DEBUG_USARTx,"状态 指令识别成功"); /*播放声音,播放声音时候LED会灭掉,就是BY接口用来检测是否繁忙的*/
  104. Delay_ms(200);
  105. break;
  106. }
  107. default:break;
  108. }
  109. nAsrStatus = LD_ASR_NONE;
  110. break;
  111. case LD_ASR_FOUNDZERO:
  112. default:
  113. nAsrStatus = LD_ASR_NONE;
  114. break;
  115. }//switch

LD3320.h

  1. #ifndef _LD3320_H__
  2. #define _LD3320_H__
  3. #include "stm32f10x_exti.h"
  4. #include "stm32f10x_spi.h"
  5. ///以下三个状态定义用来记录程序是在运行ASR识别还是在运行MP3播放
  6. #define LD_MODE_IDLE 0x00
  7. #define LD_MODE_ASR_RUN 0x08
  8. #define LD_MODE_MP3 0x40
  9. ///以下五个状态定义用来记录程序是在运行ASR识别过程中的哪个状态
  10. #define LD_ASR_NONE 0x00 //表示没有在作ASR识别
  11. #define LD_ASR_RUNING 0x01 //表示LD3320正在作ASR识别中
  12. #define LD_ASR_FOUNDOK 0x10 //表示一次识别流程结束后,有一个识别结果
  13. #define LD_ASR_FOUNDZERO 0x11 //表示一次识别流程结束后,没有识别结果
  14. #define LD_ASR_ERROR 0x31 // 表示一次识别流程中LD3320芯片内部出现不正确的状态
  15. #define CLK_IN 24/* user need modify this value according to clock in */
  16. #define LD_PLL_11 (uint8_t)((CLK_IN/2.0)-1)
  17. #define LD_PLL_MP3_19 0x0f
  18. #define LD_PLL_MP3_1B 0x18
  19. #define LD_PLL_MP3_1D (uint8_t)(((90.0*((LD_PLL_11)+1))/(CLK_IN))-1)
  20. #define LD_PLL_ASR_19 (uint8_t)(CLK_IN*32.0/(LD_PLL_11+1) - 0.51)
  21. #define LD_PLL_ASR_1B 0x48
  22. #define LD_PLL_ASR_1D 0x1f
  23. #define MIC_VOL 0x43
  24. ///识别码(客户修改处)
  25. #define CODE_LSD 1 //唤醒
  26. #define CODE_SS 2 //名字
  27. #define LDE_1 3 //打开红
  28. #define LDE_2 4 //打开绿
  29. #define LDE_3 5 //打开蓝
  30. #define LDE_All_OFF 6 //状态
  31. #define voice_J_0 7 //提高音量
  32. #define LDE_1_1 51 //打开红
  33. #define LDE_2_1 52 //打开绿
  34. #define LDE_3_1 53 //打开蓝
  35. #define voice_J_1 57 //降低音量
  36. #define chen 90 //娱乐
  37. #define chen_1 91 //娱乐
  38. ///LD3320引脚相关定义
  39. #define LD3320RST_PIN GPIO_Pin_15
  40. #define LD3320RST_GPIO_PORT GPIOB
  41. #define LD3320RST_GPIO_CLK RCC_APB2Periph_GPIOB
  42. #define LD_RST_H() GPIO_SetBits(GPIOB, GPIO_Pin_15)
  43. #define LD_RST_L() GPIO_ResetBits(GPIOB, GPIO_Pin_15)
  44. ///LD3320引脚相关定义
  45. #define LD3320CS_PIN GPIO_Pin_4
  46. #define LD3320CS_GPIO_PORT GPIOA
  47. #define LD3320CS_GPIO_CLK RCC_APB2Periph_GPIOA
  48. #define LD_CS_H() GPIO_SetBits(GPIOA, GPIO_Pin_4)
  49. #define LD_CS_L() GPIO_ResetBits(GPIOA, GPIO_Pin_4)
  50. #define LD3320IRQ_GPIO_CLK RCC_APB2Periph_GPIOB
  51. #define LD3320IRQ_PIN GPIO_Pin_12
  52. #define LD3320IRQ_GPIO_PORT GPIOB
  53. #define LD3320IRQEXIT_PORTSOURCE GPIO_PortSourceGPIOB
  54. #define LD3320IRQPINSOURCE GPIO_PinSource12
  55. #define LD3320IRQEXITLINE EXTI_Line12
  56. #define LD3320IRQN EXTI15_10_IRQn
  57. #define LD3320WR_PIN GPIO_Pin_13
  58. #define LD3320WR_GPIO_PORT GPIOB
  59. #define LD3320WR_GPIO_CLK RCC_APB2Periph_GPIOB
  60. #define LD_SPIS_H() GPIO_SetBits(GPIOB, GPIO_Pin_13)
  61. #define LD_SPIS_L() GPIO_ResetBits(GPIOB, GPIO_Pin_13)
  62. #define LD3320SPI SPI1
  63. #define LD3320SPI_CLK RCC_APB2Periph_SPI1
  64. #define LD3320SPIMISO_PIN GPIO_Pin_6
  65. #define LD3320SPIMISO_GPIO_PORT GPIOA
  66. #define LD3320SPIMISO_GPIO_CLK RCC_APB2Periph_GPIOA
  67. #define LD3320SPIMOSI_PIN GPIO_Pin_7
  68. #define LD3320SPIMOSI_GPIO_PORT GPIOA
  69. #define LD3320SPIMOSI_GPIO_CLK RCC_APB2Periph_GPIOA
  70. #define LD3320SPISCK_PIN GPIO_Pin_5
  71. #define LD3320SPISCK_GPIO_PORT GPIOA
  72. #define LD3320SPISCK_GPIO_CLK RCC_APB2Periph_GPIOA
  73. ///相关初始化
  74. void LD3320_main(void);
  75. void LD3320_init(void);
  76. static void LD3320_GPIO_Cfg(void);
  77. static void LD3320_EXTI_Cfg(void);
  78. static void LD3320_SPI_cfg(void);
  79. ///中间层
  80. static void LD3320_delay(unsigned long uldata);
  81. uint8_t RunASR(void);
  82. void LD_reset(void);
  83. void LD_AsrStart(void);
  84. uint8_t LD_Check_ASRBusyFlag_b2(void);
  85. ///寄存器操作
  86. uint8_t spi_send_byte(uint8_t byte);
  87. void LD_WriteReg(uint8_t data1,uint8_t data2);
  88. uint8_t LD_ReadReg(uint8_t reg_add);
  89. uint8_t LD_AsrAddFixed(void);
  90. uint8_t LD_GetResult(void);
  91. uint8_t LD_AsrRun(void);
  92. void ProcessInt(void);
  93. void LD_Init_Common(void);
  94. void LD_Init_ASR(void);
  95. #endif

LD3320.c

  1. #include "LD3320.h"
  2. #include "string.h"
  3. #include "bsp_SysTick.h"
  4. #include <string.h>
  5. #include "bsp_usart1.h"
  6. #include "SYN6288.h"
  7. #include "bsp_led.h"
  8. /************************************************************************************
  9. // nAsrStatus 用来在main主程序中表示程序运行的状态,不是LD3320芯片内部的状态寄存器
  10. // LD_ASR_NONE: 表示没有在作ASR识别
  11. // LD_ASR_RUNING: 表示LD3320正在作ASR识别中
  12. // LD_ASR_FOUNDOK: 表示一次识别流程结束后,有一个识别结果
  13. // LD_ASR_FOUNDZERO: 表示一次识别流程结束后,没有识别结果
  14. // LD_ASR_ERROR: 表示一次识别流程中LD3320芯片内部出现不正确的状态
  15. 首先:
  16. LD3320
  17. SPI总线
  18. RST—PB15
  19. CS--PA4
  20. IRQ—PB12
  21. WR—PB13
  22. MISO—PA6
  23. MOSI—PA7
  24. SCK—PA5
  25. *********************************************************************************/
  26. extern uint8_t nAsrStatus;
  27. uint8_t nLD_Mode = LD_MODE_IDLE;//用来记录当前是在进行ASR识别还是在播放MP3
  28. uint8_t ucRegVal;
  29. static void LD3320_GPIO_Cfg(void)
  30. {
  31. GPIO_InitTypeDef GPIO_InitStructure;
  32. RCC_APB2PeriphClockCmd(LD3320RST_GPIO_CLK | LD3320CS_GPIO_CLK,ENABLE);
  33. //LD_CS /RSET
  34. GPIO_InitStructure.GPIO_Pin =LD3320CS_PIN;
  35. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  36. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  37. GPIO_Init(LD3320CS_GPIO_PORT,&GPIO_InitStructure);
  38. GPIO_InitStructure.GPIO_Pin =LD3320RST_PIN;
  39. GPIO_Init(LD3320RST_GPIO_PORT,&GPIO_InitStructure);
  40. }
  41. static void LD3320_EXTI_Cfg(void)
  42. {
  43. EXTI_InitTypeDef EXTI_InitStructure;
  44. NVIC_InitTypeDef NVIC_InitStructure;
  45. GPIO_InitTypeDef GPIO_InitStructure;
  46. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
  47. RCC_APB2PeriphClockCmd(LD3320IRQ_GPIO_CLK, ENABLE);
  48. GPIO_InitStructure.GPIO_Pin =LD3320IRQ_PIN;
  49. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  50. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  51. GPIO_Init(LD3320IRQ_GPIO_PORT, &GPIO_InitStructure);
  52. //外部中断线配置
  53. GPIO_EXTILineConfig(LD3320IRQEXIT_PORTSOURCE, LD3320IRQPINSOURCE);
  54. EXTI_InitStructure.EXTI_Line = LD3320IRQEXITLINE;
  55. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  56. EXTI_InitStructure.EXTI_Trigger =EXTI_Trigger_Falling;
  57. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  58. EXTI_Init(&EXTI_InitStructure);
  59. //中断嵌套配置
  60. NVIC_InitStructure.NVIC_IRQChannel = LD3320IRQN;
  61. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  62. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  63. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  64. NVIC_Init(&NVIC_InitStructure);
  65. }
  66. static void LD3320_SPI_cfg(void)
  67. {
  68. SPI_InitTypeDef SPI_InitStructure;
  69. GPIO_InitTypeDef GPIO_InitStructure;
  70. //spi端口配置
  71. RCC_APB2PeriphClockCmd(LD3320SPI_CLK,ENABLE);
  72. RCC_APB2PeriphClockCmd(LD3320WR_GPIO_CLK | LD3320SPIMISO_GPIO_CLK | LD3320SPIMOSI_GPIO_CLK | LD3320SPISCK_GPIO_CLK,ENABLE);
  73. GPIO_InitStructure.GPIO_Pin = LD3320SPIMISO_PIN;
  74. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  75. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  76. GPIO_Init(LD3320SPIMISO_GPIO_PORT,&GPIO_InitStructure);
  77. GPIO_InitStructure.GPIO_Pin = LD3320SPIMOSI_PIN;
  78. GPIO_Init(LD3320SPIMOSI_GPIO_PORT,&GPIO_InitStructure);
  79. GPIO_InitStructure.GPIO_Pin = LD3320SPISCK_PIN;
  80. GPIO_Init(LD3320SPISCK_GPIO_PORT,&GPIO_InitStructure);
  81. GPIO_InitStructure.GPIO_Pin = LD3320WR_PIN;
  82. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  83. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  84. GPIO_Init(LD3320WR_GPIO_PORT, &GPIO_InitStructure);
  85. LD_CS_H();
  86. SPI_Cmd(LD3320SPI, DISABLE);
  87. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //全双工
  88. SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //主模式
  89. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8
  90. SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //时钟极性 空闲状态时,SCK保持低电平
  91. SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //时钟相位 数据采样从第一个时钟边沿开始
  92. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //软件产生NSS
  93. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; //波特率控制 SYSCLK/128
  94. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //数据高位在前
  95. SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC多项式寄存器初始值为7
  96. SPI_Init(LD3320SPI, &SPI_InitStructure);
  97. SPI_Cmd(LD3320SPI, ENABLE);
  98. }
  99. ///中间层
  100. void EXTI15_10_IRQHandler(void)
  101. {
  102. if(EXTI_GetITStatus(LD3320IRQEXITLINE)!= RESET )
  103. {
  104. ProcessInt();
  105. EXTI_ClearFlag(LD3320IRQEXITLINE);
  106. EXTI_ClearITPendingBit(LD3320IRQEXITLINE);//清除LINE上的中断标志位
  107. }
  108. }
  109. static void LD3320_delay(unsigned long uldata)
  110. {
  111. unsigned int i = 0;
  112. unsigned int j = 0;
  113. unsigned int k = 0;
  114. for (i=0;i<5;i++)
  115. {
  116. for (j=0;j<uldata;j++)
  117. {
  118. k = 200;
  119. while(k--);
  120. }
  121. }
  122. }
  123. uint8_t RunASR(void)
  124. {
  125. uint8_t i=0;
  126. uint8_t asrflag=0;
  127. for (i=0; i<5; i++) //防止由于硬件原因导致LD3320芯片工作不正常,所以一共尝试5次启动ASR识别流程
  128. {
  129. LD_AsrStart(); //初始化ASR
  130. LD3320_delay(100);
  131. if (LD_AsrAddFixed()==0) //添加关键词语到LD3320芯片中
  132. {
  133. LD_reset(); //LD3320芯片内部出现不正常,立即重启LD3320芯片
  134. LD3320_delay(50); //并从初始化开始重新ASR识别流程
  135. continue;
  136. }
  137. LD3320_delay(10);
  138. if (LD_AsrRun() == 0)
  139. {
  140. LD_reset(); //LD3320芯片内部出现不正常,立即重启LD3320芯片
  141. LD3320_delay(50);//并从初始化开始重新ASR识别流程
  142. continue;
  143. }
  144. asrflag=1;
  145. break; //ASR流程启动成功,退出当前for循环。开始等待LD3320送出的中断信号
  146. }
  147. return asrflag;
  148. }
  149. void LD_reset(void)
  150. {
  151. uint8_t i;
  152. LD_RST_H();
  153. LD3320_delay(100);
  154. LD_RST_L();
  155. LD3320_delay(100);
  156. LD_RST_H();
  157. LD3320_delay(100);
  158. LD_CS_L();
  159. LD3320_delay(100);
  160. LD_CS_H();
  161. LD3320_delay(100);
  162. i=LD_ReadReg(0x6);
  163. LD_WriteReg(0x35, 0x33);
  164. LD_WriteReg(0x1b, 0x55);
  165. LD_WriteReg(0xb3, 0xaa);
  166. i=LD_ReadReg(0x35);
  167. i=LD_ReadReg(0x1b);
  168. i=LD_ReadReg(0xb3);
  169. i=i;
  170. LD_RST_H();
  171. LD3320_delay(100);
  172. LD_RST_L();
  173. LD3320_delay(100);
  174. LD_RST_H();
  175. LD3320_delay(100);
  176. LD_CS_L();
  177. LD3320_delay(100);
  178. LD_CS_H();
  179. i=LD_ReadReg(0x6);
  180. LD3320_delay(100);
  181. i=LD_ReadReg(0x35);
  182. i=LD_ReadReg(0x1b);
  183. i=LD_ReadReg(0xb3);
  184. i=i;
  185. }
  186. void LD_AsrStart(void)
  187. {
  188. LD_Init_ASR();
  189. }
  190. uint8_t LD_Check_ASRBusyFlag_b2(void)
  191. {
  192. uint8_t j;
  193. uint8_t flag = 0;
  194. for (j=0; j<10; j++)
  195. {
  196. if (LD_ReadReg(0xb2) == 0x21)
  197. {
  198. flag = 1;
  199. break;
  200. }
  201. LD3320_delay(10);
  202. }
  203. return flag;
  204. }
  205. ///寄存器操作
  206. uint8_t spi_send_byte(uint8_t byte)
  207. {
  208. while (SPI_I2S_GetFlagStatus(LD3320SPI, SPI_I2S_FLAG_TXE) == RESET);
  209. SPI_I2S_SendData(LD3320SPI,byte);
  210. while (SPI_I2S_GetFlagStatus(LD3320SPI,SPI_I2S_FLAG_RXNE) == RESET);
  211. return SPI_I2S_ReceiveData(LD3320SPI);
  212. }
  213. void LD_WriteReg(uint8_t data1,uint8_t data2)
  214. {
  215. LD_CS_L();
  216. LD_SPIS_L();
  217. spi_send_byte(0x04);
  218. spi_send_byte(data1);
  219. spi_send_byte(data2);
  220. LD_CS_H();
  221. }
  222. uint8_t LD_ReadReg(uint8_t reg_add)
  223. {
  224. uint8_t i;
  225. LD_CS_L();
  226. LD_SPIS_L();
  227. spi_send_byte(0x05);
  228. spi_send_byte(reg_add);
  229. i=spi_send_byte(0x00);
  230. LD_CS_H();
  231. return(i);
  232. }
  233. uint8_t LD_GetResult(void)
  234. {
  235. return LD_ReadReg(0xc5);
  236. }
  237. uint8_t LD_AsrRun(void)
  238. {
  239. LD_WriteReg(0x35, MIC_VOL);
  240. LD_WriteReg(0x1C, 0x09);
  241. LD_WriteReg(0xBD, 0x20);
  242. LD_WriteReg(0x08, 0x01);
  243. LD3320_delay( 5 );
  244. LD_WriteReg(0x08, 0x00);
  245. LD3320_delay( 5);
  246. if(LD_Check_ASRBusyFlag_b2() == 0)
  247. {
  248. return 0;
  249. }
  250. LD_WriteReg(0xB2, 0xff);
  251. LD_WriteReg(0x37, 0x06);
  252. LD_WriteReg(0x37, 0x06);
  253. LD3320_delay(5);
  254. LD_WriteReg(0x1C, 0x0b);
  255. LD_WriteReg(0x29, 0x10);
  256. LD_WriteReg(0xBD, 0x00);
  257. return 1;
  258. }
  259. void ProcessInt(void)
  260. {
  261. uint8_t nAsrResCount=0;
  262. ucRegVal = LD_ReadReg(0x2B);
  263. // 语音识别产生的中断
  264. //(有声音输入,不论识别成功或失败都有中断)
  265. LD_WriteReg(0x29,0) ;
  266. LD_WriteReg(0x02,0) ;
  267. if((ucRegVal & 0x10) && LD_ReadReg(0xb2)==0x21 && LD_ReadReg(0xbf)==0x35)
  268. {
  269. nAsrResCount = LD_ReadReg(0xba);
  270. if(nAsrResCount>0 && nAsrResCount<=4)
  271. {
  272. nAsrStatus=LD_ASR_FOUNDOK;
  273. }
  274. else
  275. {
  276. nAsrStatus=LD_ASR_FOUNDZERO;
  277. }
  278. }
  279. else
  280. {
  281. nAsrStatus=LD_ASR_FOUNDZERO;//执行没有识别
  282. }
  283. LD_WriteReg(0x2b,0);
  284. LD_WriteReg(0x1C,0);//0:ADC不可用
  285. LD_WriteReg(0x29,0);
  286. LD_WriteReg(0x02,0);
  287. LD_WriteReg(0x2B,0);
  288. LD_WriteReg(0xBA,0);
  289. LD_WriteReg(0xBC,0);
  290. LD_WriteReg(0x08,1);//清除FIFO_DATA
  291. LD_WriteReg(0x08,0);//清除FIFO_DATA后 再次写0
  292. }
  293. void LD_Init_Common(void)
  294. {
  295. LD_ReadReg(0x06);
  296. LD_WriteReg(0x17, 0x35);
  297. LD3320_delay(5);
  298. LD_ReadReg(0x06);
  299. LD_WriteReg(0x89, 0x03);
  300. LD3320_delay(5);
  301. LD_WriteReg(0xCF, 0x43);
  302. LD3320_delay(5);
  303. LD_WriteReg(0xCB, 0x02);
  304. /*PLL setting*/
  305. LD_WriteReg(0x11, LD_PLL_11);
  306. if (nLD_Mode == LD_MODE_MP3)
  307. {
  308. LD_WriteReg(0x1E, 0x00);
  309. LD_WriteReg(0x19, LD_PLL_MP3_19);
  310. LD_WriteReg(0x1B, LD_PLL_MP3_1B);
  311. LD_WriteReg(0x1D, LD_PLL_MP3_1D);
  312. }
  313. else
  314. {
  315. LD_WriteReg(0x1E,0x00);
  316. LD_WriteReg(0x19, LD_PLL_ASR_19);
  317. LD_WriteReg(0x1B, LD_PLL_ASR_1B);
  318. LD_WriteReg(0x1D, LD_PLL_ASR_1D);
  319. }
  320. LD3320_delay(5);
  321. LD_WriteReg(0xCD, 0x04);
  322. LD_WriteReg(0x17, 0x4c);
  323. LD3320_delay(1);
  324. LD_WriteReg(0xB9, 0x00);
  325. LD_WriteReg(0xCF, 0x4F);
  326. LD_WriteReg(0x6F, 0xFF);
  327. }
  328. void LD_Init_ASR(void)
  329. {
  330. nLD_Mode=LD_MODE_ASR_RUN;
  331. LD_Init_Common();
  332. LD_WriteReg(0xBD, 0x00);
  333. LD_WriteReg(0x17, 0x48);
  334. LD3320_delay(5);
  335. LD_WriteReg(0x3C, 0x80);
  336. LD_WriteReg(0x3E, 0x07);
  337. LD_WriteReg(0x38, 0xff);
  338. LD_WriteReg(0x3A, 0x07);
  339. LD_WriteReg(0x40, 0);
  340. LD_WriteReg(0x42, 8);
  341. LD_WriteReg(0x44, 0);
  342. LD_WriteReg(0x46, 8);
  343. LD3320_delay( 1 );
  344. }
  345. ///相关初始化
  346. void LD3320_init(void)
  347. {
  348. LD3320_GPIO_Cfg();
  349. LD3320_EXTI_Cfg();
  350. LD3320_SPI_cfg();
  351. LD_reset();
  352. }
  353. static uint8_t LD_AsrAddFixed(void)
  354. {
  355. uint8_t k, flag;
  356. uint8_t nAsrAddLength;
  357. #define DATE_A 20 //数组二维数值
  358. #define DATE_B 50 //数组一维数值
  359. //添加关键词,用户修改
  360. uint8_t sRecog[DATE_A][DATE_B] = {
  361. "xiao die xiao die",\
  362. "ni jiao shen me",\
  363. "ni shi shei",\
  364. "da kai hong deng",\
  365. "da kai lv deng",\
  366. "da kai lan deng",\
  367. "guan bi hong deng",\
  368. "guan bi lv deng",\
  369. "guan bi lan deng",\
  370. "chen meng die shi shei",\
  371. "chen xiang xin shi shei",\
  372. "yin liang ti gao",\
  373. "yin liang jiang da",\
  374. "guan bi suo you deng" \
  375. };
  376. uint8_t pCode[DATE_A] = {
  377. CODE_LSD, \
  378. CODE_SS, \
  379. CODE_SS, \
  380. LDE_1, \
  381. LDE_2, \
  382. LDE_3, \
  383. LDE_1_1, \
  384. LDE_2_1, \
  385. LDE_3_1, \
  386. chen, \
  387. chen_1, \
  388. voice_J_0, \
  389. voice_J_1, \
  390. LDE_All_OFF \
  391. }; //添加识别码,用户修改
  392. flag = 1;
  393. for (k=0; k<DATE_A; k++)
  394. {
  395. if(LD_Check_ASRBusyFlag_b2() == 0)
  396. {
  397. flag = 0;
  398. break;
  399. }
  400. LD_WriteReg(0xc1, pCode[k] );
  401. LD_WriteReg(0xc3, 0);
  402. LD_WriteReg(0x08, 0x04);
  403. LD3320_delay(1);
  404. LD_WriteReg(0x08, 0x00);
  405. LD3320_delay(1);
  406. for (nAsrAddLength=0; nAsrAddLength<DATE_B; nAsrAddLength++)
  407. {
  408. if (sRecog[k][nAsrAddLength] == 0)
  409. break;
  410. LD_WriteReg(0x5, sRecog[k][nAsrAddLength]);
  411. }
  412. LD_WriteReg(0xb9, nAsrAddLength);
  413. LD_WriteReg(0xb2, 0xff);
  414. LD_WriteReg(0x37, 0x04);
  415. }
  416. return flag;
  417. }
  418. 用户修改
  419. //void LD3320_main(void)
  420. //{
  421. // uint8_t nAsrRes=0;
  422. //
  423. // printf("1、流水灯\r\n");
  424. // printf("2、闪烁\r\n");
  425. // printf("3、按键触发\r\n");
  426. // printf("4、全灭\r\n");
  427. // printf("5、状态\r\n");
  428. // nAsrStatus = LD_ASR_NONE;//初始状态:没有在作ASR
  429. // while(1)
  430. // {
  431. // switch(nAsrStatus)
  432. // {
  433. // case LD_ASR_RUNING:
  434. //
  435. // case LD_ASR_ERROR:
  436. // break;
  437. // case LD_ASR_NONE:
  438. // nAsrStatus=LD_ASR_RUNING;
  439. // if (RunASR()==0)//启动一次ASR识别流程:ASR初始化,ASR添加关键词语,启动ASR运算
  440. // {
  441. // nAsrStatus = LD_ASR_ERROR;
  442. // }
  443. // break;
  444. // case LD_ASR_FOUNDOK:
  445. // nAsrRes = LD_GetResult( );//一次ASR识别流程结束,去取ASR识别结果
  446. // printf("\r\n识别码:%d", nAsrRes);
  447. //
  448. // switch(nAsrRes) //对结果执行相关操作,客户修改
  449. // {
  450. // case CODE_LSD: //命令“流水灯”
  451. // {
  452. // printf(" 流水灯 指令识别成功\r\n");
  453. // SYN6288_Speech(DEBUG_USARTx,"现在展示流水灯"); /*播放声音,播放声音时候LED会灭掉,就是BY接口用来检测是否繁忙的*/
  454. // Delay_ms(500);
  455. // break;
  456. // }
  457. // case CODE_SS: //命令“闪烁”
  458. // {
  459. // printf(" 闪烁 指令识别成功\r\n");
  460. // SYN6288_Speech(DEBUG_USARTx,"现在展示闪烁"); /*播放声音,播放声音时候LED会灭掉,就是BY接口用来检测是否繁忙的*/
  461. // Delay_ms(500);
  462. // break;
  463. // }
  464. // case CODE_AJCF: //命令“按键触发”
  465. // {
  466. // printf(" 按键触发 指令识别成功\r\n");
  467. // SYN6288_Speech(DEBUG_USARTx,"按键触发"); /*播放声音,播放声音时候LED会灭掉,就是BY接口用来检测是否繁忙的*/
  468. // Delay_ms(500);
  469. // break;
  470. // }
  471. //
  472. // case CODE_QM: //命令“全灭”
  473. // {
  474. // printf(" 全灭 指令识别成功\r\n");
  475. // SYN6288_Speech(DEBUG_USARTx,"全灭 指令识别成功"); /*播放声音,播放声音时候LED会灭掉,就是BY接口用来检测是否繁忙的*/
  476. // Delay_ms(500);
  477. // break;
  478. // }
  479. // case CODE_ZT: //命令“状态”
  480. // {
  481. // printf(" 状态 指令识别成功\r\n");
  482. // SYN6288_Speech(DEBUG_USARTx,"状态 指令识别成功"); /*播放声音,播放声音时候LED会灭掉,就是BY接口用来检测是否繁忙的*/
  483. // Delay_ms(500);
  484. // }
  485. // default:break;
  486. // }
  487. // nAsrStatus = LD_ASR_NONE;
  488. // break;
  489. // case LD_ASR_FOUNDZERO:
  490. // default:
  491. // nAsrStatus = LD_ASR_NONE;
  492. // break;
  493. // }//switch
  494. // //开发板测试
  495. // Board_text(nAsrRes );
  496. // }// while
  497. //}

参考

STM32F103C8T6+LD3320语音识别模块智能灯控icon-default.png?t=N7T8https://blog.csdn.net/weixin_53129688/article/details/127982201?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170212843516800222862352%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=170212843516800222862352&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~top_click~default-2-127982201-null-null.nonecase&utm_term=ld3320&spm=1018.2226.3001.4450

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/374771
推荐阅读
相关标签
  

闽ICP备14008679号