Discussion in "8051 Discussion Forum" started by    shama3ey    Oct 26, 2009.
Wed Nov 18 2009, 01:44 am
#21


I did these calculations and it worked so nice and smooth... but still Thanks to Exp. and Ajay who helped me figure out how to send pulse to the sensor and capture the time the pulse takes to bounce back to the sensor

shama3ey


Glad it worked in the end and thanks for the update.
I'll take all the thanks I can get
Wed Nov 18 2009, 03:19 am
#22
congrats

I would like to have your Ping application in my vault it might help others in future. If you are ready to share then pass it on to my email.
Wed Nov 18 2009, 03:23 am
#23
sure I will email it to you
Wed Nov 18 2009, 03:38 am
#24
finish it completely with circuit, code, hex file, screenshot(optional) and documentation (optional)

email is contact (@) rickeyworld.info
Wed Nov 18 2009, 03:39 am
#25
Alright then give me two weeks to do so as I turn my report I will have all the documentation ready as well as the coding
Wed Nov 18 2009, 05:20 pm
#26
take your time good luck!
Fri Mar 11 2011, 07:33 am
#27
Hi,
I am doing similar project using ping sensor and also the 8051 microcontroller
however I am very new to this programming language (MikroC pro for 8051).
I tried to create the program using the pseudocode above
I need your help to comment on my program.
Are there any addtional program that I need to add it up?

#include <8051.h>
#include <stdio.h>

void main(void) {
unsigned char distance;
unsigned char time;
unsigned int temp;

sfr P3_2 = 0;
sfr P3_2= 1;
//SonarO connect to P3.2
delay_us(5);
//wait for 5 microsecond
sfr P3_2=0;
//Complete LHL

delay_us(750);
//t holdoff
sfr P3_2 = 1;
//make input pin to read pulse width
TMOD = 0x01;
// sets timer to a 16 bit continuous timer
while(1)
{
TR0 = 1; // Start Timer0
TH0 = 0XFF; // Initialize timer0 MSB (timer for 100us)
TL0 = 0X9C; // Initialize timer0 LSB
while(sfr P3_2==0);//wait till pin is low
while(sfr P3_2==1);//wait for pulse to finish
temp = TH0;
temp = (temp<<8) | TL0;
TR0 = 0;//completely stop timer
time= (temp*1.08*10^-6)/2;
distance = 344 *( time/2);
delay_us(200);
}

}

Comment will be deeply appreciated.
Sun Mar 20 2011, 12:36 pm
#28
@samid91 please create your own thread..
Wed Apr 23 2014, 12:31 am
#29
Actually there are 2 types of sensor in market 1) 3pin Ultrasonic sensor which gives Serial out (i.e ping) and 2) 4pin Ultrasonic sensor with PWM out (i.e. HC SR04)
Just copy paste following program ready to use
The program to calculate distance between obstacle and sensor is ::::>>

// Author: Swapnil Nalawade ---------Email:- [email protected]
// Board: Ultrasonic Sensor PWM Out (i.e 4 PIN china ultrasonic sensor OR HC_SR04) 100 % working and Tested
// The program can Measure distance and output as serial string
// Compiler: Keil C51 MCU: AT89S52

#include <reg52.h> // standard 8051 defines
#include <stdio.h> // for printf()
#include<intrins.h> // for nop()

// Sensor Connections
sbit TRIG = P3^3;
// Note: ECHO pin is connected to P3_2 which is also INT0 interrupt pin, this cannot be changed

unsigned int T0_ISR_count = 0;

unsigned long high_time_us; // 32 bit variable, stores pulse time in uS
unsigned long distance_cm; // 32 bit variable, Distance Value in Centimeter
unsigned long distance_in; // 32 bit variable, Distance Value in Inches

unsigned int i;

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// -=-= Timer 0 Overflow Interrupt -=-=-=-=
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void T0_ISR (void) interrupt 1
{
T0_ISR_count++; // Increment overflow counter
TF0 = 0; // Reset the interrupt request
}

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// -=-=-=-=- Delay x ms -=-=-=-=-=-=-=
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void delay_ms(int x) // delays x msec (at fosc=11.0592MHz)
{
int y=0;
while(x>=0)
{
for (y=0; y<100; y++);
x--;
}
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// -=-=-=-=- Delay 10uS -=-=-=-=-=-=-=
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#define delay_10us() _nop_();_nop_();_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_()

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// -=-=-=-=- Init Serial Port -=-=-=-=-=-=-=
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void serial_init() {
/*--------------------------------------
Set serial port for 9600 baud at
11.0592 MHz. Note that we use Timer 1
for the baud rate generator.
--------------------------------------*/
SCON = 0x50;
TMOD |= 0x20;
TH1 = 0xFA;
TR1 = 1;
TI = 1;
PCON |= 0x80;
}

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// -=-=-=-=- Main Program -=-=-=-=-=-=-=
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void main (void)
{

serial_init(); // Init serial port at 9600

TRIG = 0; // Keep pin low at startup

printf ("\nUltrasonic Distance Sensor\n");

/*--------------------------------------
Enable interrupts for timer 0.
--------------------------------------*/
ET0 = 1;
EA = 1;

/*--------------------------------------
Set Timer0 for 16-bit interval timer
mode.
--------------------------------------*/
TMOD = (TMOD & 0xF0) | 0x09;

while (1) // Loop Forever
{

// Send 10uS trigger Pulse
TRIG = 1; // Trigger Pin Made High
delay_10us(); // Wait 10uS
TRIG = 0; // Make it low, this starts ranging process


// Start pulse measurement
/*--------------------------------------
Clear the timer overflow counter and
the timer low and high registers. Then,
start the timer.
--------------------------------------*/

T0_ISR_count = 0; // Timer0 Counter made zero
TH0 = 0;
TL0 = 0;

TR0 = 1; // Start Timer0

/*--------------------------------------
Wait for the pulse to start. Then, wait for the pulse to end.
--------------------------------------*/
i=0;
while (!INT0)
{
i++;
if(i>20000) // timeout, so abort
break;
}
i=0;
while (INT0)
{
i++;
if(i>20000) // timeout, so abort
break;
}

/*--------------------------------------
Compute the width of the pulse -- one
clock cycle is 1.085us for a standard 8051 at 11.0592 Mhz Crystal
--------------------------------------*/

high_time_us = (unsigned long)((TH0 << 8) | TL0 | ((unsigned long)T0_ISR_count << 16))*1.085;

distance_cm = high_time_us/58; // calculate Distance in Centimeters
distance_in = high_time_us/148; // calculate Distance in Inches

// Print the values to serial port
if(distance_cm > 0 && distance_cm < 500) // check valid range then only print
printf("Distance: %6lu cm %6lu Inch PW=%6lu uS\n", distance_cm, distance_in, high_time_us);

delay_ms(250);
}
}

/*---------------------------------------------------------
---------------------------------------------------------*/

Wed Apr 23 2014, 11:35 am
#30
@Androswap you can share your code/project with us. It will be useful for everyone. Please email/PM me details at contact [at] rickeyworld [dot] info

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Lewisuhakeply
Thu Apr 18 2024, 06:00 pm
Darrellciz
Thu Apr 18 2024, 11:07 am
Charlessber
Thu Apr 18 2024, 09:29 am
BartonSem
Thu Apr 18 2024, 04:56 am
DonaldKnown
Thu Apr 18 2024, 12:24 am
utaletxcyw
Wed Apr 17 2024, 10:21 am
Anthonyvab
Wed Apr 17 2024, 08:48 am
RobertCix
Wed Apr 17 2024, 06:46 am