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.

[SOLVED] PIC18F4550 USART protocol

Status
Not open for further replies.

pranavm1502

Newbie level 6
Joined
Jun 18, 2012
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
India
Activity points
1,364
Hi,
I have written this code to check out USART protocol on PIC18F4550 (44-pin). Tx and Rx are shorted with wire. The LED connected to RB3 and ground should go off when the data is transmitted.
It is not at all working. Please help in debugging.
Code:
#include <p18f4550.h>
#define baud 9600
#define freq 12000000
#define spbrg_value (((freq/64)/baud)-1)

void rx_data()
{
	while (PIR1bits.RCIF == 0); //wait until 8-bit data is received
	if(RCREG==0xFF)
		PORTB=0;
	


}

	
	

void tx_data(char data)
{
	TXREG=data;				// store data on transmit register
	while (PIR1bits.TXIF == 0);	//Wait until transmitter register to get full
	
}



void main()
{
	int i,j;
	unsigned char serial_data;
	TRISB=0;
        PORTBbits.RB3=1;
	INTCON2bits.RBPU=0;
	
	SPBRG=spbrg_value;  //Input the spbgr resister for baud rate
	RCSTAbits.SPEN = 1;		//Activate serial comm. (Tx and Rx pins)
	TXSTAbits.TXEN = 1;		//Enable transmission
	RCSTAbits.CREN = 1;		//Enable cont. receiving.
	RCSTAbits.RX9 = 0;		//Setting it on 8-bit mode
	while(1)
	
	{
			//Receive data
		tx_data(0xFF);		//Transmit recieved data
		//for(i=0;i<1000;i++)
		//	for(j=0;j<760;j++);
		rx_data;
	}
}
 

In your main while(1) loop rx_data should be rx_data() so it calls rx_data.

Here is some old PIC18F serial coms code i've used with MPLAB.

Code:
unsigned char TxBuf[TXBUFSIZE+1];		// Transmit FIFO buffer
unsigned char TxBufIn = 0;				// Transmit buffer index for input
unsigned char TxBufOut = 0;				// Transmit buffer index for output
unsigned char TxBufState = 0;			// Number of bytes in the transmit buffer

unsigned char RxBuf[RXBUFSIZE+1];		// Receive FIFO buffer
unsigned char RxBufIn = 0;				// Receive buffer index for input
unsigned char RxBufOut = 0;				// Receive buffer index for output
unsigned char RxBufState = 0;			// Number of unread bytes in the Receive buffer

// Setup
TXSTA = 0x20;		// 8 bit asynchronous mode
RCSTA = 0x90;		// Enable UART 8 bit mode
BAUDCON = 0x00;
SPBRGH	= 0x00;
SPBRG = 0x03; 		// SPBRG = ((FOSC / Baud) / 64) - 1
PIE1bits.RCIE = 1;	// Enable Tx and Rx interrupts 


// --- ISR ---
#pragma code InterruptVectorHigh = 0x08		// High priority interrupt vector
void InterruptVectorHigh(void)
{
  _asm
    goto InterruptHandlerHigh //jump to interrupt routine
  _endasm
}

// --- ISR handler ---
#pragma code								// High priority interrupt routine
#pragma interrupt InterruptHandlerHigh
void InterruptHandlerHigh(void)
{
    if (PIE1bits.TXIE && PIR1bits.TXIF)				// Tx hardware buffer empty interrupt. Interrupt flag clear on write of TXREG
    {                                   
    	if (TxBufState)							// If data in tx buffer
    	{
			TXREG = TxBuf[TxBufOut++];			// Read next value from buffer
	   		TxBufOut &= TXBUFSIZE;			// Wrap index for output 
	   		TxBufState--; 					// Decrement number of bytes in tx buffer
	 }
    	else 									// Tx buffer empty
	{
			PIE1bits.TXIE = 0;            			// Clear interrupt flag 
			//while (!TXSTAbits.TRMT);		        // Wait for last byte to be shifted out 
	}
    }	

    if (PIE1bits.RCIE && PIR1bits.RCIF)				// Byte received interrupt. Interrupt flag clear on read of RXREG
   {
        if (RxBufState <= RXBUFSIZE)				        // If space in Rx FIFO buffer
	{
	    RxBuf[RxBufIn++] = RCREG;				// Put the received byte in the buffer
	    RxBufIn &= RXBUFSIZE;					// Wrap the index
	    RxBufState++;							// Increment the number of bytes in the Rx buffer
	}
	else Temp = RCREG;						// Read and dump the byte to clear Interrupt flag
	
	if (RCSTAbits.OERR || RCSTAbits.FERR)		       // If UART error toggle CREN to clear error
	{
		RCSTAbits.CREN = 0;
		RCSTAbits.CREN = 1;
	}
   }
}
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top