|
|
View previous topic :: View next topic |
Author |
Message |
daviz
Joined: 19 Nov 2004 Posts: 11
|
RS232 with 7bits communication |
Posted: Wed Jul 25, 2007 12:17 am |
|
|
Hi,
I have to communicate with a device thats using 7bits.
Currently, I am testing my circuit with hyperterminal. The communication are perfect when i use 8bit, 1stop, N, 4800.
However when i switch to 7bit, cannot work at all.
Hope someone can advise me solution on this, as I can only use 7bits comms.
The chip i using is PIC16F688.
From the ccs complier help file, under topic #use Rs232, it mention that
(5-7 may not be used with the SCI). Can someone tell me what is SCI? |
|
|
Ttelmah Guest
|
|
Posted: Wed Jul 25, 2007 2:33 am |
|
|
Big question. Does the other device require parity?.
If not, then you have a problem. If you read the data sheet for the serial hardware in the PIC, it does not support 7bit mode. This is the 'SCI' (serial communication interface). The name of this part in the PIC, has changed at times, and SCI was commonly used in the past.
The software RS232 does allow 7bit operation, but brings with it the problems of missing characters (no buffering), timing problems if interrupts are being used for other things, etc. etc..
Fortunately, most '7bit' devices, date from a time, when parity was needed, and if so, the problem is fixable.
The solution, is to send an eight bit character, with the eighth bit being the parity bit.
Best Wishes |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Jul 25, 2007 9:15 am |
|
|
How about sending 8 bits with the last bit set as a Stop bit. That would look just like 7 bit data with 2 Stop bits, which is the same as 7 bits with one Stop bit and an extra short pause between characters.
If you try to receive 7 bit data with an 8 bit PIC UART you should get the 7 bits fine, but with a framing error unless the device sends 2 Stop bits in which case you are fine. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Ttelmah Guest
|
|
Posted: Wed Jul 25, 2007 9:22 am |
|
|
Yes, this too is a good solution if the extra stop is acceptable.
Best Wishes |
|
|
daviz
Joined: 19 Nov 2004 Posts: 11
|
|
Posted: Thu Jul 26, 2007 1:26 am |
|
|
Thanks Ttelmah and SherpaDoug comments.
However, both your solution could not solve the problem, I tried using hyperterminal.
The device can select parity, but its data bits is 7, and 1 stop bit is fix.
What should i do? any other ideas.. i must communicate with this device. Please help me to brainstorm. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Jul 26, 2007 2:32 am |
|
|
Quote: | However, both your solution could not solve the problem, I tried using hyperterminal. | Most likely your test setup has a flaw. Could you describe in more details how you performed the tests?
Especially the suggested workaround by having the PIC adding an extra stop bit should work. Don't change anything to the receiving device, just leave it configured as 7 bits, no parity, 1 stop bit. Now the beauty of this workaround is that there is no electrical difference between a stop bit and a long gap between individual characters, i.e. the specification for 1 stop bit in the receiver means that you have to send a _minimum_ of 1 stop bits, but if you want to you could send a 100 stop bits without the receiver caring.
Interesting though is that I don't see how you configured Hyperterm to emulate the above test as I wouldn't know how to do it. |
|
|
Ttelmah Guest
|
|
Posted: Thu Jul 26, 2007 2:36 am |
|
|
If it can select parity, then there is no problem.
Set the PIC up to use 8 bits _no parity_, and the device to use 7 bits even parity. Then when data is received from the device, at the PIC, the eighth bit in the data, will be the parity bit. You can either test this, or simply mask it off. To send data to the device, you generate the parity bit, and add this to the outgoing data. Something like:
Code: |
void putcwithparity(int8 val){
int8 bitcount=0,ctr;
for(c=0;ctr<7;ctr++) if (bit_test(val,ctr)) bitcount++;
//here 'bitcount', is the number of 'on' bits, in the bottom seven bits
//of val.
if (bitcount&1) val!=128;
else val&=0x7F;
//reverse the statements in the last two lines for odd parity
putc(val);
}
|
The 'point' is to forget what the bits are called.
In asynch serial, if you send 7bits with parity, then you send a start bit, seven data bits, a parity bit, and a stop bit. If you send 8 bits no parity, you send a start bit, 8 data bits, and a stop bit. If the 8bit byte you send, already has the eighth bit set to the 'parity' value, then the result is exactly the same.
The same is true of SerpaDog's suggestion. For sending data, if you send the 8bit byte _with the eighth bit set_, and the device at the other end expects a seven bit value, all it will 'see', is an extra bit time 'pause' between the characters. No problem at all. The only difficulty here, is if it is sending _you_ data, and sends it continuously (without pauses between the characters), then it'll start sending the second byte before you are ready. So, if you only need to send data, then you can still use SherpaDog's solution, and just set the eighth bit of the character you send. If you need to both send and receive, use the parity solution.
Best Wishes |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Thu Jul 26, 2007 10:09 am |
|
|
If the device sends 7 bits with parity, all the PIC needs to do to receive is just read 8 bits, call the last of the 8 parity and ignore it.
If the device needs to read 7 bits with parity then have the PIC take the 7 bits, calculate parity and set or clear the 8th bit as parity, and send the 8 bits. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
daviz
Joined: 19 Nov 2004 Posts: 11
|
Thanks |
Posted: Sun Jul 29, 2007 8:08 pm |
|
|
Hi,
SherpaDoug & Ttelmah, thanks for your help.
The solutions really works.
If you ever come to Singapore, feel free to look for me.
Regards,
Daviz Poh |
|
|
hillcraft
Joined: 22 Sep 2003 Posts: 101 Location: Cape Town (South africa)
|
Confused? |
Posted: Tue Jul 31, 2007 11:03 am |
|
|
Code: |
void putcwithparity(int8 val){
int8 bitcount=0,ctr;
for(c=0;ctr<7;ctr++) if (bit_test(val,ctr)) bitcount++;
//here 'bitcount', is the number of 'on' bits, in the bottom seven bits
//of val.
if (bitcount&1) val!=128;
else val&=0x7F;
//reverse the statements in the last two lines for odd parity
putc(val);
}
|
I am confused about this code. I have used it to speak to my PLC at 7E1 but it does not work.
Is this correct:
Code: |
if (bitcount&1) val!=128;
else val&=0x7F;
|
|
|
|
SET
Joined: 15 Nov 2005 Posts: 161 Location: Glasgow, UK
|
|
Posted: Fri Aug 03, 2007 7:15 am |
|
|
Ttelmah did say 'something like' i.e. he was showing the concept
But if you didnt spot the typos..
Code: | void putcwithparity(int8 val){
int8 bitcount=0,ctr;
for(ctr=0;ctr<7;ctr++)
if (bit_test(val,ctr))
bitcount++;
//here 'bitcount', is the number of 'on' bits, in the bottom seven bits
//of val.
if (bitcount&1)
val |=0x80;
else
val &=0x7F;
//reverse the statements in the last two lines for odd parity
putc(val);
} |
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|