Ping ultrasonic distance sensor(parallax)_lcd_htc

Discussion in 'Embedded Systems and Microcontrollers' started by Jaden5165, Oct 24, 2011.

  1. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0
    hi,can somebody provide me a code that using the ping ultrasonic sensor #28015 when it sense it will send the distance and show in lcd.I need a code which cab be cimpiled using Hi tech c.thank you.i'm using pic16f877a with 4mhz crystal


    this is the datasheet of my ping untrasonic sensor:
    http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
     
  2. Jon Wilder

    New Member

    Oct 25, 2011
    23
    1
    If it were my code, I would use one of the timers on the PIC to time the ping time. Set the emitter to emit the sonic pulse, clear the timer, then poll the I/O pin that the detector is on until it changes state (ultrasonic pulse detected). Once it changes state, read the value of the timer and use math to convert that value into distance.
     
  3. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    And if it were my code, I'd use one of the capture pins, Portc.1 or Portc.2. When an event occurs on the pin (highgoing or lowgoing) the value of Timer1 is loaded to a buffer, and an interrupt is triggered. So you'd start a rangefinder reading by sending the output pulse on the same pin, then set the pin to input, zero the timer, start the timer and enable the interrupt, or skip that last step and plan to poll the interrupt flag instead.
     
  4. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0
    ermmm,do you(@Jon Wilder and @John P) mind to post your code here?i hope you can as i can understand more clearly.thank you.
     
  5. Jon Wilder

    New Member

    Oct 25, 2011
    23
    1
    In order to write code for a circuit we'd first need to see the circuit so we know what hardware is on what pin.
     
  6. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    My compiler is CCS, not Hi Tech. But what I'd do is like this (remembering that at 4MHz, an instruction is 1usec). Untested code, worth what you paid for it:

    Code ( (Unknown Language)):
    1.  
    2.   bit_clear(trisc, 2);            // Make pin an output
    3.   bit_set(portc, 2);              // Output high
    4.   #ASM
    5.     NOP
    6.     NOP                              // Hold it 3 usec
    7.   #ENDASM
    8.   bit_clear(portc, 2);            // Ultrasonic sensor triggered
    9.   bit_clear(tmr1con, 0);        // Stop TMR1
    10.   tmr1h = tmr1l = 0;             // Clear timer registers
    11.   bit_set(trisc, 2);               // Pin is now an input
    12.   ccp1con = 0b00000100;     // Capture on falling edge
    13.   bit_clear(pir1, 2);              // Clear interrupt flag (in case it was already set)
    14.   bit_set(tmr1con, 0);          // Start TMR1
    15.   ......                               // Wait for echo return--no need to poll frequently
    16.   if (bit_test(pir1, 2))           // Heard echo?
    17.     distance = (ccpr1h << 8) + ccpr1l;    // Time to end of sensor pulse, in usec
    18.  
     
  7. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0

    @John

    I'm using pic16f877a and a 4mhz crystal.I want the ping ultrasonic sensor when detect a range it will then calculate and show in lcd.i havent connect the circuit buti need the code to have more understanding.can you write me a code that can be understood by HTC?
    due to time constraints i really need it as i still hv to combine my other programming to become a project but i stuck in this very long so i need help here.
    thank you^^
     
  8. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0
    Code ( (Unknown Language)):
    1.  
    2.  
    3. #include<htc.h>
    4. #define _XTAL_FREQ 4e6
    5. __CONFIG(0x3F3A);
    6. #define RS RC4
    7. #define EN RC5
    8. #define databits PORTB
    9. #define pingPin RC7
    10.  
    11.  
    12.  
    13.  
    14. void usrt_init()
    15. {
    16. TXEN=1;
    17. SYNC=0;
    18. BRGH=0; // low baud rate
    19. SPBRG=5.5; //baud rate 9600
    20. TRMT=1;
    21. SPEN=1;
    22. CREN=1;
    23. }
    24.  
    25. void timer()
    26. {
    27. int count=0;
    28. while(pingPin==0)
    29. {
    30. __delay_us(1);
    31. count++;
    32. if(count==19000)
    33. {
    34. break;
    35. }
    36. else
    37. {
    38. //distance calculation???
    39. }
    40. }
    41.  
    42.  
    43.  
    44. void ultrasonic()
    45. {
    46. while(1)
    47. {
    48. TRISC7=0;
    49. pingPin=0;//low
    50. __delay_us(2);
    51. pingPin=1;//high
    52. __delay_us(5);
    53. pingPin=0;//low
    54. TRISC7=1;
    55. timer()
    56. }
    57.  
    58. }
    59.  
    60. void LCD_STROBE(void)
    61. {
    62. EN = 1;
    63. __delay_us(2);
    64. EN = 0;
    65. }
    66.  
    67. void lcddata(unsigned char c)
    68. {
    69. RS = 1;
    70. __delay_us(50);
    71. databits = (c >> 4);
    72. LCD_STROBE();
    73. databits = (c);
    74. LCD_STROBE();
    75. }
    76.  
    77. void cmd(unsigned char c)
    78. {
    79. RS = 0;
    80. __delay_us(50);
    81. databits = (c >> 4);
    82. LCD_STROBE();
    83. databits = (c);
    84. LCD_STROBE();
    85. }
    86.  
    87. void clear(void)
    88. {
    89. cmd(0x01);
    90. __delay_ms(2);
    91. }
    92.  
    93. void lcd_init()
    94. {
    95. __delay_ms(15);
    96. cmd(0x28);
    97. __delay_ms(1);
    98. cmd(0x28);
    99. __delay_us(100);
    100. cmd(0x28);
    101. cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
    102. cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
    103. cmd(0x0c); // Make cursorinvisible
    104. clear(); // Clear screen
    105. cmd(0x06); // Set entry Mode(auto increment of cursor)
    106. cmd(0x80);
    107. }
    108.  
    109. /*void interrupt_enable()
    110. {
    111. GIE=1;
    112. PEIE=1;
    113. RCIE=1;
    114. }*/
    115.  
    116. /*void interrupt UART() //interrupt service routine
    117. {
    118.     tag_id[i]=RCREG;
    119.     i++;   
    120. }*/
    121.  
    122.  
    123.  
    124. void main()
    125. {
    126. TRISB=0;
    127. TRISC4 = 0;
    128. TRISC5 = 0;
    129. usrt_init();
    130. interrupt_enable();
    131. lcd_init();
    132. ultrasonic();
    133.  
    134.  
    135.  
    136.  
    137.  
    138.  
    139.  
    140.  
    141.  
    142.  
    143.  
    144. }
    145. [/i]




    Can you guys help me see my coding and how am i going to continue?
    actually i'm using PIC16f877A and the timer method different from here so i'm confusing...

    http://www.microcontrollerboard.com/pic-timer1-tutorial.html
    the link is the timer method for pic16f877,and i'm using 4Mhz crystal means that if use the formula (1/fosc)*12=3us.


    i really try my best to read and read and understand but i still cannot really understand as i not really know well in using timer and i still dont know how to calcualte the distance to be shown.


    please correct my code if there is any mistakes.
     
Loading...