找回密码
 立即注册

QQ登录

只需一步,快速开始

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

怎么提高超声波测距的范围?附STM32程序

[复制链接]
跳转到指定楼层
楼主
ID:433796 发表于 2018-12-7 16:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本人的程序OLED只能显示0~99cm的范围,就算设置成3位数,超过99cm就直接显示为999cm。求方法解决

  1. #include "oled.h"
  2. #include "oledfont.h"           
  3. #include "delay.h"

  4. u8 OLED_GRAM[128][8];         

  5. //?üD???′?μ?LCD               
  6. void OLED_Refresh_Gram(void)
  7. {
  8.         u8 i,n;                    
  9.         for(i=0;i<8;i++)  
  10.         {  
  11.                 OLED_WR_Byte (0xb0+i,OLED_CMD);   
  12.                 OLED_WR_Byte (0x00,OLED_CMD);      
  13.                 OLED_WR_Byte (0x10,OLED_CMD);      
  14.                 for(n=0;n<128;n++)
  15.                 OLED_WR_Byte(OLED_GRAM[n][i],OLED_DATA);
  16.         }   
  17. }

  18. void OLED_WR_Byte(unsigned char dat ,unsigned char cmd)
  19. {
  20.     unsigned char i=8, temp=0;
  21.     LCD_DC(cmd);
  22.     for(i=0;i<8;i++)
  23.     {
  24.         LCD_SCL(0);
  25.       
  26.         temp = dat&0x80;
  27.         if (temp == 0)
  28.         {
  29.             LCD_SDA(0);
  30.         }
  31.         else
  32.         {
  33.             LCD_SDA(1);
  34.         }
  35.         LCD_SCL(1);
  36.         dat<<=1;;        
  37.     }     
  38. }                                       

  39. //?a??OLED??ê?
  40. void OLED_Display_On(void)
  41. {
  42.         OLED_WR_Byte(0X8D,OLED_CMD);  
  43.         OLED_WR_Byte(0X14,OLED_CMD);  
  44.         OLED_WR_Byte(0XAF,OLED_CMD);  
  45. }
  46. //1?±?OLED??ê?   
  47. void OLED_Display_Off(void)
  48. {
  49.         OLED_WR_Byte(0X8D,OLED_CMD);  
  50.         OLED_WR_Byte(0X10,OLED_CMD);  
  51.         OLED_WR_Byte(0XAE,OLED_CMD);  
  52. }                                            
  53. //???áoˉêy,??íê?á,?????á??ê?oúé?μ?!oí??μ?ááò??ù!!!        
  54. void OLED_Clear(void)  
  55. {  
  56.         u8 i,n;  
  57.         for(i=0;i<8;i++)for(n=0;n<128;n++)OLED_GRAM[n][i]=0X00;  
  58.         OLED_Refresh_Gram();
  59. }
  60. //?-
  61. //x:0~1
  62. //y:0~
  63. //t:1 ì?3? 0,????                                
  64. void OLED_DrawPoint(u8 x,u8 y,u8 t)
  65. {
  66.         u8 pos,bx,temp=0;
  67.         if(x>127||y>63)return;
  68.         pos=7-y/8;
  69.         bx=y%8;
  70.         temp=1<<(7-bx);
  71.         if(t)OLED_GRAM[x][pos]|=temp;
  72.         else OLED_GRAM[x][pos]&=~temp;            
  73. }
  74. //x1,y1,x2,y2 ì?3???óòμ?????×?
  75. //è·±£x1<=x2;y1<=y2 0<=x1<=127 0<=y1<=63               
  76. //dot:0,????;1,ì?3?        
  77. void OLED_Fill(u8 x1,u8 y1,u8 x2,u8 y2,u8 dot)  
  78. {  
  79.         u8 x,y;  
  80.         for(x=x1;x<=x2;x++)
  81.         {
  82.                 for(y=y1;y<=y2;y++)OLED_DrawPoint(x,y,dot);
  83.         }                                                                                                            
  84.         OLED_Refresh_Gram();
  85. }
  86. //?ú???¨??????ê?ò???×?·?,°üà¨2?·?×?
  87. //x:0~1
  88. //y:0~
  89. //mode:0,·′°×??ê?;1,?y3£??ê?                              
  90. //size:????×?ì? 12/16/
  91. void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 size,u8 mode)
  92. {                                 
  93.         u8 temp,t,t1;
  94.         u8 y0=y;
  95.         u8 csize=(size/8+((size%8)?1:0))*(size/2);               
  96.         chr=chr-' ';
  97.         for(t=0;t<csize;t++)
  98.         {   
  99.         if(size==12)temp=asc2_1206[chr][t];                  
  100.         else if(size==16)temp=asc2_1608[chr][t];        
  101.         else if(size==24)temp=asc2_2412[chr][t];        
  102.         else return;                                                               
  103.                         for(t1=0;t1<8;t1++)
  104.         {
  105.                 if(temp&0x80)OLED_DrawPoint(x,y,mode);
  106.                 else OLED_DrawPoint(x,y,!mode);
  107.                 temp<<=1;
  108.                 y++;
  109.                 if((y-y0)==size)
  110.                 {
  111.                         y=y0;
  112.                         x++;
  113.                         break;
  114.                 }
  115.         }           
  116.         }
  117.         OLED_Refresh_Gram();               
  118. }
  119. //m^noˉ
  120. u32 mypow(u8 m,u8 n)
  121. {
  122.         u32 result=1;         
  123.         while(n--)result*=m;   
  124.         return result;
  125. }                                 
  126. //??ê?2??êy
  127. //x,y :?eμ?×?±ê      
  128. //len :êy×?μ???
  129. //size:×?ì?′ó
  130. //mode:?£ê?        0,ì?3??£ê?;1,μt?ó?£
  131. //num:êy?μ(0~4294967295);                        
  132. void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size)
  133. {               
  134.         u8 t,temp;
  135.         u8 enshow=0;                                                  
  136.         for(t=0;t<len;t++)
  137.         {
  138.                 temp=(num/mypow(10,len-t-1))%10;
  139.                 if(enshow==0&&t<(len-1))
  140.                 {
  141.                         if(temp==0)
  142.                         {
  143.                                 OLED_ShowChar(x+(size/2)*t,y,' ',size,1);
  144.                                 continue;
  145.                         }else enshow=1;
  146.                           
  147.                 }
  148.                  OLED_ShowChar(x+(size/2)*t,y,temp+'0',size,1);
  149.         }
  150. }
  151. //??ê?×?·?
  152. //x,y:?eμ?×?±ê
  153. //size:×?ì?′ó
  154. /
  155. void OLED_Init(void)   
  156. {  
  157.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  158.           GPIO_InitTypeDef GPIO_InitStructure;
  159.          
  160.                 GPIO_InitStructure.GPIO_Pin =GPIO_Pin_4 | GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8;
  161.                 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;   
  162.                 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  
  163.                 GPIO_InitStructure.GPIO_PuPd        = GPIO_PuPd_DOWN;
  164.                 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  165.                 GPIO_Init(GPIOC, &GPIO_InitStructure);      
  166.       
  167.                 GPIO_ResetBits(GPIOC,GPIO_Pin_4);
  168.       
  169.     LCD_SCL(1);
  170.     LCD_RST(0);

  171.     delay_ms(100);
  172.     LCD_RST(1);      
  173.                
  174.   OLED_WR_Byte(0xAE,OLED_CMD);
  175.         OLED_WR_Byte(0xD5,OLED_CMD);
  176.         OLED_WR_Byte(80,OLED_CMD);   
  177.         OLED_WR_Byte(0xA8,OLED_CMD);
  178.         OLED_WR_Byte(0X3F,OLED_CMD);
  179.         OLED_WR_Byte(0xD3,OLED_CMD);
  180.         OLED_WR_Byte(0X00,OLED_CMD);

  181.         OLED_WR_Byte(0x40,OLED_CMD);
  182.                                                                                                             
  183.         OLED_WR_Byte(0x8D,OLED_CMD);
  184.         OLED_WR_Byte(0x14,OLED_CMD);
  185.         OLED_WR_Byte(0x20,OLED_CMD);
  186.         OLED_WR_Byte(0x02,OLED_CMD);
  187.         OLED_WR_Byte(0xA1,OLED_CMD);
  188.         OLED_WR_Byte(0xC0,OLED_CMD);
  189.         OLED_WR_Byte(0xDA,OLED_CMD);
  190.         OLED_WR_Byte(0x12,OLED_CMD);
  191.                  
  192.         OLED_WR_Byte(0x81,OLED_CMD);
  193.         OLED_WR_Byte(0xEF,OLED_CMD);
  194.         OLED_WR_Byte(0xD9,OLED_CMD);
  195.         OLED_WR_Byte(0xf1,OLED_CMD);
  196.         OLED_WR_Byte(0xDB,OLED_CMD);
  197.         OLED_WR_Byte(0x30,OLED_CMD);

  198.         OLED_WR_Byte(0xA4,OLED_CMD);
  199.         OLED_WR_Byte(0xA6,OLED_CMD);
  200.         OLED_WR_Byte(0xAF,OLED_CMD);
  201.         OLED_Clear();

  202. }
  203. 超声波代码
  204. #include "sonic.h"
  205. #include "timer.h"
  206. #include "delay.h"

  207. extern u8 TIM_Flag;

  208. void Sonic_Config(void)
  209. {
  210.                 GPIO_InitTypeDef GPIO_InitStructure;
  211.       
  212.           RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
  213.       
  214.          
  215.          
  216.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  217.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  218.           GPIO_Init(GPIOC,&GPIO_InitStructure);
  219.       
  220.          
  221.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  222.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  223.     GPIO_Init(GPIOC,&GPIO_InitStructure);      
  224. }

  225. void Sonic_Launch(void)
  226. {
  227.                 Trig_High;
  228.           delay_ms(1);
  229.           Trig_Low;
  230. }

  231. u16 Sonic_Measure(void)
  232. {
  233.           u16 Distance;
  234.                 u16 TIM = 0;
  235.       
  236.           Sonic_Launch();
  237.          
  238.           TIM_Cmd(TIM2,ENABLE);
  239.          
  240.           while(Sonic_Status == 0 && TIM_Flag == 0);
  241.           TIM2 -> CNT = 0;
  242.           while(Sonic_Status == 1 && TIM_Flag == 0);
  243.           TIM_Cmd(TIM2,DISABLE);
  244.       
  245.           if(TIM_Flag == 0)
  246.                 {
  247.                                 TIM = TIM_GetCounter(TIM2);
  248.                           Distance = TIM * 0.015;
  249.                 }
  250.                 else
  251.                 {
  252.                         TIM_Flag = 0;
  253.                         Distance = 999;
  254.                 }
  255.                
  256.                 return Distance;
  257.                

  258. }        

复制代码


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

使用道具 举报

沙发
ID:123289 发表于 2018-12-8 14:08 | 只看该作者
这就好比是:小小班的小朋友只会从1数到10。发恢一下你上大班想像力。
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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