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.

[dspic33] UART - Transmits ok, but receives garbage

Status
Not open for further replies.

poorchava

Advanced Member level 1
Advanced Member level 1
Joined
May 21, 2009
Messages
429
Helped
71
Reputation
142
Reaction score
71
Trophy points
1,318
Location
Wrocław, Poland
Visit site
Activity points
4,780
I'm having a strange problem with dsPIC33FJ128MC804. I'm trying to get it to communicate with PC by rs-232. I'm using standard application of MAX3232CSE for level conversion. Data format is 57600 baud, 8 bits, 1 stop, no parity. My code is as follows

Code:
#include "main.h"
#include "p33FJ128MC804.h"
#include "pps.h"
#include "uart.h"

#define FCY 40000000
#define UART_BAUD 57600
#define BRGVAL ((FCY/UART_BAUD)/16)-1

_FGS(GWRP_OFF&GCP_OFF);
_FOSCSEL (FNOSC_PRI&IESO_ON);
_FOSC(POSCMD_HS&OSCIOFNC_OFF&IOL1WAY_OFF&FCKSM_CSECME);
_FWDT(WDTPOST_PS32768&WDTPRE_PR128&WINDIS_OFF&FWDTEN_OFF);
_FPOR(FPWRT_PWR2&ALTI2C_OFF);
_FICD(ICS_PGD1&JTAGEN_OFF);

unsigned char RcvBuff=0;
int RcvBuffPtr=0;
int main(void)
{
	unsigned int UxMODEval=0;
	unsigned int UxSTAval=0;
	unsigned int UxINTval=0;
	
	_TRISC5=0;
	_TRISC8=1;
	_TRISC9=0;	
	PLLFBD=38;
	CLKDIVbits.PLLPRE=0b00010;
	CLKDIVbits.PLLPOST=0;
	__builtin_write_OSCCONH(0x03);
	__builtin_write_OSCCONL(0x01);
	while (OSCCONbits.COSC != 0b011);
	while(OSCCONbits.LOCK != 1) {};
	
	PPSUnLock;
	
	RPINR18bits.U1RXR=24;
	RPOR12bits.RP25R=3;
	
	PPSLock;
	
	UxMODEval = UART_EN & UART_IDLE_CON & UART_IrDA_DISABLE & UART_MODE_SIMPLEX &UART_UEN_00 & UART_DIS_WAKE &UART_DIS_LOOPBACK & UART_DIS_ABAUD &UART_BRGH_SIXTEEN & UART_NO_PAR_8BIT & UART_1STOPBIT;

	UxSTAval = UART_INT_TX_BUF_EMPTY  & UART_TX_ENABLE & UART_INT_RX_CHAR & UART_ADR_DETECT_DIS & UART_RX_OVERRUN_CLEAR & UART_IrDA_POL_INV_ZERO & UART_SYNC_BREAK_DISABLED;
	
	UxINTval=UART_RX_INT_EN&UART_RX_INT_PR4&UART_TX_INT_DIS&UART_TX_INT_PR4;
	
	OpenUART1(UxMODEval, UxSTAval, BRGVAL);	
	ConfigIntUART1(UxINTval);
	
	_U1RXIF=0;
	_U1RXIE=1;
	
	putsUART1("PWNED as FUCK!\0");
	while (1)
	{	
	}	
}

void __attribute__((interrupt,no_auto_psv)) _U1RXInterrupt(void)
{	
	RcvBuff=ReadUART1();
	WriteUART1(RcvBuff);
	_U1RXIF=0;	
}

Now the strange thing is that transmission from MCU to computer works fine, but receiving from computer is totally messed up. When i for example send 0xaa (0b10101010) i get 0b00010101 in U1RXREG. Sending 0xff results in U1RXREG containing 0x00. 0b00000111 gives 0b0111100. I can't quite find any patterns on how these bts are messed up, but the same char transmitted from PC gives always the same incorrect output in receive buffer. I've checked with scope, and the level translation works fine. Fo example this is how 0b10101010 looks like:

This seems correct to me since there is idle high state, then 1 low start bit, then 01010101 (transmission starts from lsb) and then 1 stop bit and idle high state.

Has anyone had similiar problem? I'm kind of running out of ideas about what might be wrong. I know that correct value is being fed to MCU pin and that U1RXREG containg invalid info. Why?
 

Hi,

1. Try with lower baud rate
2. SG is grounded in that DB9 Connector.
3. Check that the Pulse duration is 17.3 us for the baud rate of 57600 with the data 0xaa
 

1. Lower (and higher) baud rates give the same effect.
2. SG and Shield of DB9 connector are grounded
3. Checked pulse duration

It turns out to be 17.6 uS, but i think that's measurement error. When measured at lower timebase it gives 17.4 uS. I think that is ok.

EDIT:
FOUND IT! It turns out, that There is one bit in U1MODE register, which is not taken care of in the library. It is URXINV. I think that library sets this bit to 1 (beats me why though...). Anyway, after manually setting it to zero everything seems to run fine.

On the other hand, for some reason the first byte transmitted is always 0x00. The transmission occurs without actually writing anything to U1TXREG. And first byte received is always 0xff, and the interrupt occurs without any transmission actually going. Later everything works fine, but as startup it's bizarre.
 
Last edited:

Hi,

1.Check with some delays while transmitting and receiving.
2. Check the wires are properly in contact. if there is some loose contact in the wires, sometimes i suffer with the chung data of 0x00 and 0xff
 

I found what was causing the erroneous transmission from MCU to PC. I changed my software to first configure the UART and then map RX and TX to pins. It turns out, that if at the very beggining of the program i set the pin intended to be TX to output and set it to high logic state, then the erroneous transmission doesn't occur. I think it has something to do with idle state for UART being high state.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top