Ultra sonic sensor

Discussion in 'Embedded Systems and Microcontrollers' started by RRITESH KAKKAR, Nov 4, 2014.

  1. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    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)



    Code (C):
    1. #include <htc.h>
    2. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    3. #define _XTAL_FREQ 20000000
    4. main(){
    5.   TRISB = 0b00010000;
    6.    RB5=1;
    7. __delay_ms(1000);
    8. RB5=0;
    9. __delay_ms(500);
    10. RB5=1;
    11. __delay_ms(1000);
    12. RB5=0;
    13. __delay_ms(500);
    14.  
    15. while(1){
    16.  
    17.  
    18.  
    19.  
    20.  
    21. int a;
    22.   T1CON = 0x10;  //Initialize Timer Module
    23.   TMR1H = 0;  //Sets the Initial Value of Timer
    24.   TMR1L = 0;  //Sets the Initial Value of Timer
    25.  
    26.   RB0 = 1;  //TRIGGER HIGH
    27.   __delay_us(10);  //10uS Delay
    28.   RB0 = 0;  //TRIGGER LOW
    29.  
    30.   while(!RB4);  //Waiting for Echo
    31.   TMR1ON=1;  //Timer Starts
    32.   while(RB4);  //Waiting for Echo goes LOW
    33.   TMR1ON=0;  //Timer Stops
    34.  
    35.   a = (TMR1L | (TMR1H<<8));  //Reads Timer Value
    36.   a = a/58;  //Converts Time to Distance
    37.   a = a + 1;  //Distance Calibration
    38.   if(a>=2 && a<=400)  //Check whether the result is valid or not
    39.   {
    40.   RB5=1;
    41.   }
    42.   else
    43.   {
    44.   RB5=0;
    45.   }
    46.   __delay_ms(400);
    47.   }
    48. }
    49.  
    50.  
    51.  
    52.  
    53.  
    54.  
    55.  
    56.  
    57.  
    58.  
    59.  
    60.  
    61.  
    62.  
    63.  
    64.  
    65.  
    66.  
    67.  
    68.  
    69.  







    [/CODE]










     
  2. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    Hi,
    How to use this to show distance in Cm

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




    Code (C):
    1. #include <htc.h>
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4.  
    5. __CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
    6. #define _XTAL_FREQ 20000000
    7.  
    8. #define DATA PORTC
    9. #define EN RB7
    10. #define RS RB6
    11. #define N 1
    12. #define M 1            // DELAY  FOR LCD
    13.  
    14.  
    15. unsigned long number=0;        // Number MUST be a long for fixed point
    16. int ch=0;
    17. unsigned char LCDbuffer[17];
    18. unsigned char m[21]="WATER LEVEL";
    19. unsigned char n[21]="OUT OF RANGE";
    20. void LCDcmd(char x), LCDdata(char x);
    21. void LCD_goto(char line, char column)    ;
    22.  
    23. void LCD_printR(char *str);
    24. main(){
    25.   TRISB = 0b00010000;
    26.  
    27.     TRISC=0X00;
    28.  
    29. PORTC=0X00;
    30. PORTB=0X00;
    31. __delay_ms(50);
    32.      LCDcmd(0x38); // init
    33.     __delay_ms(N);
    34.     LCDcmd(0x38); // init
    35.     __delay_ms(N);
    36.     LCDcmd(0x38); // Function set
    37. __delay_ms(N);
    38.      LCDcmd(0x06); // Cursor move increase, no display shift
    39.     __delay_ms(N);
    40.     LCDcmd(0x0C); // Display on, cursor off, not blinking
    41.     __delay_ms(N);
    42.     LCDcmd(0x01); // Clear display. goto pos 1
    43.     __delay_ms(N);
    44.  
    45.     RB5=1;                                                                                                                                                                                                                                                                                                                                                                                    
    46. __delay_ms(1000);
    47. RB5=0;
    48. __delay_ms(500);
    49. RB5=1;                                                                                                                                                                                                                                                                                                                                                                                    
    50. __delay_ms(1000);
    51. RB5=0;
    52. __delay_ms(500);
    53.  
    54. while(1){
    55.  
    56. LCD_goto(1,1);
    57.         LCD_printR(m);
    58.  
    59.  
    60.  
    61. int a;
    62.     T1CON = 0x10;                 //Initialize Timer Module
    63.     TMR1H = 0;                  //Sets the Initial Value of Timer
    64.     TMR1L = 0;                  //Sets the Initial Value of Timer
    65.  
    66.     RB0 = 1;               //TRIGGER HIGH
    67.     __delay_us(10);               //10uS Delay
    68.     RB0 = 0;               //TRIGGER LOW
    69.  
    70.     while(!RB4);           //Waiting for Echo
    71.     TMR1ON=1;               //Timer Starts
    72.     while(RB4);            //Waiting for Echo goes LOW
    73.     TMR1ON=0;               //Timer Stops
    74.  
    75.     a = (TMR1L | (TMR1H<<8));   //Reads Timer Value
    76.     a = a/147;                //Converts Time to Distance
    77.                       //Distance Calibration
    78.     if(a>=2 && a<=400)          //Check whether the result is valid or not
    79.     {
    80.  
    81.        RB5=1;
    82. LCD_goto(2,1);
    83.          
    84.         LCDbuffer[0] = ((a % 1000) / 100)+ 48;
    85.         LCDbuffer[1] = ((a % 100) / 10)+ 48;
    86.         LCDbuffer[2] = (a % 10) + 48;
    87.     LCDbuffer[3] = 0;
    88. LCDbuffer[4] = 0;
    89. LCDbuffer[5] = 0;
    90. LCDbuffer[6] = 0;
    91.      [B]   LCD_printR(LCDbuffer);
    92. sprintf(LCDbuffer, "level = %d",a);[/B]
    93.  
    94.  
    95.  
    96.  
    97.     }
    98.     else
    99.     {
    100.  
    101. LCD_goto(2,1);
    102.         LCD_printR(n);
    103.       RB5=0;
    104.     }
    105.     __delay_ms(400);
    106.   }
    107. }
    108.  
    109.  
    110.  
    111.  
    112.  
    113.  
    114.  
    115.  
    116.  
    117.  
    118.  
    119. void LCDcmd(char x)
    120.     {
    121.     __delay_ms(N);
    122.     RS=0;
    123.  
    124.     __delay_ms(M);
    125.     EN=1;
    126.     __delay_ms(M);
    127.     DATA = x;    // Command data
    128. __delay_ms(M);
    129.     EN=0;
    130.     __delay_ms(M);
    131.     }
    132. void LCDdata(char x)
    133.     {
    134.     RS=1;
    135.  
    136.     __delay_ms(M);
    137.     EN=1;
    138.     __delay_ms(M);
    139.     DATA = x;  //  Print data
    140.     __delay_ms(M);
    141.     EN=0;
    142.     __delay_ms(M);
    143.     }
    144. void LCD_goto(char line, char column)        // combines line and lineW
    145.     {
    146.     unsigned char data = 0x80;                // default to 1
    147.     if(line == 2)data = 0xc0;                // change to 2
    148.     data += column;                            // add in  column
    149.      LCDcmd(data);
    150.     }
    151.  
    152. void LCD_printR(char * str)             // This passes the start of a RAM character array
    153.     {                                // by default the pointer points to data section
    154.     while(*str != 0)                // while the character pointed to isn't 0
    155.         LCDdata(*str++);            // print out the character, then increment
    156.     }
    157.  
    158.  
    159.  
    160.  
    161.  
    162.  
    163.  
    164.  
    165.  
    166.  
    167.  
    168.  
    169.  
     
    Last edited: Nov 6, 2014
Loading...