2017-03-28 36 views
0

当我使用两个超声波传感器时,我感到困惑,但是当使用一个传感器时,代码工作正常。请帮助我附上两个ultarsnic sensores(HC-SR04)到我的代码,并使这工作得很好,如何将两个超声波传感器添加到PIC微控制器

在这里我打算做的事情是我想保持与范围内的对象,如果对象移动LED灯光需要打开。

我使用的PIC 16F877A

工作代码用于一个超声波传感器

void main() 

    { 

    int a; 

     int b; 


     TRISB = 0b00010000;   //RB4 as Input PIN (ECHO) 

     TRISC = 0;   //C as Output PINs (LED) 


     T1CON = 0x10;     //Initialize Timer Module 


     TRISD = 1; // 1 input 0 output ,related to LED (display) 


     while(1) 

     { 

     for(b=1; b<3; b++){ 


      if (b=2){ 


          TMR1H = 0;     //Sets the Initial Value of Timer 

          TMR1L = 0;     //Sets the Initial Value of Timer 



          PORTB.F1 = 1;    //TRIGGER HIGH 

          Delay_us(10);    //10uS Delay 

          PORTB.F1 = 0;    //TRIGGER LOW 




          while(!PORTD.F0);   //Waiting for Echo 

          T1CON.F0 = 1;    //Timer Starts 


          while(PORTD.F0);   //Waiting for Echo goes LOW 

          T1CON.F0 = 0;    //Timer Stops 




          a = (TMR1L | (TMR1H<<8)); //Reads Timer Value 

          a = a/58.82;    //Converts Time to Distance 

          a = a + 1;     //Distance Calibration\ 
            //formular s=ut 

         if(a != 4){ 


           PORTC = 0b01111111;    // turn on LEDs in the PIC 

           Delay_ms(5); 

           PORTC = 0;    // turn on LEDs in the PIC 


          } else { 

           //out of range 

           PORTC = 0b00000000; 

          } 



       } 


       } 


     } 

    } 

不两个ultarsonic sensers

void main() 

    { 

    int a; 

     int b; 


     TRISB = 0b00010000;   //RB4 as Input PIN (ECHO) 

     TRISC = 0;   //C as Output PINs (LED) 


     T1CON = 0x10;     //Initialize Timer Module 


     TRISD = 1; // 1 input 0 output ,related to LED (display) 


     while(1) 

     { 

     for(b=1; b<3; b++){ 


       if(b=1){ 



        TMR1H = 0;     //Sets the Initial Value of Timer 

        TMR1L = 0;     //Sets the Initial Value of Timer 



        PORTB.F0 = 1;    //TRIGGER HIGH 

        Delay_us(10);    //10uS Delay 

        PORTB.F0 = 0;    //TRIGGER LOW 




        while(!PORTD.F1);   //Waiting for Echo 

        T1CON.F0 = 1;    //Timer Starts 


        while(PORTD.F1);   //Waiting for Echo goes LOW 

        T1CON.F0 = 0;    //Timer Stops 




        a = (TMR1L | (TMR1H<<8)); //Reads Timer Value 

        a = a/58.82;    //Converts Time to Distance 

        a = a + 1;     //Distance Calibration\ 
            //formular s=ut 

        if(a != 4){ 
      //  PORTB.F3 = 1; 
       //  PORTD = 0xFC;    // display number two in LED displa 
       // Delay_ms(5); 
        // PORTB.F3 = 0; 


          PORTC = 0b00000011;    // turn on LEDs in the PIC 

          Delay_ms(5); 

          PORTC = 0;    // turn on LEDs in the PIC 


         } else { 

          //out of range 

          PORTC = 0b00000000; 

         } 


       } 


       else if (b=2){ 


         TMR1H = 0;     //Sets the Initial Value of Timer 

         TMR1L = 0;     //Sets the Initial Value of Timer 



         PORTB.F1 = 1;    //TRIGGER HIGH 

         Delay_us(10);    //10uS Delay 

         PORTB.F1 = 0;    //TRIGGER LOW 




         while(!PORTD.F0);   //Waiting for Echo 

         T1CON.F0 = 1;    //Timer Starts 


         while(PORTD.F0);   //Waiting for Echo goes LOW 

         T1CON.F0 = 0;    //Timer Stops 




         a = (TMR1L | (TMR1H<<8)); //Reads Timer Value 

         a = a/58.82;    //Converts Time to Distance 

         a = a + 1;     //Distance Calibration\ 
            //formular s=ut 

         if(a != 4){ 


           PORTC = 0b01111111;    // turn on LEDs in the PIC 

           Delay_ms(5); 

           PORTC = 0;    // turn on LEDs in the PIC 


          } else { 

           //out of range 

           PORTC = 0b00000000; 

          } 



       } 


       } 


     } 

    } 
