I bought a Ultrasonic sensor lately and read its datasheet carefully. I tried using it but it doesn't reply......
code:
I am new to 8051 and sensor, hence maybe its a silly mistake, please help me.
I am attaching my circuit and us100 datasheet below,
thanx in advance.....
code:
Rich (BB code):
#include<AT89x51.h>
void LCD_init();
void LCD_busy();
void LCD_command(unsigned char);
void LCD_data(unsigned char);
void LCD_string(unsigned char[]);
char itoc(int a);
#define Tx P2_6
#define Rx P2_7
unsigned char b,mode;
int c;
void delayer() interrupt 1{ //timer 0 interrupt service routine
TH0=0XFC;
TL0=0X18; // for 1ms delay
c++;
if(mode==0){
if(c == 1){
Tx = 0;
}
else if(c == 2){
Tx = 1;
}
else if(c == 12){// 10 ms signal
Tx = 0;
TR0 = 0;
c = 0;
b = 0;//stop waiting
mode = 1;
}
}
}
void main(){
Rx = 1;//config as input
EA = 1;//enable interrupts
ET0 = 1;//for timer 0
LCD_init();//initialize lcd
LCD_command(0x01);//clear display
LCD_string("Whats up!");//send string to lcd
while(1){
Tx = 0;//transmition line is low
mode = 0;//for timing purpose
c = 0;//milli second counter
b = 1;//for waiting
TMOD = 0x01;// timer 0 16 bit autoreload
TH0 = 0xFC;
TL0 = 0x18; // for 1ms delay
TR0 = 1;//start timer
while(b); //wait
TH0 = 0xFC;
TL0 = 0x18;// for 1ms delay
while(!Rx);//wait for reply
c = 0; //reload counter
TR0 = 1;//start counting time periiod i.e pulse width
while(Rx);//pulse end
TR0 = 0;//stop counting
LCD_command(0x01);
itoc(c);//convert counted millliseconds (integer) to string and send to lcd
}
}
I am new to 8051 and sensor, hence maybe its a silly mistake, please help me.
I am attaching my circuit and us100 datasheet below,
thanx in advance.....
Attachments
-
321.7 KB Views: 75
-
143.2 KB Views: 51
Last edited by a moderator: