UART Lib for PIC18 in HTPIC18 compiler

I am a PIC programmer, so I would like to share this library I made before and hopy you can improve to make it better and to be perfect.

This souces is made under PIC18 compiler. I have tested this lib under some chips and it works well.

UART Library description and source

1. sysdef.h

The sysdef.h includes some constant values or defined values which are used in the library, you can change it to fit your system.

#ifndef SYSDEF_H
#define SYSDEF_H

// Crystal
#define	Fosc	20000000	

// UART Module
#define TX		RC6
#define RX 		RC7

#define TRIS_TX		TRISC6
#define TRIS_RX		TRISC7

// UART Data temporary
#define	UART_Data	TMR1H
#endif    // EOF

2. UART.h

  • UART.h includes some needed functions:
  • UART_Init: initialize the UART module. For example: UART_Init(9600)
  • UART_PrChar: to put a char to the RS232 port. For example: UART_PrChar(‘a’)
  • UART_PrString: to put a string to the RS232 port. For example: UART_PrString(“I love you”)
  • UART_detectBaudRate: to auto detect the PIC baudrate. After calling this function, the peripheral should send one special character U to the UART port to let PIC18 detect the baudrate. It is a new feature compared to PIC16.

You can download this library here: PIC18_UART_LIb (654). Password:

How to use this library

Simply you can copy all library files into your project directory and include the header file in every file you want to use this library.

# include "uart.h"

In this library, I already enable UART receive interrupt so you should clear the RCIF flag in the interrupt service routine.

Get fun!

{ 1 comment… add one }
  • Jardel February 24, 2012, 10:27 am

    Very Good. Thanks!


Leave a Comment