HC-SR03:可測量2cm~4m之間的距離
stm32f103將超聲波測量得到的距離信息進行處理并發送給語音合成模塊
XFS5152語音合成模塊對距離信息進行播報
答主自寫,為電子設計競賽做的準備
0.png (9.32 KB, 下載次數: 51)
下載附件
2019-8-2 04:06 上傳
單片機源程序如下:
- /*2019-7-13 超聲波測試模塊 HC-SR04*/
- /*2019-7-15 語音測試模塊 XFS5152*/
- #include "stm32f10x.h"
- #include "tim2.h"
- #include "Sonic.h"
- #include "SysTick.h"
- #include "bsp_ili9341_lcd.h"
- #include "bsp_usart.h"
- #include "string.h"
- #include "stdio.h"
- extern u32 Distance;
- float str[200]; // 字符串暫存
- uint8_t number_detect(uint16_t number);
- char *show(uint16_t distance);
- void number_apart(uint16_t number);
- void my_str_cat(char *a, char *b);//實現指針中的數據連接
- int a;
- int b;
- int c;
- int main(void)
- {
- char *p;
- char *fasong;//發送給STC單片機的結果
-
- p="{x10bfd00080100bfaacabcc1cb4f08}";//測試語音用例
-
- ILI9341_Init (); //LCD 初始化
- LCD_SetFont(&Font8x16);
- LCD_SetColors(BLUE,WHITE);
- ILI9341_Clear(0,0,LCD_X_LENGTH,LCD_Y_LENGTH); /* 清屏,顯示全黑 */
- ILI9341_DispStringLine_EN(LINE(3)," ultrasonic rangefinder ");
-
- SysTick_Init();
-
- USART1_Config(); /* 初始化USART1 */
- USART2_Config(); /* 初始化USART2 */
-
- TIM2_Config(); /* 定時器TIM2初始化 */
- TIM6_Init(); //TIM6 Init
- Sonic_Init(); //Sonic_Init
-
- printf("歡迎使用野火STM32開發板!\r\n");
-
- while(1)
- {
- Delay_ms(1000);
- /*串口顯示*/
- printf("The Distance is:%d cm\r\n",Distance);
- /*LCD顯示*/
- sprintf((char *)str," DISTANCE = %3d cm ",Distance);
-
- ILI9341_DispStringLine_EN(LINE(5),(char*)str);
-
- fasong=show(Distance);
-
- printf("合成結果為:%s\r\n",fasong);
-
- Usart_SendString(USART2,fasong);
-
- }
- }
- /******************* (C) COPYRIGHT 2019 *****END OF FILE************/
- char* show(uint16_t distance)
- {
-
- uint8_t num;
- char prefix1[100]={'{','x','1'};
- char *prefixp1=prefix1;//前綴1:開始標志
-
- char prefix2[100]={'1','4'};
- char *prefixp2=prefix2; //前綴2:總幀長,默認為22
-
- char prefix3[100]={'f','d','0','0'};
- char *prefixp3=prefix3;//前綴3
-
-
- char prefix4[100]={'1','1'};
- char *prefixp4=prefix4; //前綴4:數據幀長,默認為19
-
- char prefix5[100]={'0','1','0','0'};
- char *prefixp5=prefix5; //前綴5
-
- char suffix[100]="4f08}"; //后綴
- char *suffixp=suffix;
-
- char datage[100]="30"; //距離數值個位
- char *datagep=datage;
-
- char datashi[100]="31"; //距離數值十位
- char *dataship=datashi;
-
- char databai[100]="31"; //距離數值百位
- char *databaip=databai;
-
- char juli[50]="b5b1c7b0bee0c0ebceaa"; //提示1:當前距離為
- char *julip=juli;
-
- char limi[50]="c0e5c3d7"; //提示2:厘米
- char *limip=limi;
-
-
- char play[100]=""; //最后結果
- char *playp=play;
- char shi[50]="caae";
- char *ship=shi; //十
-
- char bai[50]="b0d9";
- char *baip=bai; //百
-
-
- num=number_detect(distance);//數據位數檢測
- number_apart(distance); //數據每位分離
-
- //測試用例:{x1 06 fd 00 03 01 00 30 4f 08} 成功
- //播放結果:0
- printf("num=%d \r\n",num);
- printf("a=%d b=%d c=%d \r\n",a,b,c);
-
- if(num==1){
-
- if(c==0) strcpy(datage,"30"); //數字轉化成字符
- else if(c==1) strcpy(datage,"31");
- else if(c==2) strcpy(datage,"32");
- else if(c==3) strcpy(datage,"33");
- else if(c==4) strcpy(datage,"34");
- else if(c==5) strcpy(datage,"35");
- else if(c==6) strcpy(datage,"36");
- else if(c==7) strcpy(datage,"37");
- else if(c==8) strcpy(datage,"38");
- else if(c==9) strcpy(datage,"39");
-
-
- my_str_cat(playp,prefixp1);
-
- my_str_cat(playp,prefixp2);
-
- my_str_cat(playp,prefixp3);
-
- my_str_cat(playp,prefixp4);
-
- my_str_cat(playp,prefixp5);
-
- my_str_cat(playp,julip);
-
- my_str_cat(playp,datage);
-
- my_str_cat(playp,limip);
-
- my_str_cat(playp,suffix);
-
- printf("得到結果為:%s\r\n",play);
-
- printf("得到結果指針為:%s\r\n",playp);
-
-
- }
-
- else if(num==2){
-
- strcpy(prefix2,"17");
- strcpy(prefix4,"14");
-
-
- if(b==1) strcpy(datashi,"31");
- else if(b==2) strcpy(datashi,"32");
- else if(b==3) strcpy(datashi,"33");
- else if(b==4) strcpy(datashi,"34");
- else if(b==5) strcpy(datashi,"35");
- else if(b==6) strcpy(datashi,"36");
- else if(b==7) strcpy(datashi,"37");
- else if(b==8) strcpy(datashi,"38");
- else if(b==9) strcpy(datashi,"39");
-
-
- if(c==0){
- strcpy(datage,"");
- strcpy(prefix2,"16");
- strcpy(prefix4,"13");
- } //數字轉化成字符
- else if(c==1) strcpy(datage,"31");
- else if(c==2) strcpy(datage,"32");
- else if(c==3) strcpy(datage,"33");
- else if(c==4) strcpy(datage,"34");
- else if(c==5) strcpy(datage,"35");
- else if(c==6) strcpy(datage,"36");
- else if(c==7) strcpy(datage,"37");
- else if(c==8) strcpy(datage,"38");
- else if(c==9) strcpy(datage,"39");
-
- my_str_cat(playp,prefixp1);
-
- my_str_cat(playp,prefixp2);
-
- my_str_cat(playp,prefixp3);
-
- my_str_cat(playp,prefixp4);
-
- my_str_cat(playp,prefixp5);
-
- my_str_cat(playp,julip);
-
- my_str_cat(playp,datashi);
-
- my_str_cat(playp,shi);
-
- my_str_cat(playp,datage);
-
- my_str_cat(playp,limip);
-
- my_str_cat(playp,suffix);
-
- printf("得到結果為:%s\r\n",play);
-
- printf("得到結果指針為:%s\r\n",playp);
-
-
- }
- else if(num==3){
-
- strcpy(prefix2,"1a");
- strcpy(prefix4,"17");
-
-
- if(a==1) strcpy(databai,"31");
- else if(a==2) strcpy(databai,"32");
- else if(a==3) strcpy(databai,"33");
-
-
- if(b==1) strcpy(datashi,"31");
- else if(b==2) strcpy(datashi,"32");
- else if(b==3) strcpy(datashi,"33");
- else if(b==4) strcpy(datashi,"34");
- else if(b==5) strcpy(datashi,"35");
- else if(b==6) strcpy(datashi,"36");
- else if(b==7) strcpy(datashi,"37");
- else if(b==8) strcpy(datashi,"38");
- else if(b==9) strcpy(datashi,"39");
-
-
- if(c==0){
- strcpy(datage,"");
- strcpy(prefix2,"19");
- strcpy(prefix4,"16");
- } //數字轉化成字符
- else if(c==1) strcpy(datage,"31");
- else if(c==2) strcpy(datage,"32");
- else if(c==3) strcpy(datage,"33");
- else if(c==4) strcpy(datage,"34");
- else if(c==5) strcpy(datage,"35");
- else if(c==6) strcpy(datage,"36");
- else if(c==7) strcpy(datage,"37");
- else if(c==8) strcpy(datage,"38");
- else if(c==9) strcpy(datage,"39");
-
- my_str_cat(playp,prefixp1);
-
- my_str_cat(playp,prefixp2);
-
- my_str_cat(playp,prefixp3);
-
- my_str_cat(playp,prefixp4);
-
- my_str_cat(playp,prefixp5);
-
- my_str_cat(playp,julip);
-
- my_str_cat(playp,databai);
-
- my_str_cat(playp,bai);
-
- my_str_cat(playp,datashi);
-
- my_str_cat(playp,shi);
-
- my_str_cat(playp,datage);
-
- my_str_cat(playp,limip);
-
- my_str_cat(playp,suffix);
-
- printf("得到結果為:%s\r\n",play);
-
- printf("得到結果指針為:%s\r\n",playp);
-
- }
-
-
-
- return playp;
-
- }
- uint8_t number_detect(uint16_t number)
- {
- uint8_t count=1;
- while(number>=10)
- {
- count++;
- number=number/10;
- }
- return count;
- }
- void number_apart(uint16_t number)
- {
- a=b=c=0;
- if (number<10 && number>=0) c=number;
- else if(number<100 && number>=10){
- c=number%10;
- b=number/10;
- }
- else if(number<350 && number>=100){
-
- //number=186
- c=number%10; //c=6,number=186
- number=number/10; //number=18
- b=number%10; //b=8
- number=number/10; //number=1;
- a=number;
- }
- }
- void my_str_cat(char *a, char *b)
- {
- while(*a !=0)a++;
-
- while(*b != 0){
- *a=*b;
- a++;
- b++;
- }
-
- *a=0;
-
- //printf("a為%s",a);
- }
復制代碼
所有資料51hei提供下載:
超聲波測距(最終版本).7z
(228.31 KB, 下載次數: 61)
2019-8-2 04:06 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|