Thursday, August 24, 2017

UART Example for PIC16F887 microcontroller using CCS PIC C compiler


The microcontroller PIC16F887 has a build in USART (Universal Synchronous/Asynchronous Receiver/Transmitter) module. This module can be used as UASAT or UART.
This small post shows an example for the usage of the UART protocol with PIC16F887 microcontroller.
Hardware Required:
  • PIC16F887 microcontroller
  • MAX232 -- datasheet
  • 4 x 10uF polarized capacitor
  • Female COM port
  • Breadboard
  • 5V power source
  • Jumper wires
UART Example for PIC16F887 circuit:
PIC16F887 microcontroller UART (over RS232) example MAX232 circuit
In this example the microcontroller PIC16F887 uses its internal oscillator and MCLR pin function is disabled.
The female COM port is connected to the PC using RS232 cable, this cable has to be male-female because the PC COM port is male.
UART Example for PIC16F887 CCS C code:
The code used in this example is shown below.
The function #use rs232(UART1, baud = 9600) is used to configure the UART protocol. Here the hardware UART module is used. If the pins TX and RX (RC6 and RC7) are used by an other application we can use software UART. Software UART is generated by the compiler with the same previous function. For example TX is mapped to pin RD0 and RX to pin RD1:
#use rs232(xmit = PIN_D0, rcv = PIN_D0, baud = 9600)
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.
// CCS C UART example for PIC16F887 microcontroller
// http://ccspicc.blogspot.com/
// electronnote@gmail.com

#include <16F887.h>
#fuses NOMCLR, INTRC_IO, NOBROWNOUT, NOLVP
#use delay(clock = 8MHz)
#use rs232(UART1, baud = 9600)

const char message[] = "PIC16F887 microcontroller UART example" ;
int8 i = 0, j;
void main(){
  setup_oscillator(OSC_8MHZ);                    // Set internal oscillator to 8MHz
  delay_ms(2000);                                // Wait 2 seconds
  // Print text
  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
    delay_ms(500);
  }
  // Receive and send data via UART
  while(TRUE){
    if(kbhit()){                                 // If a character available
      j = getc();                                // UART read
      putc(j);                                   // Send it back
    }
  }
}
After I build the circuit and burned the HEX file into the PIC16F887 using PICkit 3 programmer, I got a result as shown in this video (using CCS C Compiler IDE Serial Monitor tool):