简体   繁体   English

如何在PIC​​微控制器中添加两个超声波传感器

[英]how to add two ultrasonic sensors to PIC microcontroller

i am in troble with when using two ulrasonic sensores, but when working with one sensore, code is working fine. 我在使用两个超声波传感器时遇到麻烦,但是在使用一个超声波传感器时,代码工作正常。 please help me to attach two ultarsnic sensores (HC-SR04) to my code and make this worke fine, 请帮助我在我的代码上附加两个ultansnic传感器(HC-SR04),并使其正常工作,

here i am tring to do thing is i want to keep a object with in the range and if the object move LED lights need to be on. 在这里,我想做的事情是我想将物体保持在范围内,如果物体移动,则需要打开LED灯。

i am using the PIC 16F877A 我正在使用PIC 16F877A

Working code for one ultrasonic sensor 一个超声波传感器的工作代码

    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;

                          }



                }


              }


      }

    }

not working code for two ultarsonic sensers 两个ultarsonic传感器的无效代码

    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;

                          }



                }


              }


      }

    }

Because of all the blocking waits on signal values to change, what you have effectively done is to use only one sensor at a time. 由于所有阻塞都等待信号值的改变,因此您实际上要做的是一次只使用一个传感器。 If you want to use multiple sensors at the same time, you'll need to either interleave the logic for both sensors or use interrupts. 如果要同时使用多个传感器,则需要为两个传感器交错逻辑或使用中断。

Additionally, depending on the scale of the distances being measured, the cut off point of distance between object and sensor should be adjusted to take the relative distance between sensors themselves into account. 此外,根据被测距离的大小,应调整对象与传感器之间的距离的截止点,以考虑到传感器自身之间的相对距离。

Here is the answer code for two ultrasonic sensors 这是两个超声波传感器的答案代码

    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;

                          }



                }


              }


      }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM