Distance measurement w/ HC-SR04 and LCD - Proteus Simulator

General discussion on mikroC PRO for 8051.
Post Reply
Author
Message
PedroPst
Posts: 1
Joined: 13 Apr 2019 20:36

Distance measurement w/ HC-SR04 and LCD - Proteus Simulator

#1 Post by PedroPst » 13 Apr 2019 20:44

Hello, I'm trying to make a simulation of a SR-HC04 and display the distance measured on LCD.

I did some progress, I guess, but the result isn't what I expected. First of all, the display isn't static, shows a lot of diferent numbers, and I don't know why. Second, the distance measured isn't right. Could someone give me a help?

I attached my Proteus file and MikroC for 8051 file.

Code: Select all

#define TRIG P2_3_bit
#define ECHO P2_4_bit

#define clockPeriod 1000000
#define soundVelocity 343

sbit LCD_RS at P3_0_bit;
sbit LCD_EN at P3_1_bit;
sbit LCD_D7 at P3_5_bit;
sbit LCD_D6 at P3_4_bit;
sbit LCD_D5 at P3_3_bit;
sbit LCD_D4 at P3_2_bit;

int factor, distance;
char txt[16];


void settingUpDelay()
{
  TR0_bit = 0;
  TF0_bit = 0;
  TMOD = 0x01;
}

void beginDelay()
{
  settingUpDelay();
  TL0 = 0xF0;
  TH0 = 0xFF;
  TR0_bit = 1;
  while(TF0_bit == 0);
  TR0_bit = 0;
  TF0_bit = 0;
}

void sendingTriggerPulse()
{
  TRIG = 1;
  beginDelay();
  TRIG = 0;
}

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

  while(1)
  {
    sendingTriggerPulse();
    while(ECHO == 1)
    {
      TR0_bit = 1;
    }
    while(ECHO == 0 && TF0 == 1)
    {
      TR0_bit = 0;
    }
    factor = (clockPeriod * soundVelocity)/2;
    distance = (TL0|(TH0<<8));
    distance = distance*factor*100;

    IntToStr(distance, txt);
    Lcd_Out(1, 3, txt);
  }
}
Attachments
ParkingLot.rar
(66.76 KiB) Downloaded 121 times

kashifmirza
Posts: 16
Joined: 26 Jan 2019 14:58

Re: Distance measurement w/ HC-SR04 and LCD - Proteus Simulator

#2 Post by kashifmirza » 20 Nov 2022 06:56

Hi
Which version of Proteus you are using.proteus 8 or Proteus 7
i am engineer from microsolution my website is
https://projectiot123.com

Post Reply

Return to “mikroC PRO for 8051 General”