View previous topic :: View next topic |
Author |
Message |
robleso7473
Joined: 25 Mar 2009 Posts: 66
|
Implementing SW UART |
Posted: Thu Sep 04, 2025 9:46 am |
|
|
Hello Again,
I am using the new PIC16F17576 MCU for a test fixture. I just discovered that I will require three (3) UARTS but this device is limited to two (2) in hardware . I can't use I2C or SPI - it must be a UART comms interface.
The big question: Is it possible to implement a simple UART in SW?
Followup question: IF implementing a SW UART is not an option, has anyone had experience multiplexing a UART? I could multiplex the 2nd UART to talk to two devices if possible
I don't require handshaking and baud rate can be as low as 19200 or even 9600 bps.
I have coded SW I2C drivers in the past but never a UART.
Any advice or direction would be greatly appreciated.
Cheers,
Oscar |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19939
|
|
Posted: Thu Sep 04, 2025 10:21 am |
|
|
One big question. Do you need to receive, or only transmit?????/
The CCS drivers will implement a software transmit driver automatically
for you if you use pins that are not allocated to a hardware UART, or you
select FORCE_SW in the #USE RS232 setup.
This works well, and is usually no problem at all (select DISABLE_INTS,
if you are using interrupt driven hardware UARTs at the same time).
If however you need receive, this is much harder. I posted some time ago
an example software UART at 9600bps using a timer interrupt, which I
did add to the code library.
Basically if you have a channel that does not need receive, make this the
one that you use the software UART for. On transmit, you can happily
make i very fast, and in fact this reduces the problems caused by disabling
other interrupts when handling this. |
|
 |
robleso7473
Joined: 25 Mar 2009 Posts: 66
|
|
Posted: Thu Sep 04, 2025 10:46 am |
|
|
Hi Ttelmah,
Thanks for getting back to me promptly.
Unfortunately I need to transceive (both tx and rx). However, I will not require larges amounts of received data, in fact I just need to receive one or two confirmation characters from the end device.
What is the name of the example SW UART file you created?
Thanks!
Oscar |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9575 Location: Greensville,Ontario
|
|
Posted: Thu Sep 04, 2025 12:57 pm |
|
|
Could you add an 'SPI<> UART' interface chip ?? |
|
 |
robleso7473
Joined: 25 Mar 2009 Posts: 66
|
|
Posted: Thu Sep 04, 2025 3:01 pm |
|
|
That would be plan B but I'm running low on gpio.
It can't be that difficult to clock in 10 bits (1 start, 8 data, and 1 stop) at a low enough rate (< 50kbps). The UART protocol is stupidly simple and I'm going to be running this micro at 18.432MHz.
Yeah, I think I'm going to hack out a stone-age UART receiver routine.
Wish me luck. |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9575 Location: Greensville,Ontario
|
|
Posted: Thu Sep 04, 2025 4:50 pm |
|
|
hmm you need 2 pins for the 3rd UART, I2C needs 2 pins...
so.... use an I2C UART 'bridge'. Seems to be the 'word' Google likes to find the chip. While is does cost space and cash, have a TRUE 3rd UART should be better ? |
|
 |
robleso7473
Joined: 25 Mar 2009 Posts: 66
|
|
Posted: Thu Sep 04, 2025 4:51 pm |
|
|
You read my mind, I was just looking at nexperia's i2c to uart bridge: SC16IS740IPW
Thanks for responding. |
|
 |
