EUSART Serial Communication help

Status
Not open for further replies.

gwntd

Newbie level 5
Joined
Apr 22, 2012
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,353
Hi, I'm new to programming with the pic, so I've been having some trouble setting up a serial communication, EUSART1, from my pic to my computer. I am using the pic18f27j13 with a max232 to try and send signals at a baudrate of 115200 to the computer. I'm using realterm to read the signals. But, non-correct Hex values are being read. My attempt is to set the Fosc to be 48Mhz from the internal clock and a baudrate of 115200. Can anyone kindly help me by looking at my code and pointing out any mistakes that I may have made. Thank you.

Some funky things that I have noticed with realterm. Is that if I send 0x88 it comes out as 0xFA // 1000 1000 -> and get 1111 1010.
0x00 -> gets 0xF0
0xFF -> gets 0xFF
0x1F -> gets 0xF3

I really don't see any pattern.
Code:
#include <p18f27j13.h>
#include <stdio.h>

#pragma config WDTEN = OFF   // Sets watchdog OFF
#pragma config PLLDIV = 2 	 // Sets divider to 2. Provides output 4Mhz for 96Mhz PLL
#pragma config OSC = INTOSCPLL // Sets OSC as INTOSC(8Mhz) Postscalled with a PLL
#pragma config CFGPLLEN = ON //enables PLL on startup
#pragma config PLLSEL = PLL96 // selects 96Mhz
#pragma config XINST = OFF
 

// setup functions
void delay();
void Wait_Buffer1();
void setupUSART();

//

void delay ()
{
	unsigned long int count = 0;
	for (count = 0; count < 0x0003FFFF; count ++)
	{
	}
}

void Wait_Buffer1(void) 
{
	while (!TXSTA1bits.TRMT) 
	{}
}	
void setupUSART()
{

	OSCCONbits.SCS = 0x00; // Selects Primary clock(FOSC) as the clock source


	stdout = _H_USART;

    TXSTA =   0b00100000; //Enable transmit and low baud rate
    RCSTA =   0b10010000; //Enable serial port
    BAUDCON = 0b00001000; //Enable 16 bit baud generator
    
    SPBRGH = 0;
    SPBRG = 25;

	//target baudrate 115200, 

}


void main ()
{
	unsigned int loop_count = 0;
	TRISCbits.TRISC7 = 1;
	TRISCbits.TRISC6 = 0;
	setupUSART();

	while (1)
	{
	delay();
	TXREG1 = 0x1F;
	delay();

	
		
	}
}
 

you are calculating bit rate based on 48Mhz, but setting the PLL to 96MHz
#pragma config PLLSEL = PLL96 // selects 96Mhz
 

I wasn't sure because based on the data sheet from the diagram. On page 36, I was thinking selecting 96Mhz will automatically mean that it will get divided by 2. Is it true that this is not the case? So I should set #pragma config PLLSEL = PLL96 --> #pragma config PLLSEL = PLL48

--Edit- I've rechecked the data sheet. There are only two selections for the PLLSEL, which is PLL96 and 4xPLL. The latter which does not serve my intended purpose of 48Mhz Fosc. So I believe setting PLLSEL = PLL96 should be correct.
 
Last edited:

Gwntd, your oscillator settings and baud rate look correct to me. 8MHz internal clock, fed through the PLL Prescaler at /2 and the finally-resulting 48MHz selected as system clock (with OSCCON at default value 00)

(I use CCS myself though, so I'm not used to setting the configuration directly. I've done a quick review of the datasheet regarding all the oscillator configuration registers but I might have missed something. Just covering my back, lol.)

So, provided the internal oscillator is accurate enough, and the datasheet says it is typically 0.15%, then I don't see what is wrong.

Have you tried using a much lower baud rate to test? That's what I do in serial problems - try 9600 baud and see if that works reliably.

Do you have an oscilloscope that you could check the serial timing with?
 
Reactions: gwntd

    gwntd

    Points: 2
    Helpful Answer Positive Rating

Thank you so much, setting the baud rate to 9600 did work for characters F0 - FE. And I am reading the correct values. However this is only at a baudrate of 57600 read from realterm. and any other charcters F0 and down, will usually generate a framing error. So I am assuming that my Fosc is generated as correctly as as i assumed. I'm attempting to see how off I am with a oscilloscope, but I don't have too much experience with it.
When operating at 9600 for realterm, i end up getting no signals at all.

I've made some progress. By setting the baudrate to 57600 on the pic, I can transmit and receive all signals correctly. :shock:

But when setting 9600 baudrate on the pic, I can only transmit and recieve signals 0xF0-FF.
 
Last edited:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…