|
单片机驱动语音模块实现定点播放语音- /*************************************************
- 功 能:STC12C5A60S2驱动ISD1760语音模块
- 单片机:STC12C5A60S2
- 晶 振:11.0592M
- 作 者:苏义江根据1730改编
- 时 间:2016-8-4
- 说 明:单片机4个IO口接1760的SS,SCLK,MOSI,MOSO.
- 地址从0x0010开始安自己所录得语音长度调节
- 通过按键或传感器定点播放'你好欢迎光临。你好请避让。
- 这是你点的菜谢谢。欢迎再次光临再见'
- ***************************************************/
- #include<stc12c5a60s2.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define PU 0x01// 上电
- #define STOP 0x02//停止
- #define RESET 0x03//复位
- #define CLI_INT 0x04//清除EOM标志和中断信号
- #define RD_STATUS 0x05//返回状态寄存器内容
- //在1,2字节返回状态寄存器信息,2,3字节返回放音指针
- #define RD_PLAY_PTR 0x06
- #define PD 0x07//芯片下电
- //在1,2字节返回状态寄存器信息,2,3字节返回录音指针
- #define RD_REC_PTR 0x08
- #define RD_DEVID 0x09//读取芯片ID号码
- #define PLAY 0x40 //从当前地址放音
- #define REC 0x41 //从当前地址录音
- #define ERASE 0x42 //擦除当前语音段
- #define G_ERASE 0x43 //擦除芯片所有语音
- //返回状态寄存器信息在1,2字节,APC寄存器信息在3字节
- #define RD_APC 0x44
- //将<D11:D0>写入APC寄存器
- #define WR_APC1 0x45
- #define WR_NVCFG 0x46//将APC内容写入NVCFG
- #define LD_NVCFG 0x47 //将NVCFG内容写入APC
- #define FWD 0x48 //将放音指针指向下一段语音地址处
- #define CHK_MEM 0x49//检查环状存储体系
- #define EXTCLK 0x4a //外部时钟使能
- //将<D11:D0>写入APC寄存器,并由<D2:D0>位控制音量
- #define WR_APC2 0x65
- #define SET_PLAY 0x80//从起始地址<S10:S0>放音到结束地址<E10:E0>结束
- #define SET_REC 0x81//从起始地址<S10:S0>录音到结束地址<E10:E0>结束
- #define SET_ERASE 0x82//从起始地址<S10:S0>擦除到结束地址<E10:E0>结束
- #define HIGH 1
- #define LOW 0
- #define NOP _nop_()//定义除命令外剩余字节个数
- #define Surplus_One_BYTE 1 //定义字节个数为4
- #define Surplus_Two_BYTE 2 //定义字节个数为2
- #define Surplus_Three_BYTE 3 //定义字节个数为3
- #define ENISD1760_SS ISD1760_SS=0
- #define DISENISD1760_SS ISD1760_SS=1
- #define ISD1760_MISO_L ISD1760_MISO=0
- #define ISD1760_MISO_H ISD1760_MISO=1
- #define ISD1760_MOSI_L ISD1760_MOSI=0
- #define ISD1760_MOSI_H ISD1760_MOSI=1
- #define ISD1760_SCLK_L ISD1760_SCLK=0
- #define ISD1760_SCLK_H ISD1760_SCLK=1
- ////接收1730内部返回的数
- uchar SR0H; // bdata
- uchar SR0L; // bdata
- uchar SR1; // bdata
- uchar SR2;
- sbit ISD1760_SS = P1^3;
- sbit ISD1760_SCLK= P1^2;
- sbit ISD1760_MOSI= P1^1;
- sbit ISD1760_MISO= P1^0;
- sbit led =P3^7;
- sbit s1=P3^0;
- sbit s2=P3^1;
- sbit s3=P2^6;
- sbit s4=P2^7;
- volatile uchar ISD1760_Spio(volatile uchar dat) ;
- void ISD1760_Write_APC2(void) ;
- void ISD1760_delay(uint Count)
- {
- uint x,y;
- for(x=Count;x>0;x--)
- for(y=420;y>0;y--);
- }
- //功能:ISD1730 SPI驱动函数
- //输入:dat:要传送到ISD1730的数据
- //返回:receive:接收的数据
- static uchar ISD1760_Spio(uchar dat)
- {
- uchar i;
- // uchar recevice;
- ISD1760_SCLK_L; //SCLK==0
- ENISD1760_SS ;//为低选择当前设备,并开启SPI
- for (i=0; i<8; i++)
- //将dat字节的8个位数据一一送到MOSI线上
- {
- ISD1760_SCLK_L;
- //先将SCLK拉低
- // if(dat&0x01==0x01)
- if(dat&0x01)//先发低位
- {
- ISD1760_MOSI_H; //输入到1760 上升沿输入
- }
- else
- {
- ISD1760_MOSI_L;
- //MOSI和MISO信号同时改变数据,但有前后关系如:左程序
- }
- dat>>=1;
- // ISD1760_delay(1);
- // ISD1760_SCLK_H;
- // ISD1760_delay(1);
- // recevice>>=1;
- if(ISD1760_MISO == 1)
- {
- dat|=0x80;
- }
- ISD1760_SCLK_H;
- // ISD1760_SCLK_L;
- //SCLK来一个上升沿锁存MOSI信号
- }
- ISD1760_MOSI_L;
- //非忙线时MOSI要拉低
- return(dat);
- }
- //功能:ISD1730上电操作
- static void ISD1760_PU(void)
- {
- DISENISD1760_SS;
- ISD1760_delay(1);
- ISD1760_SCLK_H;
- ISD1760_delay(1);
- ISD1760_MOSI_L;
- ISD1760_delay(1);
- ENISD1760_SS;
- ISD1760_delay(1);
- SR0L = ISD1760_Spio(PU); //发送命令字节
- SR0H = ISD1760_Spio(0x00); //发送数据字节1
- DISENISD1760_SS;
- ISD1760_delay(10);
- }
- //功能:写入APC参数
- void ISD1760_Write_APC2(void)
- {
- DISENISD1760_SS;
- ISD1760_delay(1);
- ISD1760_SCLK_H;
- ISD1760_delay(1);
- ISD1760_MOSI_L;
- ISD1760_delay(1);
- ENISD1760_SS;
- ISD1760_delay(1);
- //SPI总线准备好
- SR0L = ISD1760_Spio(WR_APC2); //发送命令字节
- SR0H = ISD1760_Spio(0x40); //发送数据字节1
- // SR0H = ISD1760_Spio(0x70); //发送数据字节1
- SR1 = ISD1760_Spio(0x0C);
-
- DISENISD1760_SS; //一个命令完毕
- ISD1760_delay(10);
- }
- //功 能: 取出当前语音的首末地址
- void GetToneAdd(uchar NUM, uint *ipStartAdd, uint *ipEndAdd)
- {
- uint caToneAdd[]={
- 0x0010,0x0020, 0x0021, 0x0031,0x0032,0x0042,
- 0x0043,0x0058
- //0x0010,0x00ff//
- };
- *ipStartAdd=caToneAdd[NUM*2];
- *ipEndAdd=caToneAdd[NUM*2+1];
- }
- //定点地址播放
- void ISD1760_Set_Play(uchar VOICE_NUM)
- {
- uint Add_ST, Add_ED; //ST起始地址,ED结束地址
- uchar Add_ST_H, Add_ST_L, Add_ED_H, Add_ED_L;
- //起始地址高8位,起始地址低8位,结束地址高8位,结束地址低8位
- GetToneAdd( VOICE_NUM, &Add_ST, &Add_ED);
- Add_ST_L=(uchar)(Add_ST&0x00ff);
- Add_ST_H=(uchar)((Add_ST>>8)&0x00ff);
- Add_ED_L=(uchar)(Add_ED&0x00ff);
- Add_ED_H=(uchar)((Add_ED>>8)&0x00ff);
- DISENISD1760_SS;
- ISD1760_delay(1);
- ISD1760_SCLK_H;
- ISD1760_delay(1);
- ISD1760_MOSI_L;
- ISD1760_delay(1);
- ENISD1760_SS;
- ISD1760_delay(1);
- //发送命令
- SR0L = ISD1760_Spio(SET_PLAY);
- //发送命令字节
- SR0H = ISD1760_Spio(0x00);
- //发送数据字节1
- SR1 = ISD1760_Spio(Add_ST_L);
- //发送数据字节2,放音起始地址低8位<S7:S0>
- SR2 = ISD1760_Spio(Add_ST_H);
- //发送数据字节3,放音起邕地址高3位<S10:S8>,ISD1730不用
- SR0H = ISD1760_Spio(Add_ED_L);
- //发送数据字节4,放音结束地址低8位<E7:E0>
- SR0H = ISD1760_Spio(Add_ED_H);
- //发送数据字节5,放音结束地址低8位<E10:E8>,ISD1730不用
- SR0H = ISD1760_Spio(0x00);
- //发送数据字节6 DISENISD1730_SS; //
- ISD1760_delay(10);
- }
- //功能:ISD1730下电命令
- void ISD1760_PD(void)
- {
- DISENISD1760_SS;
- ISD1760_delay(1);
- ISD1760_SCLK_H;
- ISD1760_delay(1);
- ISD1760_MOSI_L;
- ISD1760_delay(1);
- ENISD1760_SS;
- ISD1760_delay(1);
- SR0L = ISD1760_Spio(PD);
- //发送命令字节
- SR0H = ISD1760_Spio(0x00);
- //发送数据字节1
- DISENISD1760_SS;
- ISD1760_delay(10);
- }
- //功能:播放指定的语音信息
- //输入:voice_num,语音索引编号(第几段:0 第一段,1第二段。。。)
- void ISD1760_PLAY(uchar voice_name)
- {
- ISD1760_PU(); // delay10ms(5);
- ISD1760_delay(5);
- ISD1760_Set_Play(voice_name); // delay10ms(350);
- ISD1760_delay(50);
- ISD1760_PD(); // delay10ms(15);
- ISD1760_delay(5);
- }
- //功能: 停止
- void ISD_Stop(void)
- {
- ENISD1760_SS;
- ISD1760_Spio(STOP); //停止当前操作
- ISD1760_Spio(0x00);
- DISENISD1760_SS;
- ISD1760_delay(50);
- }
- //功能: 掉电
- void ISD_PD(void)
- {
- ENISD1760_SS;
- ISD1760_Spio(PD); //Power down 掉电
- ISD1760_Spio(0x00);
- DISENISD1760_SS;
- ISD1760_delay(50);
- }
- void keyscan()
- {
- if(s1==0)
- {
- ISD1760_delay(10);
- if(s1==0)
- {
- ISD1760_PLAY(0) ; //欢迎光临
-
- ISD_PD();
- ISD_Stop();
- led =~led;ISD1760_delay(3000);
- }
- }
- if(s2==0)
- {
- ISD1760_delay(10);
- if(s2==0)
- {
- ISD1760_PLAY(1) ;//你好请避让
-
- ISD_PD();
- ISD_Stop();
- led =~led;ISD1760_delay(3000);
- }
- }
- if(s3==0)
- {
- ISD1760_delay(10);
- if(s3==0)
- {
- ISD1760_PLAY(2) ; //你好这是你点的菜谢谢
-
- ISD_PD();
- ISD_Stop();
- led =~led;ISD1760_delay(3000);
- }
- }
- if(s4==0)
- {
- ISD1760_delay(10);
- if(s4==0)
- {
- ISD1760_PLAY(3) ; //欢迎再次光临再见
-
- ISD_PD();
- ISD_Stop();
- led =~led;ISD1760_delay(3000);
- }
- }
- }
- void main(void)
- {
- // ISD1760_Write_APC2(); //设置芯片模拟输入方式:模拟寄存器APC2
- while(1)
- {
- ISD1760_Write_APC2();
- keyscan();
- }
- }
复制代码
|
|