PIC18F250 USART receiver receives junk bytes

Find out how to setup your programmer's software and how to solve many common problems.

Moderator: phalanx

Post Reply
flukeco
Posts: 3
Joined: Fri Apr 25, 2014 4:54 am

PIC18F250 USART receiver receives junk bytes

Post by flukeco » Fri Apr 25, 2014 4:59 am

Hi,

First of all, I want to apologize in advance for my bad English since I'm not a native speaker. I'll try my best to describe the issue I have and hopefully someone here would help solving my problem. Any suggestions/comments would be highly appreciated.

I'm trying to implement simple USART echo program. The program should work as follows
1. The user type a character on terminal one character at a time
2. The character is echoed back to the terminal

The problem is that when I type the first character, that character is correctly displayed on the terminal. However, when I type a second character, some random bytes (usually one or two bytes: 0xFE and/or 0xFF) are first display followed by the second character that I type. For example:
1. I type 'a'
2. The terminal have 'a'
3. I type 'b'
4. The terminal now have 'a 0xfe 0xff b'

I've read a lot of threads and found that RXREG is 2 bytes deep buffered + one bytes RSR (receive shift register). Many of them suggest that RXREG should be read 3 times to make sure that FIFO is cleared. I've already tried that, but no luck.
I'm using PIC18F2580 with 40Mhz internal oscillator, XC8 compiler, and FT232RL USB USART.

The following is my complete code:

Code: Select all

/* EUSART configuration */
#define _SPBRGH 0x00 /* 9600 bps baud rate for Fosc = 40 Mhz */
#define _SPBRG 0x40 /* with 0.16% error (refer to data sheet) */
#define _SYNC 0x00 /* asynchronous */
#define _BRG16 0x00 /* 8-bit baud rate generator */
#define _BRGH 0x00 /* low speed baud rate */

void init_eusart() {
TXSTAbits.TXEN = 1; /* enable transmission */
TXSTAbits.BRGH = _BRGH; /* high baud rate select bit */
RCSTAbits.CREN = 1; /* enable reception */

SPBRGH = _SPBRGH; /* EUSART baud rate generator register (high byte) */
SPBRG = _SPBRG; /* EUSART baud rate generator register (low byte) */

BAUDCONbits.BRG16 = _BRG16; /* 16-bit baud rate register enable bit */
TXSTAbits.SYNC = _SYNC; /* EUSART mode select bit */

RCSTAbits.SPEN = 1; /* enable serial port */
}

unsigned char eusart_getc() {
/* polling until RCIF is set (reception completed) */
while(!PIR1bits.RCIF) ;
return RCREG; /* data recieved is stored in RCREG register */
/* read of RCREG register clear RCIF */
}

void eusart_putc(unsigned char data) {
while(!TXSTAbits.TRMT); /* wait until transmit shift register is empty */

TXREG = data; /* load data to TXREG register to start data
* transmission. load to TXREG clears TXIF
*/

Nop(); /* Wait 1 instruction cycle for TXIF to be cleared */

while(!PIR1bits.TXIF); /* polling until TXIF is set (transmission completed) */
}

int main(int argc, char** argv) {
char c;
init_eusart();

while(1) {
c = eusart_getc();
eusart_putc_(c);
}
return (EXIT_SUCCESS);
}
Again I have stuck with this for 3 days. So any suggestions/comments would be highly appreciated.
Thank you in advance for your help.
Pat

skimask
Support Volunteer
Posts: 1244
Joined: Sun Nov 09, 2008 2:11 am
Location: Minot, N.D.

Re: PIC18F250 USART receiver receives junk bytes

Post by skimask » Fri Apr 25, 2014 8:14 pm

40Mhz from the internal oscillator?
I don't think so.
Go to the data sheet...
I ignore "one post wonders".

User avatar
phalanx
Non-SFE Guru
Posts: 1992
Joined: Sun Nov 30, 2003 8:57 am
Location: Candia, NH

Re: PIC18F250 USART receiver receives junk bytes

Post by phalanx » Sat Apr 26, 2014 12:20 pm

I'm going to assume you meant to say PIC18F2550 because the PIC18F250 doesn't exist.

In this particular PIC, the internal oscillator cannot be combined with the PLL so you are limited to at most an 8MHz clock speed. The internal oscillator is fed through a postscaler (to divide it down to slower speeds) which by default after a reset is set to divide-by-8. This gives you a default clock speed of 1MHz. Other postscalar ratios can be chosen on the fly by using the OSCCON register.

Go back and verify how you have the oscillator configured in your project and report back here. If you are using a different clock speed than you realize, your serial transmission will be hosed up!

-Bill

skimask
Support Volunteer
Posts: 1244
Joined: Sun Nov 09, 2008 2:11 am
Location: Minot, N.D.

Re: PIC18F250 USART receiver receives junk bytes

Post by skimask » Sat Apr 26, 2014 5:39 pm

phalanx wrote:I'm going to assume you meant to say PIC18F2550 because the PIC18F250 doesn't exist.
-Bill
Says 18F2580 in the 1st post, but, meh, pretty much same things apply.

And if that's the 'complete code' that's posted, where's the CONFIG bits?
I ignore "one post wonders".

User avatar
phalanx
Non-SFE Guru
Posts: 1992
Joined: Sun Nov 30, 2003 8:57 am
Location: Candia, NH

Re: PIC18F250 USART receiver receives junk bytes

Post by phalanx » Sun Apr 27, 2014 10:13 am

I missed the 18F2580 part. In that case, the PLL can be used with the internal oscillator but only to a maximum speed of 32MHz. By default, the oscillator is still divided down to 1MHz.

-Bill

skimask
Support Volunteer
Posts: 1244
Joined: Sun Nov 09, 2008 2:11 am
Location: Minot, N.D.

Re: PIC18F250 USART receiver receives junk bytes

Post by skimask » Sun Apr 27, 2014 11:46 am

Seems like I get bit by that about 50% of the time I build up something new...(sigh)...I'll never learn.
I ignore "one post wonders".

Post Reply