Discussion in "Project Doubts" started by    batara    Jan 13, 2013.
Sun Jan 13 2013, 03:36 PM
#1
I want to make a project to measure distance using ultrasonic sensor HC-S*04 and LCD to display the distance in cm. I use AT89C51, the trigger pin for ultrasonic sensor is P3.5 and the echo pin is P3.2. The echo pin is connected to P3.2 because I use interrupt so that whenever there is a HIGH it will activate timer 0 and the duration of HIGH condition (in microsecond) will be converted to cm (divided by 59). I already make sure that the ultrasonic sensor works well. The problem is the LCD always display output the same number 45. It is not varying although the distance between object and ultrasonic sensor change. Can anyone help me to solve this problem?

Code:
#include<REGX51.h>

#include<intrins.h>
// for using _nop_() function
#define port P2
#define dataport P0 
sfr16 DPTR =0x82;
sbit trig=P3^5;
sbit rs=port^0;
sbit rw=port^1;
sbit e=port^2;
 
 
void delay(unsigned int msec)
{
int i,j;
for(i=0;i<msec;i++)
    for(j=0;j<1275;j++);
}
 
void lcd_cmd(unsigned char item) // Function to send command to LCD
{
dataport = item;
rs= 0;
rw=0;
e=1;
delay(1);
e=0;
return;
}
 
void lcd_data(unsigned char item) // Function to send data to LCD
{
dataport = item;
rs= 1;
rw=0;
e=1;
delay(1);
e=0;
return;
}
 
void lcd_data_string(unsigned char *str) // Function to send string to LCD
{
int i=0;
while(str[i]!='\0')
{
  lcd_data(str[i]);
  i++;
  delay(1);
}
return;
}
 
void send_pulse(void) //to generate 10 microseconds delay
{
TH0=0x00;TL0=0x00; 
 trig=1;
 _nop_();_nop_();_nop_();_nop_();_nop_();
 _nop_();_nop_();_nop_();_nop_();_nop_();
 trig=0;
    
} 
 
unsigned int get_range(void)
{
 int range=0;
 int s;
 send_pulse();
 delay(40);//          generate a delay of 40 Milli seconds=40000 micro 
 DPH=TH0;DPL=TL0;  
 TH0=0xFF;TL0=0xFF;
 lcd_cmd(0x81);
 delay(2);
 lcd_data_string("output:");
 lcd_cmd(0x8a);
 if(DPTR<35000)
 {//actually you need to use 38000 but the sensor may not work at higher levels 
  range=DPTR/59;
  s=range/100;
  range=range%100;
  if(s!=0)
  {
  lcd_data(s+48);
  }
  else
  {
  lcd_cmd(0x06);
  s=range/10;
  range=range%10;
  lcd_data(s+48);
  lcd_data(range+48);
  lcd_data(' ');
  }
 }
 else 
 {
 range=0; // indicates that there is no obstacle in front of the sensor 
 lcd_cmd(0x06);
 lcd_data(0);
 }
 return range;
}
 
void main()
{
  lcd_cmd(0x38);
  lcd_cmd(0x0c);
  delay(2);
  lcd_cmd(0x01);
  delay(2);
  lcd_cmd(0x81);
  delay(2);
  lcd_data_string("start");
  delay(20);
  TMOD=0x09;//timer0 in 16 bit mode with gate enable
  TR0=1;//timer run enabled
  TH0=0x00;TL0=0x00;
 
  P3=0x04;//setting pin P3.2 
     
  
 while(1)
 { get_range();
   delay(2);   
 }
}




Mon Jan 14 2013, 06:44 PM
#2
@ batara
can you elaborate the logic in your words
why your using DPTR register
how you calculating the distance ?
Tue Jan 15 2013, 07:17 AM
#3
The idea is like this:
The trigger pin P3.5 output HIGH state for 10uS to make the ultrasonic sensor transmit 40KHz sound wave. Then, the echo will be received by echo pin P3.2 which is used as interrupt. Whenever the echo(HIGH state) is received by P3.2, the interrupt will activate timer0 to start. If the echo line goes LOW the timer0 will stop. Therefore, timer0 is used to record the duration of HIGH state from echo line. This duration will be converted to distance (in centimeter) by dividing with 59. When timer0 stops recording the time, the duration is hold by DPTR. You can find in the code above DPH=TH0; DPL=TL0. If DPTR<35000 then it will be divided by 59 to get the distance and the LCD will display that distance. The function to display the distance you can see:
if(s!0)
{
....
...
}
else
{
range=0;
...
...
}
return range;
}

