PDA

View Full Version : Interfacing ASCII based Ultrasonic Sensor With PIC16F877A



nitinmathew2007
06-04-2011, 10:37 AM
DETAILS:
I use PIC16F877A, MP LAB IDE, PROTEUS, Hi-Tech C Compiler.

The US sensor i have is:
http://www.rhydolabz.com/documents/sensors/Ultrasonic%20Distance%20Sensor%20Serial%20Output-User%20Manual.pdf

Its an ASCII based.
I have decided to choose Asynchronous Reception! Is tat ok??

As per datasheet of PIC16F877A:
When setting up an Asynchronous Reception, follow
these steps:
1. Initialize the SPBRG register for the appropriate
baud rate. If a high-speed baud rate is desired,
set bit BRGH (Section 10.1 “USART Baud
Rate Generator (BRG)”).
2. Enable the asynchronous serial port by clearing
bit SYNC and setting bit SPEN.
3. If interrupts are desired, then set enable bit
RCIE.
4. If 9-bit reception is desired, then set bit RX9.
5. Enable the reception by setting bit CREN.
6. Flag bit RCIF will be set when reception is complete
and an interrupt will be generated if enable
bit RCIE is set.
7. Read the RCSTA register to get the ninth bit (if
enabled) and determine if any error occurred
during reception.
8. Read the 8-bit received data by reading the
RCREG register.
9. If any error occurred, clear the error by clearing
enable bit CREN.
10. If using interrupts, ensure that GIE and PEIE
(bits 7 and 6) of the INTCON register are set.

Based on that I began my coding as:
void initialise_sensor()
{
TRISC=0x80;
PORTC=0x80;
SPBRG=25;
SPEN=1;
BRGH=1;
SYNC=0;
GIE=1;
PEIE=1;
CREN=1;
RX9=0;
SREN=0;
}

Is it right as far as i did?? How do i check dis by simulation?

Another major doubt I have:
As per data sheet of Sensor, Its given that
*The serial output data consist of six ASCII bytes ie xxx.x<CR>
*The distance output is in centimeters (cm)
*The output baud rate is 9600 bps.
So I got confused.USART sets the interrupt RCIF only after it receives the 8bit data. But here the data itself is 6bit long. So how do I make interrupt after receiving the 6bit data?
Pl help me!!