Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Ultrasonic sensor code explanation

Status
Not open for further replies.

Hasan Malik

Junior Member level 1
Joined
May 26, 2012
Messages
18
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,380
Can someone explain this comment of that code, how does timer0 one cycle calculated to be 4.352 cm>>???



//speed of sound 340m/s
//1 TMR0 cycle = (340/1000000)*256*0.5*100 = 4.352 in cm


#define TRIG PORTD.F0
#define ECHO PORTD.F1

double distance;
void main() {

TRISD.F0 = 1;
TRISD.F1 = 0;
TRISC=0;

OPTION_REG.T0CS = 0;
OPTION_REG.PSA = 0;
OPTION_REG.PS0 = 1;
OPTION_REG.PS1 = 1;
OPTION_REG.PS2 = 1;

while(1){

TRIG = 0;
delay_us(10000);
TRIG = 1;
delay_us(10);
TRIG = 0;

while(ECHO==0);
TMR0=0;
while(ECHO==1);
distance = (double)TMR0 * 4.352;
PORTC = (char)distance;//to check
Delay_ms(100);
}

}
 

This might be a little bit late but the answer is fairly simple. If you consider the clock frequency as 2Mhz, the person who wrote this code is taking an 8 bit counter (256 counts) to find out the time it takes to fill up the counter. He then multiplies that with the speed of sound in air, giving the distance traveled. All the 10's in the equation are just conversions from one unit to the other.
 

hey..i've been trying to use the interrupt capture unit to get the time..... you can even look into that as well.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top