However, the LCD always display output=45 and it is not varying according to distance. I also found that even if I disconnect the trigger wire to ultrasonic sensor, LCD still display output=45. Any idea to fix this problem?
Tue Jan 15 2013, 06:48 PM
#4
Whatever the problem is, using the DPTR like this is a bad idea.

It is very likely that Kiel uses the DPTR, so its contents cannot be relied upon.
Just create a 16 bit integer variable to hold your measurement.
Tue Jan 15 2013, 08:10 PM
#5
i agree with ExperimenterUK
DPTR is SFR and may be keil using it or may be not
and its values are over written
Sun Jan 20 2013, 11:56 PM
#6
you can instead use an intiger for this purpose e.g.

int timerval;

timerval = TH0;
timerval = (timerval << 8) | TL0;

now timerval has 16bit timer value which you can use anywhere in your code. Do not use SFRs in C code as its not recommended. It can break your code without showing any error.
 batara like this.
Mon Feb 04 2013, 08:37 AM
#7
Ajay Bhargav could yo explain the code you write. I don't understand --> timerval=(timerval << 8) | TL0;
Mon Feb 04 2013, 08:50 AM
#8
timerval << 8 //shift timerval 8 bit left.
// e.g. if timerval is 0x43 then shifting it 8-bit gives you
// 0x4300

(timerval << 8) | TL0
// then this value is ORed with TL0
// e.g. if TL0 is 0x22 then
// 0x4300 | 0x22 will give you 0x4322
// which is a 16-bit combined value
 batara like this.
Mon Feb 04 2013, 11:08 AM
#9
Yes, it works. The LCD can display distance correctly. Thank you for your help
Mon Feb 04 2013, 06:46 PM
#10
If you can make a simple project on obstacle distance measurement, I can upload it on this website. good luck!
 rammichael like this.

Get Social

Information

Powered by e107 Forum System

Conversation

Thu May 13 2021, 07:18 PM
Rufus
Pretty section of content. I just stumbled upon your weblog and in accession capital to assert that I get actually enjoyed account your blog posts. Any way I will be subscribing to your augment and even I achievement you access consistently quickly. https://mkfinstitute.com Trisha Trisha
Wed May 12 2021, 01:23 PM
Jed Roan
Now you can promote your site all over the internet for FREE: http://www3.listofsitesthatacceptfreeads.club
Wed May 12 2021, 08:20 AM
Edgardo
If some one wants to be updated with most recent technologies afterward he must be visit this web page and be up to date every day. annual credit report https://creditscoresetf.com
Wed May 12 2021, 06:11 AM
Laurinda
Great website. A lot of useful information here. I'm sending it to some pals ans also sharing in delicious. And of course, thanks on your effort! annual credit report https://creditscorecheckn.com
Wed May 12 2021, 05:37 AM
Joann
Does your site have a contact page? I'm having problems locating it but, I'd like to shoot you an email. I've got some recommendations for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it improve over time. get credit report https://freecreditreportww.com

Downloads

Comments

Mirza123
Thu Jun 25 2020, 03:46 PM
eunicelove124
Mon Jun 22 2020, 03:03 PM
cerouno
Tue Jun 16 2020, 05:10 PM
Marce
Sat Jun 13 2020, 09:43 PM
Davidthils
Thu May 21 2020, 12:44 PM
Jakeror
Thu May 21 2020, 01:28 AM
motorCar
Wed May 20 2020, 07:05 PM
Vordrync
Wed May 20 2020, 11:52 AM

Online

Guests: 112, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 05:37 AM

Members: 38215
Newest member: Mirza123