ctzof
Full Member level 3
Hello,l
I have a problem with using UART2 for a Dspic33ev256m106 micro-controller. My code compiles just fine and I am able to see signal coming out from the port (RB4), but the data I receive at terminal are wrong. I am using dsPIC33EV 5V CAN-LIN starter Kit and a CP2102 UART-USB bridge for connection. Below the code
I have a problem with using UART2 for a Dspic33ev256m106 micro-controller. My code compiles just fine and I am able to see signal coming out from the port (RB4), but the data I receive at terminal are wrong. I am using dsPIC33EV 5V CAN-LIN starter Kit and a CP2102 UART-USB bridge for connection. Below the code
Code:
#include <p33EV256GM106.h>
#define BAUDRATE 115200
#define BRGVAL ((FCAN/BAUDRATE)/16)-1
#define TRIS_MON _TRISB4
#define FCAN 40000000
#define Foscil 80000000
#define DELAY 50000
#define U_TX 0x0400
#define U_ENABLE 0x8008
// Macros for Configuration Fuse Registers
_FOSCSEL(FNOSC_PRIPLL);
_FOSC(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMD_XT);
// Startup directly into XT + PLL
// OSC2 Pin Function: OSC2 is Clock Output
// Primary Oscillator Mode: XT Crystal
_FWDT(FWDTEN_OFF); // Watchdog Timer Enabled/disabled by user software
_FICD(ICS_PGD2); // PGD3 for external PK3/ICD3/RealIce, use PGD2 for PKOB
_FPOR(BOREN0_OFF); // no brownout detect
_FDMT(DMTEN_DISABLE); // no deadman timer <<< *** New feature, important to DISABLE
void putU2(int c);
void InitMonitor(void);
void oscConfig(void);
void putsU2(int *s);
void oscConfig(void)
{
// Configure Oscillator to operate the device at 80MHz/40MIPs
// Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
// Fosc= 8M*40/(2*2)=80Mhz for 8M input clock
// To be safe, always load divisors before feedback
CLKDIVbits.PLLPOST = 0; // N1=2
CLKDIVbits.PLLPRE = 0; // N2=2
PLLFBD = 38; // M=(40-2), Fcyc = 40MHz for ECAN baud timer
// Disable Watch Dog Timer
RCONbits.SWDTEN = 0;
}
void putU2(int c)
{
while ( U2STAbits.UTXBF); // wait while Tx buffer full
U2TXREG = c;
} // putU2
void putsU2(int *s)
{
while (*s)
{ // loop until *s =\0, end of string
putU2(*s++);
} // send the character and point to the next one
}
void InitMonitor(void)
{
// digital output
TRIS_MON = 0;
RPOR1bits.RP36R = 0x03; // map UART2 TXD to pin RB4
U2BRG = BRGVAL;
U2MODE = U_ENABLE;
U2STA = U_TX;
}
int main()
{
oscConfig();
// Initialize the monitor UART2 module
InitMonitor();
T1CON = 0x8030;
while(1){
putsU2("Hello");
while (U2STAbits.TRMT == 0);
U2TXREG = 0x0a;
while (U2STAbits.TRMT == 0);
U2TXREG = 0x0d;
TMR1 = 0;
while ( TMR1 < DELAY);
}
return 0;
}