poorchava
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
- 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
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?
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?