PIC16F84A interrupt based software UART code and Proteus simulation

Summary of PIC16F84A interrupt based software UART code and Proteus simulation


This article details an interrupt-based software UART implementation for the PIC16F84A microcontroller using C language and HI-TECH C compiler in MPLAB. Since the chip lacks a built-in UART, bit-banging is used with RA0 as TX and RB0 as RX. The system sends "Hi" on startup and echoes received characters via external interrupts triggered by start bits. Simulation was performed in Proteus at 4800 bps with a 20MHz crystal frequency.

Parts used in the Software UART Project:

  • PIC16F84A microcontroller
  • MPLAB IDE
  • HI-TECH C compiler
  • Proteus simulation software
  • External crystal oscillator (20MHz or 12MHz)
  • Virtual terminal

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.

PIC16F84A interrupt based software UART

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.

PIC16F84A interrupt based software UART Schematic

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

Quick Solutions to Questions related to Software UART Project:

  • How does the PIC16F84A handle UART functionality?
    The microcontroller uses interrupt-based software bit-banging since it lacks a built-in UART module.
  • Can I change the baud rate in this project?
    Yes, the baud rate is set to 4800 bps but can be changed to any desired value.
  • Which pins are used for transmission and reception?
    RA0 pin is used as the TX pin and RB0 pin is used as the RX pin.
  • What triggers the interrupt service routine?
    An external interrupt on pin RB0 is triggered by a high-to-low transition representing the start bit.
  • What message is sent when the code starts executing?
    The PIC16F84A sends the string Hi to the virtual terminal upon startup.
  • How should I adjust the code if I use a different crystal frequency?
    You must update the _XTAL_FREQ definition to match your new crystal frequency, such as changing it to 12000000 for a 12MHz crystal.
  • What software tools were used to compile and simulate the code?
    The code was compiled in MPLAB v8.85 with HI-TECH C v9.83 and simulated in Proteus v7.10.
  • Does the microcontroller echo back typed characters?
    Yes, after startup, the PIC16F84A echoes back whatever character is sent to it from the virtual terminal.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter