2017-03-28 40 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),此條線需要隨條件變化 –

相關問題