Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC18F2525 EUSART Simple Program

Status
Not open for further replies.

mrlooneytoon

New Member
Hello

I've decided to play around with some serial communication on this PIC and am trying to work off examples from datasheet/app notes but am not very good with C.

My program simply reads a value that a user types on HyperTerminal and displays what he/she typed.

Big surprise, my code doesn't compile , I've highlighted in my code in ALL CAPS where the errors occur. From reading the data sheet RCREG is a 1 byte register and char is 1 byte as well so I don't see why there's a mismatch. Any help please? I know this is a simple fix. Thanks!

Also, I have no idea if this will work, I haven't tested it out on hardware yet but will soon. If anyone has the HW to test this for now, it would be much appreciated :)

Code:
// Include Files
#include <p18f2525.h>		// Include PIC18F2525 registers
#include <usart.h>			// Include EUSART functions
#include <delays.h>			// Delay functions

// Global function prototyping
void initialize(void);
void ReceiveSerial(void);	

// Main function
void main (void) {

	// Declare variable for the received byte
	unsigned char chByte;

	// Initialize EUSART
	initialize();

	// Write a header
	putrsUSART("\nEnter any character into Hyperterminal\n\n");

	// Process a keyboard character and display it onto the terminal
	while (!DataRdyUSART()) { 
    	chByte = ReceiveSerial();        /******ERROR: TYPE MISMATCH IN ASSIGNMENT*******/


		putrsUSART ((const far rom char *)"\r\n  You typed: ");
		WriteUSART (chByte);
		putrsUSART ((const far rom char *)"\r\n");
	}

}

// EUSART and PORT initializations
void initialize(void) {

	TRISC 	= 0xC0;			// Set PORTC bits for RX and TX

	RCSTA 	= 0b10010000; 	// Enable serial port & continuous receive
	TXSTA 	= 0b00100100; 	// Enable transmit, BRGH = 1, BRG16 = 0
	SPBRG 	= 0x19; 		// dec -> 25, 9600 Baud @ 4MHz, Actual = 9615 Baud, Error: 0.16%

	Delay10TCYx(10);		// Create a delay after initializing to ensure EUSART is set

}

// RX Function
void ReceiveSerial(void) {
	
	unsigned char chByteReceived;
	
	if( PIR1bits.RCIF == 1) {				// If RCREG is full with a byte then RCIF = 1     
		chByteReceived = RCREG;

		if(RCSTAbits.OERR == 1) {			// Check overrun error bit
			TXSTAbits.TXEN=0;
			TXSTAbits.TXEN=1;
			RCSTAbits.CREN=0;				// Simply clear RCREG and renable reception
			RCSTAbits.CREN=1;
      	}
		else if(RCSTAbits.FERR == 1) {		// Check framing error bit
			unsigned char chDummy;
			chDummy = RCREG;				// Discard the wrong byte
			TXSTAbits.TXEN=0;
			TXSTAbits.TXEN=1;
		}
		else								// If message is recieved correctly then return the byte
			return chByteReceived;        /******ERROR: UNEXPECTED RETURN VALUE*******/

	}
}
 
I don't have the hardware to try this out but did notice a few quirks with your code.

You have no config line but you can configure that elsewhere.
You are using the library code to send data and your own code to receive it. mixing it like this may cause problems.
void ReceiveSerial(void){ needs to be unsigned char ReceiveSerial(void){.
edit, you need to change this at the top of the code as well.


Woops, just spotted your error text.
They will both be fixed by the last change above.


Mike.
P.S. do you have a Junebug?
 
Last edited:
I don't have the hardware to try this out but did notice a few quirks with your code.

You have no config line but you can configure that elsewhere.
You are using the library code to send data and your own code to receive it. mixing it like this may cause problems.
void ReceiveSerial(void){ needs to be unsigned char ReceiveSerial(void){.
edit, you need to change this at the top of the code as well.


Woops, just spotted your error text.
They will both be fixed by the last change above.


Mike.
P.S. do you have a Junebug?

Hi Mike

Thanks for replying. That fix worked perfectly but I ended up using only USART library functions. The reason I made my own receive function is because I thought the library one didn't check overrun and framing error, but looking at the lib code apparently it does, so that was a waste of time on my part. :)

Tried it on HW today and works great. Thanks!
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top