I want to continuously transmit the data on the UART2
of my dsPIC33EP512MU810
. UART2 Tx
pin i am configuring it to Pin 51 of the controller which is RP99.
Now the UART
output is being converted to RS485
using ST4485EB
on board IC. 3 number pin of ST4485EB
IC is connected with controller's 55 number pin which is RA2. I am making RA2 pin high to activate the IC.
My board also has a on board crstal TXC 4.00 MFC
which is a 4 MHZ crystal.
Trying to configure the UART with baudrate of 38400 8 data bits, 1 stop bit, none parity and none flow control. Using the following code but not getting the desired result. Please help me out with this.
/******************************************************************************/
/* Files to Include */
/******************************************************************************/
/* Device header file */
#if defined(__XC16__)
#include <xc.h>
#elif defined(__C30__)
#if defined(__dsPIC33E__)
#include <p33Exxxx.h>
#elif defined(__dsPIC33F__)
#include <p33Fxxxx.h>
#endif
#endif
#define FP 40000000
#define BAUDRATE 9600
#define BRGVAL ((FP/BAUDRATE)/16)-1
unsigned int i;
#define DELAY_105uS asm volatile ("REPEAT, #4201"); Nop(); // 105uS delay
#include <stdint.h> /* Includes uint16_t definition */
#include <stdbool.h> /* Includes true/false definition */
unsigned int i;
#define DELAY_105uS asm volatile ("REPEAT, #4201"); Nop(); // 105uS delay
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <xc.h>
#include <float.h>
#include <libpic30.h>
#include <p33Exxxx.h>
#include "system.h" /* System funct/params, like osc/peripheral config */
#include "user.h" /* User funct/params, such as InitApp */
/******************************************************************************/
/* Global Variable Declaration */
/******************************************************************************/
/* i.e. uint16_t <variable_name>; */
/******************************************************************************/
/* Main Program */
/******************************************************************************/
int16_t main(void)
{
TRISAbits.TRISA2=0;
PORTAbits.RA2 = 1;
RPOR8bits.RP99R = 3; // Assign U2TX peripheral to pin RP84
RPINR19bits.U2RXR = 19; // Assign pin RPI16 to U2RX peripheral
U2MODEbits.STSEL = 0; // 1-Stop bit
U2MODEbits.PDSEL = 0; // No Parity, 8-Data bits
U2MODEbits.ABAUD = 0; // Auto-Baud disabled
U2MODEbits.BRGH = 0; // Standard-Speed mode
U2BRG = BRGVAL; // Baud Rate setting for 9600
U2STAbits.UTXISEL0 = 0; // Interrupt after one TX character is transmitted
U2STAbits.UTXISEL1 = 0;
IEC1bits.U2TXIE = 1; // Enable UART TX interrupt
U2MODEbits.UARTEN = 1; // Enable UART
U2STAbits.UTXEN = 1; // Enable UART TX
/* Wait at least 105 microseconds (1/9600) before sending first char */
DELAY_105uS
U2TXREG = 'a'; // Transmit one character
while(1)
{
}
}
void __attribute__((__interrupt__)) _U2TXInterrupt(void)
{
IFS1bits.U2TXIF = 0; // Clear TX Interrupt flag
U2TXREG = 'a'; // Transmit one character//comment this once testing is done and UART is working.
}
I can see the data on the PC terminal but its not 'a'. Tried different baud rate but none of them is giving me the result 'a' which I am sending.