找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1620|回复: 0
打印 上一主题 下一主题
收起左侧

STM32+RT+SYN8266播报+红外定时器程序

[复制链接]
跳转到指定楼层
楼主
老规矩,附上产品图


SYN8266配置文件.c
  1. /**
  2. ******************************************************************************
  3. *  @File     SYN6288.c
  4. *  @Author   Velscode  
  5. *  @Brief    TTS 芯片 SYN6288驱动源代码文件(基于STM32F10x)
  6. *            使用了USART2(A2\A3)
  7. ******************************************************************************
  8. */

  9. /* Internal Function Declaration ------------------------------------------- */
  10. void usart2_Init(unsigned int bound);

  11. /* Header Files ------------------------------------------------------------ */
  12. #include "SYN6288.h"
  13. #include "string.h"
  14. /* RT-Thread相关头文件 */
  15. #include <rthw.h>
  16. #include <rtthread.h>
  17. #include <string.h>
  18. struct  STRUCT_USARTx_Fram strUSART_Fram_Record = { 0 };

  19. /**
  20.   * @brief  配置嵌套向量中断控制器NVIC
  21.   * @param  无
  22.   * @retval 无
  23.   */
  24. static void NVIC_Configuration(void)
  25. {
  26.   NVIC_InitTypeDef NVIC_InitStructure;
  27.   
  28.   /* 嵌套向量中断控制器组选择 */
  29.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  30.   
  31.   /* 配置USART为中断源 */
  32.   NVIC_InitStructure.NVIC_IRQChannel = DEBUG_USART_IRQ_SYN6288;
  33.   /* 抢断优先级*/
  34.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  35.   /* 子优先级 */
  36.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  37.   /* 使能中断 */
  38.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  39.   /* 初始化配置NVIC */
  40.   NVIC_Init(&NVIC_InitStructure);
  41. }
  42. //读忙
  43. void SYN6288_GPIO_Config ( void )
  44. {               
  45.         /*定义一个GPIO_InitTypeDef类型的结构体*/
  46.         GPIO_InitTypeDef GPIO_InitStructure;


  47.         /* 配置 LED1 引脚 */
  48.         SYN6288_GPIO_APBxClock_FUN(SYN6288_GPIO_CLK, ENABLE);                                                                                                                           
  49.         GPIO_InitStructure.GPIO_Pin = SYN6288_GPIO_PIN;        
  50.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;   
  51.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  52.         GPIO_Init ( SYN6288_GPIO_PORT, & GPIO_InitStructure );        
  53.         
  54. }

  55. /**
  56.   * @brief  USART GPIO 配置,工作参数配置
  57.   * @param  无
  58.   * @retval 无
  59.   */
  60. void SYN688_USART_Config(void)
  61. {
  62.         GPIO_InitTypeDef GPIO_InitStructure;
  63.         USART_InitTypeDef USART_InitStructure;

  64.         // 打开串口GPIO的时钟
  65.         DEBUG_USART_GPIO_APBxClkCmd(DEBUG_USART_GPIO_CLK_SYN6288, ENABLE);
  66.         
  67.         // 打开串口外设的时钟
  68.         DEBUG_USART_APBxClkCmd_SYN6288(DEBUG_USART_CLK_SYN6288, ENABLE);

  69.         // 将USART Tx的GPIO配置为推挽复用模式
  70.         GPIO_InitStructure.GPIO_Pin = DEBUG_USART_TX_GPIO_PIN_SYN6288;
  71.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  72.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  73.         GPIO_Init(DEBUG_USART_TX_GPIO_PORT_SYN6288, &GPIO_InitStructure);

  74.   // 将USART Rx的GPIO配置为浮空输入模式
  75.         GPIO_InitStructure.GPIO_Pin = DEBUG_USART_RX_GPIO_PIN_SYN6288;
  76.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  77.         GPIO_Init(DEBUG_USART_RX_GPIO_PORT_SYN6288, &GPIO_InitStructure);
  78.         
  79.         // 配置串口的工作参数
  80.         // 配置波特率
  81.         USART_InitStructure.USART_BaudRate = DEBUG_USART_BAUDRATE;
  82.         // 配置 针数据字长
  83.         USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  84.         // 配置停止位
  85.         USART_InitStructure.USART_StopBits = USART_StopBits_1;
  86.         // 配置校验位
  87.         USART_InitStructure.USART_Parity = USART_Parity_No ;
  88.         // 配置硬件流控制
  89.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  90.         // 配置工作模式,收发一起
  91.         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  92.         // 完成串口的初始化配置
  93.         USART_Init(DEBUG_USART_SYN6288, &USART_InitStructure);
  94.         
  95.         // 串口中断优先级配置
  96.         NVIC_Configuration();
  97.         
  98.         // 使能串口接收中断
  99.         USART_ITConfig(DEBUG_USART_SYN6288, USART_IT_RXNE, ENABLE);        
  100.         
  101.         // 使能串口
  102.         USART_Cmd(DEBUG_USART_SYN6288, ENABLE);               

  103.   // 清除发送完成标志
  104.         //USART_ClearFlag(USART1, USART_FLAG_TC);     
  105. }
  106. //其实是USART2_Send_Byte
  107. /*****************  发送一个字符 **********************/
  108. void Usart_SYN6288_SendByte( USART_TypeDef * pUSARTx, uint8_t ch)
  109. {
  110.         /* 发送一个字节数据到USART */
  111.         USART_SendData(pUSARTx,ch);
  112.                
  113.         /* 等待发送数据寄存器为空 */
  114.         while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);        
  115. }
  116. /*****************  发送字符串 **********************/
  117. void Usart_SSYN6288_endString( USART_TypeDef * pUSARTx, char *str)
  118. {
  119.         unsigned int k=0;
  120.   do
  121.   {
  122.       Usart_SYN6288_SendByte( pUSARTx, *(str + k) );
  123.       k++;
  124.   } while(*(str + k)!='\0');
  125.   
  126.   /* 等待发送完成 */
  127.   while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET)
  128.   {}
  129. }

  130. //语音合成
  131. void SYN6288_Speech( USART_TypeDef * pUSARTx,char * str )
  132. {
  133.         
  134.         if(SYN6288_Read_GPIO_IN()==Bit_RESET)/* x us后仍为高电平表示数据“1” */
  135.         {

  136.                                 char * p = str;
  137.                 int len = 0,check=0xFD,i;
  138.                
  139.                 while( *p++ != 0 )
  140.                 {
  141.                                 len++;
  142.                
  143.                 }
  144.                
  145.                 len+=3;
  146.                
  147.                 Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0xFD);
  148.                
  149.                 Usart_SYN6288_SendByte( DEBUG_USART_SYN6288,len / 256 );
  150.                 Usart_SYN6288_SendByte( DEBUG_USART_SYN6288,len % 256 );
  151.                 check  = check ^ ( len / 256 ) ^ ( len % 256 );
  152.                
  153.                 Usart_SYN6288_SendByte( DEBUG_USART_SYN6288,0x01 );
  154.                 Usart_SYN6288_SendByte( DEBUG_USART_SYN6288,0x01 );
  155.                 check = check ^ 0x01 ^ 0x01;
  156.                
  157.                 for( i = 0; i < len-3; i++ )
  158.                 {
  159.                                 Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,*str);
  160.                                 check ^= ( *str );
  161.                                 str++;
  162.                 }
  163.                 Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,check);   
  164.                                 
  165.                 rt_thread_delay(150*len);        
  166.         }        

  167. }
  168. /* 音量控制 */
  169. void Volinme(uint8_t Y_L)
  170. {
  171.                 uint8_t num ;
  172.         num = Y_L+48;
  173.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0xFD);
  174.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x00);
  175.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x06);
  176.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x01);
  177.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x01);
  178.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x5B);
  179.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x76);
  180.         //控制音量
  181.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,num);
  182.         
  183.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x5D);
  184. //        uint8_t num[9] ;
  185. //        
  186. //        num[0] = 0xFD;
  187. //        num[1] = 0x00;
  188. //        num[2] = 0x06;
  189. //        num[3] = 0x01;
  190. //        num[4] = 0x01;
  191. //        num[5] = 0x5B;
  192. //        num[6] = 0x76;
  193. //        //控制音量
  194. //        num[7] = Y_L+48;
  195. //        num[8] = 0x5D;
  196. //        
  197. //        Usart_SendByte(DEBUG_USARTx,num[8]);
  198. }
  199. /* 语调控制 */
  200. void Intonation(uint8_t Y_L)
  201. {
  202.         uint8_t num ;
  203.         num = Y_L+48;
  204.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0xFD);
  205.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x00);
  206.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x06);
  207.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x01);
  208.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x01);
  209.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x5B);
  210.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x74);
  211.         //控制音量
  212.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,num);
  213.         
  214.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x5D);
  215.         

  216. }
  217. /* 语速控制 */
  218. void Speed_pacing(uint8_t Y_L)
  219. {
  220.         uint8_t num ;
  221.         num = Y_L+48;
  222.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0xFD);
  223.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x00);
  224.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x06);
  225.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x01);
  226.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x01);
  227.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x5B);
  228.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x73);
  229.         //控制音量
  230.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,num);
  231.         
  232.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x5D);
  233.         
  234. }
  235. /* 人控制 */
  236. void speed_man(uint8_t Y_L)
  237. {
  238.         uint8_t num ;
  239.         num = Y_L+48;
  240.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0xFD);
  241.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x00);
  242.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x07);
  243.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x01);
  244.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x01);
  245.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x5B);
  246.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x6D);
  247.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x35);
  248.         //控制音量
  249.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,num);
  250.         
  251.         Usart_SYN6288_SendByte(DEBUG_USART_SYN6288,0x5D);
  252.         
  253. }
  254. /* End of File ------------------------------------------------------------- */



