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);
}
}