Thursday, February 4, 2016

PIC12F1822 UART example

MikroC UART example for PIC12F1822
This is a small example shows how to use UART module of PIC12F1822 microcontroller using mikroC compiler. Here mikroC Pro for PIC UART library will be used for more details about the UART library go to the following page: UART Library.
PIC12F1822 UART connection circuit schematic:
Pins RA0 and RA1 are used for the UART (serial) communication between the microcontroller and the computer (by default). To change between TTL and RS232 signals, an IC is needed which is max232 and its connection as shown in the following schematic:
 pic12f1822 uart usart example max 232 serial makroc code

The internal oscillator of the microcontroller is used (8MHz) and MCLR pin function is disabled.
PIC12F1822 UART example mikroC code:

 // PIC12F1822 UART example
 // Written by: BENCHEROUDA Okba
 char i ;
 void main(){
   OSCCON = 0x70; // Internal oscillator @ 8MHz
   ANSELA = 0;    // All pins are configured as digital
   UART1_Init(19200);            // Initialize USART module
                                  // (8 bit, 19200 baud rate, no parity bit...)
   UART1_Write_Text("Hello world!");
   UART1_Write(13);      // Start a new line
   UART1_Write_Text("PIC12F1822 UART example");
   UART1_Write(13);     // Start a new line
   while (1) {
     if (UART1_Data_Ready()) { // If data has been received
       i = UART1_Read();     // read it
       UART1_Write(i);       // and send it back