SYN6288中文语音合成 程序

51单片机比较麻烦些,用到串口,因为会被占用,所以每次下载程序都要重新拔插。

注意接收和发送两头别接同错了。

#include <reg51.h>
#include <stdio.h>

#define uchar unsigned char
#define uint unsigned int

//*****************************************************
sbit BUSY = P1^7;        //开发板上SYN6288的BUSY引脚固定连接到CPU的P3.7端口 发送接3.2,接受3.1
uchar nBkm = 0x00;

//*****************************************************

#define HEADLEN       5  //数据包头的长度
#define BKM_OFFSET    4  //背景音乐命令偏移
#define LEN_OFFSET    2  //长度字节的偏移量(一般不会超过255字节,因此只使用1字节长度)
#define BKM_MAX      15	 //背景音乐数量

//*****************************************************

//数据包头(0xFD + 2字节长度 + 1字节命令字 + 1字节命令参数)
uchar head[HEADLEN] = {0xfd,0x00,0x00,0x01,0x00};

//****************************************************

//延时
void Delay(uint z)
{

	uint x,y;
for(x=z; x>0; x--)
  for(y=920; y>0; y--);   
}

//串口初始化,开发板晶体频率固定为22.1184MHz,SYN6288波特率默认为9600bps。
//采用其他CPU语自行修正串口初始化功能。
void UART_Init(void)
{
	SCON=0xD8 ;   
	TMOD=0x20 ;
	PCON=0x00 ; 	 
	TH1 = 0xFD;	 //晶振为11.059MHZ时,设定串口波特率为9600bit/s,方式3 
	//TH1=0xFA ;   //晶振为22.1184MHZ时,设定串口波特率为9600bit/s,方式3  
	TR1=1;	
}

//串口发送数据
void SendChar(uchar n)
{
	SBUF = n;
	while (TI==0);//发送数据
	TI=0;
}				

//背景音乐(参数为0表示关闭背景音乐)
void BkMusic(uchar num)
{
	num %= BKM_MAX + 1;
	nBkm = num;
}

//发声程序
void Speech(uchar *buf)
{
	uchar i = 0;          //循环计数变量
	uchar xor = 0x00;     //校验码初始化
	uchar ch = 0x00;
    uchar len = 0x00;

    while(buf[len++]);

	//发送数据包头(0xFD + 2字节长度 + 1字节命令字 + 1字节命令参数)
	for(i = 0; i < HEADLEN; i++)
	{
		if(i == BKM_OFFSET)
			ch = nBkm << 3; //写入背景音乐
		else if(i == LEN_OFFSET)
			ch = len + 3;
		else
			ch = head[i];

		xor ^= ch;
		SendChar(ch);
		Delay(1);
   	}

	//发送文字内容
	for(i = 0; i < len; i++)
	{
		xor ^= buf[i];
		SendChar(buf[i]);
		Delay(1);
	}

	SendChar(xor);         //发送校验位

	Delay(50);
	//while(BUSY);         //等待合成结束
	Delay(50);

}

//主函数
void main()
{
	uchar nBkm = 0x01;  //演示背景音乐编号

	UART_Init();        //初始化串口为 9600bps   
	
	BkMusic(0);         //关闭背景音乐
	Delay(50);

  
	Speech("呵呵");
	Delay(2000);   
	while(1);
}

猜你喜欢

转载自blog.csdn.net/zy19981110/article/details/89947596