PIC12F1822 UART example with CCS C compiler

The tiny microcontroller PIC12F1822 has a built-in hardware UART module which allows us to easily transmit and receive data. This topic shows a simple UART example using CCS PIC C compiler. In this example I’m going to send/receive data to/from the PC over RS232.

Hardware Required:

  • PIC12F1822 microcontroller
  • MAX232 — datasheet
  • 4 x 10uF polarized capacitor
  • Female RS232 connector
  • Breadboard
  • 5V power source
  • Jumper wires

PIC12F1822 UART example circuit:

PIC12F1822 UART example max232 circuit

To interface the PIC12F1822 MCU with the PC we need MAX232 signal level converter as shown in the circuit diagram. There are 2 lines between the PIC12F1822 and the MAX232 and also two lines between the MAX232 and the female COM connector. One line for data transmit and the other one for data receive. The PIC12F1822 is configured in the C code (default configuration) so that the UART module RX function is on pin RA1 and TX function is on pin RA0.
The female COM connector is connected to the PC using RS232 cable, this cable has to be male-female because the PC RS232 port type is male.
In this example the PIC12F1822 MCU runs with its internal oscillator and MCLR pin function is disabled.

UART Example for PIC16F84A microcontroller C code:
The function #use rs232(UART1, baud = 9600) is used to configure the UART protocol. Here the hardware UART module is used.
where:
9600 is the baud rate.
The functions used in the C code are:
printf: sends a string of characters over RS232 transmission pin (TX).
putc: send a character over RS232 transmission pin (TX).
if(kbhit()): test if a character is ready for getc() function.
getc(): read the character.
Complete C code for CCS C compiler is below.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top