CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Implementing SW UART
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
robleso7473



Joined: 25 Mar 2009
Posts: 66

View user's profile Send private message

Implementing SW UART
PostPosted: Thu Sep 04, 2025 9:46 am     Reply with quote

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 Bummed Out. 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

View user's profile Send private message

PostPosted: Thu Sep 04, 2025 10:21 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Sep 04, 2025 10:46 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Sep 04, 2025 12:57 pm     Reply with quote

Could you add an 'SPI<> UART' interface chip ??
robleso7473



Joined: 25 Mar 2009
Posts: 66

View user's profile Send private message

PostPosted: Thu Sep 04, 2025 3:01 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Sep 04, 2025 4:50 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Sep 04, 2025 4:51 pm     Reply with quote

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

View user's profile Send private message Send e-mail Visit poster's website

Re: Implementing SW UART
PostPosted: Fri Sep 05, 2025 12:44 am     Reply with quote

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 Bummed Out. 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

View user's profile Send private message

PostPosted: Fri Sep 05, 2025 5:23 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Sep 05, 2025 8:13 am     Reply with quote

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 Wink
temtronic



Joined: 01 Jul 2010
Posts: 9575
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Sep 05, 2025 9:03 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Sep 05, 2025 9:36 am     Reply with quote

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 Very Happy. 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

View user's profile Send private message

PostPosted: Fri Sep 05, 2025 10:14 am     Reply with quote

Just in case you guys were interested. MikroE sells this DPQT UART MUX board using TI's SN74LV4052A Dual 4-Channel MUX: https://www.mikroe.com/uart-mux-click?srsltid=AfmBOopxik_tmTzFdzpON_lp_QDvc2ObCRFSJJZPx_K-xoG9yRQXYlPC. It's $8 at Digikey.

Going to order one of these to test out the idea.
dyeatman



Joined: 06 Sep 2003
Posts: 1966
Location: Norman, OK

View user's profile Send private message

PostPosted: Fri Sep 05, 2025 12:59 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Sep 05, 2025 2:09 pm     Reply with quote

Thanks for the recommendation, dyeatman. I'll take a look at this MUX.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group