Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Serial communicatiom example for PIC

Status
Not open for further replies.

bskumar7080

Member level 2
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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top