20X2 Multiplexing hserin/out?

Pongo

Senior Member
I need two hserin/out connections to a 20X2. They will be used sequentially i.e. communicate with one device, do stuff, communicate with second device, do other stuff, start over. I'm stuck with 9600 baud and the manual implies that serin would be unreliable at that speed. Does anyone have a multiplex scheme they have used? I can think of several solutions but being lazy today I wondered if someone had blazed this trail.
 

Buzby

Senior Member
If you move up to a 28X2, that has two UARTS. I'm sure someone (hippy probably) will show how to use both.

Other than that, a CD4070 or SN7486 might do the trick.

Cheers,

Buzby
 

PhilHornby

Senior Member
64MHz

I need two hserin/out connections to a 20X2... I'm stuck with 9600 baud and the manual implies that serin would be unreliable at that speed.
Don't forget the 20X2 can run @ 64MHz. 9600 baud works OK at that clock rate (I have a 20X2 talking to a HC-12 @ 9600, using 'normal' SERIN).

EDIT

Thinking about it, I have 14M2's @ 32MHz that communicate at 9600 baud using SERIN/SEROUT quite happily.
 
Last edited:

Jeremy Harris

Senior Member
Don't forget the 20X2 can run @ 64MHz. 9600 baud works OK at that clock rate (I have a 20X2 talking to a HC-12 @ 9600, using 'normal' SERIN).

EDIT

Thinking about it, I have 14M2's @ 32MHz that communicate at 9600 baud using SERIN/SEROUT quite happily.
My experience is that 32MHz is about the "sweet spot" for reliable 9600 baud communication with a software serial port. Some external devices are very fussy about serial timing, or send multiple bytes that can tend to not allow the Picaxe time to store bytes quickly enough, it seems. My most recent encounter with the latter issue was when pulling in data at 9600 baud from a Plantronics PMS5003 particulate sensor to an 08M2. The PMS5003 squirts out bursts of 32 bytes at 9600 baud, of which I only needed to read 6, but I found I had to push the clock rate up to 32MHz to make this work. I think I had to do the same trick to get the SDLogger to work at 9600 baud, as well.
 

hippy

Technical Support
Staff member
For output you can run HSEROUT to both HSERIN of your slaves and use software in the slaves to determine if data is for a particular slave or not.

There is a 9-bit transmission mode which could be used to help with that but it involves jumping through some hoops.

Or you can hardware route HSEROUT so only the intended PICAXE sees the data -

Code:
                 ___
HSEROUT |---.---|___|---.------->| HSERIN1
        |   |    ___    |
        |   `---|___|---|---.--->| HSERIN2
        |               |   |
    IO1 |---------------'   |
        |                   |
    IO2 |-------------------'
Code:
High IO1
High IO2
HSerSetup ... , %001
Do
  Input IO1 : HSerOut 0,( "To #1" ) : High IO1
  Input IO2 : HSerOut 0,( "To #2" ) : High IO2
Loop
For input you can use diode mixing -

Code:
        |   .|.
        |   |_|   .------|>|-----| HSEROUT1
        |    |    |   
 HSERIN |<---^----^------|>|-----| HSEROUT2
These examples all use UART polarity, idle high.
 

Pongo

Senior Member
Wow, good stuff, thanks folks :) I'll try to respond to all your inputs:

Unfortunately simple combination or RS-422/485 probably isn't going to work for serin. I have total handshake control over device #1, but device #2 just continuously spits out 9600 baud packets. I don't have #2 in hand yet, it's possible the packet rate is slow enough for the picaxe program to interleave a read of #1 during the quiet time but I can't rely on that at this stage.

Thanks for the chip suggestions. Never used a 28X2 but I'll get one and play.

32 MHz clock and use serin? Hmmm, could be the solution, real life experience beats the manual every time. Will the 20X2 ignore the data arriving at the serin pin after it has grabbed a data packet until it loops back to serin again?
 

hippy

Technical Support
Staff member
Perhaps you could describe exactly what your two devices are, what they are sending to the PICAXE. Diode mixing for a single input probably won't work so you will need to have two separate inputs. You may be able to use SERIN for both or use SERIN for one and HSERIN for the other.

I would suggest getting each working separately, determine if SERIN works or HSERIN is required before deciding how to combine the two.
 

PhilHornby

Senior Member
Code:
High IO1
High IO2
HSerSetup ... , %001
Do
  Input IO1 : HSerOut 0,( "To #1" ) : High IO1
  Input IO2 : HSerOut 0,( "To #2" ) : High IO2
Loop
I think this needs a slight modification - in the shape of a 'pause' - because HSerout completes before the last byte has left the UART.

I tried the following (on a 14M2)
Code:
[COLOR=Navy]#picaxe [/COLOR][COLOR=Black]14m2[/COLOR]
[COLOR=Blue]hsersetup B9600_4[/COLOR][COLOR=Black],[/COLOR][COLOR=Navy]%0   [/COLOR]
[COLOR=Blue]do
      high B.2
      hserout [/COLOR][COLOR=Navy]0[/COLOR][COLOR=Black],[/COLOR][COLOR=Blue]([/COLOR][COLOR=Red]"Hello"[/COLOR][COLOR=Blue])
      low B.2
      
      pause [/COLOR][COLOR=Navy]1000[/COLOR]
[COLOR=Blue]loop[/COLOR]
The gap @ 4MHz/9600baud is 620µS :-

HSerout.jpg
 

PhilHornby

Senior Member
Will the 20X2 ignore the data arriving at the serin pin after it has grabbed a data packet until it loops back to serin again?
For 'normal' Serin, there is no buffering whatsoever ... if no read is posted at the time, the data is just lost...
 

hippy

Technical Support
Staff member
If you move up to a 28X2, that has two UARTS. I'm sure someone (hippy probably) will show how to use both.
There might be some example code on the forum. Not sure if I have ever tried using the second UART myself.

I recall that sending should be possible by poking the right SFR's, and similar for receiving, but one would have to handle receiver status flags and take each byte received individually.
 

hippy

Technical Support
Staff member
I think this needs a slight modification - in the shape of a 'pause' - because HSerout completes before the last byte has left the UART.
That is true; the program continues while data is still being sent. The next HSEROUT should wait until it can send before doing so. Effectively a pause within the HSEROUT which lasts only as long as it needs to.

But I see what you mean and you are right; it has to explicitly PAUSE before it kills the signal and loses data which hasn't been sent.

Thanks for spotting that bug in my code.
 

Pongo

Senior Member
Perhaps you could describe exactly what your two devices are, what they are sending to the PICAXE. Diode mixing for a single input probably won't work so you will need to have two separate inputs. You may be able to use SERIN for both or use SERIN for one and HSERIN for the other.

I would suggest getting each working separately, determine if SERIN works or HSERIN is required before deciding how to combine the two.
For 'normal' Serin, there is no buffering whatsoever ... if no read is posted at the time, the data is just lost...
That's good news. hserout/in is working well for handshaking device #1 and if serin ignores the unwanted data spew from #2 then it should work for future addition of device #2. Something like:

Start
Send hserout data request to #1
Get hserin data from #1
Do stuff
serin wait/get qualified data packet from #2
Do other stuff
Back to start

Cleaner than my original thought of gating/switching hserin between the two. I'll update once the china post does its thing :)
 
Top