Friday, September 8, 2017

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 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.
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.
/* PIC12F1822 UART example with CCS C compiler.

#include <12F1822.h>
#use delay(clock=32000000)
#use rs232(UART1, baud = 9600)

const char message[] = "PIC12F1822 microcontroller UART example" ;
int8 i = 0, j;
void main() {
  setup_oscillator(OSC_8MHZ | OSC_PLL_ON);       // Set internal oscillator to 8MHz with PLL enabled (32MHz)
  delay_ms(2000);                                // Wait 2 seconds
  // Print text
  printf("\r");                                  // Set cursor to first position
  printf("Hello world!");
  // Print list of characters
  printf("\n\r");                                // Start new line
  while(message[i] != '\0'){
    putc(message[i]);                            // Write character
    delay_ms(100);                               // Wait 100 ms
    i++;                                         // Increment i
  // Print numbers
  printf("\n\r");                                // Start new line
  for(i = 0; i < 21; i++){
    printf("%u\n\r", i);                         // Print i and start new line
  // Receive and send data over UART
    if(kbhit()){                                 // If a character available
      j = getc();                                // UART read
      putc(j);                                   // Send it back