Friday, August 26, 2016

PIC16F877A UART example


PIC16F877A UART example with CCS C
This is a small example shows how to use PIC16F877A UART module using CCS PIC C compiler.
PIC16F877A UART connection circuit schematic:
Pin RC6 (TX) and pin RC7 (RX) are used for the UART (serial) communication between the microcontroller and the computer. To change between TTL logic levels (5V) and RS232 signals (+/-12V), an IC is needed which is max232.
Don't connect TX and RX pins directly to an RS232 serial port which may damage your microcontroller.
CCS C compiler serial monitor can be used to communicate with the microcontroller.
PIC16F877A microcontroller USART UART example circuit max232 with CCS PIC C
PIC16F877A UART example CCS C code:
This is the full C code for this example.
// PIC16F877A UART example with CCS C
// http://ccspicc.blogspot.com/
// electronnote@gmail.com

#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 8000000)
#use rs232(uart1, baud = 9600)                // Initialize UART module

char i;
void main(){
  putc(13);                                   // Go to first column
  printf("Hello world!");                     // UART write
  delay_ms(5000);                             // Wait 5 seconds
  putc(13);                                   // Go to first column
  putc(10);                                   // Start a new line
  printf("PIC16F877A UART example");          // UART Write
  putc(13);                                   // Go to first column
  putc(10);                                   // Start a new line
  while(TRUE){
    if(kbhit()){                              // If data has been received
      i = getc();                             // UART read
      putc(i);                                // Send it back
    }
  }
}

PIC16F877A UART example video: