NerdyRocker54
Newbie level 4
Hi I'm having trouble interfacing a sonar sensor and the 8051. I've searched around online and read the datasheet and have some code. I understand what needs to be done to implement the sensor but having a hard time getting it to work. It's a 3 pin sonar sensor. I have the signal pin connected to pin 3.2 as it uses interrupt timers that I use to capture the distance. (this is my problem) It is not capturing the distance at all. I created a main to test out the distance (if distance is less than 2 feet turn on LED 1, 2 feet - 4 feet turn on LED 2, etc..)
I've tried connecting it to an O-Scope to see if my input signal is correct and that is also not working. attached is a link to the sensor I'm using as well as code as I have.
Any help would be greatly appreciated. Thank you.
https://www.seeedstudio.com/depot/ultra-sonic-range-measurement-module-p-626.html?cPath=144_149
I've tried connecting it to an O-Scope to see if my input signal is correct and that is also not working. attached is a link to the sensor I'm using as well as code as I have.
Any help would be greatly appreciated. Thank you.
https://www.seeedstudio.com/depot/ultra-sonic-range-measurement-module-p-626.html?cPath=144_149
Code:
/*
* sonartest.c
* When the sonsar function is called, a short pulse of
* 8 us is sent to the sonar sensor to trigger it through
* P3.2, the the UC waits for the sonar to use the provided
* echo to send an ultrasonic signal that hits the objects
* within its range and bounces back to the sonar's reciever
* indicating some object at a certain distance was detected
* P3.2 which is an interrupt controlled pin that allows the
* timers to capture the time when an interrupt happens.
* We use such feature along with Timer0 to capture the time
* from when the sonar sends the ultrasonic signal until it
* recieves the boucned back signal
* then we convert the time captured to distance to find
* how close is the object from the fan
*/
#include <reg51rd2.h>
#include <intrins.h>
void delay(unsigned int NumMillisecs);
void sonar();
void InitISR();
// get high order byte from word
#define High(X) ((unsigned char)((X&0xFF00)>>8))
// get low order byte from word
#define Low(X) ((unsigned char)(X&0x00FF))
// 1 millisecond
#define Msec1 64536
// counter for T1 interrupt
volatile unsigned long Ticker;
unsigned int time;
unsigned int distance;
unsigned int temp;
unsigned int d;
sbit sonarO = P3^2;
sbit LED = P1^0;
sbit LED2 = P1^1;
sbit LED3 = P1^2;
void main()
{
//init the variables once
distance = 0;
time = 0;
temp = 0;
LED = 0;
LED2=0;
LED3=0;
//init the timers
InitISR();
//loop forever
while(1)
{
//measure the distance
sonar();
//if distance is less than or equal 61 cm (2 feet) turn on led 1.
if (distance <= 61 && distance !=38)
{
LED = 1;
LED2 =0;
LED3 = 0;
}
//if distance is less than 122 cm (4 feet) but greater than 61 cm (2ft).
else if (distance <=122 && distance >61)
{
LED =0;
LED2 = 1;
LED3 =0;
}
//if distance is less than 183cm (6ft) bu greater than 122(4ft)
else if (distance <=183 && distance >122)
{
LED = 0;
LED2 = 0;
LED3=1;
}
} // end while
}//end main
void sonar()
{
//make the pin as an output to send a short pulse
sonarO = 0;
sonarO = 1;
//wait 10 uS to complete the pulse signal
//_nop_() when called provies a delay of 1us
_nop_(); // 1 us
_nop_(); // 2 us
_nop_(); // 3 us
_nop_(); // 4 us
_nop_(); // 5 us
_nop_(); // 6 us
_nop_(); // 7 us
_nop_(); // 8 us
_nop_(); // 9 us
_nop_(); // 10 us
// pulse complete L-H-L
sonarO = 0;
//make it input pin to read the pulse width
sonarO = 1;
//TMOD is set to be gate controlled through P3.2 meaning
//it starts the timer when it reads an input from the sonar
//Timer0, 16bit gate controlled(P3.2)
TMOD = 0x09;
//Reset the timers
TH0 = 0;
TL0 = 0;
//Timer will only run when Pin becomes high.
TR0 = 1;
//wait till pin is low
while(!sonarO);
//when the pin becomes High again
//means we detected something
//wait for pulse to finish
while(sonarO);
//timer stopped automatically as soon as P3.2 goes low
//completely stop timer.
TR0 = 0;
//the time is caputred in TH0 and TL0
//convert it to 16 bit in temp then multiply temp by 1.08uS to get the total time traveled
//divide it by 2 to get the time from sensor to object
//then convert it to distance
temp = TH0;
temp = (temp<<8) | TL0;
time = temp;
/*We first calculate the max distance can be detected by the sonar sensor:
max distance = (18.5ms * 340 m/s)/2 = 3.145 m
then we use a prescalar in the calculation of the distance we measure...
8 is a good prescalar for those who uses a crystal of 11.059 MHZ ...
x = 1 / [(prescalar * max distance)/(frequency * 18.5 ms)]
so distance = time / x ( the 16-bit time capatured from TH0 and TL0)
this will give us the distance in meters so to get them in cm,
we divide x by 100 to equal 81.
*/
distance = (time / 58); // in centimeter
}
void InitISR()
{
Ticker = 0;
EA = 0;
TH1 = High(Msec1);
TL1 = Low(Msec1); // load initial values for timer
TMOD = (TMOD & 0x0F) | 0x19; // sets timer1 to Mode 1, a 16-bit non-
//reload, and timer 0 to be gate controlled
//through P3.2
ET1 = 1; // enable timer 1 interrupt
TR1 = 1; // turns on timer 1
EA = 1; // enable interrupts
}
// Programmable Timer 1 Interrupt Service Routine
void ProgrammableTimer1_ISR() interrupt 3 using 1
{
TH1 = High(Msec1); // Reset the clock for next interrupt
TL1 = Low(Msec1);
Ticker++; // bump ticker count...
}
// delay routine...
void delay( unsigned int NumMillisecs )
{
unsigned long DelayTickValue;
DelayTickValue = Ticker + NumMillisecs;
while( Ticker < DelayTickValue );
}