This post provides the interrupt based software UART (Bit Banging) code for PIC16F84A microcontroller. As we know, PIC16F84A microcontroller doesn’t have built in UART module, so we can create UART functionality in it’s software. This post provides the details of how to program software UART functionality in PIC16F84A. This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the ‘Downloads‘ section at the bottom of this page.
It is assumed that you know how to blink an LED with PIC16F84A microcontroller. If you don’t then please read this page first, before proceeding with this article.
The result of simulating the code in Proteus is shown below.
In the above figure, UART baudrate is currently set to 4800 bps, but you can change it to your desired value. RA0 pin is being used as TX pin and RB0 pin is the RX pin of the software UART. External interrupt on pin RB0 is enabled in the code. Whenever a high to low transition is received on RB0 pin (i-e starting bit for UART) then PIC16F84A goes into interrupt service routine and received character is echoed back.
When code starts to execute then PIC16F84A sends ‘Hi‘ to virtual terminal. After that PIC16F84A echos back whatever character is sent to it. ‘Hello’ was typed in the virtual terminal after the start up of the simulation in Proteus and PIC16F84A echoed it back.
Code
The code used to set different properties of UART is shown below. (From Software_UART.h file)
In the above figure, _XTAL_FREQ is defined to be 20000000, which is the external crystal frequency used with this PIC16F84A. If you change the crystal (e-g by attaching an external crystal of different frequency) then you will need to change _XTAL_FREQ value as well. For example, if 12MHz external crystal is used, then _XTAL_FREQ should be defined as 12000000 in the above code.
Downloads
Interrupt based software UART code for PIC16F84A was compiled in MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10. To download code and Proteus simulation click here.
For more detail: PIC16F84A interrupt based software UART code and Proteus simulation