Software UART Library

Software UART: (Page#118)

The C18 library also consists of functions to create a software UART. Like before pin assignments(for TX and RX) need to be made in the header file sw_uart.h present in the h subfolder of the compiler installation. For this function also, there are 3 delay functions that need be defined from which Baudrate is determined. Like before, the functions used require that their corresponding source files present in MCC18\src\pmc_common\SW_UART be included in the project. It consists of the following functions:

OpenUART():

This function configures the TX and RX pins for the PIC.

putcUART(arguments)/ WriteUART(arguments):

Thesefunctions is used serially transmit a byte of data.

putsUART(arguments):

This function is used serially transmit a string.

getcUART()/ReadUART():

These functions are used to read a byte of data from the software UART.

getsUART():

This function is used to read a string from the software UART.

Here is the code(requires additional files to be added in the project, download the whole folder from the link):

Code

//This is a program that demonstrates the use of software UART with

//Microchips Libraries.It simply sends "Hello World!"

//BR=4800,No Parity,1 Stop Bit

//The software UART Section is documented on P#118 of C18 Libraries

//Use 181320Basic.DSN

//Uses RA6 for TX and RA7 for RX

//Add writUART.openuart,readuart in your project.

#include<p18f1320.h>// Include files and definitions for the Processor

#include <sw_uart.h>// Include function definitions for the software USART library

#include<delays.h>// // Include function definitions for built in Delay routines

#pragma config OSC=INTIO2,WDT=OFF,LVP=OFF// Configuration settings

void main(void)

{

unsigned char data[]="Hello World!";

unsigned char message[]="Type an S to start receiving data";

unsigned char readcode;

OSCCON=0b01100000;//Internal 4MHz oscillator

ADCON1=0x7F;//Make all pins Digital

OpenUART();// Configure the output pin for Transmitting

putsUART(message);// Send introduction message

while(1)// Loop forever

{

readcode=ReadUART();// Read from RX pin

if(readcode=='S')// If read code = S

putsUART(data);// Send Data

}

}

// As indicated in the C18 libraries manual

// These 3 Delays determine your Baud Rate

// Refer to Page 118 for the MPLAB C18 Libarries Document for more detail

/****************************************************************/

// DelayTXBitUART =((((2*FOSC)/(4*baud))+1)/2)-12 cycles

// =((((2*4Mhz)/(4*4800))+1)/2)-12 cycles

// =(((8Mhz/19200)+1)/2)-12 cycles

// =((416.66+1)/2)-12 cycles

// =(417.66/2)-12 cycles

// =208.83-12 cycles

// =196.83~197 cycles

/****************************************************************/

void DelayTXBitUART(void)

{

Delay10TCYx(19);

_asm NOP _endasm

_asm NOP _endasm

_asm NOP _endasm

_asm NOP _endasm

_asm NOP _endasm

_asm NOP _endasm

_asm NOP _endasm

}

/****************************************************************/

// DelayRXHalfBitUART =((((2*FOSC)/(8*baud))+1)/2)-9 cycles

// =((((2*4Mhz)/(8*4800)+1)/2)-9 cycles

// =((8Mhz/38400)+1)/2)-9 cycles

// =(208.33+1)/2)-9 cycles

// =(209.33/2)-9 cycles

// =104.66-9 cycles

// =95.66~96 cycles

/****************************************************************/

void DelayRXHalfBitUART(void)

{

Delay10TCYx(9);

_asm NOP _endasm

_asm NOP _endasm

_asm NOP _endasm

_asm NOP _endasm

_asm NOP _endasm

_asm NOP _endasm

}

/****************************************************************/

// DelayRXBitUART =((((2*FOSC)/(4*baud))+1)/2)-14 cycles

// =(((2*4Mhz)/(4*4800)+1)/2)-14 cycles

// =208.83-14 cycles

// =194.83~195 cycles

/****************************************************************/

void DelayRXBitUART(void)

{

Delay10TCYx(19);

_asm NOP _endasm

_asm NOP _endasm

_asm NOP _endasm

_asm NOP _endasm

_asm NOP _endasm

}

The code for the Software UART functions and the additional files required to run it can be downloaded from:

http://www.box.net/shared/6z2kf3deph