Page 1 of 1

RS232 with parity and 2 stop bits

Posted: Fri Mar 16, 2012 3:19 pm
by Dirk Bubley
Hello together,

I need some help. I want to implement RS232 Uart with the following parameter into my project:

Baudrate: 100,000 should be possible via the custom setting in the RSR232 Setup Page
Start bit will be done by the PIC UART
8 data Bits as usual used for TX/RX no problem with that
Parity Bit: with even parity; (numbers of"1" in the 8 data bits; even:0 , odd:1 This is the first problem!! Maybe there is a way to use the 9Bit function in the RS232 Setup
2 Stop bits This is my second problem

ANY SUGGESTIONS ON HOW TO SOLVE PROBLEM ONE AND TWO??



PIC is 18f2420 with x-tal of 20Mhz

BR

Dirk

Re: RS232 with parity and 2 stop bits

Posted: Fri Mar 16, 2012 4:36 pm
by Benj
Hello Dirk,

Flowcode v4 and v5 for PICs supports 9-bit data mode for sending and receiving 9-bits through the UART. This extra bit could easily be used for parity. The 9-th bit is enabled through the RS232 component properties window.

A stop bit is simply a logic 1 on the bus for the length of a single bit period. As a logic 1 is the default bus state you could simply use the code customisation feature in flowcode to add a delay of 10us to the end of the send byte RS232 component macro to ensure that the second stop bit has enough time to be transmitted before sending the next byte or 9-bit packet. The second stop bit poses no problems to receiving data.

Re: RS232 with parity and 2 stop bits

Posted: Fri Mar 16, 2012 4:53 pm
by Dirk Bubley
Hi Benj,

many thanks for the fast reply.

Yes i will try the 9Bit mode with putting in the parity bit as Bit9. That sounds great.

Since I don`t have a high knowledge regarding C-Code. Do you have a example how to edit the delay/transmit part of the custom RS232 component?

BR

Dirk

Re: RS232 with parity and 2 stop bits

Posted: Tue Mar 20, 2012 4:15 pm
by Dirk Bubley
Hello Ben,
Hello together,

i`m still struggling with the 9th bit in the RS232 component to sent it as calculated 0 or 1 for parity information.

Let´s assume i want to sent EVEN parity and i will calculate the "ONES" in a byte.

If I have 4 ONES in the byte, the parity bit-9 must be 0!! Right?
With 5 Ones in the byte, the parity bit-9 must be 1!! Right?

So far so good. But how can I get this information out of the byte and set bit number 9??

Any help is highly aprecheated.

BR

Dirk

Re: RS232 with parity and 2 stop bits

Posted: Tue Mar 20, 2012 5:00 pm
by Benj
Hi Dirk,

For calculating the parity of a byte you could do something like this.

calculation: parity = 0

decision: data & 0x01
yes: calculation: parity = (parity + 1) & 0x01

decision: data & 0x02
yes: calculation: parity = (parity + 1) & 0x01

decision: data & 0x04
yes: calculation: parity = (parity + 1) & 0x01

decision: data & 0x08
yes: calculation: parity = (parity + 1) & 0x01

decision: data & 0x10
yes: calculation: parity = (parity + 1) & 0x01

decision: data & 0x20
yes: calculation: parity = (parity + 1) & 0x01

decision: data & 0x40
yes: calculation: parity = (parity + 1) & 0x01

decision: data & 0x80
yes: calculation: parity = (parity + 1) & 0x01

Then say your data variable is of type INT or 16-bit.

To write to the 9th bit you would do this.

calculation: data = (data & 0xFF) | (parity << 8 )

To read the 9th bit you would do this.

calculation: parity = (data >> 8 ) & 0x01

Hope this helps.

Re: RS232 with parity and 2 stop bits

Posted: Tue Mar 20, 2012 10:01 pm
by Dirk Bubley
Hi Ben,

your solution works perfect in simulation!! Great many thanks for your help!!


BR

Dirk

Re: RS232 with parity and 2 stop bits

Posted: Tue Mar 20, 2012 11:24 pm
by JonnyW
Hello Dirk. Just to add to what Ben posted, you can also find the parity in a single calculation box if you like:

Code: Select all

parity = (data) - ((data >> 1) & 0x55)
parity = (parity & 0x33) + ((parity & 0xCC) >> 2)
parity = (parity + (parity >> 4)) & 0x1
The result is the same: 1 for odd, 0 for even. This is only really necessary if your code needs to process the data faster.

Cheers,

Jonny

Re: RS232 with parity and 2 stop bits

Posted: Thu Mar 22, 2012 3:38 pm
by Dirk Bubley
Hi Jonny,

great idea and very good support! I will try your solution too.

In two weeks i will check out both solutions on hardware.

Many thanks!!

BR

Dirk