Serial communicatiom example for PIC

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
I programmed Serial communication program for PIC. i used the baud rate 9600.but when i connect to PC the characters were not clear . i changed different baud rate but the characters were not clear.

Can any one have the sample program for testing serial communication,

Thanks & Regards
sivakumar
 

Example based on PIC18F2550:

//#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
}
 

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