View previous topic :: View next topic |
Author |
Message |
Will Reeve
Joined: 30 Oct 2003 Posts: 209 Location: Norfolk, England
|
RS232 7-bits communications |
Posted: Mon Sep 01, 2008 6:29 am |
|
|
Hi all,
RS232 working well with bits=8, however I need bits=7. I set the PC to use 7 bits, I use the bits=7 in #use rs232 but get garbage. I am missing something obvious before I get the oscilloscope out!
Keep well. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Sep 01, 2008 7:17 am |
|
|
The obvious thing is, to my opinion, 7-Bit has to be supported by the chip. PIC processors don't.
That means, 7-Bit data has to be received as 8-Bit data. This can only work, if the sender is using either parity or two stop bits (or leaves a gap of 1 Bit length between characters). If this prerequisites are met, some mask action may be necessary, but it can work at least.
Last edited by FvM on Mon Sep 01, 2008 10:09 am; edited 2 times in total |
|
|
Ttelmah Guest
|
|
Posted: Mon Sep 01, 2008 8:49 am |
|
|
Yes.
A search here will find quite a few posts about this.
The 'key' is that if you want parity (say 7,E,1), you can send '8,N,1', from the PIC, and generate your own parity bit into the eighth bit, or set the eighth bit, to just generate an extra stop (nothing will normally 'mind' having an extra stop bit). The compiler has at times had various attempts a doing this for you, but with various degrees of success, so 'DIY', is probably safer...
Best Wishes |
|
|
Will Reeve
Joined: 30 Oct 2003 Posts: 209 Location: Norfolk, England
|
|
Posted: Thu Nov 06, 2008 3:49 pm |
|
|
Thanks, 7bits Even parity and 1 stop is what is required...there must be a better way than:
for (i=0;i<7;i++)
if (bit_test(txbyte,i)) parity++;
if (parity) bit_set(txbyte,7);
but I can't think of one at the moment! Any cheats for parity calculation? |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Thu Nov 06, 2008 5:45 pm |
|
|
If speed of parity calculations is an issue, use a table. It will mainly require 128 bytes of program memory and contains the precalculated data with parity for any 7 bit combination. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Will Reeve
Joined: 30 Oct 2003 Posts: 209 Location: Norfolk, England
|
|
Posted: Fri Nov 07, 2008 3:30 am |
|
|
Thanks, neat code.
One last question. I use printf quite a bit to pipe stuff out of the SCI.
I seem to remember there is a way to direct this through the 7bit parity function and then on to a putc? Is this possible? |
|
|
Ttelmah Guest
|
|
Posted: Fri Nov 07, 2008 3:51 am |
|
|
Yes.
Just have a parity function like:
void putc_parity(int8 val)
and call printf, with:
printf(putc_parity,"what I want to send");
The characters will each be sent in turn to your putc, and this can call the 'syste' version.
Best Wishes |
|
|
|