STM32实现的语音识别的智能垃圾桶-LD3320语音识别篇
- main.h
- LD3320.h
- LD3320.c
- main.c
- Reg.h
- Reg.c
- STC11XX.h
- usart.h
- usart.c
main.h
#ifndef _MAIN_H
#define _MAIN_H
typedef signed char int8_t;
typedef signed short int int16_t;
typedef signed int int32_t;
typedef signed long int int64_t;
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long int uint64_t;
#define ENABLE 1
#define DISABLE 0
#include <intrins.h>
#include "STC11XX.h"
#include "LD3320.h"
#include "Reg.h"
#include "usart.h"
sbit LED=P4^2;
#define TEST
#endif
LD3320.h
#ifndef LD_CHIP_H
#define LD_CHIP_H
#define uint8 unsigned char
#define uint16 unsigned int
#define uint32 unsigned long
#define LD_ASR_NONE 0x00
#define LD_ASR_RUNING 0x01
#define LD_ASR_FOUNDOK 0x10
#define LD_ASR_FOUNDZERO 0x11
#define LD_ASR_ERROR 0x31
#define CLK_IN 22.1184
#define LD_PLL_11 (uint8)((CLK_IN/2.0)-1)
#define LD_PLL_MP3_19 0x0f
#define LD_PLL_MP3_1B 0x18
#define LD_PLL_MP3_1D (uint8)(((90.0*((LD_PLL_11)+1))/(CLK_IN))-1)
#define LD_PLL_ASR_19 (uint8)(CLK_IN*32.0/(LD_PLL_11+1)-0.51)
#define LD_PLL_ASR_1B 0x48
#define LD_PLL_ASR_1D 0x1f
void LD_Reset();
void LD_Init_Common();
void LD_Init_ASR();
uint8 RunASR(void);
void LD_AsrStart();
uint8 LD_AsrRun();
uint8 LD_AsrAddFixed();
uint8 LD_GetResult();
#define CODE_START 0
#define CODE_CMD1 1
#define CODE_CMD2 2
#define CODE_CMD3 3
#define CODE_CMD4 4
#define CODE_CMD5 5
#define CODE_CMD6 6
#define CODE_CMD7 7
#define CODE_CMD8 8
#define CODE_CMD9 9
#define CODE_CMD10 10
#define CODE_CMD11 11
#define CODE_CMD12 12
#define CODE_CMD13 13
#define CODE_CMD14 14
#define CODE_CMD15 15
#define CODE_CMD16 16
#define CODE_CMD17 17
#define CODE_CMD18 18
#define CODE_CMD19 19
#define CODE_CMD20 20
#define CODE_CMD21 21
#define CODE_CMD22 22
#define CODE_CMD23 23
#define CODE_CMD24 24
#define CODE_CMD25 25
#define CODE_CMD26 26
#define CODE_CMD27 27
#define CODE_CMD28 28
#define CODE_CMD29 29
#define CODE_CMD30 30
#define CODE_CMD31 31
#define CODE_CMD32 32
#define CODE_CMD33 33
#define CODE_CMD34 34
#define CODE_CMD35 35
#define CODE_CMD36 36
#define CODE_CMD37 37
#define CODE_CMD38 38
#define CODE_CMD39 39
#define CODE_CMD40 40
#define MIC_VOL 0x55
#endif
LD3320.c
#include "main.h"
extern unsigned char idata nAsrStatus;
extern void delay(unsigned long uldata);
unsigned char idata ucRegVal;
void ProcessInt0(void);
void LD_Reset()
{
RSTB=1;
delay(5);
RSTB=0;
delay(5);
RSTB=1;
delay(5);
CSB=0;
delay(5);
CSB=1;
delay(5);
}
void LD_Init_Common()
{
LD_ReadReg(0x06);
LD_WriteReg(0x17, 0x35);
delay(10);
LD_ReadReg(0x06);
LD_WriteReg(0x89, 0x03);
delay(5);
LD_WriteReg(0xCF, 0x43);
delay(5);
LD_WriteReg(0xCB, 0x02);
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(10);
LD_WriteReg(0xCD, 0x04);
delay(5);
LD_WriteReg(0xB9, 0x00);
LD_WriteReg(0xCF, 0x4F);
LD_WriteReg(0x6F, 0xFF);
}
void LD_Init_ASR()
{
LD_Init_Common();
LD_WriteReg(0xBD,0x00);
LD_WriteReg(0x17,0x48);
delay(10);
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(1);
}
void ProcessInt0(void)
{
unsigned char 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)
{
nAsrResCount=LD_ReadReg(0xba);
if(nAsrResCount>0&&nAsrResCount<=4)
{
nAsrStatus=LD_ASR_FOUNDOK;
}
else
{
nAsrStatus=LD_ASR_FOUNDZERO;
}
}
else
{
nAsrStatus=LD_ASR_FOUNDZERO;
}
LD_WriteReg(0x2b,0);
LD_WriteReg(0x1C,0);
LD_WriteReg(0x29,0);
LD_WriteReg(0x02,0);
LD_WriteReg(0x2B,0);
LD_WriteReg(0xBA,0);
LD_WriteReg(0xBC,0);
LD_WriteReg(0x08,1);
LD_WriteReg(0x08,0);
EX0=1;
}
unsigned char RunASR(void)
{
unsigned char i=0;
unsigned char asrflag=0;
for(i=0;i<5;i++)
{
LD_AsrStart();
delay(50);
if(LD_AsrAddFixed()==0)
{
LD_Reset();
delay(50);
continue;
}
delay(10);
if(LD_AsrRun()== 0)
{
LD_Reset();
delay(50);
continue;
}
asrflag=1;
break;
}
return asrflag;
}
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(10);
}
return flag;
}
void LD_AsrStart()
{
LD_Init_ASR();
}
unsigned char LD_AsrRun()
{
EX0=0;
LD_WriteReg(0x35,MIC_VOL);
LD_WriteReg(0x1C,0x09);
LD_WriteReg(0xBD,0x20);
LD_WriteReg(0x08,0x01);
delay(1);
LD_WriteReg(0x08, 0x00);
delay(1);
if(LD_Check_ASRBusyFlag_b2()==0)
{
return 0;
}
LD_WriteReg(0x1C,0x0b);
LD_WriteReg(0xB2,0xff);
delay(1);
LD_WriteReg(0x37,0x06);
delay(1);
LD_WriteReg(0x37,0x06);
delay(5);
LD_WriteReg(0x29,0x10);
LD_WriteReg(0xBD,0x00);
EX0=1;
return 1;
}
unsigned char LD_AsrAddFixed()
{
unsigned char k, flag;
unsigned char nAsrAddLength;
#define DATE_A 41
#define DATE_B 30
unsigned char code sRecog[DATE_A][DATE_B]={
"xiao jie",\
"can jin zhi",\
"shi zhi jin",\
"bao xian mo",\
"su liao dai",\
"yi ci xing can ju",\
"zhen tou",\
"da huo ji",\
"tao ci zhi pin",\
"fei jiu bi xin",\
"hai mian",\
"shou ji dian chi",\
"deng pao",\
"sha chong ji",\
"xiao du ji",\
"lao shu yao",\
"you qi tong",\
"shui yin wen du ji",\
"guo qi yao pin",\
"fei nong yao",\
"fei deng guan",\
"shi wu can zha",\
"guo qi shi pin",\
"gu tou",\
"lan cai ye",\
"sheng fan",\
"guo pi",\
"fan qie jiang",\
"tiao wei liao",\
"hua sheng ke",\
"cha ye zha",\
"zhi he",\
"yin liao ping",\
"bo li ping",\
"su liao tong",\
"pao mo",\
"su liao",\
"jin shu",\
"bao zhi",\
"yi la guan",\
"tie ding"
};
unsigned char code pCode[DATE_A]={
CODE_START,\
CODE_CMD1,\
CODE_CMD2,\
CODE_CMD3,\
CODE_CMD4,\
CODE_CMD5,\
CODE_CMD6,\
CODE_CMD7,\
CODE_CMD8,\
CODE_CMD9,\
CODE_CMD10,\
CODE_CMD11,\
CODE_CMD12,\
CODE_CMD13,\
CODE_CMD14,\
CODE_CMD15,\
CODE_CMD16,\
CODE_CMD17,\
CODE_CMD18,\
CODE_CMD19,\
CODE_CMD20,\
CODE_CMD21,\
CODE_CMD22,\
CODE_CMD23,\
CODE_CMD24,\
CODE_CMD25,\
CODE_CMD26,\
CODE_CMD27,\
CODE_CMD28,\
CODE_CMD29,\
CODE_CMD30,\
CODE_CMD31,\
CODE_CMD32,\
CODE_CMD33,\
CODE_CMD34,\
CODE_CMD35,\
CODE_CMD36,\
CODE_CMD37,\
CODE_CMD38,\
CODE_CMD39,\
CODE_CMD40
};
flag=1;
for(k=0;k<DATE_A;k++)
{
if(LD_Check_ASRBusyFlag_b2()==0)
{
flag=0;
break;
}
LD_WriteReg(0xc1,pCode[k]);
LD_WriteReg(0xc3,0);
LD_WriteReg(0x08,0x04);
delay(1);
LD_WriteReg(0x08,0x00);
delay(1);
for(nAsrAddLength=0;nAsrAddLength<DATE_B;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;
}
unsigned char LD_GetResult()
{
return LD_ReadReg(0xc5);
}
main.c
#include "main.h"
unsigned char idata nAsrStatus=0;
unsigned char G0_flag=DISABLE;
void MCU_init();
void ProcessInt0();
void delay(unsigned long uldata);
void User_handle(unsigned char dat);
void delay200ms();
void Led_test(void);
void main(void)
{
unsigned char idata nAsrRes;
unsigned char i=0;
Led_test();
MCU_init();
LD_Reset();
UartIni();
nAsrStatus=LD_ASR_NONE;
while(1)
{
switch(nAsrStatus)
{
case LD_ASR_RUNING:
case LD_ASR_ERROR:break;
case LD_ASR_NONE:
{
nAsrStatus=LD_ASR_RUNING;
if(RunASR()==0)
{
nAsrStatus = LD_ASR_ERROR;
}
break;
}
case LD_ASR_FOUNDOK:
{
nAsrRes = LD_GetResult();
User_handle(nAsrRes);
nAsrStatus = LD_ASR_NONE;
break;
}
case LD_ASR_FOUNDZERO:
default:
{
nAsrStatus = LD_ASR_NONE;
break;
}
}
}
}
void Led_test(void)
{
LED=~ LED;
delay200ms();
LED=~ LED;
delay200ms();
LED=~ LED;
delay200ms();
LED=~ LED;
delay200ms();
LED=~ LED;
delay200ms();
LED=~ LED;
}
void MCU_init()
{
P0=0xff;
P1=0xff;
P2=0xff;
P3=0xff;
P4=0xff;
P1M0=0xff;
P1M1=0x00;
LD_MODE=0;
IE0=1;
EX0=1;
EA=1;
}
void delay200us(void)
{
unsigned char a,b;
for(b=1;b>0;b--)
for(a=97;a>0;a--);
}
void delay(unsigned long uldata)
{
unsigned int j=0;
unsigned int g=0;
while(uldata--)
delay200us();
}
void delay200ms(void)
{
unsigned char a,b,c;
for(c=4;c>0;c--)
for(b=116;b>0;b--)
for(a=214;a>0;a--);
_nop_();
}
void ExtInt0Handler(void) interrupt 0
{
ProcessInt0();
}
void User_handle(uint8 dat)
{
UARTSendByte(dat);
if(0==dat)
{
G0_flag=ENABLE;
LED=0;
}
else if(ENABLE==G0_flag)
{
G0_flag=DISABLE;
LED=1;
}
else
{
}
}
Reg.h
#ifndef REG_RW_H
#define REG_RW_H
sbit LD_MODE=P4^3;
sbit RSTB=P3^5;
sbit CSB=P2^1;
void LD_WriteReg(unsigned char address, unsigned char dataout);
unsigned char LD_ReadReg(unsigned char address);
#endif
Reg.c
#include "main.h"
#define LD_INDEX_PORT (*((volatile unsigned char xdata*)(0x8100)))
#define LD_DATA_PORT (*((volatile unsigned char xdata*)(0x8000)))
void LD_WriteReg( unsigned char address,unsigned char dataout )
{
LD_INDEX_PORT=address;
LD_DATA_PORT=dataout;
}
unsigned char LD_ReadReg(unsigned char address )
{
LD_INDEX_PORT=address;
return (unsigned char)LD_DATA_PORT;
}
STC11XX.h
sfr ACC = 0xE0;
sfr B = 0xF0;
sfr PSW = 0xD0;
sbit CY = PSW^7;
sbit AC = PSW^6;
sbit F0 = PSW^5;
sbit RS1 = PSW^4;
sbit RS0 = PSW^3;
sbit OV = PSW^2;
sbit P = PSW^0;
sfr SP = 0x81;
sfr DPL = 0x82;
sfr DPH = 0x83;
sfr PCON = 0x87;
sfr AUXR = 0x8E;
sfr AUXR1 = 0xA2;
sfr WAKE_CLKO = 0x8F;
sfr CLK_DIV = 0x97;
sfr BUS_SPEED = 0xA1;
sfr IE = 0xA8;
sbit EA = IE^7;
sbit ELVD = IE^6;
sbit EADC = IE^5;
sbit ES = IE^4;
sbit ET1 = IE^3;
sbit EX1 = IE^2;
sbit ET0 = IE^1;
sbit EX0 = IE^0;
sfr IE2 = 0xAF;
sfr IP = 0xB8;
sbit PPCA = IP^7;
sbit PLVD = IP^6;
sbit PADC = IP^5;
sbit PS = IP^4;
sbit PT1 = IP^3;
sbit PX1 = IP^2;
sbit PT0 = IP^1;
sbit PX0 = IP^0;
sfr IPH = 0xB7;
sfr IP2 = 0xB5;
sfr IPH2 = 0xB6;
sfr P0 = 0x80;
sfr P0M0 = 0x94;
sfr P0M1 = 0x93;
sfr P1 = 0x90;
sfr P1M0 = 0x92;
sfr P1M1 = 0x91;
sfr P1ASF = 0x9D;
sfr P2 = 0xA0;
sfr P2M0 = 0x96;
sfr P2M1 = 0x95;
sfr P3 = 0xB0;
sfr P3M0 = 0xB2;
sfr P3M1 = 0xB1;
sfr P4 = 0xC0;
sfr P4M0 = 0xB4;
sfr P4M1 = 0xB3;
sfr P4SW = 0xBB;
sfr P5 = 0xC8;
sfr P5M0 = 0xCA;
sfr P5M1 = 0xC9;
sfr TCON = 0x88;
sbit TF1 = TCON^7;
sbit TR1 = TCON^6;
sbit TF0 = TCON^5;
sbit TR0 = TCON^4;
sbit IE1 = TCON^3;
sbit IT1 = TCON^2;
sbit IE0 = TCON^1;
sbit IT0 = TCON^0;
sfr TMOD = 0x89;
sfr TL0 = 0x8A;
sfr TH0 = 0x8C;
sfr TL1 = 0x8B;
sfr TH1 = 0x8D;
sfr SCON = 0x98;
sbit SM0 = SCON^7;
sbit SM1 = SCON^6;
sbit SM2 = SCON^5;
sbit REN = SCON^4;
sbit TB8 = SCON^3;
sbit RB8 = SCON^2;
sbit TI = SCON^1;
sbit RI = SCON^0;
sfr SBUF = 0x99;
sfr SADEN = 0xB9;
sfr SADDR = 0xA9;
sfr S2CON = 0x9A;
sfr S2BUF = 0x9B;
sfr BRT = 0x9C;
sfr WDT_CONTR = 0xC1;
sfr CCON = 0xD8;
sbit CF = CCON^7;
sbit CR = CCON^6;
sbit CCF1 = CCON^1;
sbit CCF0 = CCON^0;
sfr CMOD = 0xD9;
sfr CL = 0xE9;
sfr CH = 0xF9;
sfr CCAPM0 = 0xDA;
sfr CCAPM1 = 0xDB;
sfr CCAP0L = 0xEA;
sfr CCAP0H = 0xFA;
sfr CCAP1L = 0xEB;
sfr CCAP1H = 0xFB;
sfr PCA_PWM0 = 0xF2;
sfr PCA_PWM1 = 0xF3;
sfr ADC_CONTR = 0xBC;
sfr ADC_RES = 0xBD;
sfr ADC_RESL = 0xBE;
sfr SPCTL = 0xCE;
sfr SPSTAT = 0xCD;
sfr SPDAT = 0xCF;
sfr IAP_DATA = 0xC2;
sfr IAP_ADDRH = 0xC3;
sfr IAP_ADDRL = 0xC4;
sfr IAP_CMD = 0xC5;
sfr IAP_TRIG = 0xC6;
sfr IAP_CONTR = 0xC7;
usart.h
#ifndef __USART_H
#define __USART_H
void UartIni(void);
void UARTSendByte(uint8_t DAT);
void PrintCom(uint8_t *DAT);
#endif
usart.c
#include "main.h"
#define FOSC 22118400L
uint32_t baud=9600;
void UartIni(void)
{
SCON=0x50;
TMOD=0x20;
TH1=TL1=-(FOSC/12/32/baud);
TR1=1;
ES=1;
EA=1;
}
void Uart_Isr() interrupt 4 using 1
{
if(RI)
{
}
}
void UARTSendByte(uint8_t DAT)
{
ES=0;
TI=0;
SBUF=DAT;
while(TI==0);
TI=0;
ES=1;
}