Ping ultrasonic distance sensor(parallax)_lcd_htc

Jon Wilder

Joined Oct 25, 2011
23
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.
 

John P

Joined Oct 14, 2008
2,025
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.
 

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
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.
 

John P

Joined Oct 14, 2008
2,025
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:

Rich (BB code):
  bit_clear(trisc, 2);            // Make pin an output
  bit_set(portc, 2);              // Output high
  #ASM
    NOP
    NOP                              // Hold it 3 usec
  #ENDASM
  bit_clear(portc, 2);            // Ultrasonic sensor triggered
  bit_clear(tmr1con, 0);        // Stop TMR1
  tmr1h = tmr1l = 0;             // Clear timer registers
  bit_set(trisc, 2);               // Pin is now an input
  ccp1con = 0b00000100;     // Capture on falling edge
  bit_clear(pir1, 2);              // Clear interrupt flag (in case it was already set)
  bit_set(tmr1con, 0);          // Start TMR1
  ......                               // Wait for echo return--no need to poll frequently
  if (bit_test(pir1, 2))           // Heard echo?
    distance = (ccpr1h << 8) + ccpr1l;    // Time to end of sensor pulse, in usec
 

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
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.

@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^^
 

Thread Starter

Jaden5165

Joined Sep 9, 2011
69
Rich (BB code):
#include<htc.h>
#define _XTAL_FREQ 4e6
__CONFIG(0x3F3A);
#define RS RC4
#define EN RC5
#define databits PORTB
#define pingPin RC7




void usrt_init()
{
TXEN=1;
SYNC=0;
BRGH=0; // low baud rate
SPBRG=5.5; //baud rate 9600
TRMT=1;
SPEN=1;
CREN=1;
}

void timer()
{
int count=0;
while(pingPin==0)
{
__delay_us(1);
count++;
if(count==19000)
{
break;
}
else
{
//distance calculation???
}
}



void ultrasonic()
{
while(1)
{
TRISC7=0;
pingPin=0;//low
__delay_us(2);
pingPin=1;//high
__delay_us(5);
pingPin=0;//low
TRISC7=1;
timer()
}

}

void LCD_STROBE(void)
{
EN = 1;
__delay_us(2);
EN = 0;
}

void lcddata(unsigned char c)
{
RS = 1;
__delay_us(50);
databits = (c >> 4);
LCD_STROBE();
databits = (c);
LCD_STROBE();
}

void cmd(unsigned char c)
{
RS = 0;
__delay_us(50);
databits = (c >> 4);
LCD_STROBE();
databits = (c);
LCD_STROBE();
}

void clear(void)
{
cmd(0x01);
__delay_ms(2);
}

void lcd_init()
{
__delay_ms(15);
cmd(0x28);
__delay_ms(1);
cmd(0x28);
__delay_us(100);
cmd(0x28);
cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
cmd(0x28); // Function set (4-bit interface, 2 lines, 5*7Pixels)
cmd(0x0c); // Make cursorinvisible
clear(); // Clear screen
cmd(0x06); // Set entry Mode(auto increment of cursor)
cmd(0x80);
}

/*void interrupt_enable()
{
GIE=1;
PEIE=1;
RCIE=1;
}*/

/*void interrupt UART() //interrupt service routine
{
	tag_id=RCREG;
	i++;	
}*/



void main()
{
TRISB=0;
TRISC4 = 0;
TRISC5 = 0;
usrt_init();
interrupt_enable();
lcd_init();
ultrasonic();










 
}




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.
 
Top