当前位置:   article > 正文

ESP32+语音识别模块(LD3320)+语音合成模块(SYN6288)_连接esp32的语音版块

连接esp32的语音版块

语音识别模块(LD3320)是这款

语音合成模块(SYN6288)是这款

ESP32与语音识别模块LD3320的接线方式

ESP32LD3320
3.3VVCC
GNDGND
D21SDA
D22SCL

 ESP32与语音合成模块SYN6288的接线方式:

        注意:编译的时候要断开开发板和SYN6288的连接

ESP32SYN6288
3.3VVCC
GNDGND
RXTX
TXRX

示例代码

 ASR.CPP

  1. #include "ASR.h"
  2. /****************************
  3. 寄存器设置函数
  4. *****************************/
  5. bool I2CWrite(unsigned char reg_addr,unsigned char date)
  6. {
  7. Wire.beginTransmission(ASR_I2C_ADDR); //发送Device地址
  8. Wire.write(reg_addr); //发送要操作的寄存器地址
  9. Wire.write(date); //发送要设置的值
  10. if(Wire.endTransmission()!=0) //发送结束信号
  11. {
  12. delay(10);
  13. return false;
  14. }
  15. delay(10);
  16. return true;
  17. }
  18. /****************************
  19. 检测值读取函数
  20. *****************************/
  21. bool WireReadData(unsigned char reg_addr,unsigned char *value,int num)
  22. {
  23. Wire.beginTransmission(ASR_I2C_ADDR); //发送Device地址
  24. Wire.write(reg_addr); //发送要操作的寄存器地址
  25. delay(10);
  26. if(Wire.endTransmission()!=0) //发送结束信号
  27. {
  28. delay(10);
  29. return false;
  30. }
  31. delay(10);
  32. Wire.requestFrom(ASR_I2C_ADDR, num);
  33. while(Wire.available())
  34. {
  35. char ff = Wire.read(); // receive a byte as character
  36. *value = ff;
  37. value++;
  38. delay(10);
  39. }
  40. return true;
  41. }
  42. /*****************************
  43. RGB设置函数
  44. ******************************/
  45. bool RGB_Set(unsigned char R,unsigned char G,unsigned char B)
  46. {
  47. Wire.beginTransmission(ASR_I2C_ADDR); //发送Device地址
  48. Wire.write(ASR_RGB_ADDR);
  49. Wire.write(R);
  50. Wire.write(G);
  51. Wire.write(B);
  52. if(Wire.endTransmission()!=0) //发送结束信号
  53. {
  54. delay(10);
  55. return false;
  56. }
  57. delay(10);
  58. return true;
  59. }
  60. /*****************************
  61. 单字节读取函数
  62. ******************************/
  63. bool I2CWrite_byte(unsigned char date)
  64. {
  65. Wire.beginTransmission(ASR_I2C_ADDR); //发送Device地址
  66. Wire.write(date); //发送要设置的值
  67. if(Wire.endTransmission()!=0) //发送结束信号
  68. {
  69. delay(10);
  70. return false;
  71. }
  72. delay(10);
  73. return true;
  74. }
  75. /*****************************
  76. 忙闲等待取函数
  77. ******************************/
  78. void BusyWait(void)
  79. {
  80. unsigned char busy_flag = 0xff;
  81. while(busy_flag != 0)
  82. {
  83. WireReadData(ASR_BUSY,&busy_flag,1);
  84. delay(100);
  85. }
  86. }
  87. /*****************************
  88. 添加词条函数
  89. ******************************/
  90. void AsrAddWords(unsigned char idNum,const char * words)
  91. {
  92. unsigned char date_num = strlen(reinterpret_cast<const char*>(words))+2;
  93. unsigned char str_num = strlen(reinterpret_cast<const char*>(words));
  94. I2CWrite_byte(ASR_ADD_WORD_ADDR); //发送词组寄存器地址
  95. I2CWrite_byte(date_num); //发送本次发送的数据长度,计算方式:词组号+字符串长度+0作为字符串结束符
  96. I2CWrite_byte(idNum); //发送词组号
  97. for(int i = 0; i<str_num;i++)
  98. {
  99. I2CWrite_byte(words[i]); //发送词字符
  100. }
  101. I2CWrite_byte(0); //发送0作为结束符
  102. }

