找回密码
 立即注册

QQ登录

只需一步,快速开始

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

stm32F103单片机+超声波测距模块HC-SR03+讯飞语音合成模块XFS5152代码

[复制链接]
跳转到指定楼层
楼主
HC-SR03:可测量2cm~4m之间的距离
stm32f103将超声波测量得到的距离信息进行处理并发送给语音合成模块
XFS5152语音合成模块对距离信息进行播报
答主自写,为电子设计竞赛做的准备

单片机源程序如下:
  1. /*2019-7-13 超声波测试模块 HC-SR04*/
  2. /*2019-7-15 语音测试模块   XFS5152*/

  3. #include "stm32f10x.h"
  4. #include "tim2.h"
  5. #include "Sonic.h"
  6. #include "SysTick.h"
  7. #include "bsp_ili9341_lcd.h"
  8. #include "bsp_usart.h"
  9. #include "string.h"
  10. #include "stdio.h"

  11. extern u32  Distance;
  12. float str[200]; // 字符串暂存


  13. uint8_t number_detect(uint16_t number);
  14. char *show(uint16_t distance);
  15. void number_apart(uint16_t number);
  16. void my_str_cat(char *a, char *b);//实现指针中的数据连接


  17. int a;
  18. int b;
  19. int c;

  20. int main(void)
  21. {         
  22.         char *p;
  23.         char *fasong;//发送给STC单片机的结果
  24.        
  25.         p="{x10bfd00080100bfaacabcc1cb4f08}";//测试语音用例
  26.        
  27.         ILI9341_Init ();          //LCD 初始化
  28.         LCD_SetFont(&Font8x16);
  29.         LCD_SetColors(BLUE,WHITE);

  30.         ILI9341_Clear(0,0,LCD_X_LENGTH,LCD_Y_LENGTH);        /* 清屏,显示全黑 */
  31.         ILI9341_DispStringLine_EN(LINE(3),"     ultrasonic rangefinder    ");
  32.        
  33.         SysTick_Init();
  34.        
  35.         USART1_Config();                                        /* 初始化USART1 */
  36.         USART2_Config();          /* 初始化USART2 */
  37.        
  38.         TIM2_Config();                                                 /* 定时器TIM2初始化 */       
  39.         TIM6_Init();                                //TIM6 Init
  40.         Sonic_Init();                                 //Sonic_Init
  41.   

  42.         printf("欢迎使用野火STM32开发板!\r\n");

  43.        
  44.         while(1)
  45.         {
  46.                 Delay_ms(1000);

  47.                 /*串口显示*/
  48.                 printf("The Distance is:%d cm\r\n",Distance);
  49.                 /*LCD显示*/
  50.                 sprintf((char *)str,"        DISTANCE = %3d cm ",Distance);
  51.                
  52.                 ILI9341_DispStringLine_EN(LINE(5),(char*)str);
  53.                
  54.                 fasong=show(Distance);
  55.                
  56.                 printf("合成结果为:%s\r\n",fasong);
  57.                
  58.           Usart_SendString(USART2,fasong);
  59.                                
  60.         }
  61. }
  62. /******************* (C) COPYRIGHT 2019 *****END OF FILE************/

  63. char* show(uint16_t distance)
  64. {  
  65.        
  66.          uint8_t num;
  67.          char prefix1[100]={'{','x','1'};
  68.          char *prefixp1=prefix1;//前缀1:开始标志
  69.        
  70.          char prefix2[100]={'1','4'};
  71.          char *prefixp2=prefix2;    //前缀2:总帧长,默认为22
  72.        
  73.          char prefix3[100]={'f','d','0','0'};
  74.          char *prefixp3=prefix3;//前缀3
  75.        
  76.          
  77.    char prefix4[100]={'1','1'};
  78.          char *prefixp4=prefix4;    //前缀4:数据帧长,默认为19
  79.          
  80.          char prefix5[100]={'0','1','0','0'};
  81.          char *prefixp5=prefix5;     //前缀5
  82.          
  83.          char suffix[100]="4f08}";     //后缀
  84.          char *suffixp=suffix;
  85.          
  86.          char datage[100]="30";         //距离数值个位  
  87.    char *datagep=datage;
  88.          
  89.          char datashi[100]="31";         //距离数值十位  
  90.    char *dataship=datashi;
  91.          
  92.          char databai[100]="31";         //距离数值百位  
  93.    char *databaip=databai;
  94.    
  95.          char juli[50]="b5b1c7b0bee0c0ebceaa"; //提示1:当前距离为
  96.          char *julip=juli;
  97.          
  98.          char limi[50]="c0e5c3d7";         //提示2:厘米
  99.          char *limip=limi;
  100.                  
  101.          
  102.          char play[100]="";         //最后结果
  103.    char *playp=play;   

  104.          char shi[50]="caae";
  105.          char *ship=shi;          //十
  106.          
  107.          char bai[50]="b0d9";
  108.          char *baip=bai;          //百
  109.          
  110.          
  111.          num=number_detect(distance);//数据位数检测
  112.    number_apart(distance);     //数据每位分离
  113.        
  114.     //测试用例:{x1 06  fd  00  03        01        00        30        4f         08} 成功
  115.                 //播放结果:0
  116.    printf("num=%d \r\n",num);
  117.          printf("a=%d   b=%d   c=%d \r\n",a,b,c);
  118.        
  119.                  if(num==1){
  120.                          
  121.                  if(c==0) strcpy(datage,"30");    //数字转化成字符
  122.      else if(c==1) strcpy(datage,"31");
  123.                  else if(c==2) strcpy(datage,"32");
  124.                  else if(c==3) strcpy(datage,"33");
  125.                  else if(c==4) strcpy(datage,"34");
  126.                  else if(c==5) strcpy(datage,"35");
  127.                  else if(c==6) strcpy(datage,"36");
  128.                  else if(c==7) strcpy(datage,"37");
  129.                  else if(c==8) strcpy(datage,"38");
  130.                  else if(c==9) strcpy(datage,"39");
  131.                          
  132.                          
  133.                 my_str_cat(playp,prefixp1);
  134.          
  135.           my_str_cat(playp,prefixp2);
  136.          
  137.           my_str_cat(playp,prefixp3);
  138.                
  139.                 my_str_cat(playp,prefixp4);
  140.                
  141.                 my_str_cat(playp,prefixp5);
  142.                
  143.                 my_str_cat(playp,julip);
  144.                
  145.                 my_str_cat(playp,datage);
  146.                
  147.                 my_str_cat(playp,limip);
  148.                
  149.                 my_str_cat(playp,suffix);
  150.                
  151.                 printf("得到结果为:%s\r\n",play);
  152.                
  153.                 printf("得到结果指针为:%s\r\n",playp);
  154.                
  155.                
  156.                 }
  157.                  
  158.                 else if(num==2){
  159.                        
  160.                         strcpy(prefix2,"17");
  161.                         strcpy(prefix4,"14");
  162.                        
  163.        

  164.      if(b==1) strcpy(datashi,"31");
  165.                  else if(b==2) strcpy(datashi,"32");
  166.                  else if(b==3) strcpy(datashi,"33");
  167.                  else if(b==4) strcpy(datashi,"34");
  168.                  else if(b==5) strcpy(datashi,"35");
  169.                  else if(b==6) strcpy(datashi,"36");
  170.                  else if(b==7) strcpy(datashi,"37");
  171.                  else if(b==8) strcpy(datashi,"38");
  172.                  else if(b==9) strcpy(datashi,"39");
  173.                        
  174.                        
  175.                  if(c==0){
  176.                          strcpy(datage,"");  
  177.                          strcpy(prefix2,"16");
  178.                          strcpy(prefix4,"13");
  179.      }                         //数字转化成字符
  180.      else if(c==1) strcpy(datage,"31");
  181.                  else if(c==2) strcpy(datage,"32");
  182.                  else if(c==3) strcpy(datage,"33");
  183.                  else if(c==4) strcpy(datage,"34");
  184.                  else if(c==5) strcpy(datage,"35");
  185.                  else if(c==6) strcpy(datage,"36");
  186.                  else if(c==7) strcpy(datage,"37");
  187.                  else if(c==8) strcpy(datage,"38");
  188.                  else if(c==9) strcpy(datage,"39");
  189.                        
  190.                 my_str_cat(playp,prefixp1);
  191.          
  192.           my_str_cat(playp,prefixp2);
  193.          
  194.           my_str_cat(playp,prefixp3);
  195.                
  196.                 my_str_cat(playp,prefixp4);
  197.                
  198.                 my_str_cat(playp,prefixp5);
  199.                
  200.                 my_str_cat(playp,julip);
  201.                
  202.                 my_str_cat(playp,datashi);
  203.                
  204.                 my_str_cat(playp,shi);
  205.                
  206.                 my_str_cat(playp,datage);
  207.                
  208.                 my_str_cat(playp,limip);
  209.                
  210.                 my_str_cat(playp,suffix);
  211.                
  212.                 printf("得到结果为:%s\r\n",play);
  213.                
  214.                 printf("得到结果指针为:%s\r\n",playp);
  215.                
  216.                
  217.                 }
  218.                 else if(num==3){
  219.                        
  220.                         strcpy(prefix2,"1a");
  221.                         strcpy(prefix4,"17");
  222.                        
  223.        

  224.                  if(a==1) strcpy(databai,"31");
  225.                  else if(a==2) strcpy(databai,"32");
  226.                  else if(a==3) strcpy(databai,"33");
  227.                        
  228.                        
  229.      if(b==1) strcpy(datashi,"31");
  230.                  else if(b==2) strcpy(datashi,"32");
  231.                  else if(b==3) strcpy(datashi,"33");
  232.                  else if(b==4) strcpy(datashi,"34");
  233.                  else if(b==5) strcpy(datashi,"35");
  234.                  else if(b==6) strcpy(datashi,"36");
  235.                  else if(b==7) strcpy(datashi,"37");
  236.                  else if(b==8) strcpy(datashi,"38");
  237.                  else if(b==9) strcpy(datashi,"39");
  238.                        
  239.                        
  240.                  if(c==0){
  241.                          strcpy(datage,"");  
  242.                          strcpy(prefix2,"19");
  243.                          strcpy(prefix4,"16");
  244.                  }                 //数字转化成字符
  245.      else if(c==1) strcpy(datage,"31");
  246.                  else if(c==2) strcpy(datage,"32");
  247.                  else if(c==3) strcpy(datage,"33");
  248.                  else if(c==4) strcpy(datage,"34");
  249.                  else if(c==5) strcpy(datage,"35");
  250.                  else if(c==6) strcpy(datage,"36");
  251.                  else if(c==7) strcpy(datage,"37");
  252.                  else if(c==8) strcpy(datage,"38");
  253.                  else if(c==9) strcpy(datage,"39");
  254.                        
  255.                 my_str_cat(playp,prefixp1);
  256.          
  257.           my_str_cat(playp,prefixp2);
  258.          
  259.           my_str_cat(playp,prefixp3);
  260.                
  261.                 my_str_cat(playp,prefixp4);
  262.                
  263.                 my_str_cat(playp,prefixp5);
  264.                
  265.                 my_str_cat(playp,julip);
  266.                
  267.                 my_str_cat(playp,databai);
  268.                
  269.                 my_str_cat(playp,bai);
  270.                
  271.                 my_str_cat(playp,datashi);
  272.                
  273.                 my_str_cat(playp,shi);
  274.                
  275.                 my_str_cat(playp,datage);
  276.                
  277.                 my_str_cat(playp,limip);
  278.                
  279.                 my_str_cat(playp,suffix);
  280.                
  281.                 printf("得到结果为:%s\r\n",play);
  282.                
  283.                 printf("得到结果指针为:%s\r\n",playp);
  284.                
  285.                 }
  286.                        
  287.    
  288.                
  289.                 return playp;               
  290.                
  291. }


  292. uint8_t number_detect(uint16_t number)
  293. {
  294. uint8_t count=1;

  295. while(number>=10)
  296. {
  297.   count++;
  298.   number=number/10;
  299. }
  300. return count;
  301. }

  302. void number_apart(uint16_t number)
  303. {
  304.         a=b=c=0;
  305.   if (number<10 && number>=0) c=number;
  306.         else if(number<100 && number>=10){
  307.                 c=number%10;
  308.                 b=number/10;
  309.         }
  310.         else if(number<350 && number>=100){
  311.                
  312.                 //number=186
  313.                 c=number%10;         //c=6,number=186
  314.                 number=number/10;    //number=18
  315.                 b=number%10;         //b=8
  316.                 number=number/10;    //number=1;
  317.                 a=number;
  318.         }
  319. }

  320. void my_str_cat(char *a, char *b)
  321. {
  322.     while(*a !=0)a++;
  323.        
  324.     while(*b != 0){
  325.                         *a=*b;
  326.                         a++;
  327.                         b++;
  328.                 }
  329.        
  330.     *a=0;
  331.           
  332.           //printf("a为%s",a);
  333. }
复制代码

所有资料51hei提供下载:
超声波测距(最终版本).7z (228.31 KB, 下载次数: 61)



评分

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

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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