how to implement UART bit banging code for 8051

Thread Starter


Joined Feb 15, 2023
I am interested in learning more about UART bit banging code and would appreciate any guidance or resources you may be able to provide.

Specifically, I would like to know how to write code to communicate with other devices using the Universal Asynchronous Receiver/Transmitter (UART) protocol without relying on the built-in hardware UART of the 8051 microcontroller. I am looking to implement this using bit banging techniques.

If you have any experience or knowledge on this topic, I would be grateful if you could share your insights with me.

Thank you for your time and consideration. I look forward to hearing from you.


Joined Feb 24, 2006
Bit banging is a useful skill to know about. If you have the hardware, I recommend that you use it. It is ubiquitous and cheap for modern systems. After you are familiar with the hardware you can circle back and revisit the topic.

ETA: Are you familiar with the various framing specifications for asynchronous serial communication? That would be a good place to start.
Last edited:

John P

Joined Oct 14, 2008
I would say sending is easy and receiving is a bit more difficult, but really not too bad. However, using a fast baud rate and full duplex communication may not work very well, because you might get conflicting interrupts while you're simultaneously sending and receiving characters. If you can be sure that it's half duplex (as it would be for RS-485, for instance) then it's OK.


Joined Jun 30, 2021
For any asynchronous interface, you need to have the timing correct. Example, say the baud rate is 19.2KBaud. For sending a byte, you need to first send the start bit with 1/19.2K or 52 microseconds followed by the data bits and the stop bits. The timing can be off a little as long as it does not exceed 1/3 a bit time during the byte transmission (recommend disabling interrupts while sending or receiving a byte so the timing does not get screwed up). The receiver will synchronize to the start bit, so you only need enough resolution to push out all 10 bits (1 start, 8 data, 1 stop) without exceeding 1/3 a bit time error or in this case around 17 microseconds. For receiving you just wait for the start bit to go low, (you will probably need to use an interrupt for that). Then wait 1.5 bit times (78 microseconds) and then capture the receivers first bit. Wait another 1 bit time (52 microseconds) for the next, etc. until all 8 bits are received. I would recommend using a timer, to get the bit rate correct, but it is also possible to use NOPs in the code or loops to get the same timing as long as you know how long each instruction takes. It is not hard to do, but it can be a little subtle to get the timing correct. If the UART speed is really high, like 1Mbuad or more, it probably will not be possible to do bit banging with proper timing, unless the processor speed is fairly high.
Last edited: