赞
踩
一、原理图
二、源代码
LD3320.c
#include "ld3320.h"
#include "delay.h"
#include "usart.h"
#include "string.h"
u8 ld3320Data;
extern u8 nAsrStatus;
unsigned long nMp3StartPos=0;
unsigned long nMp3Size=0;
unsigned long nMp3Pos=0;
u8 bMp3Play=0; // 用来记录播放MP3的状态
u8 nLD_Mode = LD_MODE_IDLE; // 用来记录当前是在进行ASR识别还是在播放MP3
u8 ucRegVal;
u8 ucHighInt;
u8 ucLowInt;
u8 ucStatus;
u8 ucSPVol=15; // MAX=15 MIN=0 // Speaker喇叭输出的音量
void ld3320_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE); //使能PB,PE端口时钟
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_15);
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_14|GPIO_Pin_15);
LD3320_MD=0; //选择并口
LD3320_RST=1;
LD3320_A0=0;
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //使能复用功能时钟
// 中断线以及中断初始化配置 下降沿触发
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource11);
EXTI_InitStructure.EXTI_Line=EXTI_Line11;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; //使能外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00; //抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; //子优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
}
//外部中断服务程序
void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line11)!= RESET )
{
ld3320_process_init();
EXTI_ClearFlag(EXTI_Line11);
EXTI_ClearITPendingBit(EXTI_Line11);//清除LINE上的中断标志位
}
}
void ld3320_write_reg( unsigned char address, unsigned char dataout )
{
ld3320Data=address;
LD3320_D0=bita0;LD3320_D1=bita1;LD3320_D2=bita2;LD3320_D3=bita3;
LD3320_D4=bita4;LD3320_D5=bita5;LD3320_D6=bita6;LD3320_D7=bita7;
delay_us(1);
LD3320_A0 =1;
LD3320_CS = 0;
LD3320_WR = 0;
LD3320_WR = 1;
LD3320_CS = 1;
ld3320Data=dataout;
LD3320_D0=bita0;LD3320_D1=bita1;LD3320_D2=bita2;LD3320_D3=bita3;
LD3320_D4=bita4;LD3320_D5=bita5;LD3320_D6=bita6;LD3320_D7=bita7;
delay_us(1);
LD3320_A0 = 0;
LD3320_CS = 0;
LD3320_WR = 0;
LD3320_WR = 1;
LD3320_CS = 1;
}
unsigned char ld3320_read_reg( unsigned char address )
{
unsigned char datain;
ld3320Data=address;
LD3320_D0=bita0;LD3320_D1=bita1;LD3320_D2=bita2;LD3320_D3=bita3;
LD3320_D4=bita4;LD3320_D5=bita5;LD3320_D6=bita6;LD3320_D7=bita7;
LD3320_A0 = 1;
delay_us(1);
LD3320_CS = 0;
delay_us(1);
LD3320_WR = 0;
delay_us(1);
LD3320_WR = 1;
delay_us(1);
LD3320_CS = 1;
delay_us(1);
LD3320_A0 = 0;
delay_us(1);
LD3320_CS = 0;
delay_us(1);
LD3320_RD = 0;
LD3320_DATA_IN();
delay_us(1);
bita0=LD3320_D0_R;bita1=LD3320_D1_R;bita2=LD3320_D2_R;bita3=LD3320_D3_R;
bita4=LD3320_D4_R;bita5=LD3320_D5_R; bita6=LD3320_D6_R;bita7=LD3320_D7_R;
datain= ld3320Data;
LD3320_DATA_OUT();
LD3320_RD = 1;
delay_us(1);
LD3320_CS = 1;
return datain;
}
void ld3320_reset()
{
LD3320_RST=1;
delay_ms(1);
LD3320_RST=0;
delay_ms(1);
LD3320_RST=1;
delay_ms(1);
LD3320_CS=0;
delay_ms(1);
LD3320_CS=1;
delay_ms(1);
}
u8 ld3320_check()
{
u8 a[3];
memset(a,0x00,sizeof(a));
ld3320_reset();
delay_ms(1);
ld3320_read_reg(0x06);
delay_ms(1);
ld3320_write_reg(0x35, 0x33);
delay_ms(1);
ld3320_write_reg(0x1b, 0x55);
delay_ms(1);
ld3320_write_reg(0xb3, 0xaa);
a[0]=ld3320_read_reg(0x35);
delay_ms(5);
a[1]=ld3320_read_reg(0x1b);
delay_ms(5);
a[2]=ld3320_read_reg(0xb3);
if(a[0]!=0x33||a[1]!=0x55||a[2]!=0xaa)
return 1;
return 0;
}
void ld3320_init_common()
{
bMp3Play = 0;
ld3320_read_reg(0x06);
ld3320_write_reg(0x17, 0x35);
delay_ms(10);
ld3320_read_reg(0x06);
ld3320_write_reg(0x89, 0x03);
delay_ms(5);
ld3320_write_reg(0xCF, 0x43);
delay_ms(5);
ld3320_write_reg(0xCB, 0x02);
/*PLL setting*/
ld3320_write_reg(0x11, LD_PLL_11);
if (nLD_Mode == LD_MODE_MP3)
{
ld3320_write_reg(0x1E, 0x00);
ld3320_write_reg(0x19, LD_PLL_MP3_19);
ld3320_write_reg(0x1B, LD_PLL_MP3_1B);
ld3320_write_reg(0x1D, LD_PLL_MP3_1D);
}
else
{
ld3320_write_reg(0x1E,0x00);
ld3320_write_reg(0x19, LD_PLL_ASR_19);
ld3320_write_reg(0x1B, LD_PLL_ASR_1B);
ld3320_write_reg(0x1D, LD_PLL_ASR_1D);
}
delay_ms(10);
ld3320_write_reg(0xCD, 0x04);
ld3320_write_reg(0x17, 0x4c);
delay_ms(5);
ld3320_write_reg(0xB9, 0x00);
ld3320_write_reg(0xCF, 0x4F);
ld3320_write_reg(0x6F, 0xFF);
}
void ld3320_init_mp3()
{
nLD_Mode = LD_MODE_MP3;
ld3320_init_common();
ld3320_write_reg(0xBD,0x02);
ld3320_write_reg(0x17, 0x48);
delay_ms(10);
ld3320_write_reg(0x85, 0x52);
ld3320_write_reg(0x8F, 0x00);
ld3320_write_reg(0x81, 0x00);
ld3320_write_reg(0x83, 0x00);
ld3320_write_reg(0x8E, 0xff);
ld3320_write_reg(0x8D, 0xff);
delay_ms(1);
ld3320_write_reg(0x87, 0xff);
ld3320_write_reg(0x89, 0xff);
delay_ms(1);
ld3320_write_reg(0x22, 0x00);
ld3320_write_reg(0x23, 0x00);
ld3320_write_reg(0x20, 0xef);
ld3320_write_reg(0x21, 0x07);
ld3320_write_reg(0x24, 0x77);
ld3320_write_reg(0x25, 0x03);
ld3320_write_reg(0x26, 0xbb);
ld3320_write_reg(0x27, 0x01);
}
void ld3320_init_asr()
{
nLD_Mode=LD_MODE_ASR_RUN;
ld3320_init_common();
ld3320_write_reg(0xBD, 0x00);
ld3320_write_reg(0x17, 0x48);
delay_ms(10);
ld3320_write_reg(0x3C, 0x80);
ld3320_write_reg(0x3E, 0x07);
ld3320_write_reg(0x38, 0xff);
ld3320_write_reg(0x3A, 0x07);
ld3320_write_reg(0x40, 0);
ld3320_write_reg(0x42, 8);
ld3320_write_reg(0x44, 0);
ld3320_write_reg(0x46, 8);
delay_ms(10);
}
void ld3320_reload_mp3data()
{
unsigned long nCurMp3Pos;
u8 val;
u8 k;
nCurMp3Pos = nMp3StartPos + nMp3Pos;
ucStatus = ld3320_read_reg(0x06);
while ( !(ucStatus&MASK_FIFO_STATUS_AFULL) && (nMp3Pos<nMp3Size) )
{
val=0;
for(k=0;k<8;k++)
{
}
ld3320_write_reg(0x01,val);
nMp3Pos++;
ucStatus = ld3320_read_reg(0x06);
}
}
void ld3320_reload_mp3data_2()
{
unsigned long nCurMp3Pos;
u8 val;
u8 k;
nCurMp3Pos = nMp3StartPos + nMp3Pos;
//读取音乐文件写入
ucStatus = ld3320_read_reg(0x06);
while ( !(ucStatus&MASK_FIFO_STATUS_AFULL) && (nMp3Pos<nMp3Size) )
{
val=0;
nMp3Pos++;
ucStatus = ld3320_read_reg(0x06);
}
}
void ld3320_process_init()
{
u8 nAsrResCount=0;
ucRegVal = ld3320_read_reg(0x2B);
if(nLD_Mode == LD_MODE_ASR_RUN)
{
// 语音识别产生的中断
// (有声音输入,不论识别成功或失败都有中断)
ld3320_write_reg(0x29,0) ;
ld3320_write_reg(0x02,0) ;
if((ucRegVal & 0x10) &&
ld3320_read_reg(0xb2)==0x21 &&
ld3320_read_reg(0xbf)==0x35)
{
nAsrResCount = ld3320_read_reg(0xba);
if(nAsrResCount>0 && nAsrResCount<=4)
{
nAsrStatus=LD_ASR_FOUNDOK;
}
else
{
nAsrStatus=LD_ASR_FOUNDZERO;
}
}
else
{
nAsrStatus=LD_ASR_FOUNDZERO;
}
ld3320_write_reg(0x2b, 0);
ld3320_write_reg(0x1C,0);
return;
}
// 声音播放产生的中断,有三种:
// A. 声音数据已全部播放完。
// B. 声音数据已发送完毕。
// C. 声音数据暂时将要用完,需要放入新的数据。
ucHighInt = ld3320_read_reg(0x29);
ucLowInt=ld3320_read_reg(0x02);
ld3320_write_reg(0x29,0) ;
ld3320_write_reg(0x02,0) ;
if(ld3320_read_reg(0xBA)&CAUSE_MP3_SONG_END)
{
// A. 声音数据已全部播放完。
ld3320_write_reg(0x2B, 0);
ld3320_write_reg(0xBA, 0);
ld3320_write_reg(0xBC,0x0);
bMp3Play=0; // 声音数据全部播放完后,修改bMp3Play的变量
ld3320_write_reg(0x08,1);
delay_ms(5);
ld3320_write_reg(0x08,0);
ld3320_write_reg(0x33, 0);
return ;
}
if(nMp3Pos>=nMp3Size)
{
// B. 声音数据已发送完毕。
ld3320_write_reg(0xBC, 0x01);
ld3320_write_reg(0x29, 0x10);
// bMp3Play=0; // 此时,只是主控MCU把所有MP3数据发送到LD3320芯片内,但是还没有把送入的数据全部播放完毕
return;
}
// C. 声音数据暂时将要用完,需要放入新的数据。
ld3320_reload_mp3data_2();
ld3320_write_reg(0x29,ucHighInt);
ld3320_write_reg(0x02,ucLowInt) ;
}
void ld3320_play()
{
nMp3Pos=0;
bMp3Play=1;
if (nMp3Pos >= nMp3Size)
return ;
ld3320_reload_mp3data();
ld3320_write_reg(0xBA, 0x00);
ld3320_write_reg(0x17, 0x48);
ld3320_write_reg(0x33, 0x01);
ld3320_write_reg(0x29, 0x04);
ld3320_write_reg(0x02, 0x01);
ld3320_write_reg(0x85, 0x5A);
}
void ld3320_adjust_volume(u8 val)
{
ucSPVol = val;
val = ((15-val)&0x0f) << 2;
ld3320_write_reg(0x8E, val | 0xc3);
ld3320_write_reg(0x87, 0x78);
}
// Return 1: success.
u8 ld3320_check_asrbusyflag_b2()
{
u8 j;
u8 flag = 0;
for (j=0; j<10; j++)
{
if (ld3320_read_reg(0xb2) == 0x21)
{
flag = 1;
break;
}
delay_ms(10);
}
return flag;
}
void LD_AsrStart()
{
ld3320_init_asr();
}
// Return 1: success.
u8 ld3320_asrun()
{
ld3320_write_reg(0x35, MIC_VOL);
ld3320_write_reg(0x1C, 0x09);
ld3320_write_reg(0xBD, 0x20);
ld3320_write_reg(0x08, 0x01);
delay_ms( 1 );
ld3320_write_reg(0x08, 0x00);
delay_ms( 1 );
if(ld3320_check_asrbusyflag_b2() == 0)
{
return 0;
}
ld3320_write_reg(0xB2, 0xff);
ld3320_write_reg(0x37, 0x06);
delay_ms( 10 );
ld3320_write_reg(0x1C, 0x0b);
ld3320_write_reg(0x29, 0x10);
ld3320_write_reg(0xBD, 0x00);
return 1;
}
// Return 1: success.
// 添加识别关键词语,开发者可以学习"语音识别芯片LD3320高阶秘籍.pdf"中关于垃圾词语吸收错误的用法
u8 ld3320_asr_addFixed()
{
u8 k, flag;
u8 nAsrAddLength;
const char sRecog[24][30] = {"xiao hui hui",
"ni you xi fu mei",
"jiang ge gu shi",
"ni shi bu shi dan teng",
"ni ban ben hao duo shao",
"jin tian duo shao hao",
"jin tian ji hao le",
"xian zai ji dian le",
"jin nian ji sui le",
"ni sheng wa le mei",
"da kai ji dian qi yi",
"da kai ji dian qi er",
"guan bi ji dian qi yi",
"guan bi ji dian qi er",
"da kai suo you ji dian qi",
"guan bi suo you ji dian qi",
"tiao gao yin liang",
"zeng jia tin liang",
"tiao xiao yin liang",
"jiang di yin lliang",
"tiao gao yu su",
"zeng jia yu su",
"tiao xiao yu su",
"jiang di yu su",
};
const Order pCode[24] = {
name,
wife,
speakStore,
eggPain,
visionNum,
todayDate,
whyToday,
whatTime,
howOld,
haveChild,
openRelayOne,
openRelayTwo,
closeRelayOne,
closeRelayTwo,
openRelay,
closeRelay,
upVolume,
upVolume,
downVolume,
downVolume,
upSpeed,
upSpeed,
downSpeed,
downSpeed,
};
flag = 1;
for (k=0; k<24; k++)
{
if(ld3320_check_asrbusyflag_b2() == 0)
{
flag = 0;
break;
}
ld3320_write_reg(0xc1, pCode[k] );
ld3320_write_reg(0xc3, 0 );
ld3320_write_reg(0x08, 0x04);
delay_ms(1);
ld3320_write_reg(0x08, 0x00);
delay_ms(1);
for (nAsrAddLength=0; nAsrAddLength<30; nAsrAddLength++)
{
if (sRecog[k][nAsrAddLength] == 0)
break;
ld3320_write_reg(0x5, sRecog[k][nAsrAddLength]);
}
ld3320_write_reg(0xb9, nAsrAddLength);
ld3320_write_reg(0xb2, 0xff);
ld3320_write_reg(0x37, 0x04);
}
return flag;
}
u8 ld3320_get_result()
{
return ld3320_read_reg(0xc5 );
}
/************************************************************************************/
// RunASR()函数实现了一次完整的ASR语音识别流程
// LD_AsrStart() 函数实现了ASR初始化
// LD_AsrAddFixed() 函数实现了添加关键词语到LD3320芯片中
// LD_AsrRun() 函数启动了一次ASR语音识别流程
//
// 任何一次ASR识别流程,都需要按照这个顺序,从初始化开始进行
/************************************************************************************/
u8 ld3320_run_asr()
{
u8 i=0;
u8 asrflag=0;
for (i=0; i<5; i++) // 防止由于硬件原因导致LD3320芯片工作不正常,所以一共尝试5次启动ASR识别流程
{
LD_AsrStart();
delay_ms(10);
if (ld3320_asr_addFixed()==0)
{
ld3320_reset(); // LD3320芯片内部出现不正常,立即重启LD3320芯片
delay_ms(100); // 并从初始化开始重新ASR识别流程
continue;
}
delay_ms(300);
if (ld3320_asrun() == 0)
{
ld3320_reset(); // LD3320芯片内部出现不正常,立即重启LD3320芯片
delay_ms(100); // 并从初始化开始重新ASR识别流程
continue;
}
asrflag=1;
break; // ASR流程启动成功,退出当前for循环。开始等待LD3320送出的中断信号
}
return asrflag;
}
LD3320.h
#ifndef __LD3320_H
#define __LD3320_H
#include "sys.h"
typedef struct
{
unsigned char b0:1;
unsigned char b1:1;
unsigned char b2:1;
unsigned char b3:1;
unsigned char b4:1;
unsigned char b5:1;
unsigned char b6:1;
unsigned char b7:1;
} BIT8;
extern u8 ld3320Data;
#define bita0 (((BIT8*) & ld3320Data) -> b0)
#define bita1 (((BIT8*) & ld3320Data) -> b1)
#define bita2 (((BIT8*) & ld3320Data) -> b2)
#define bita3 (((BIT8*) & ld3320Data) -> b3)
#define bita4 (((BIT8*) & ld3320Data) -> b4)
#define bita5 (((BIT8*) & ld3320Data) -> b5)
#define bita6 (((BIT8*) & ld3320Data) -> b6)
#define bita7 (((BIT8*) & ld3320Data) -> b7)
#define LD3320_A0 PBout(15)
#define LD3320_RD PBout(14)
#define LD3320_WR PAout(12)
#define LD3320_CS PAout(11)
#define LD3320_RST PBout(10)
#define LD3320_MD PBout(13)
#define LD3320_D0 PBout(5)
#define LD3320_D1 PBout(4)
#define LD3320_D2 PBout(3)
#define LD3320_D3 PAout(15)
#define LD3320_D4 PBout(9)
#define LD3320_D5 PBout(8)
#define LD3320_D6 PBout(7)
#define LD3320_D7 PBout(6)
#define LD3320_D0_R PBin(5)
#define LD3320_D1_R PBin(4)
#define LD3320_D2_R PBin(3)
#define LD3320_D3_R PAin(15)
#define LD3320_D4_R PBin(9)
#define LD3320_D5_R PBin(8)
#define LD3320_D6_R PBin(7)
#define LD3320_D7_R PBin(6)
#define LD3320_DATA_IN() {GPIOA->CRH&=0X0FFFFFFF;GPIOA->CRH|=(u32)8<<28;GPIOB->CRL&=0X00000FFF;GPIOB->CRL|=0X88888000;GPIOB->CRH&=0XFFFFFF00;GPIOB->CRH|=0X00000088;}
#define LD3320_DATA_OUT() {GPIOA->CRH&=0X0FFFFFFF;GPIOA->CRH|=(u32)3<<28;GPIOB->CRL&=0X00000FFF;GPIOB->CRL|=0X33333000;GPIOB->CRH&=0XFFFFFF00;GPIOB->CRH|=0X00000033;}
#define LD3320_DATA_WRITE(x) {ld3320Data = x;LD3320_D0=bita0;LD3320_D1=bita1;LD3320_D2=bita2;LD3320_D3=bita3;LD3320_D4=bita4;LD3320_D5=bita5;LD3320_D6=bita6;LD3320_D7=bita7;}
#define LD3320_DATA_READ(x) {bita0=LD3320_D0_R;bita1=LD3320_D1_R;bita2=LD3320_D2_R;bita3=LD3320_D3_R;bita4=LD3320_D4_R;bita5=LD3320_D5_R; bita6=LD3320_D6_R;bita7=LD3320_D7_R;x= ld3320Data;}
// 以下三个状态定义用来记录程序是在运行ASR识别还是在运行MP3播放
#define LD_MODE_IDLE 0x00
#define LD_MODE_ASR_RUN 0x08
#define LD_MODE_MP3 0x40
// 以下五个状态定义用来记录程序是在运行ASR识别过程中的哪个状态
#define LD_ASR_NONE 0x00 // 表示没有在作ASR识别
#define LD_ASR_RUNING 0x01 // 表示LD3320正在作ASR识别中
#define LD_ASR_FOUNDOK 0x10 // 表示一次识别流程结束后,有一个识别结果
#define LD_ASR_FOUNDZERO 0x11 // 表示一次识别流程结束后,没有识别结果
#define LD_ASR_ERROR 0x31 // 表示一次识别流程中LD3320芯片内部出现不正确的状态
#define CLK_IN 8 /* user need modify this value according to clock in */
#define LD_PLL_11 (u8)((CLK_IN/2.0)-1)
#define LD_PLL_MP3_19 0x0f
#define LD_PLL_MP3_1B 0x18
#define LD_PLL_MP3_1D (u8)(((90.0*((LD_PLL_11)+1))/(CLK_IN))-1)
#define LD_PLL_ASR_19 (u8)(CLK_IN*32.0/(LD_PLL_11+1) - 0.51)
#define LD_PLL_ASR_1B 0x48
#define LD_PLL_ASR_1D 0x1f
// LD chip fixed values.
#define RESUM_OF_MUSIC 0x01
#define CAUSE_MP3_SONG_END 0x20
#define MASK_INT_SYNC 0x10
#define MASK_INT_FIFO 0x04
#define MASK_AFIFO_INT 0x01
#define MASK_FIFO_STATUS_AFULL 0x08
typedef enum
{
name=1, //小灰灰
wife=2, //你有媳妇没
speakStore=3,//讲个故事
eggPain=4, //你最近是不是蛋疼
visionNum=5, //你版本号是多少
todayDate=6,//今天多少号
whyToday=6, //今天几号了
whatTime=7, //现在几点了
howOld=8, //今年几岁了
haveChild=9, //你生娃了没
openRelayOne=10, //打开继电器一
openRelayTwo=11, //打开继电器二
closeRelayOne=12, //关闭继电器一
closeRelayTwo=13, //关闭继电器二
openRelay=14, //打开所有继电器
closeRelay=15, //关闭所有继电器
upVolume=17, //调高音量
downVolume=18, //降低音量
upSpeed=19, //提高语速
downSpeed=20, //降低语速
}Order;
extern unsigned long nMp3StartPos;
extern unsigned long nMp3Size;
extern unsigned long nMp3Pos;
extern u8 bMp3Play; // 用来记录播放MP3的状态
extern u8 nLD_Mode; // 用来记录当前是在进行ASR识别还是在播放MP3
extern u8 ucRegVal;
extern u8 ucHighInt;
extern u8 ucLowInt;
extern u8 ucStatus;
extern u8 ucSPVol; // MAX=15 MIN=0 // Speaker喇叭输出的音量
#define MIC_VOL 0x55
u8 ld3320_check(void);
void ld3320_init(void);
void ld3320_process_init(void);
void ld3320_reset(void);
void ld3320_init_common(void);
void ld3320_init_mp3(void);
void ld3320_init_asr(void);
void ld3320_reload_mp3data(void);
void ld3320_reload_mp3data_2(void);
void ld3320_play(void);
void ld3320_adjust_volume(u8 val);
u8 ld3320_check_asrbusyflag_b2(void);
void LD_AsrStart(void);
u8 ld3320_asrun(void);
u8 ld3320_asr_addFixed(void);
u8 ld3320_get_result(void);
u8 ld3320_run_asr(void);
#endif
freq.c
#include "freq.h"
void ld3320_freq_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE); //使能PB,PE端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_8);
RCC_MCOConfig(RCC_MCO_HSE); //RCC_MCO_NoClock --- 无时钟输出
//RCC_MCO_SYSCLK --- 输出系统时钟(SysCLK)
//RCC_MCO_HSI --- 输出内部高速8MHz的RC振荡器的时钟(HSI)
//RCC_MCO_HSE --- 输出高速外部时钟信号(HSE)
//RCC_MCO_PLLCLK_Div2 --- 输出PLL倍频后的二分频时钟(PLLCLK/2)
}
freq.h
#ifndef __FREQ_H
#define __FREQ_H
#include "sys.h"
void ld3320_freq_init(void);
#endif
main.c
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "ld3320.h"
#include "freq.h"
#include "malloc.h"
u8 nAsrStatus=0;
extern u8 bMp3Play; // 用来记录播放MP3的状态
u8 voiceSpeed=5,voiceVolume=10;
int main(void)
{
u8 nAsrRes;
u16 i;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(115200);
delay_init();
ld3320_freq_init();
led_init();
mem_init(); //初始化内存池
ld3320_init();
delay_ms(1000);
ld3320_reset();
while(ld3320_check())
{
printf("LD3320 Error!!\r\n");
turn_r_led();
delay_ms(500);
}
nAsrStatus = LD_ASR_NONE; // 初始状态:没有在作ASR
while(1)
{
if (bMp3Play!=0) // 如果还在播放MP3中,则等待,直到MP3播放完毕 即bMp3Play==0
continue; // bMp3Play 是定义的一个全局变量用来记录MP3播放的状态,不是LD3320芯片内部的寄存器
switch(nAsrStatus)
{
case LD_ASR_RUNING: break;
case LD_ASR_ERROR: break;
case LD_ASR_NONE:
{
nAsrStatus=LD_ASR_RUNING;
if (ld3320_run_asr()==0)
{
nAsrStatus = LD_ASR_ERROR;
}
}break;
case LD_ASR_FOUNDOK:
{
nAsrRes = ld3320_get_result();
printf("nAsrRes=%d\r\n",nAsrRes);
switch(nAsrRes)
{
case name:
{
printf("小灰灰\r\n");
}break;
case wife:
{
printf("你有媳妇没\r\n");
}break;
case speakStore:
{
printf("讲个故事\r\n");
}break;
case eggPain:
{
printf("你最近是不是蛋疼\r\n");
}break;
case visionNum:
{
printf("你版本号是多少\r\n");
}break;
case todayDate:
{
printf("今天多少号\r\n");
}break;
case whatTime:
{
printf("现在几点了\r\n");
}break;
case howOld:
{
printf("今年几岁了\r\n");
}break;
case haveChild:
{
printf("你生娃了没\r\n");
}break;
case openRelayOne:
{
printf("打开继电器一\r\n");
}break;
case openRelayTwo:
{
printf("打开继电器二\r\n");
}break;
case closeRelayOne:
{
printf("关闭继电器一\r\n");
}break;
case closeRelayTwo:
{
printf("关闭继电器二\r\n");
}break;
case openRelay:
{
printf("打开所有继电器\r\n");
}break;
case closeRelay:
{
printf("关闭所有继电器\r\n");
}break;
case upVolume:
{
printf("调高音量\r\n");
}break;
case downVolume:
{
printf("降低音量\r\n");
}break;
case upSpeed:
{
printf("提高语速\r\n");
}break;
case downSpeed:
{
printf("降低语速\r\n");
}break;
}
nAsrStatus = LD_ASR_NONE;
}break;
case LD_ASR_FOUNDZERO:
default:
{
nAsrStatus = LD_ASR_NONE;
}break;
}
delay_us(2);
i++;
if(i>=60000)
{
i=0;
turn_g_led();
}
}
}
完整资料下载:https://download.csdn.net/download/qq_15181569/11263439
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。