asmallri
Joined: 12 Aug 2004 Posts: 1657 Location: Perth, Australia
|
Re: Implementing SW UART |
Posted: Fri Sep 05, 2025 12:44 am |
|
|
robleso7473 wrote: | Hello Again,
I am using the new PIC16F17576 MCU for a test fixture. I just discovered that I will require three (3) UARTS but this device is limited to two (2) in hardware . I can't use I2C or SPI - it must be a UART comms interface.
The big question: Is it possible to implement a simple UART in SW?
Followup question: IF implementing a SW UART is not an option, has anyone had experience multiplexing a UART? I could multiplex the 2nd UART to talk to two devices if possible
I don't require handshaking and baud rate can be as low as 19200 or even 9600 bps.
I have coded SW I2C drivers in the past but never a UART.
Any advice or direction would be greatly appreciated.
Cheers,
Oscar |
So.. It is definitely possible to implement your own software part capable of transmitting and receiving at 9600 baud but it is not a trivial task. It requires something like running an interrupt driven timer at 3 or more times the bit rate and using this for sampling reception. Transmission is easier. The same interrupt can be used for both transmission and reception.
Regarding the second question, can you multiplex a UART - this depends on your implementation requirement. If you mean for remapping I/O with user definable mapping of a UART to different pins - that depends on the PIC.
However if you mean "can you add an external multiplexer" then the answer is you definitely can. I use this mechanism with digital muxes to share a single serial port to service multiple sensors and peripherals. It works very well but you do need to make sure you pull up the RX inputs of all serial peripherals as you do not want the RX inputs to float for the unselected devices.
If you only need to share two ports, you can use an analog mux. This also works very well. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9575 Location: Greensville,Ontario
|
|
Posted: Fri Sep 05, 2025 5:23 am |
|
|
re: muxing option.
Easy for the transmitting of data as you KNOW when to control data going to 'serial device one' or 'serial device two'.
It's RECEIVING the data that's tricky. Say the 'mux' is set to 'serial device two' AND ,of course, serial device one tries sending you it's data ! Oopsy....
I actually bought the CCS compiler (V2.534) as they showed me how to setup a 2nd serial transmit in software. Course back then the manual was a coil ringed book. Now PICs have 2-3 or more UARTS and lots of other interesting peripherals ! |
|
 |
robleso7473
Joined: 25 Mar 2009 Posts: 66
|
|
Posted: Fri Sep 05, 2025 8:13 am |
|
|
Thanks, Asmallri & Temtronic.
I'm debating what path to take here. The 3 choices are 1)SW UART, 2)I2C to UART bridge, or 3)UART MUX.
Since the MCU is controlling the flow of transmissions on both UART end devices, missed RXs from one or the other are not an issue since I will only be communicating with one at a time. I will transmit at most 2 characters and await 1 character response (CRC-8 checksum for valid reception of xmitted TX character).
I'm leaning towards the UART MUX using analog switches since that will give me the best timing with the internal baud rate generator and I don't have to worry about creating an RX routine in SW. I will investigate adequate MUXs for the job, the 2nd UART will run at 115.2kbps so we're not talking about lightning speed here  |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9575 Location: Greensville,Ontario
|
|
Posted: Fri Sep 05, 2025 9:03 am |
|
|
gee 115k200 to me IS lightning speed ! Started with 110 Baud Teletypes and my remote control energy control systems were 24 baud( yes, 24 bits per second ).
another possible option ? low powered DPDT relay ?? |
|
 |
robleso7473
Joined: 25 Mar 2009 Posts: 66
|
|
Posted: Fri Sep 05, 2025 9:36 am |
|
|
24 bps?? Well, I guess if you weren't sending large amounts of data it should have done the job right? What's that 3 characters/sec? Ahh, the good old days . Nowadays you have people at home getting upset because they can't get 100Mbps internet for mundane things like streaming or gaming, I think they're spoiled to be honest. I understand the need at companies or server farms, etc., for Gigabit speeds - it's crucial in those applications.
Mechanical relays are an option (I'm already using some SPDT relays on the measurement test head). I'm worried, however, that the clatter/bounce, when closing them may be seen as start condition on the receiving UART or on this PIC. I'll start with the analog MUXes to see if those work out.
Thanks for the additional suggestions. |
|
 |
robleso7473
Joined: 25 Mar 2009 Posts: 66
|
|
 |
dyeatman
Joined: 06 Sep 2003 Posts: 1966 Location: Norman, OK
|
|
Posted: Fri Sep 05, 2025 12:59 pm |
|
|
I have used the TCA9548A to handle eight I2C UARTS connected to
workroom floor equipment and it worked with no issues.
Also used this with multiple VL6180X distance sensors in robotics.
Got my TCA9548A breakout board from Adafruit.for $7 _________________ Google and Forum Search are some of your best tools!!!! |
|
 |
robleso7473
Joined: 25 Mar 2009 Posts: 66
|
|
Posted: Fri Sep 05, 2025 2:09 pm |
|
|
Thanks for the recommendation, dyeatman. I'll take a look at this MUX. |
|
 |
|