复制代码
.h
  1. #ifndef _SYN6288_H_
  2. #define _SYN6288_H_
  3. #include "stm32f10x.h"
  4. #include <stdio.h>
  5. #include <stdbool.h>



  6. #if defined ( __CC_ARM   )
  7. #pragma anon_unions
  8. #endif
  9. /******************************* ESP8266 外部全局变量声明 ***************************/
  10. #define RX_BUF_MAX_LEN     1024                                     //最大接收缓存字节数

  11. extern struct  STRUCT_USARTx_Fram                                  //串口数据帧的处理结构体
  12. {
  13.         char  Data_RX_BUF [ RX_BUF_MAX_LEN ];
  14.         
  15.   union {
  16.     __IO u16 InfAll;
  17.     struct {
  18.                   __IO u16 FramLength       :15;                               // 14:0
  19.                   __IO u16 FramFinishFlag   :1;                                // 15
  20.           } InfBit;
  21.   };
  22.         
  23. } strUSART_Fram_Record;




  24. /* 开发板硬件相关头文件 */

  25. /**  
  26.   ******************************************************************************
  27.   *  @File     SYN6288.h
  28.   *  @Author   Velscode  
  29.   *  @Brief    TTS 芯片 SYN6288驱动头文件(基于STM32F10x)
  30.   *            使用了USART2(A2\A3)
  31.   ******************************************************************************
  32.   */

  33. //extern struct STRUCT_USARTx_Fram strUSART_Fram_Record;        
  34.         /****************************** SYN6288 引脚配置参数定义***************************************/
  35. #define             SYN6288_GPIO_APBxClock_FUN              RCC_APB2PeriphClockCmd
  36. #define             SYN6288_GPIO_CLK                        RCC_APB2Periph_GPIOB
  37. #define             SYN6288_GPIO_PORT                       GPIOB
  38. #define             SYN6288_GPIO_PIN                        GPIO_Pin_2
  39. #define                                      SYN6288_Read_GPIO_IN()                        GPIO_ReadInputDataBit ( SYN6288_GPIO_PORT, SYN6288_GPIO_PIN )

  40. // 串口2-USART2
  41. #define  DEBUG_USART_SYN6288                    USART3
  42. #define  DEBUG_USART_CLK_SYN6288                 RCC_APB1Periph_USART3
  43. #define  DEBUG_USART_APBxClkCmd_SYN6288          RCC_APB1PeriphClockCmd
  44. #define  DEBUG_USART_BAUDRATE           9600

  45. // USART GPIO 引脚宏定义
  46. #define  DEBUG_USART_GPIO_CLK_SYN6288            (RCC_APB2Periph_GPIOB)
  47. #define  DEBUG_USART_GPIO_APBxClkCmd    RCC_APB2PeriphClockCmd
  48.    
  49. #define  DEBUG_USART_TX_GPIO_PORT_SYN6288        GPIOB   
  50. #define  DEBUG_USART_TX_GPIO_PIN_SYN6288         GPIO_Pin_10
  51. #define  DEBUG_USART_RX_GPIO_PORT_SYN6288        GPIOB
  52. #define  DEBUG_USART_RX_GPIO_PIN_SYN6288         GPIO_Pin_11

  53. #define  DEBUG_USART_IRQ_SYN6288                 USART3_IRQn
  54. #define  DEBUG_USART_IRQHandler_SYN6288          USART3_IRQHandler



  55. void SYN6288_GPIO_Config ( void );
  56. void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch);
  57. void SYN6288_Speech( USART_TypeDef * pUSARTx,char * str );
  58. void SYN688_USART_Config(void);
  59. void Usart_SYN6288_SendByte( USART_TypeDef * pUSARTx, uint8_t ch);
  60. void SYN_FrameInfo(char *HZdata);
  61. void Usart_SSYN6288_endString( USART_TypeDef * pUSARTx, char *str);
  62. /* 音量控制 */
  63. void Volinme(uint8_t Y_L);
  64. /* 语调控制 */
  65. void Intonation(uint8_t Y_L);
  66. /* 语速控制 */
  67. void Speed_pacing(uint8_t Y_L);
  68. /* 人控制 */
  69. void speed_man(uint8_t Y_L);
  70. #endif /*_SYN6288_H_*/
  71. /* End of File ------------------------------------------------------------- */



