How to recognize the start bit in an asynchronous serial bit stream

I am writing code for a microprocessor to communicate with an external device via asynchronous serial communication over a single wire.

I can recognize the transition to wire from low / high (anyway), so I can find the boundaries of the bits. Given that I know the baud rate that the device is using, I can start to synchronize the bits, so I can read the bit stream coming from the device.

What I am afraid of conceptually is recognition of a start bit - search for the beginning of a byte frame (provided that I get 8 bits, without parity, 1 start bit, 1 stop bit). I understand that every frame starts with a start bit and ends with a stop bit, but I understand that the start and stop bit looks like any other bits, so there is nothing special in that they identify them as start or stop bits (except for their position )

The only way I can determine the start bit is the first high bit after a long period of inactivity - that is, since I expect 8 bits to have no parity, if I get 9 or lower bits, then the line is idle, and the next most significant bit will be the start. This is all fine, but what if I start listening to a mid-bitstream device and there is no downtime of 9 bits or more on the wire? I turn off the bits, but how do I know which bit is the start bit so that I can read the bytes? If I turn off the bits, then everything that is between the frames can only be an integer number of bits (so the β€œstop” bit cannot be, for example, 1.5 bits), so everything just looks like bits.

Hope I make sense ... thanks for any help.

+4
source share
2 answers

The start bit is what your code receives to receive the byte. Best explained by state machine. You have 4 main conditions:

  • The wait state: an example of a data row. When you see the start bit, start the timer at a speed of 1.5 * bits and go to the "data" state
  • State "data": wait for the timer, then draw a data line to record the bit. Restart the timer to 1.0 * bit-time. Repeat until you have received all the bits. Move to stop state when all bits received
  • Stop state: wait for the timer and try the data line to check the stop bit. Go to the error state, if this is not correct, add a byte to the receive buffer, if it is not. Back to the wait state.
  • Error Condition: Complaint. Wait for deus ex machina to return to its idle state.

So, the basic ideas from this is that you need a start bit to get the code that receives the byte. And that the stop bit is important, so you can reliably see the start bit for the next byte.

+3
source

I would like your program to read the sequences and decode the patterns at one point or another, moving 9 bits back and forth, the data will make sense, depending on the chains that you transmit. After the sample is recognized, it can recognize if the data has one period, then I would look if at any point in the line a bit does the match byte codes of the period (ASCII 46), and then calculate the space back and forth and set the beginning and stop bits.

0
source

Source: https://habr.com/ru/post/1439687/


All Articles