Reading 9 Bits Of Data

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
jburrow
Posts: 11
Joined: 03 January 2011, 15:27 PM

Reading 9 Bits Of Data

Post by jburrow »

I have seen some posts here previously about reading 9 data bits from a network, but I'm afraid I still don't understand how accomplish this.

I am successfully reading data from an RS485 network, via a MAX485 line driver, but I think I'm only getting 8 of the 9 bits.
The 9th bit is used to signal the start of a new message, and I can't get the code to work without it.

Here's the section from the network manual:

"XpressNet is based on the EIA RS-485-Standard at the link layer using half duplex with differential signal
transmission. The specific characteristics are:
1 start element (0), 9 data bits, 1 stop bit (1), no parity bit
Baudrate: 62.5 kilobits per second"

So, in simple terms - can I read the 9 bits? I'm using COM 2 on the ZX-1280 dev board.

Thanks, John Burrow
San Diego
spamiam
Posts: 739
Joined: 13 November 2005, 6:39 AM

Re: Reading 9 Bits Of Data

Post by spamiam »

jburrow wrote:I have seen some posts here previously about reading 9 data bits from a network, but I'm afraid I still don't understand how accomplish this.
I saw the a forum topic on this, and it said to TRANSMIT manually using ShiftOutEX(). You would shift out the respective bits at the proper speed. The bits would include the start, parity and stop bits.

It is harder to do the receive: The regular ZBasic serial functions only support 7 or 8 bits as far as I can tell.

The hardware UART does support 9 bits of data, but the 9th bit is read/set first in a different location, then the byte of the lower 8 bits is read/set in the usual fashion. The ZBasic language reference manual makes no mention of 9 bit functionality.

I can't imagine how to force the 9th bit to be read by ZBasic built-in functions. I think that you would have to write your own receive function. You would have to poll the RX pin for the start bit, then 4x oversample the 9 databits, parity bit and stop bit(s). This could be done with ShiftInEX() in a similar manner to the transmit.

The bit time for 9600 baud is 1536 clocks, so 4x oversampling would use a bit time of 384 clocks. Then you would read the data by looking for a transition on about every 4th bit in the incoming data. You would shift the bit frame left or right if you see the transition coming in early or late. Since 2 bits might be of the same value, you have to correct the bit frame only when you see a transition off by 1 (maybe 2). If you don't see a transition then you probably should just move the frame forward by 4 bits and look for a transition again.

-Tony
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Reading 9 Bits Of Data

Post by dkinzer »

jburrow wrote:So, in simple terms - can I read the 9 bits? I'm using COM 2 on the ZX-1280 dev board.
There is a "9 bit" mode but it involves eight data bits and a parity bit. The hardware UART on the AVR supports 5, 6, 7, 8 and 9 data bits plus an optional parity bit. Of these possible configurations, the ZBasic routines only support 7 or 8 data bits plus an optional parity bit.

If you're using a native mode device, you can write your routines (either polled or interrupt-driven) to support any of the other configurations. For a VM device, you can only implement polled operation.
- Don Kinzer
Post Reply