复制代码
这里要在stm32f10x.it中添加中断
  1. // 串口中断服务函数
  2. void DEBUG_USART_IRQHandler_SYN6288(void)
  3. {
  4.   uint8_t ucCh;
  5.         if ( USART_GetITStatus ( DEBUG_USART_SYN6288, USART_IT_RXNE ) != RESET )
  6.         {
  7.                 ucCh  = USART_ReceiveData( DEBUG_USART_SYN6288 );
  8.                
  9.                 if ( strUSART_Fram_Record .InfBit .FramLength < ( RX_BUF_MAX_LEN - 1 ) )                       //预留1个字节写结束符
  10.                            strUSART_Fram_Record .Data_RX_BUF [ strUSART_Fram_Record .InfBit .FramLength ++ ]  = ucCh;

  11.         }
  12.                   
  13.         if ( USART_GetITStatus( DEBUG_USART_SYN6288, USART_IT_IDLE ) == SET )                                         //数据帧接收完毕
  14.         {
  15.     strUSART_Fram_Record .InfBit .FramFinishFlag = 1;               
  16.                
  17.                 ucCh = USART_ReceiveData( DEBUG_USART_SYN6288 );                                                              //由软件序列清除中断标志位(先读USART_SR,然后读USART_DR)        
  18.   }        
  19. }
