Hi,
I was working on Ultra sonic sensor for that i need to calculate the distance from it, i have this code working fine in my PIC16F877 the LED is connected for signal (working or not)
anyway i need to know the use of 58 value, actually it was float so, i convert to decimal.
LCD will be added that part i will do myself.
but i want to increase it sensitivity. more and need to know the CALCULATION 20Mhz Crystal is being used.
a = a/58; //Converts Time to Distance
a = a + 1; //Distance Calibration
Fosc is the oscillator frequency, here we are using 8MHz crystal hence Fosc = 8MHz.
Time = (TMR1H:TMR1L)*(1/Internal Clock)*Prescaler
Internal Clock = Fosc/4 = 8MHz/4 = 2MHz
Therefore, Time = (TMR1H:TMR1L)*2/(2000000) = (TMR1H:TMR1L)/1000000
Distance Calculation
[/CODE]
I was working on Ultra sonic sensor for that i need to calculate the distance from it, i have this code working fine in my PIC16F877 the LED is connected for signal (working or not)
anyway i need to know the use of 58 value, actually it was float so, i convert to decimal.
LCD will be added that part i will do myself.
but i want to increase it sensitivity. more and need to know the CALCULATION 20Mhz Crystal is being used.
a = a/58; //Converts Time to Distance
a = a + 1; //Distance Calibration
Fosc is the oscillator frequency, here we are using 8MHz crystal hence Fosc = 8MHz.
Time = (TMR1H:TMR1L)*(1/Internal Clock)*Prescaler
Internal Clock = Fosc/4 = 8MHz/4 = 2MHz
Therefore, Time = (TMR1H:TMR1L)*2/(2000000) = (TMR1H:TMR1L)/1000000
Distance Calculation
- Distance = Speed * Time
- Let d be the distance between Ultrasonic Sensor and Target
- Total distance traveled by the ultrasonic burst : 2d (forward and backward)
- Speed of Sound in Air : 340 m/s = 34000 cm/s
- Thus, d = (34000*Time)/2, where Time = (TMR1H:TMR1L)/(1000000)
- Therefore, d = (TMR1H:TMR1L)/58.82 cm
- TMR1H:TMR1L = TMR1L | (TMR1H<<8)
C:
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
main(){
TRISB = 0b00010000;
RB5=1;
__delay_ms(1000);
RB5=0;
__delay_ms(500);
RB5=1;
__delay_ms(1000);
RB5=0;
__delay_ms(500);
while(1){
int a;
T1CON = 0x10; //Initialize Timer Module
TMR1H = 0; //Sets the Initial Value of Timer
TMR1L = 0; //Sets the Initial Value of Timer
RB0 = 1; //TRIGGER HIGH
__delay_us(10); //10uS Delay
RB0 = 0; //TRIGGER LOW
while(!RB4); //Waiting for Echo
TMR1ON=1; //Timer Starts
while(RB4); //Waiting for Echo goes LOW
TMR1ON=0; //Timer Stops
a = (TMR1L | (TMR1H<<8)); //Reads Timer Value
a = a/58; //Converts Time to Distance
a = a + 1; //Distance Calibration
if(a>=2 && a<=400) //Check whether the result is valid or not
{
RB5=1;
}
else
{
RB5=0;
}
__delay_ms(400);
}
}
[/CODE]