如何使用easyPic v7微控制器连接超声波传感器hc-sr04

时间:2016-06-03 19:05:10

标签: microcontroller sensor

晚上好人!

有没有人知道如何将超声波传感器与PIC单片机easyPic v7与PIC18F45K22芯片接口,以便让人反击。

我发现了一个有用的代码,我尝试编辑它但仍然没有用...

这是我的代码:

// Lcd module connections
sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

void main()
{
  int a;
  Lcd_Init();
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Cmd(_LCD_CURSOR_OFF);

  Lcd_Out(1,5,"ITCE444");
  Lcd_Out(2,3,"Term Project");

  Delay_ms(3000);
  Lcd_Cmd(_LCD_CLEAR);

  TRISA.RA0 = 0;  //RB0 as Input PIN (TRG)
  TRISA.RA4 = 0;  //RB4 as Input PIN (ECHO)

  while(1)
  {
    if(PORTA.RA4==1 && PORTA.RA0==1)
    {
      a = a + 1;
      Lcd_Cmd(_LCD_CLEAR);
      Lcd_Out(1,1,"Person in: ");
      Lcd_Out(1,12,a);
      Lcd_Out(1,15,"Person");
    }
    else
    {
      a = a - 1;
      Lcd_Cmd(_LCD_CLEAR);
      Lcd_Out(1,1,"Person out: ");
      Lcd_Out(1,13,a);
    }
    Delay_ms(400);
  }
}

谢谢和问候......

2 个答案:

答案 0 :(得分:0)

您上面发布的代码100%不正确。 Here is代码应该是什么样子。

HC-SR04是超声波距离传感器。它测量传感器和障碍物之间的距离。虽然你绝对可以使用距离信息来制作一个计数器,但这似乎比你的技能要高一点。

所以有三个选择:

  1. 学习使用pic微控制器并编写大量 timer 代码 从传感器读取距离。

  2. 切换到Arduino并使用它的内置库来获取 距离。

  3. 更改硬件。您不需要超声波传感器(或 这个问题的微控制器)使一个人反击。我建议 你只需使用一对红外LED即可让自己变得简单 当一个人走过它们时发出脉冲的电路。

答案 1 :(得分:0)

感谢您回复 Hassan Nadeem先生,我对代码进行了更新,当我实施代码时,它几乎可以正常工作。

看看并告诉我你的想法:

// LCD module connections
sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

// Ultrasonic module connection
sbit Ultrasonic at RD0_bit;
sbit Ultrasonic_Direction at TRISD0_bit;
// End of Ultrasonic module connections

#define Pole_Height 200

void main()
{
   unsigned long Tm;
   unsigned char Tl, Th;
   unsigned int h, Person_Height,dist;

   char Txt[7];

   int cont =0 ;

   ANSELB = 0;
   ANSELD = 0;

   Lcd_Init();
   Lcd_Cmd(_LCD_CURSOR_OFF);
   Lcd_Out(2,3,"Term Project");
   Lcd_Out(1,5,"ITCE444");
   Delay_Ms(2000);T0CON = 0x00;

   for(;;)
   {
      Ultrasonic_Direction = 0;
      TMR0H = 0;
      TMR0L = 0;

      Ultrasonic = 0;
      Delay_us(3);
      Ultrasonic = 1;
      Delay_us(10);
      Ultrasonic = 0;

      while(PORTD.RD5 == 0);
         T0CON.TMR0ON = 1;
      while(PORTD.RD5 == 1);
         T0CON.TMR0ON = 0;

      Tl = TMR0L;
      Th = TMR0H;
      Tm = Th*256 + Tl;
      Tm = Tm / 2;
      Tm = 34 * Tm;
      Tm = Tm / 1000;
      h = (unsigned int)Tm;
      Person_Height= Pole_Height - h;

      if (Person_Height > 168 && Person_Height < 196)
         cont = cont + 1 ;
      else if(Person_Height > 132 && Person_Height < 160 && cont>0)
         cont = cont - 1 ;

      IntToStr(cont, Txt);

      Lcd_Cmd(_LCD_CLEAR);
      Lcd_Out(1,1, "Person in");
      Lcd_Out(2,1, Txt);
      Delay_Ms(1000);
   }
}