USART Library

USART Functions: (Page # 70)

This library consists of several functions that can be used to configure and use the USART module of PIC18. The USART library consists of the following functions. The functions containing an x are used when there are multiple USARTs on a chip, x stands for the USART number.

OpenUSART(arguments)/ OpenxUSART(arguments)

This function is used to configure the USART module.

WriteUSART(arguments)/WritexUSART and putcUSART(arguments)/putcxUSART(arguments)

These functions are used to write a single byte to the USART.

putsUSART(arguments)/putsxUSART and putrsUSART(arguments)/putrsxUSART(arguments)

These two functions are used to send strings to the USART. The putrsUSART/ putrsxUSART function requires that the string be stored in program memory.

BusyUSART()/ BusyxUSART()

This function is used to check whether the USART is busy with an ongoing transmission.

DataRdyUSART()/DataRdyxUSART()

This function is used to check whether there is any data available in the USARTs read buffer.

getcUSART()/getcxUSART() and ReadUSART()/ReadxUSART()

These functions are used to read a byte from the USARTs read buffer.

baudUSART(arguments)/ baudxUSART(arguments)

These functions are used to change the baud settings of an enhanced USART.

CloseUSART()/ClosexUSART()

This function is used to disable the USART.

Here is the code:

Code

//This is a program that displays the use of EUSART with

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

//"S" is sent from the PC.BR=4800,No Parity,1 Stop Bit

//The EUSART Section is documented on P#70 of C18 Libraries

//Use 181320Basic.DSN

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

//For Baud Rate calculation refer to Page 137 of datasheet

//Baud Rate=Fosc/[64(n+1)], n=value of SPBRGH:SPBRG register pair

//For a Baud Rate of 4800, we need to find value of SPBRGH:SPBRG register pair

//SPBRGH:SPBRG=((FOSC/Desired Baud Rate)/64) – 1

// =((4Mhz/4800)/64)-1

// =(833.33/64)-1

// =13.02-1

// =12.02~12

//SPBRGH:SPBRG=12

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

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

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

#pragma config OSC=INTIO2,WDT=OFF,LVP=OFF// Configuration settings for the 18f1320.

void Senddata(void);// Define function for sending data

void chk_isr(void);// ISR handler

void hi_prioriint(void)// Hi priority interrupt vector handler

{

_asm

GOTO chk_isr

_endasm

}

#pragma interrupt chk_isr

void chk_isr(void)

{

unsigned char startcode;

if(PIR1bits.RCIF==1)

{

unsigned char startcode='S';

unsigned char startread;

if(PIR1bits.RCIF==1)// If any thing received go ahead.

{

startread=ReadUSART();//Read the character recieved and save in startread

if(startcode==startread)// If "S" received then send data, otherwise not

{

Senddata();

}

}

}

}

void main(void)

{

OSCCON=0b01100000; //Internal 4MHZ Oscillator

ADCON1=0x7F;//Make all ports Digital

OpenUSART(USART_TX_INT_OFF &// Initialize USART, Transmit interrupt off

USART_RX_INT_ON &// Receive interrupt ON

USART_ASYNCH_MODE & // Use USART in asynchronous mode

USART_EIGHT_BIT &// Eight bit data

USART_CONT_RX &// Enable continous receiving

USART_BRGH_LOW,// Do not use baud rate multiplication

12);// For a baud rate of 9600 value of SPBRGH:SPBRG register pair

INTCONbits.PEIE=1;// Enable peripheral interrupts

INTCONbits.GIEH=1;// Enable all interrupts

putrsUSART ("\nPlease type a S to Start");

while(1);// Stay here(wait for interrupts to occur)

CloseUSART();// Disable USART Module.

}

void Senddata(void)// To send data

{

putrsUSART("\nHello World!\n");// Send Hello world to the serial port.

}

The code for the USART functions can also be downloaded from:

http://www.box.net/shared/dbi0ri1rc7