Serial Port example program for PIC18F2550

Status
Not open for further replies.

bskumar7080

Member level 2
Joined
Feb 8, 2010
Messages
48
Helped
4
Reputation
8
Reaction score
3
Trophy points
1,288
Location
India
Activity points
1,579
Hi.
Can anyone guide me to write a C program for Serial communication using C18 compiler

Regards
Sivakumar
 

//#define USE_OR_MASKS
#include <P18f2550.h>
#include <usart.h>
//-------------------------------Configuration setting ----------------------------------------------

unsigned char Rxdata[25];
unsigned char Txdata[] = "MICROCHIP_USART";

// BAUD_RATE_GEN is calculated as = [Fosc / (64 * Desired Baudrate)] - 1
// It needs to be changed depending upon oscillator frequency.
// 8MHz / (64 * 2400) - 1 = 51 (approx.)
#define BAUD_RATE_GEN 51 // Fosc = 8MHz, Baud Rate = 2400 bps

void main(void)
{
//-------------------------configure USART ---------------------------------------------------------
// API configures USART for desired parameters:
// - TX/RX interrupts turned off
// - Asynchronous mode
// - 8 bits
// - Continuous Receive Enabled
// - Low speed baud rate generator mode (Fosc / 16)
OpenUSART(USART_TX_INT_OFF | USART_RX_INT_OFF | USART_ASYNCH_MODE | USART_EIGHT_BIT | USART_CONT_RX | USART_BRGH_LOW, BAUD_RATE_GEN);
//baudUSART(BAUD_8_BIT_RATE | BAUD_AUTO_OFF);


//------------USART Transmission ----------------------------------------------------------------
putsUSART((char *)Txdata); // transmit the string

//-----------USART Reception ---------------------------------------------------------------------
getsUSART((char *)Rxdata, 24); // receive data up to 24 bytes
Rxdata[24] = 0; // NULL terminate the string for putsUSART call.
putsUSART((char *)Rxdata); // echo back the data recieved back to host

CloseUSART();
while(1); // end of program
}

Hope this helps!
 

Can i known what oscillator is used, configuration settings, Programmer and compiler used
 

look here

**broken link removed**
 

Hi,

I personaly use C18 C compiler from MPLAB that you can download from the microchip web site.

I strongly recommend for you to check out the 2.10 "USART functions" and 3.6 "Software UART functions" chapters from the the "C 18 C compiler librairies" document from microchip that you can find here: https://ww1.microchip.com/downloads/en/devicedoc/mplab_c18_libraries_51297f.pdf

You will find very useful short examples that will help you a lot. It won't take you too much time. After that you will be able to easily adapt your UART functions to your hardware.

I hope that was helpful for you!
 
Last edited:

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…