语音识别模块(LD3320)是这款
语音合成模块(SYN6288)是这款
ESP32与语音识别模块LD3320的接线方式
ESP32 | LD3320 |
3.3V | VCC |
GND | GND |
D21 | SDA |
D22 | SCL |
ESP32与语音合成模块SYN6288的接线方式:
注意:编译的时候要断开开发板和SYN6288的连接
ESP32 | SYN6288 |
3.3V | VCC |
GND | GND |
RX | TX |
TX | RX |
示例代码
ASR.CPP
#include "ASR.h"
/****************************
寄存器设置函数
*****************************/
bool I2CWrite(unsigned char reg_addr,unsigned char date)
{
Wire.beginTransmission(ASR_I2C_ADDR); //发送Device地址
Wire.write(reg_addr); //发送要操作的寄存器地址
Wire.write(date); //发送要设置的值
if(Wire.endTransmission()!=0) //发送结束信号
{
delay(10);
return false;
}
delay(10);
return true;
}
/****************************
检测值读取函数
*****************************/
bool WireReadData(unsigned char reg_addr,unsigned char *value,int num)
{
Wire.beginTransmission(ASR_I2C_ADDR); //发送Device地址
Wire.write(reg_addr); //发送要操作的寄存器地址
delay(10);
if(Wire.endTransmission()!=0) //发送结束信号
{
delay(10);
return false;
}
delay(10);
Wire.requestFrom(ASR_I2C_ADDR, num);
while(Wire.available())
{
char ff = Wire.read(); // receive a byte as character
*value = ff;
value++;
delay(10);
}
return true;
}
/*****************************
RGB设置函数
******************************/
bool RGB_Set(unsigned char R,unsigned char G,unsigned char B)
{
Wire.beginTransmission(ASR_I2C_ADDR); //发送Device地址
Wire.write(ASR_RGB_ADDR);
Wire.write(R);
Wire.write(G);
Wire.write(B);
if(Wire.endTransmission()!=0) //发送结束信号
{
delay(10);
return false;
}
delay(10);
return true;
}
/*****************************
单字节读取函数
******************************/
bool I2CWrite_byte(unsigned char date)
{
Wire.beginTransmission(ASR_I2C_ADDR); //发送Device地址
Wire.write(date); //发送要设置的值
if(Wire.endTransmission()!=0) //发送结束信号
{
delay(10);
return false;
}
delay(10);
return true;
}
/*****************************
忙闲等待取函数
******************************/
void BusyWait(void)
{
unsigned char busy_flag = 0xff;
while(busy_flag != 0)
{
WireReadData(ASR_BUSY,&busy_flag,1);
delay(100);
}
}
/*****************************
添加词条函数
******************************/
void AsrAddWords(unsigned char idNum,const char * words)
{
unsigned char date_num = strlen(reinterpret_cast<const char*>(words))+2;
unsigned char str_num = strlen(reinterpret_cast<const char*>(words));
I2CWrite_byte(ASR_ADD_WORD_ADDR); //发送词组寄存器地址
I2CWrite_byte(date_num); //发送本次发送的数据长度,计算方式:词组号+字符串长度+0作为字符串结束符
I2CWrite_byte(idNum); //发送词组号
for(int i = 0; i<str_num;i++)
{
I2CWrite_byte(words[i]); //发送词字符
}
I2CWrite_byte(0); //发送0作为结束符
}
ASR.h
#ifndef ASR_H
#define ASR_H
#include "Arduino.h"
#include <Wire.h>
#define ASR_I2C_ADDR 0x0f //语音识别模块地址
#define ASR_ADD_WORD_ADDR 0x01 //词条添加地址
#define ASR_MODE_ADDR 0x02 //识别模式设置地址,值为0-2,0:循环识别模式 1:口令模式 ,2:按键模式,默认为循环检测
#define ASR_RGB_ADDR 0x03 //RGB灯设置地址,需要发两位,第一个直接为灯号1:蓝 2:红 3:绿 ,
//第二个字节为亮度0-255,数值越大亮度越高
#define ASR_REC_GAIN 0x04 //识别灵敏度设置地址,灵敏度可设置为0x00-0x7f,值越高越容易检测但是越容易误判,
//建议设置值为0x40-0x55,默认值为0x40
#define ASR_CLEAR_ADDR 0x05 //清除掉电缓存操作地址,录入信息前均要清除下缓存区信息
#define ASR_KEY_FLAG 0x06 //用于按键模式下,设置启动识别模式
#define ASR_VOICE_FLAG 0x07 //用于设置是否开启识别结果提示音
#define ASR_RESULT 0x08 //识别结果存放地址
#define ASR_BUZZER 0x09 //蜂鸣器控制写1开启,写0关闭
#define ASR_NUM_CLECK 0x0a //录入词条数目校验
#define FIRMWARE_VERSION 0x0b //读取固件版本
#define ASR_BUSY 0x0c//忙闲标志
bool I2CWrite(unsigned char reg_addr,unsigned char date);
bool WireReadData(unsigned char reg_addr,unsigned char *value,int num);
bool RGB_Set(unsigned char R,unsigned char G,unsigned char B);
void AsrAddWords(unsigned char idNum,const char * words);
void BusyWait(void);
#endif
SYN6288.h(可以自己找厂家拿合成器,合成自己想要的)
#ifndef SYN6288_H
#define SYN6288_H
void speech(){
unsigned char i = 0;
unsigned char head[15];
head[0] = 0xFD;
head[1] = 0x00;
head[2] = 0x0C;
head[3] = 0x01;
head[4] = 0x00;
head[5] = 0xD6;
head[6] = 0xF7;
head[7] = 0xC8;
head[8] = 0xCB;
head[9] = 0x2C;
head[10] = 0xCE;
head[11] = 0xD2;
head[12] = 0xD4;
head[13] = 0xDA;
head[14] = 0xEC;
for(i=0; i<15; i++){
Serial.write(head[i]);
}
}
void speech_kaideng(){
unsigned char i = 0;
unsigned char head[24];
head[0] = 0xFD;
head[1] = 0x00;
head[2] = 0x15;
head[3] = 0x01;
head[4] = 0x00;
head[5] = 0xBA;
head[6] = 0xC3;
head[7] = 0xB5;
head[8] = 0xC4;
head[9] = 0xD6;
head[10] = 0xF7;
head[11] = 0xC8;
head[12] = 0xCB;
head[13] = 0xD2;
head[14] = 0xD1;
head[15] = 0xCE;
head[16] = 0xAA;
head[17] = 0xC4;
head[18] = 0xFA;
head[19] = 0xBF;
head[20] = 0xAA;
head[21] = 0xB5;
head[22] = 0xC6;
head[23] = 0xFC;
for(i=0; i<24; i++){
Serial.write(head[i]);
}
}
void speech_guandeng(){
unsigned char i = 0;
unsigned char head[24];
head[0] = 0xFD;
head[1] = 0x00;
head[2] = 0x15;
head[3] = 0x01;
head[4] = 0x00;
head[5] = 0xBA;
head[6] = 0xC3;
head[7] = 0xB5;
head[8] = 0xC4;
head[9] = 0xD6;
head[10] = 0xF7;
head[11] = 0xC8;
head[12] = 0xCB;
head[13] = 0xD2;
head[14] = 0xD1;
head[15] = 0xCE;
head[16] = 0xAA;
head[17] = 0xC4;
head[18] = 0xFA;
head[19] = 0xB9;
head[20] = 0xD8;
head[21] = 0xB5;
head[22] = 0xC6;
head[23] = 0x88;
for(i=0; i<24; i++){
Serial.write(head[i]);
}
}
void speech_an(){
unsigned char i = 0;
unsigned char head[24];
head[0] = 0xFD;
head[1] = 0x00;
head[2] = 0x15;
head[3] = 0x01;
head[4] = 0x00;
head[5] = 0xBA;
head[6] = 0xC3;
head[7] = 0xB5;
head[8] = 0xC4;
head[9] = 0xD6;
head[10] = 0xF7;
head[11] = 0xC8;
head[12] = 0xCB;
head[13] = 0xB5;
head[14] = 0xC6;
head[15] = 0xB9;
head[16] = 0xE2;
head[17] = 0xD2;
head[18] = 0xD1;
head[19] = 0xB5;
head[20] = 0xF7;
head[21] = 0xB0;
head[22] = 0xB5;
head[23] = 0xAF;
for(i=0; i<24; i++){
Serial.write(head[i]);
}
}
void speech_liang(){
unsigned char i = 0;
unsigned char head[24];
head[0] = 0xFD;
head[1] = 0x00;
head[2] = 0x15;
head[3] = 0x01;
head[4] = 0x00;
head[5] = 0xBA;
head[6] = 0xC3;
head[7] = 0xB5;
head[8] = 0xC4;
head[9] = 0xD6;
head[10] = 0xF7;
head[11] = 0xC8;
head[12] = 0xCB;
head[13] = 0xB5;
head[14] = 0xC6;
head[15] = 0xB9;
head[16] = 0xE2;
head[17] = 0xD2;
head[18] = 0xD1;
head[19] = 0xB5;
head[20] = 0xF7;
head[21] = 0xC1;
head[22] = 0xC1;
head[23] = 0xAA;
for(i=0; i<24; i++){
Serial.write(head[i]);
}
}
#endif
LD3320.ino
#include <Wire.h>
#include "ASR.h"
#include <Arduino.h>
#include "SYN6288.h"
void setup() {
Serial.begin(9600);
Wire.begin(21,22);
Wire.setClock(100000);
unsigned char cleck = 0xff;
unsigned char asr_version = 0;
WireReadData(FIRMWARE_VERSION,&asr_version,1);
#if 1
I2CWrite(ASR_CLEAR_ADDR,0x40);//清除掉电保存区,录入前需要清除掉电保存区
BusyWait();
I2CWrite(ASR_MODE_ADDR,0);//设置检测模式
BusyWait();
AsrAddWords(0,"xiao ya");
BusyWait();
AsrAddWords(1, "kai deng");
BusyWait();
AsrAddWords(2,"guan deng");
BusyWait();
AsrAddWords(3,"an yi dian");
BusyWait();
AsrAddWords(4,"liang yi dian");
BusyWait();
AsrAddWords(5,"kai feng shang");
BusyWait();
AsrAddWords(6,"guan feng shang");
BusyWait();
while(cleck != 7)
{
WireReadData(ASR_NUM_CLECK,&cleck,1);
delay(100);
}
#endif
I2CWrite(ASR_REC_GAIN,0x40); //识别的灵敏度,建议0x40-0x55
I2CWrite(ASR_VOICE_FLAG,1); //识别结果提示音开关设置
I2CWrite(ASR_BUZZER,1); //开启蜂鸣器
RGB_Set(255,255,255);//设置模块的RGB灯为白色
delay(500);
I2CWrite(ASR_BUZZER,0); //关闭蜂鸣器
RGB_Set(0,0,0);//关闭RGB灯
}
void loop() {
unsigned char result;
WireReadData(ASR_RESULT,&result,1);//读取识别序号值,并赋值给result,默认是0xff
delay(100);
if(result == 0)//小亚
{
speech();
}
else if(result == 1)//开灯
{
if(ledBrightness==0){
ledBrightness=400;
}
speech_kaideng();
}
else if(result == 2)//关灯
{
speech_guandeng();
}
else if(result == 3)//暗一点
{
speech_an();
}
else if(result == 4)//亮一点
{
speech_liang();
}
}