基于STM32F1的语音合成芯片SYN6288驱动

说明

基于USART2制作,封装了各种通信协议

SYN6288.h

#ifndef _SYN6288_H_
#define _SYN6288_H_
#include "sys.h"
#include "vTime.h"
/**  
  ******************************************************************************
  *  @File     SYN6288.h
  *  @Author   Velscode  
  *  @Email    velscode@gmail.com
  *  @Brief    TTS 芯片 SYN6288驱动头文件(基于STM32F10x)
  *            使用了USART2(A2\A3)
  ******************************************************************************
  */
  
  void SYN6288_Init(unsigned int bound);	
  void SYN6288_Send_Byte( unsigned char byte );
  void SYN6288_Speech( char * str );
  void SYN6288_Speech_Time( struct vTime nt );
  void SYN6288_Set_Volume( int volume );
  void SYN6288_Play_Sound( int id );
  void SYN6288_Play_Msg( int id );
  void SYN6288_Play_Ring( int id );

#endif /*_SYN6288_H_*/
/* End of File ------------------------------------------------------------- */

SYN6288.c

/**
  ******************************************************************************
  *  @File     SYN6288.c
  *  @Author   Velscode  
  *  @Email    velscode@gmail.com
  *  @Brief    TTS 芯片 SYN6288驱动源代码文件(基于STM32F10x)
  *            使用了USART2(A2\A3)
  ******************************************************************************
  */
  
  /* Internal Function Declaration ------------------------------------------- */
  void usart2_Init(unsigned int bound);
  
  /* Header Files ------------------------------------------------------------ */
  #include "SYN6288.h"
  #include "string.h"
  #include "delay.h"
  
	void SYN6288_Play_Ring( int id )
	{
		char str[6] = "ringx";
		
		if( id >= 25 )
			id = 25;
		if( id <= 0 )
			id = 0;
		
		str[4] = 'a'+ id;
		
		SYN6288_Speech(str);
		delay_ms(16);
	}	
  
    //播放和弦提示音
	void SYN6288_Play_Msg( int id )
	{
		char str[5] = "msgx";
		
		if( id >= 8 )
			id = 8;
		if( id <= 0 )
			id = 0;
		
		str[3] = 'a'+ id;
		
		SYN6288_Speech(str);
		delay_ms(16);
	}
  
	//播放提示音
	void SYN6288_Play_Sound( int id )
	{
		char str[7] = "soundx";
		
		if( id >= 25 )
			id = 25;
		if( id <= 0 )
			id = 0;
		
		str[5] = 'a'+ id;
		
		SYN6288_Speech(str);
	}
		
	//音量控制,16级可调
	void SYN6288_Set_Volume( int volume )
	{
		char str[6]="[vxx]";
		
		if( volume >= 16 )
			volume = 16;
		if( volume <= 0 )
			volume = 0;
		
		str[2] = volume/10 +'0';
		str[3] = volume%10 +'0';
		
		SYN6288_Speech(str);
		delay_ms(16);
	}
	//播报时间已完成对整点的转换支持
	void SYN6288_Speech_Time( struct vTime nt )
	{	
		//							12  16  20  24  28  32   40
		char str[50] = "现在时间是20xx年xx月xx日xx时xx分xx秒星期x";
		
		str[12] = (nt.year/10)+'0';
		str[13] = (nt.year%10)+'0';
		
		str[16] = (nt.month/10)+'0';
		str[17] = (nt.month%10)+'0';
		
		str[20] = (nt.day/10)+'0';
		str[21] = (nt.day%10)+'0';
		
		str[24] = (nt.hour/10)+'0';
		str[25] = (nt.hour%10)+'0';
		
		if( nt.minute == 0 && nt.second == 0 )
		{
			str[28] = 0;
			strcat( str, "整" );
		}
		else
		{
			str[28] = (nt.minute/10)+'0';
			str[29] = (nt.minute%10)+'0';
			
			str[32] = (nt.second/10)+'0';
			str[33] = (nt.second%10)+'0';
			
			str[40] = (nt.week)+'0';
		}
		
		SYN6288_Speech(str);
		delay_ms(16);
	}
  
    //语音合成
	void SYN6288_Speech( char * str )
	{
		char * p = str;
		int len = 0,check=0xFD,i;

		while( *p++ != 0 )
		{
			len++;
		}
		
		len+=3;
		
		SYN6288_Send_Byte(0xFD);
		
		SYN6288_Send_Byte( len / 256 );
		SYN6288_Send_Byte( len % 256 );
		check  = check ^ ( len / 256 ) ^ ( len % 256 );
		
		SYN6288_Send_Byte( 0x01 );
		SYN6288_Send_Byte( 0x01 );
		check = check ^ 0x01 ^ 0x01;
		
		for( i = 0; i < len-3; i++ )
		{
			SYN6288_Send_Byte(*str);
			check ^= ( *str );
			str++;
		}
		SYN6288_Send_Byte(check);	
		delay_ms(16);
	}


	//其实是初始化USART2
	void SYN6288_Init(unsigned int bound)
	{
		NVIC_InitTypeDef NVIC_InitStructure;
		GPIO_InitTypeDef GPIO_InitStructure;
		USART_InitTypeDef USART_InitStructure;

		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	// GPIOA时钟
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //串口2时钟使能

		USART_DeInit(USART2);  //复位串口2
		 //USART2_TX   PA2
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA2
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	//复用推挽输出
		GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA2

		//USART2_RX	  PA3
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
		GPIO_Init(GPIOB, &GPIO_InitStructure);  //初始化PA3

		USART_InitStructure.USART_BaudRate = bound;//波特率设置
		USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
		USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
		USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
		USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
		USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;	//收发模式

		USART_Init(USART2, &USART_InitStructure); //初始化串口2


		USART_Cmd(USART2, ENABLE);                    //使能串口 

		//使能接收中断
		USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启中断   

		//设置中断优先级
		NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//抢占优先级3
		NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;		//子优先级3
		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能
		NVIC_Init(&NVIC_InitStructure);	//根据指定的参数初始化VIC寄存器
	}
	
	//其实是USART2_Send_Byte
	void SYN6288_Send_Byte( unsigned char byte )
	{
		while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
			USART_SendData(USART2,byte); 
	}

  
  /* End of File ------------------------------------------------------------- */

posted @ 2018-12-11 21:46  Velscode  阅读(2343)  评论(0编辑  收藏  举报