ASR.h

  1. #ifndef ASR_H
  2. #define ASR_H
  3. #include "Arduino.h"
  4. #include <Wire.h>
  5. #define ASR_I2C_ADDR 0x0f //语音识别模块地址
  6. #define ASR_ADD_WORD_ADDR 0x01 //词条添加地址
  7. #define ASR_MODE_ADDR 0x02 //识别模式设置地址,值为0-2,0:循环识别模式 1:口令模式 ,2:按键模式,默认为循环检测
  8. #define ASR_RGB_ADDR 0x03 //RGB灯设置地址,需要发两位,第一个直接为灯号1:蓝 2:红 3:绿 ,
  9. //第二个字节为亮度0-255,数值越大亮度越高
  10. #define ASR_REC_GAIN 0x04 //识别灵敏度设置地址,灵敏度可设置为0x00-0x7f,值越高越容易检测但是越容易误判,
  11. //建议设置值为0x40-0x55,默认值为0x40
  12. #define ASR_CLEAR_ADDR 0x05 //清除掉电缓存操作地址,录入信息前均要清除下缓存区信息
  13. #define ASR_KEY_FLAG 0x06 //用于按键模式下,设置启动识别模式
  14. #define ASR_VOICE_FLAG 0x07 //用于设置是否开启识别结果提示音
  15. #define ASR_RESULT 0x08 //识别结果存放地址
  16. #define ASR_BUZZER 0x09 //蜂鸣器控制写1开启,写0关闭
  17. #define ASR_NUM_CLECK 0x0a //录入词条数目校验
  18. #define FIRMWARE_VERSION 0x0b //读取固件版本
  19. #define ASR_BUSY 0x0c//忙闲标志
  20. bool I2CWrite(unsigned char reg_addr,unsigned char date);
  21. bool WireReadData(unsigned char reg_addr,unsigned char *value,int num);
  22. bool RGB_Set(unsigned char R,unsigned char G,unsigned char B);
  23. void AsrAddWords(unsigned char idNum,const char * words);
  24. void BusyWait(void);
  25. #endif