+0

请格式化代码为可读文本和岗位作为这样的答案代码。行间距使其几乎不可读。另外,如果不直接访问硬件和调试器,SO用户很难修复您的代码。你自己做这件事更容易。 “不工作”无助于描述你的问题。 – ThingyWotsit

+0

'if(b = 1)'可能是错误的,你可能想'if(b == 1)'。你有几个这样的问题。 – linuxfan

+0

好的,我会更新这个。 Linuxfan你是如果条件的核心。很快我会用正确的代码更新这篇文章。 –

回答

1

由于上信号值的所有阻塞等待的工作代码要改变,你有效的做法是一次只使用一个传感器。如果您想同时使用多个传感器,则需要交叉传感器的逻辑或使用中断。

此外,根据测量距离的大小,物体和传感器之间的距离的截断点应进行调整,以考虑传感器本身之间的相对距离。

+0

你能帮我解决一下这段代码吗?在这里我不打算长距离,只有10厘米或更少。上面我把答案也有一些问题 –

0

下面是两个超声波传感器

void main() 

    { 

    int a; 

     int b; 


     TRISB = 0b00011000;   //RB4 and RB5 as Input PIN (ECHO) 
     TRISC = 0;   //C as Output PINs (LED) 
     T1CON = 0x10;     //Initialize Timer Module 


     TRISD = 1; // 1 input 0 output ,related to LED (display) 

     while(1) 

     { 
     for(b=1; b<3; b++){ 

       if(b==1){ 

        TMR1H = 0;     //Sets the Initial Value of Timer 
        TMR1L = 0;     //Sets the Initial Value of Timer 



        PORTB.F0 = 1;    //TRIGGER HIGH 
        Delay_us(10);    //10uS Delay 
        PORTB.F0 = 0;    //TRIGGER LOW 




        while(!PORTB.F4);   //Waiting for Echo 
        T1CON.F0 = 1;    //Timer Starts 

        while(PORTB.F4);   //Waiting for Echo goes LOW 
        T1CON.F0 = 0;    //Timer Stops 


        a = (TMR1L | (TMR1H<<8)); //Reads Timer Value 
        a = a/58.82;    //Converts Time to Distance 
        a = a + 1;     //Distance Calibration\ 
            //formular s=ut 

        if(a != 4){   

          PORTC = 0b00000011;    // turn on LEDs in the PIC 

          Delay_ms(5); 
          PORTC = 0;    // turn on LEDs in the PIC 


         } else { 

          //out of range 

          PORTC = 0b00000000; 

         } 


       } 


       else if (b==2){ 


         TMR1H = 0;     //Sets the Initial Value of Timer 

         TMR1L = 0;     //Sets the Initial Value of Timer 



         PORTB.F1 = 1;    //TRIGGER HIGH 

         Delay_us(10);    //10uS Delay 

         PORTB.F1 = 0;    //TRIGGER LOW 


         while(!PORTB.F5);   //Waiting for Echo 

         T1CON.F0 = 1;    //Timer Starts 


         while(PORTB.F5);   //Waiting for Echo goes LOW 

         T1CON.F0 = 0;    //Timer Stops 



         a = (TMR1L | (TMR1H<<8)); //Reads Timer Value 
         a = a/58.82;    //Converts Time to Distance 
         a = a + 1;     //Distance Calibration\ 
            //formular s=ut 

         if(a != 4){ 


           PORTC = 0b01111111;    // turn on LEDs in the PIC 
           Delay_ms(5); 
           PORTC = 0;    // turn on LEDs in the PIC 

          } else { 

           //out of range 
           PORTC = 0b00000000; 

          } 



       } 


       } 


     } 

    } 
+0

TRISB = 0b00011000; // RB4和RB5作为输入PIN(ECHO),此条线需要随条件变化 –

相关问题