UARTS are great. Almost all peripherals have the option to communicate using a simple serial protocol. For my latest project a weighbridge HMI interface I needed 2 serial ports but wanted to use a low end PIC18 in order to use up my stock of them.
If you are using the higher pin count devices such as 80 pin and above you will usually have 2 or more uarts, but for some reason the lower pin count PIC devices only give you 1 UART which is a little annoying but easily overcome. So here is my small tip for people using low end or low pin count microprocessors on how to convert that single UART port into 2, 3 or 4 UARTs for well under $0.50. Its good for any micro such as a PIC, ARM or AVR
Enter the CD4052 CMOS analog multiplexer chip (also known as the 74HC4052D)
By Setting S0 and S1 to 1 or 0 we can select which of the 4 lines is connected to the common line. Being MOSFETS the lines are bi-directional and have a low typically 70 ohm on resistance and the chip can be used at 5V or 3.3V.
So in a typical micro where I have 1 UART and want 4 UARTS I would wire it up like below and use two digital pins to control which line is selected.
The observant amongst you will notice that there are a couple of gotchas
- As only 1 line can be active at one time, the system will never see what is coming in on the other 3 lines and will totally fail to respond to them. This is not a problem if your device is the master and knows which line it is expecting a response from, eg in my application I will select the UART going to the weighing machine when the user presses the weigh key, then after I have weighed in I will ask the user to confirm the action by scanning their RFID tag and then select the serial line going to the RFID reader. If one serial line is always expecting an input then it can be selected by default when none of the others are active, this way you will see and be able to respond to activity on it.
- The second problem is when not selected the line goes open circuit. In serial comms this is known as a break condition and the slave device may interpret this condition unexpectedly. The easy way out of this is to have a small pull-up (or pull-down) depending upon the idle state expected on the line.
- The third problem is different devices may have different baud rates etc and you may need to switch baud-rates before you select the line and enable the interrupt, this may slow down the switching considerably depending upon the MPU
You will still need to implement your own buffering and level conversion if going off-board such as a MAX232 chip and the system may not be suitable for very high baud rates over 115K, best to test your own particular situation.
If you only require 2 UARTS then you can put line S1 to ground and only switch S0 this will then only use the first two of the four lines and reduce you MPU pin requirement.
Generally I have used this successfully in many projects, dating back to the early eighties and it was a well known hack back then too!