复制代码



如何使用语音播报
1.首先初始化
  1. //语音播报系统
  2.           SYN6288_GPIO_Config();
  3.           SYN688_USART_Config();
复制代码
2.然后主函数中使用

  1. //APP2
  2. static void APP2_thread_entry(void* parameter)
  3. {               
  4.         rt_uint8_t key=0;
  5.         rt_uint8_t dispBuff[100];
  6.         rt_uint8_t t=0;        
  7.         rt_uint8_t *str=0;
  8.         while(1)
  9.         {

  10.           key=Remote_Scan();
  11.                         
  12.                 if(key)
  13.                 {        
  14.                 switch(key)
  15.                         {
  16.                                 case 162:
  17.                                                 //1
  18.                                                 str= "· time 10min·";
  19.                                                 munite = 10;
  20.                                                 hour=0;               
  21.                                                 second=0;
  22.                                     SYN6288_Speech(DEBUG_USART_SYN6288,"定时十分钟");
  23.                                     
  24.             break;
  25.         
  26.         case 98:
  27.                                                 //2                                       
  28.                                           str= "· time 20min·";
  29.             munite = 20;
  30.                                                 hour=0;        
  31.                                                 second=0;
  32.                                     SYN6288_Speech(DEBUG_USART_SYN6288,"定时二十分钟");
  33.             break;
  34.         
  35.         case 226://3
  36.                                          str= "· time 30min·";
  37.                                          munite = 30;
  38.                                         hour=0;        
  39.                                                 second=0;
  40.             SYN6288_Speech(DEBUG_USART_SYN6288,"定时三十分钟");
  41.                                                 break;
  42.         
  43.         case 34://4
  44.                                          str= "· time 40min·";
  45.                                                 munite = 40;
  46.                                           hour=0;        
  47.                                     second=0;
  48.                                                 SYN6288_Speech(DEBUG_USART_SYN6288,"定时四十分钟");
  49.             break;
  50.         
  51.         case 2://5
  52.                                          str= "· time 50min·";
  53.            munite = 50;
  54.                                          hour=0;
  55.                                          second=0;
  56.                                          SYN6288_Speech(DEBUG_USART_SYN6288,"定时五十分钟");
  57.             break;
  58.         
  59.         case 194://6
  60.                                          str= "· time 60min·";
  61.            munite = 60;
  62.                                         hour=0;
  63.                                         second=0;
  64.                                         SYN6288_Speech(DEBUG_USART_SYN6288,"定时六十分钟");
  65.             break;
  66.         
  67.         case 224://7
  68.                                          str= "· time 90min·";
  69.                                          hour = 1;
  70.            munite = 30;
  71.                                          second=0;
  72.                                    SYN6288_Speech(DEBUG_USART_SYN6288,"定时一个半小时");
  73.             break;
  74.         
  75.         case 168://8
  76.                                          str= "· time  2h ·";
  77.            hour = 1;
  78.                                         munite = 60;
  79.                                         second=0;
  80.                                         SYN6288_Speech(DEBUG_USART_SYN6288,"定时两个小时");
  81.             break;
  82.         
  83.         case 144://9
  84.                                         SYN6288_Speech(DEBUG_USART_SYN6288,"定时三个小时");
  85.                                          str= "· time  3h ·";
  86.            hour = 2;
  87.                                                 munite = 60;
  88.                                                 second=0;
  89.                                    
  90.             break;
  91.         
  92.         case 152:
  93.                                          str= "detected code 0";
  94.             name();
  95.                                           
  96.             break;
  97.         
  98.         case 104:
  99.                                          str= "detected code *";
  100.            
  101.             break;
  102.         
  103.         case 176:
  104.                                          str= "detected code #";
  105.            
  106.             break;
  107.                
  108.         case 24:
  109.                                          str= "detected code ↑";
  110.          
  111.             break;
  112.                
  113.         case 16:
  114.                                          str= "detected code ←";
  115.             
  116.             break;
  117.         
  118.         case 74:
  119.                                           str= "detected code ↓";
  120.          
  121.             break;
  122.         
  123.         case 90:
  124.                                           str= "detected code →";
  125.             break;
  126.         
  127.         case 56:
  128.             str= "detected code OK ";
  129.             break;
  130.         
  131.         default:
  132.                                           str= "EMOURE";
  133.          
  134.             break;

  135.                         }
  136.                         Draw_Font16B(4,100,BRRED,str);        //显示SYMBOL
  137.                         key = 0;
  138. //                        uwRet = rt_thread_resume(APP3_thread);/* 恢复APP3线程! */
  139. //                        if(RT_EOK == uwRet)
  140. //                        {
  141. //                                rt_kprintf("恢复APP3线程成功!\n");
  142. //                        }
  143. //                        else
  144. //                        {
  145. //                                rt_kprintf("恢复APP3线程失败!失败代码:0x%lx\n",uwRet);
  146. //                        }
  147.                         
  148.                 }
  149.                
  150.         }

  151. }
复制代码
Keil代码下载: RT红外定时器+语音提示 代码.7z (625.98 KB, 下载次数: 12)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表