直接下载附件吧,兄弟们,程序里面都有,关于MSP430的LD3320代码
单片机源程序如下:
- #include "main.h"
- uint8 nLD_Mode = LD_MODE_IDLE; // 用来记录当前是在进行ASR识别还是在播放MP3
- unsigned char ucRegVal;
- unsigned char ucHighInt;
- unsigned char ucLowInt;
- /*****************************************
- 函数功能 ;LD3320外部中断初始化P1_6
- 入口参数:无
- 出口参数:无
- ******************************************/
- void LD3320_EXTERINT_Init()
- {
- P1SEL&=~BIT6;
- P1DIR&=~BIT6;
- P1REN|=BIT6;
- P1OUT|=BIT6;
- P1IES|=BIT6;
- P1IE|=BIT6;
- P1IFG&=~BIT6;
- }
- /*****************************************
- 函数功能 ;LD3320控制端口初始化
- 入口参数:无
- 出口参数:无
- ******************************************/
- void LD3320_Port_Init()
- {
- P7OUT|=(BIT3+BIT4+BIT5+BIT6+BIT7);
- P7REN|=(BIT3+BIT4+BIT5+BIT6+BIT7);
- P7DIR|=(BIT3+BIT4+BIT5+BIT6+BIT7);
- DIR_LD_CS_OUT;
- DIR_LD_CLK_OUT;
- DIR_LD_DI_OUT;
- DIR_LD_DO_IN;
- DIR_LD_RST_OUT;
- DIR_LD_SPIS_OUT;
- }
- /*****************************************
- 函数功能 ;LD3320 SPI通信测试
- 入口参数:无
- 出口参数:无
- ******************************************/
- void Test_Information()
- {
- //测试读写是否正常
- LD_reset();
- LD_ReadReg(0x06);
- LD_WriteReg(0x35, 0x33);
- LD_WriteReg(0x1b, 0x55);
- LD_WriteReg(0xb3, 0xaa);
- nAsrRes=(LD_ReadReg(0x35));
- nAsrRes=(LD_ReadReg(0x1b));
- nAsrRes=(LD_ReadReg(0xb3));
-
- //测试寄存器初始值是否正常
- LD_reset();
- nAsrRes=(LD_ReadReg(0x06));
- nAsrRes=(LD_ReadReg(0x06));
- nAsrRes=(LD_ReadReg(0x35));
- nAsrRes=(LD_ReadReg(0xb3));
- }
- /*****************************************
- 函数功能 ;LD3320写函数
- 入口参数:address LD3320地址
- dataout LD3320数据
- 出口参数:无
- ******************************************/
- void LD_WriteReg( unsigned char address, unsigned char dataout )
- {
- unsigned char i = 0;
- unsigned char command=0x04;
- CLR_LD_CS;//打开片选
- __delay_cycles(500);
- ///////////////////////写命令
- for(i=0;i<8;i++)
- {
- if ((command & 0x80)>0)
- SET_LD_DI;
- else
- CLR_LD_DI;
- __delay_cycles(500);
- CLR_LD_CLK;
- command = (command << 1);
- __delay_cycles(500);
- SET_LD_CLK;
- }
- /////////////////////写地址
- for(i=0;i<8;i++)
- {
- if ((address & 0x80) >0)
- SET_LD_DI;
- else
- CLR_LD_DI;
- __delay_cycles(500);
- CLR_LD_CLK;
- address = (address << 1);
- __delay_cycles(500);
- SET_LD_CLK;
- }
- //////////////////////写数据
- for(i=0;i<8;i++)
- {
- if ((dataout & 0x80) >0)
- SET_LD_DI;
- else
- CLR_LD_DI;
- __delay_cycles(500);
- CLR_LD_CLK;
- dataout = (dataout << 1);
- __delay_cycles(500);
- SET_LD_CLK;
- }
-
- __delay_cycles(500);
- SET_LD_CS;//关闭片选
-
- }
- /*****************************************
- 函数功能 ;LD3320读函数
- 入口参数:address LD3320地址
- 出口参数:datain LD3320读出的收据
- ******************************************/
- unsigned char LD_ReadReg( unsigned char address )
- {
- unsigned char i = 0;
- unsigned char datain =0 ;
- unsigned char command=0x05;
- CLR_LD_CS;//打开片选
- __delay_cycles(2000);
- ///////////////////////写命令
- for(i=0;i<8;i++)
- {
- if ((command & 0x80)>0)
- SET_LD_DI;
- else
- CLR_LD_DI;
- __delay_cycles(2000);
- CLR_LD_CLK;
- command = (command << 1);
- __delay_cycles(2000);
- SET_LD_CLK;
- }
- /////////////////////写地址
- for(i=0;i<8;i++)
- {
- if ((address & 0x80) >0)
- SET_LD_DI;
- else
- CLR_LD_DI;
- __delay_cycles(2000);
- CLR_LD_CLK;
- address = (address << 1);
- __delay_cycles(2000);
- SET_LD_CLK;
- __delay_cycles(2000);
- }
- /////////////////////读数据
- DIR_LD_DO_IN;
- for(i=0;i<8;i++)
- {
- datain = datain << 1;
- CLR_LD_CLK;
- if(LD_DO) datain|=0x01;
- __delay_cycles(200);
- SET_LD_CLK;
- __delay_cycles(200);
- }
- __delay_cycles(2000);
- SET_LD_CS;//关闭片选
- return datain;
- }
- /*****************************************
- 函数功能 ;LD3320复位函数
- 入口参数:无
- 出口参数:无
- ******************************************/
- void LD_reset()
- {
-
- SET_LD_RST;
- __delay_cycles(200);
- CLR_LD_RST;
- __delay_cycles(200);
- SET_LD_RST;
- __delay_cycles(200);
-
- CLR_LD_CS;
- __delay_cycles(200);
- SET_LD_CS;
- __delay_cycles(200);
- }
- void LD_Init_Common()
- {
- LD_ReadReg(0x06);
- LD_WriteReg(0x17, 0x35);
- __delay_cycles(200);
- LD_ReadReg(0x06);
- LD_WriteReg(0x89, 0x03);
- __delay_cycles(200);
- LD_WriteReg(0xCF, 0x43);
- __delay_cycles(200);
- LD_WriteReg(0xCB, 0x02);
-
- /*PLL setting*/
- LD_WriteReg(0x11, LD_PLL_11);
-
- LD_WriteReg(0x1E,0x00);
- LD_WriteReg(0x19, LD_PLL_ASR_19);
- LD_WriteReg(0x1B, LD_PLL_ASR_1B);
- LD_WriteReg(0x1D, LD_PLL_ASR_1D);
- __delay_cycles(2000);
-
- LD_WriteReg(0xCD, 0x04);
- LD_WriteReg(0x17, 0x4c);
- __delay_cycles(200);
- LD_WriteReg(0xB9, 0x00);
- LD_WriteReg(0xCF, 0x4F);
- LD_WriteReg(0x6F, 0xFF);
- }
- void LD_Init_ASR()
- {
- nLD_Mode=LD_MODE_ASR_RUN;
- LD_Init_Common();
- LD_WriteReg(0xBD, 0x00);
- LD_WriteReg(0x17, 0x48);
- __delay_cycles(200);
- LD_WriteReg(0x3C, 0x80);
- LD_WriteReg(0x3E, 0x07);
- LD_WriteReg(0x38, 0xff);
- LD_WriteReg(0x3A, 0x07);
-
- LD_WriteReg(0x40, 0);
- LD_WriteReg(0x42, 8);
- LD_WriteReg(0x44, 0);
- LD_WriteReg(0x46, 8);
- __delay_cycles(200);
- }
- // Return 1: success.
- unsigned char LD_Check_ASRBusyFlag_b2()
- {
- unsigned char j;
- unsigned char flag = 0;
- for (j=0; j<10; j++)
- {
- if (LD_ReadReg(0xb2) == 0x21)
- {
- flag = 1;
- break;
- }
- __delay_cycles(200);
- }
- return flag;
- }
- /************************************************************************
- 功能描述: 启动ASR
- 入口参数: none
- 返 回 值: none
- 其他说明: none
- **************************************************************************/
- void LD_AsrStart()
- {
- LD_Init_ASR();
- }
- // Return 1: success.
- unsigned char LD_AsrRun()
- {
- LD_WriteReg(0x35, MIC_VOL);
- LD_WriteReg(0x1C, 0x09);
- LD_WriteReg(0xBD, 0x20);
- LD_WriteReg(0x08, 0x01);
- __delay_cycles(20000);
- LD_WriteReg(0x08, 0x00);
- __delay_cycles(20000);
- if(LD_Check_ASRBusyFlag_b2() == 0)
- {
- return 0;
- }
- LD_WriteReg(0xB2, 0xff);
- LD_WriteReg(0x37, 0x06);
-
- __delay_cycles(20000);
- LD_WriteReg(0x1C, 0x0b); //配置麦克风做为输入信号
- //LD_WriteReg(0x1C, 0x07); //配置双通道音频信号做为输入信号
- LD_WriteReg(0x29, 0x10);
-
- LD_WriteReg(0xBD, 0x00);
- //EX0=1;
- return 1;
- }
- /************************************************************************
- 功能描述:添加关键词,此处为用户修改部分
- 调用函数:
- 入口参数:
- 返回函数:
- **************************************************************************/
- // Return 1: success.
- // 添加识别关键词语,开发者可以学习"语音识别芯片LD3320高阶秘籍.pdf"中关于垃圾词语吸收错误的用法
- unsigned char LD_AsrAddFixed()
- {
- unsigned char k, flag;
- uint8 nAsrAddLength;
- //uint8 code sRecog[4][20] = {"","kai fa ban yan zheng", "dai ma ce shi","jian cha wan bi"}; /*添加关键词,用户修改*/
- unsigned char sRecog[4][20] = {"","wo shi", "ce shi cheng gong ","jian yan wan bi"};
- unsigned char pCode[4] = {0,CODE_KFBYZ, CODE_DMCS, CODE_JCWB}; /*添加识别码,用户修改*/
- flag = 1;
- for (k=0; k<4; k++)
- {
-
- if(LD_Check_ASRBusyFlag_b2() == 0)
- {
- flag = 0;
- break;
- }
-
- LD_WriteReg(0xc1, pCode[k] );
- LD_WriteReg(0xc3, 0 );
- LD_WriteReg(0x08, 0x04);
- __delay_cycles(2000);
- LD_WriteReg(0x08, 0x00);
- __delay_cycles(2000);
- for (nAsrAddLength=0; nAsrAddLength<20; nAsrAddLength++)
- {
- if (sRecog[k][nAsrAddLength] == 0)
- break;
- LD_WriteReg(0x5, sRecog[k][nAsrAddLength]);
- }
- LD_WriteReg(0xb9, nAsrAddLength);
- LD_WriteReg(0xb2, 0xff);
- LD_WriteReg(0x37, 0x04);
- }
- return flag;
- }
- /************************************************************************************/
- // RunASR()函数实现了一次完整的ASR语音识别流程
- // LD_Init_ASR() 函数实现了ASR初始化
- // LD_AsrAddFixed() 函数实现了添加关键词语到LD3320芯片中
- // LD_AsrRun() 函数启动了一次ASR语音识别流程
- //
- // 任何一次ASR识别流程,都需要按照这个顺序,从初始化开始进行
- /************************************************************************************/
- unsigned char RunASR()
- {
- unsigned char i=0;
- unsigned char asrflag=0;
- for (i=0; i<5; i++) // 防止由于硬件原因导致LD3320芯片工作不正常,所以一共尝试5次启动ASR识别流程
- {
- LD_AsrStart();
- __delay_cycles(20000);
- if (LD_AsrAddFixed()==0)
- {
- LD_reset(); // LD3320芯片内部出现不正常,立即重启LD3320芯片
- __delay_cycles(20000); // 并从初始化开始重新ASR识别流程
- continue;
- }
- __delay_cycles(20000);
- if (LD_AsrRun() == 0)
- {
- LD_reset(); // LD3320芯片内部出现不正常,立即重启LD3320芯片
- __delay_cycles(20000); // 并从初始化开始重新ASR识别流程
- continue;
- }
- asrflag=1;
- break; // ASR流程启动成功,退出当前for循环。开始等待LD3320送出的中断信号
- }
- return asrflag;
- }
- /************************************************************************
- 功能描述: 获取识别结构
- 调用函数:
- 入口参数:
- 返回函数:
- **************************************************************************/
- unsigned char LD_GetResult()
- {
- return LD_ReadReg(0xc5 );
- }
- void ProcessInt0(void)
- {
- uint8 nAsrResCount=0;
- //EX0=0;
- ucRegVal = LD_ReadReg(0x2B);
- LD_WriteReg(0x29,0) ;
- LD_WriteReg(0x02,0) ;
- if((ucRegVal & 0x10) &&
- LD_ReadReg(0xb2)==0x21 &&
- LD_ReadReg(0xbf)==0x35) /*识别成功*/
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
MSP430 源程序.zip
(194.56 KB, 下载次数: 16)
|