Thursday, February 4, 2016

PIC18F4550 UART example

MikroC UART example for PIC18F4550
This is a small example shows how to use UART module of PIC18F4550 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.
PIC18F4550 UART connection circuit schematic:
Pins RC6 and RC7 are used for the UART (serial) communication between the microcontroller and the computer. To change between TTL and RS232 signals, an IC is needed which is max232 and its connection as shown in the following schematic:

pic18f4550 uart usart example mikroc code microcontroller projects 

The internal oscillator of the microcontroller is used (8MHz).
PIC18F4550 UART example mikroC code:

/*
 * PIC18F4550 UART example
 * Written by: BENCHEROUDA Okba
 * E-mail: electronnote@gmail.com
 * Website: http://elecnote.blogspot.com/
 * Internal oscillator used at 8MHz
 * Use at your own risk
*/
 char i ;
 void main() {
   OSCCON =  0X70;    // Set internal oscillator to 8MHz
   ADCON1 = 0x0F;     // Configure AN pins as digital
   CMCON = 7;         // Disable comparators
   UART1_Init(19200);  // Initialize USART module
                       // (8 bit, 19200 baud rate, no parity bit...)
   delay_ms(2000);
   UART1_Write_Text("Hello world!");
   UART1_Write(13);      // Start a new line
   UART1_Write(10);
   UART1_Write_Text("PIC18F4550 UART example");
   UART1_Write(13);     // Start a new line
   UART1_Write(10);
   while (1) {       // Endless loop
     if (UART1_Data_Ready()) { // If data has been received
      i = UART1_Read();     // read it
      UART1_Write(i);       // and send it back
     }
   }
 }

The following video has some descriptions of the code and some tests: