Software serial I/O

Started by upand_at_them August 15, 2004
I need to implement two UARTS on a PIC. I can use the hardware UART,
but I'll still have to use software big banging to implement the
other.

The code I've seen looks straightforward, but I have a question. The
receive routine, obviously, starts with the detection of the start
bit by waiting for a low on the receive pin.

But how do you know that the low bit is in fact the start bit and not
one of the data bits?

Mike




> The code I've seen looks straightforward, but I have a question. The
> receive routine, obviously, starts with the detection of the start
> bit by waiting for a low on the receive pin.
>
> But how do you know that the low bit is in fact the start bit and not
> one of the data bits?

In the worst case you don't. Asynch datastreams do exist that have morev
than one valid interpretation - depening on which bit you interpret as
start bit.

But in practice there are gaps between the characters, and the first low
bit after a gap that is longer than a valid character must be a start
bit.

Wouter van Ooijen

-- -------
Van Ooijen Technische Informatica: www.voti.nl
consultancy, development, PICmicro products



So detecting a start bit, when the byte is 8 bits with 1 stop bit,
would be:

1) detect high for 10 bit lengths
2) detect low

Yes?

Mike --- In , "Wouter van Ooijen" <wouter@v...>
wrote:
> > The code I've seen looks straightforward, but I have a question.
The
> > receive routine, obviously, starts with the detection of the
start
> > bit by waiting for a low on the receive pin.
> >
> > But how do you know that the low bit is in fact the start bit and
not
> > one of the data bits?
>
> In the worst case you don't. Asynch datastreams do exist that have
morev
> than one valid interpretation - depening on which bit you interpret
as
> start bit.
>
> But in practice there are gaps between the characters, and the
first low
> bit after a gap that is longer than a valid character must be a
start
> bit.
>
> Wouter van Ooijen
>
> -- -------
> Van Ooijen Technische Informatica: www.voti.nl
> consultancy, development, PICmicro products





> So detecting a start bit, when the byte is 8 bits with 1 stop bit,
> would be:
>
> 1) detect high for 10 bit lengths
> 2) detect low
>
> Yes?

I would not do that. I suggets: when your routine is entered and the
signal is high, assume you are inbetween characters. Then if you find a
falling edge, assume you have a start bit. If you enter and the signal
is already low, assume you are just after the start of a start bit.

Wouter van Ooijen

-- -------
Van Ooijen Technische Informatica: www.voti.nl
consultancy, development, PICmicro products



But if I assume the first falling edge is a start bit (and it turns
out to be a data bit) when do I ever get sync'd?

Mike

--- In , "Wouter van Ooijen" <wouter@v...>
wrote:
> I suggets: when your routine is entered and the
> signal is high, assume you are inbetween characters. Then if you
find a
> falling edge, assume you have a start bit. If you enter and the
signal
> is already low, assume you are just after the start of a start bit.
>
> Wouter van Ooijen
>
> -- -------
> Van Ooijen Technische Informatica: www.voti.nl
> consultancy, development, PICmicro products





----- Original Message -----
From: "upand_at_them" <>
To: <>
Sent: Sunday, August 15, 2004 10:45 PM
Subject: [piclist] Re: Software serial I/O > But if I assume the first falling edge is a start bit (and it turns
> out to be a data bit) when do I ever get sync'd?

You could have some sort of simple protocol with a sync character that is
recognised and acknowledged.

Leon




Grab AN712 from www.microchip.com

--- In , "Leon Heller" <leon.heller@d...>
wrote:
> ----- Original Message -----
> From: "upand_at_them" <upand_at_them@y...>
> To: <>
> Sent: Sunday, August 15, 2004 10:45 PM
> Subject: [piclist] Re: Software serial I/O > > But if I assume the first falling edge is a start bit (and it
turns
> > out to be a data bit) when do I ever get sync'd?
>
> You could have some sort of simple protocol with a sync character
that is
> recognised and acknowledged.
>
> Leon




Well, I wrote it this way and it seems to work. I'm getting 100%
accuracy when typing through a terminal program. Data coming from a
GPS unit...I get about 3 good sentences and the rest junk...so I
think I still have to adjust the delays.

I'm still not sure *why* this works. It seems to me that a falling
edge isn't enough information to define a start bit since the data
bits could have falling edges too.

Mike --- In , "Wouter van Ooijen" <wouter@v...>
wrote:
> > So detecting a start bit, when the byte is 8 bits with 1 stop
bit,
> > would be:
> >
> > 1) detect high for 10 bit lengths
> > 2) detect low
> >
> > Yes?
>
> I would not do that. I suggets: when your routine is entered and the
> signal is high, assume you are inbetween characters. Then if you
find a
> falling edge, assume you have a start bit. If you enter and the
signal
> is already low, assume you are just after the start of a start bit.
>
> Wouter van Ooijen
>
> -- -------
> Van Ooijen Technische Informatica: www.voti.nl
> consultancy, development, PICmicro products




> But if I assume the first falling edge is a start bit (and it turns
> out to be a data bit) when do I ever get sync'd?

If the sender is sending at full speed: maybe never. There is no
solution to this, other than the sender inserting a pause every now and
then.

Wouter van Ooijen

-- -------
Van Ooijen Technische Informatica: www.voti.nl
consultancy, development, PICmicro products



----- Original Message -----
From: "Wouter van Ooijen" <>
To: <>
Sent: Monday, August 16, 2004 8:14 AM
Subject: RE: [piclist] Re: Software serial I/O > > But if I assume the first falling edge is a start bit (and it turns
> > out to be a data bit) when do I ever get sync'd?
>
> If the sender is sending at full speed: maybe never. There is no
> solution to this, other than the sender inserting a pause every now and
> then.

This may be seen by sending the same character continuously to comms
software running on the PC. If the PC program is started after the
transmission has started, most times it never detects the start bit, and the
wrong character is displayed continuously.

Leon