Ultra sonic sensor

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
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
  • 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]










 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hi,
How to use this to show distance in Cm

sprintf( Distance, "%dcm",a);




C:
#include <htc.h>
#include <stdio.h>
#include <stdlib.h>

__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000

#define DATA PORTC
#define EN RB7
#define RS RB6
#define N 1
#define M 1            // DELAY  FOR LCD


unsigned long number=0;        // Number MUST be a long for fixed point
int ch=0;
unsigned char LCDbuffer[17];
unsigned char m[21]="WATER LEVEL";
unsigned char n[21]="OUT OF RANGE";
void LCDcmd(char x), LCDdata(char x);
void LCD_goto(char line, char column)    ;

void LCD_printR(char *str);
main(){
  TRISB = 0b00010000;
  
    TRISC=0X00;
  
PORTC=0X00;
PORTB=0X00;
__delay_ms(50);
     LCDcmd(0x38); // init
    __delay_ms(N);
    LCDcmd(0x38); // init
    __delay_ms(N);
    LCDcmd(0x38); // Function set
__delay_ms(N);
     LCDcmd(0x06); // Cursor move increase, no display shift
    __delay_ms(N);
    LCDcmd(0x0C); // Display on, cursor off, not blinking
    __delay_ms(N);
    LCDcmd(0x01); // Clear display. goto pos 1
    __delay_ms(N);

    RB5=1;                                                                                                                                                                                                                                                                                                                                                                                    
__delay_ms(1000);
RB5=0;
__delay_ms(500);
RB5=1;                                                                                                                                                                                                                                                                                                                                                                                    
__delay_ms(1000);
RB5=0;
__delay_ms(500);

while(1){

LCD_goto(1,1);
        LCD_printR(m);



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/147;                //Converts Time to Distance
                      //Distance Calibration
    if(a>=2 && a<=400)          //Check whether the result is valid or not
    {

       RB5=1;
LCD_goto(2,1);
          
        LCDbuffer[0] = ((a % 1000) / 100)+ 48;
        LCDbuffer[1] = ((a % 100) / 10)+ 48;
        LCDbuffer[2] = (a % 10) + 48;
    LCDbuffer[3] = 0;
LCDbuffer[4] = 0;
LCDbuffer[5] = 0;
LCDbuffer[6] = 0;
     [B]   LCD_printR(LCDbuffer);
sprintf(LCDbuffer, "level = %d",a);[/B]




    }
    else
    {

LCD_goto(2,1);
        LCD_printR(n);
      RB5=0;
    }
    __delay_ms(400);
  }
}











void LCDcmd(char x)
    {
    __delay_ms(N);
    RS=0;
  
    __delay_ms(M);
    EN=1;
    __delay_ms(M);
    DATA = x;    // Command data
__delay_ms(M);
    EN=0;
    __delay_ms(M);
    }
void LCDdata(char x)
    {
    RS=1;

    __delay_ms(M);
    EN=1;
    __delay_ms(M);
    DATA = x;  //  Print data
    __delay_ms(M);
    EN=0;
    __delay_ms(M);
    }
void LCD_goto(char line, char column)        // combines line and lineW
    {
    unsigned char data = 0x80;                // default to 1
    if(line == 2)data = 0xc0;                // change to 2
    data += column;                            // add in  column
     LCDcmd(data);
    }

void LCD_printR(char * str)             // This passes the start of a RAM character array
    {                                // by default the pointer points to data section
    while(*str != 0)                // while the character pointed to isn't 0
        LCDdata(*str++);            // print out the character, then increment
    }
 
Last edited:
Top