flukeco
Junior Member level 1
- Joined
- Apr 25, 2014
- Messages
- 16
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 180
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:
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
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:
/* 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