PDA

View Full Version : How to implement 8E2 serial protocol?



imt_67
- 13th October 2005, 13:37
Hello,
I'm a totally newbie abut PIC and PICBASIC programming.
I would build a PIC reader for my weather station, using its built-in RS232 interface. Before starting, I found a big problem to solve: the weather station uses a *not so common* serial protocol: 1 start bit, 8 data bits, EVEN parity and 2 bits stop (1+8+1+2=12 bits!) with a speed of 19200 bps. I haven't found a way to obtain it, neither with SERIN/SEROUT commands, nor with hardware USART commands, so I think I must create it from scratch....

Anyone has suggestions, examples, code pieces to help me?

Thanks in advance.

PS: I think to use a 16F876 device for this project.

Dave
- 13th October 2005, 16:19
imt_67, Just use 8 data bits, 1 parity bit, and 1 stop bit for your received data. Ignor the last bit received......

Dave Purola,
N8NTA

sean-h
- 13th October 2005, 19:57
Like Dave says, ignore the last stop bit and when sending/receiving data by adding a pause after the serin command, especially if in a loop receiving byte after byte.

Or write a Bit Bang routine.

Lasy example below, but I am sure you can create a couple of loops to cut it down, have used pauseus of 104 that gives you 9600 baud, simply cut down the pause time to suit required baud rate:


To send a byte:

'Send Start Bit
portb.7=0
pauseus 104

'Send Byte out
portb.7=Byteout.bit0
pauseus 104
portb.7=Byteout.bit1
pauseus 104
portb.7=Byteout.bit2
pauseus 104
portb.7=Byteout.bit3
pauseus 104
portb.7=Byteout.bit4
pauseus 104
portb.7=Byteout.bit5
pauseus 104
portb.7=Byteout.bit6
pauseus 104
portb.7=Byteout.bit7
pauseus 104

'Send Parity bit
portb.7=Parity
pauseus 104

'Send 2 Stop Bits
portb.7=1
pauseus 104
portb.7=1
pauseus 104

*****

To receive a byte:

'Check for start bit by sampling the port twice within 104us.
check1:
if portb.6=0 then
else
goto check1
endif
pauseus 52
if portb.6=0 then
else
goto check1
endif
pauseus 104

'Receive Byte in
ByteIn.bit0=portb.6
pauseus 104
ByteIn.bit1=portb.6
pauseus 104
ByteIn.bit2=portb.6
pauseus 104
ByteIn.bit3=portb.6
pauseus 104
ByteIn.bit4=portb.6
pauseus 104
ByteIn.bit5=portb.6
pauseus 104
ByteIn.bit6=portb.6
pauseus 104
ByteIn.bit7=portb.6
pauseus 104

'Receive Parity bit
Parity=portb.6
pauseus 104

'Receive 2 Stop bits
pauseus 104
pauseus 104

Regards

Sean.

Ingvar
- 14th October 2005, 09:14
When recieving, do what Dave said, just use 8E1. PBP will see the extra stopbit as a little pause between the characters, no problems atall and nothing you need to worry about.

When sending, you need to add that extra stopbit. You do that by adding a short pause(1/19200=52us) after each character. You can also use character pacing(which does exactly that). The biggest problem with that is that SEROUT2 paces with milliseconds. If you specify 1ms it may take too much time to send the entire message, but it will work. If your messages are short it may not be a problem.

/Ingvar

imt_67
- 14th October 2005, 17:43
As all of you wrote, I tried using 19200-8-E-1 protocol to connect my weather station (now writing a little program for PC using freepascal...fast and lazy way ;-) ) and it seems to be tolerant to 1 or 2 stop bits, both for tx and rx.
So I'll try with SERIN and SERUOT as soon I'll have some time to spend (this weekend...I hope!). Fortunately I have to send only very small commands to ws ( four bytes or so... with fixed values), so also the "bit bang" idea (very nice!) may be useful...

Again, thanks a lot!

Marco.