标题: 关于单片机超声波控制数码管程序 改变距离数码管无反应 [打印本页]

作者: 1888888    时间: 2022-3-24 13:31
标题: 关于单片机超声波控制数码管程序 改变距离数码管无反应


单片机源程序如下:
  1. #include <REGX52.H>
  2. sbit SH_CP1 = P0^1;
  3. sbit DS = P0^0;
  4. sbit ST_CP1 = P0^2;
  5. int distL,distR;
  6. unsigned char code table[]={
  7. 0x06,0x5B,0x4F,0x66,0x6D,
  8. 0x7D,0x07,0x7F,0x6F,0x3F};
  9. sbit TrigL    = P2^4;                // 超声波模块的Trig管脚
  10. sbit EchoL    = P2^5;                // 超声波模块的Echo管脚
  11. sbit TrigR    = P2^6;                // 超声波模块的Trig管脚
  12. sbit EchoR    = P2^7;                // 超声波模块的Echo管脚
  13. void Delay(unsigned int xms)
  14. {
  15.         unsigned char i, j;
  16.         while(xms--)
  17.         {
  18.                 i = 2;
  19.                 j = 199;
  20.                 do
  21.                 {
  22.                         while (--j);
  23.                 } while (--i);
  24.         }
  25. }
  26. void send_data_74hc595(int data_74hc595)                    //传送数据给74hc595
  27. {
  28.         char a;
  29.                                 SH_CP1 = 0;
  30.         for(a = 0; a < 8; ++a)
  31.         {
  32.                 DS = (data_74hc595 << a) & 0x80;                    //从高位开始传输



  33.               SH_CP1 = 1;
  34.              SH_CP1 = 0;
  35.         }
  36.                
  37.       
  38. }

  39. void show_74hc595()  
  40.                                        //数据显示
  41. {
  42.       ST_CP1 = 0;
  43.       ST_CP1 = 1;
  44.     }  


  45. void Nixie()
  46. {

  47.       
  48.           switch(distR)
  49.                  {
  50.                          case 1:send_data_74hc595(table[1]);show_74hc595();break;
  51.                          case 2:send_data_74hc595(table[2]);show_74hc595();break;
  52.                          case 3:send_data_74hc595(table[3]);show_74hc595();break;
  53.                          case 4:send_data_74hc595(table[4]);show_74hc595();break;
  54.                          case 5:send_data_74hc595(table[5]);show_74hc595();break;
  55.                          case 6:send_data_74hc595(table[6]);show_74hc595();break;
  56.                          case 7:send_data_74hc595(table[7]);show_74hc595();break;
  57.                          case 8:send_data_74hc595(table[8]);show_74hc595();break;
  58.                          case 9:send_data_74hc595(table[9]);show_74hc595();break;
  59.                          case 10:send_data_74hc595(table[0]);show_74hc595();break;
  60.                  }
  61.                
  62.                  
  63.                   switch(distL)
  64.                  {
  65.                          case 1:send_data_74hc595(table[1]);show_74hc595();break;
  66.                          case 2:send_data_74hc595(table[2]);show_74hc595();break;
  67.                          case 3:send_data_74hc595(table[3]);show_74hc595();break;
  68.                          case 4:send_data_74hc595(table[4]);show_74hc595();break;
  69.                          case 5:send_data_74hc595(table[5]);show_74hc595();break;
  70.                          case 6:send_data_74hc595(table[6]);show_74hc595();break;
  71.                          case 7:send_data_74hc595(table[7]);show_74hc595();break;
  72.                          case 8:send_data_74hc595(table[8]);show_74hc595();break;
  73.                          case 9:send_data_74hc595(table[9]);show_74hc595();break;
  74.                          case 10:send_data_74hc595(table[0]);show_74hc595();break;
  75.                  
  76.                  }
  77.                          }      
  78. //模块程序
  79. void GetDistanceL()
  80. {
  81.             
  82.   TH0 = 0;
  83.         TL0 = 0;
  84.         TrigL=0;      
  85.   TrigL=1;                                      
  86.   Delay(0.001);
  87.   TrigL=0;
  88.   while(!EchoL);               
  89.          TR0=1;                            //开启计数
  90.   while(EchoL);                       
  91.          TR0=0;                                //关闭计数
  92.   //timeL=TH0*256+TL0;
  93.         // timeL *= 12/11.0592;
  94.   //s=(timeL*1.7)/100;     //算出来是CM
  95.         distL=((TH0*256+TL0)*0.034)/2;
  96.       
  97. }
  98. void GetDistanceR()
  99. {      
  100.         TH0 = 0;
  101.         TL0 = 0;
  102.         TrigR=0;
  103.   TrigR=1;                                       
  104.         Delay(0.001);
  105.   TrigR=0;
  106.   while(!EchoR);               
  107.          TR0=1;                           
  108.   while(EchoR);                       
  109.          TR0=0;                              
  110.   //timeR=TH0*256+TL0;
  111.   //s=(timeR*1.7)/100;     //算出来是CM
  112.         distR=((TH0*256+TL0)*0.034)/2;
  113. }
  114. void set()
  115. {
  116. TMOD=0x21;
  117.         TH0=0;
  118.         TL0=0;
  119.         ET0=1;
  120.         EA=1;
  121. }

  122. void main()
  123. {
  124.         while(1)
  125.         {      
  126. set();
  127. GetDistanceL();
  128. GetDistanceR();
  129. Nixie();

  130.         }
  131. }
复制代码



作者: 1888888    时间: 2022-3-24 13:32
超声波改变距离数码管无反应,求大佬们支个招
作者: lkc8210    时间: 2022-3-25 00:41
1. Delay(0.01) = Delay(0), Delay(X) X只可以是0~65535的正整数
2. TrigL=1;到TrigL=0;之间要最少要有10us延时,弄个i=3;while(i--);就够了(unsigned int i)
3. 由于Trig启动不成功,程序在while(!Echo)那里卡死了
4. Nixie();只接受0~10, 也有可能是distL和dist大于10所以没反应
5. switch(distR%11)switch(distL%11)加个取余看看




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1