EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Replies / Use this Function

Use this Function

|

Microcontroller › 8051 › Interfacing AT89S51 with ultrasonic sensor and LCD › Use this Function

January 30, 2018 at 6:29 pm #14704
Shirsendu Das
Participant

#this function will only work on 8051 @12MHz

unsigned int get_distance()
{
unsigned int time;
trig=1;trig=1;trig=1;trig=1;trig=1; // 5us /
TMOD=0x01; // 2us / use timer0 16 bit mode
TH0=0x00; // 1us / clear reg
TL0=0x00; // 1us / clear reg
trig=0; // 1us / Total(5+2+1+1+1)=10us
while(!echo); // Wait until echo beome high
TR0=1; // Start the Timer
while(echo); // Wait until echo become low
TR0=0; // Stop the timer
time=((TH0<<8)+TL0)/58; // Load timer value to register. 58us per cm.
return time;
}

RSS Recent Posts

  • Why aren’t the power windows in my 2006 Volkswagen Polo 2006 working despite repairing the control unit circuit board? May 10, 2026
  • Voltage comparator circuit verification May 10, 2026
  • What is this??? May 9, 2026
  • infiniBand Layer 1 specifications testing question May 8, 2026
  • block RF May 8, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise