Microcontroller › 8051 › Interfacing AT89S51 with ultrasonic sensor and LCD
- This topic has 40 replies, 28 voices, and was last updated 6 years, 12 months ago by Shirsendu Das.
-
AuthorPosts
-
January 17, 2013 at 7:56 pm #2076zulnorainParticipant
I want to make a project to measure distance using ultrasonic sensor HC-S04 and LCD to display the distance in cm. I use AT89S51, 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?
#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]!='