SYN6288.h(可以自己找厂家拿合成器,合成自己想要的)

  1. #ifndef SYN6288_H
  2. #define SYN6288_H
  3. void speech(){
  4. unsigned char i = 0;
  5. unsigned char head[15];
  6. head[0] = 0xFD;
  7. head[1] = 0x00;
  8. head[2] = 0x0C;
  9. head[3] = 0x01;
  10. head[4] = 0x00;
  11. head[5] = 0xD6;
  12. head[6] = 0xF7;
  13. head[7] = 0xC8;
  14. head[8] = 0xCB;
  15. head[9] = 0x2C;
  16. head[10] = 0xCE;
  17. head[11] = 0xD2;
  18. head[12] = 0xD4;
  19. head[13] = 0xDA;
  20. head[14] = 0xEC;
  21. for(i=0; i<15; i++){
  22. Serial.write(head[i]);
  23. }
  24. }
  25. void speech_kaideng(){
  26. unsigned char i = 0;
  27. unsigned char head[24];
  28. head[0] = 0xFD;
  29. head[1] = 0x00;
  30. head[2] = 0x15;
  31. head[3] = 0x01;
  32. head[4] = 0x00;
  33. head[5] = 0xBA;
  34. head[6] = 0xC3;
  35. head[7] = 0xB5;
  36. head[8] = 0xC4;
  37. head[9] = 0xD6;
  38. head[10] = 0xF7;
  39. head[11] = 0xC8;
  40. head[12] = 0xCB;
  41. head[13] = 0xD2;
  42. head[14] = 0xD1;
  43. head[15] = 0xCE;
  44. head[16] = 0xAA;
  45. head[17] = 0xC4;
  46. head[18] = 0xFA;
  47. head[19] = 0xBF;
  48. head[20] = 0xAA;
  49. head[21] = 0xB5;
  50. head[22] = 0xC6;
  51. head[23] = 0xFC;
  52. for(i=0; i<24; i++){
  53. Serial.write(head[i]);
  54. }
  55. }
  56. void speech_guandeng(){
  57. unsigned char i = 0;
  58. unsigned char head[24];
  59. head[0] = 0xFD;
  60. head[1] = 0x00;
  61. head[2] = 0x15;
  62. head[3] = 0x01;
  63. head[4] = 0x00;
  64. head[5] = 0xBA;
  65. head[6] = 0xC3;
  66. head[7] = 0xB5;
  67. head[8] = 0xC4;
  68. head[9] = 0xD6;
  69. head[10] = 0xF7;
  70. head[11] = 0xC8;
  71. head[12] = 0xCB;
  72. head[13] = 0xD2;
  73. head[14] = 0xD1;
  74. head[15] = 0xCE;
  75. head[16] = 0xAA;
  76. head[17] = 0xC4;
  77. head[18] = 0xFA;
  78. head[19] = 0xB9;
  79. head[20] = 0xD8;
  80. head[21] = 0xB5;
  81. head[22] = 0xC6;
  82. head[23] = 0x88;
  83. for(i=0; i<24; i++){
  84. Serial.write(head[i]);
  85. }
  86. }
  87. void speech_an(){
  88. unsigned char i = 0;
  89. unsigned char head[24];
  90. head[0] = 0xFD;
  91. head[1] = 0x00;
  92. head[2] = 0x15;
  93. head[3] = 0x01;
  94. head[4] = 0x00;
  95. head[5] = 0xBA;
  96. head[6] = 0xC3;
  97. head[7] = 0xB5;
  98. head[8] = 0xC4;
  99. head[9] = 0xD6;
  100. head[10] = 0xF7;
  101. head[11] = 0xC8;
  102. head[12] = 0xCB;
  103. head[13] = 0xB5;
  104. head[14] = 0xC6;
  105. head[15] = 0xB9;
  106. head[16] = 0xE2;
  107. head[17] = 0xD2;
  108. head[18] = 0xD1;
  109. head[19] = 0xB5;
  110. head[20] = 0xF7;
  111. head[21] = 0xB0;
  112. head[22] = 0xB5;
  113. head[23] = 0xAF;
  114. for(i=0; i<24; i++){
  115. Serial.write(head[i]);
  116. }
  117. }
  118. void speech_liang(){
  119. unsigned char i = 0;
  120. unsigned char head[24];
  121. head[0] = 0xFD;
  122. head[1] = 0x00;
  123. head[2] = 0x15;
  124. head[3] = 0x01;
  125. head[4] = 0x00;
  126. head[5] = 0xBA;
  127. head[6] = 0xC3;
  128. head[7] = 0xB5;
  129. head[8] = 0xC4;
  130. head[9] = 0xD6;
  131. head[10] = 0xF7;
  132. head[11] = 0xC8;
  133. head[12] = 0xCB;
  134. head[13] = 0xB5;
  135. head[14] = 0xC6;
  136. head[15] = 0xB9;
  137. head[16] = 0xE2;
  138. head[17] = 0xD2;
  139. head[18] = 0xD1;
  140. head[19] = 0xB5;
  141. head[20] = 0xF7;
  142. head[21] = 0xC1;
  143. head[22] = 0xC1;
  144. head[23] = 0xAA;
  145. for(i=0; i<24; i++){
  146. Serial.write(head[i]);
  147. }
  148. }
  149. #endif

 LD3320.ino

  1. #include <Wire.h>
  2. #include "ASR.h"
  3. #include <Arduino.h>
  4. #include "SYN6288.h"
  5. void setup() {
  6. Serial.begin(9600);
  7. Wire.begin(21,22);
  8. Wire.setClock(100000);
  9. unsigned char cleck = 0xff;
  10. unsigned char asr_version = 0;
  11. WireReadData(FIRMWARE_VERSION,&asr_version,1);
  12. #if 1
  13. I2CWrite(ASR_CLEAR_ADDR,0x40);//清除掉电保存区,录入前需要清除掉电保存区
  14. BusyWait();
  15. I2CWrite(ASR_MODE_ADDR,0);//设置检测模式
  16. BusyWait();
  17. AsrAddWords(0,"xiao ya");
  18. BusyWait();
  19. AsrAddWords(1, "kai deng");
  20. BusyWait();
  21. AsrAddWords(2,"guan deng");
  22. BusyWait();
  23. AsrAddWords(3,"an yi dian");
  24. BusyWait();
  25. AsrAddWords(4,"liang yi dian");
  26. BusyWait();
  27. AsrAddWords(5,"kai feng shang");
  28. BusyWait();
  29. AsrAddWords(6,"guan feng shang");
  30. BusyWait();
  31. while(cleck != 7)
  32. {
  33. WireReadData(ASR_NUM_CLECK,&cleck,1);
  34. delay(100);
  35. }
  36. #endif
  37. I2CWrite(ASR_REC_GAIN,0x40); //识别的灵敏度,建议0x40-0x55
  38. I2CWrite(ASR_VOICE_FLAG,1); //识别结果提示音开关设置
  39. I2CWrite(ASR_BUZZER,1); //开启蜂鸣器
  40. RGB_Set(255,255,255);//设置模块的RGB灯为白色
  41. delay(500);
  42. I2CWrite(ASR_BUZZER,0); //关闭蜂鸣器
  43. RGB_Set(0,0,0);//关闭RGB灯
  44. }
  45. void loop() {
  46. unsigned char result;
  47. WireReadData(ASR_RESULT,&result,1);//读取识别序号值,并赋值给result,默认是0xff
  48. delay(100);
  49. if(result == 0)//小亚
  50. {
  51. speech();
  52. }
  53. else if(result == 1)//开灯
  54. {
  55. if(ledBrightness==0){
  56. ledBrightness=400;
  57. }
  58. speech_kaideng();
  59. }
  60. else if(result == 2)//关灯
  61. {
  62. speech_guandeng();
  63. }
  64. else if(result == 3)//暗一点
  65. {
  66. speech_an();
  67. }
  68. else if(result == 4)//亮一点
  69. {
  70. speech_liang();
  71. }
  72. }

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

闽ICP备14008679号