Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Interfacing AT89C51 with ultrasonic sensor and LCD

Status
Not open for further replies.

giordano

Newbie level 5
Newbie level 5
Joined
Nov 28, 2012
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Indonesia
Visit site
Activity points
1,376
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 C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#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);   
 }
}

 

Attachments

  • ultralcd.JPG
    ultralcd.JPG
    216.3 KB · Views: 142

Not that I'm familiar with the u/sound sensor nor the microcontroller, but where in the code did you insert the wait state
until you see a ping response? All I saw was fixed delays, but maybe I missed it in the code.
It looks like the code is copied from here, but with the ping wait removed.
 

Ya that code is my reference but I change a little bit and add with lcd code. I also try with while state to wait the ping response(echo) and it was not working. My lcd always display number 44 and it was not varying according to distance.

- - - Updated - - -

Not that I'm familiar with the u/sound sensor nor the microcontroller, but where in the code did you insert the wait state
until you see a ping response? All I saw was fixed delays, but maybe I missed it in the code.
It looks like the code is copied from here, but with the ping wait removed.

Can you tell me the mistakes in my code?
 

Can you tell me the mistakes in my code?
What algorithm are you using to determine the distance?
I presume, the answer is, that you're waiting for a ping response.
But where in your code are you waiting for a ping response?
The code you copied from has this, but you've explicitly removed it,
and replaced it with a fixed delay (see line 68 of your code).
So, it is not surprising that it doesn't work. There may be more errors (I don't know).
 

Ya, I think you are right. I already change line 68 with the code:
while(INT0==0);
while(INT0==1);
DPH=TH0...

However the output dislpayed by lcd is always 45 and is still not varying. Any idea to fix this?
 

You could try to disconnect the trigger wire to the ultrasound transmitter, to see if the behavior changes (it should).
Also, the echo may be arriving too early (your wall needs to be a few meters away).
 

I tried to disconnect the trigger wire to the ultrasonic sensor transmitter but the behavior did not change. LCD still display output=45. I am afraid that my lcd function is not correct.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top