久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3472|回復: 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 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 91久久国产综合久久 | 免费一级淫片aaa片毛片a级 | 在线精品亚洲欧美日韩国产 | 国产日韩欧美精品 | 欧美一区二区三区免费在线观看 | 久久久国产视频 | 亚州av| 日韩中文字幕一区二区三区 | 成人在线视频免费观看 | 国产成人精品久久二区二区91 | 欧美三级成人理伦 | 久久国产欧美日韩精品 | 在线视频 中文字幕 | 日本在线看 | 热re99久久精品国产99热 | 亚洲乱码国产乱码精品精98午夜 | 成人免费一区二区三区视频网站 | 欧美日韩在线视频一区 | 国产精品久久国产精品久久 | 狠狠综合久久av一区二区小说 | 亚洲黄色片免费观看 | 天天操天天射天天舔 | 久久中文字幕一区 | 黄视频免费在线 | 久久99精品久久久 | 91精品国产一区二区三区 | 亚洲精品乱码久久久久v最新版 | 国产精品.xx视频.xxtv | 欧美激情国产日韩精品一区18 | 国产精品九九视频 | 一级在线视频 | 国产精品国产精品国产专区不蜜 | 欧美一级淫片免费视频黄 | 精品av| 日韩精品一二三区 | 亚洲国产精品视频一区 | 狼色网| 久久精品在线播放 | 97超碰人人草 | 色婷婷综合网站 | 一道本不卡 |