0

I am trying to setup basic serial communication between dsPIC33FJ64GP802 and a terminal and PC using UART module. However, UART does not transmit more than once character. I am using MPLAB-X (V 5.05). I also use snap debugger to program the dsPic. I am trying to figure the error in my code. I tried changing the dsPic itself and the Bluetooth module as well. It did not work. I don't know if I am missing some bits.

// FBS
#pragma config BWRP = WRPROTECT_OFF     // Boot Segment Write Protect (Boot Segment may be written)
#pragma config BSS = NO_FLASH           // Boot Segment Program Flash Code Protection (No Boot program Flash segment)
#pragma config RBS = NO_RAM             // Boot Segment RAM Protection (No Boot RAM)

// FSS
#pragma config SWRP = WRPROTECT_OFF     // Secure Segment Program Write Protect (Secure segment may be written)
#pragma config SSS = NO_FLASH           // Secure Segment Program Flash Code Protection (No Secure Segment)
#pragma config RSS = NO_RAM             // Secure Segment Data RAM Protection (No Secure RAM)

// FGS
#pragma config GWRP = OFF               // General Code Segment Write Protect (User program memory is not write-protected)
#pragma config GSS = OFF                // General Segment Code Protection (User program memory is not code-protected)

// FOSCSEL
#pragma config FNOSC = LPRCDIVN         // Oscillator Mode (Internal Fast RC (FRC) with divide by N)
#pragma config IESO = ON                // Internal External Switch Over Mode (Start-up device with FRC, then automatically switch to user-selected oscillator source when ready)

// FOSC
#pragma config POSCMD = NONE            // Primary Oscillator Source (Primary Oscillator Disabled)
#pragma config OSCIOFNC = OFF           // OSC2 Pin Function (OSC2 pin has clock out function)
#pragma config IOL1WAY = ON             // Peripheral Pin Select Configuration (Allow Only One Re-configuration)
#pragma config FCKSM = CSDCMD           // Clock Switching and Monitor (Both Clock Switching and Fail-Safe Clock Monitor are disabled)

// FWDT
#pragma config WDTPOST = PS32768        // Watchdog Timer Postscaler (1:32,768)
#pragma config WDTPRE = PR128           // WDT Prescaler (1:128)
#pragma config WINDIS = OFF             // Watchdog Timer Window (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = ON              // Watchdog Timer Enable (Watchdog timer always enabled)

// FPOR
#pragma config FPWRT = PWR128           // POR Timer Value (128ms)
#pragma config ALTI2C = OFF             // Alternate I2C  pins (I2C mapped to SDA1/SCL1 pins)

// FICD
#pragma config ICS = PGD1               // Comm Channel Select (Communicate on PGC1/EMUC1 and PGD1/EMUD1)
#pragma config JTAGEN = OFF             // JTAG Port Enable (JTAG is Disabled)


// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include <stdbool.h>
#include "stdio.h"
#include <string.h>


//delay in milliseconds
void delay_milliseconds(long double milliSecond)
{
    milliSecond = (milliSecond * 147); 
    long double i;
    for(i = 0; i<milliSecond;i++);
}

/*
                         Main application
 */


void UART1_setup(void) 
{
    U1BRG = 25;
    U1MODE = 0;
    U1MODEbits.UEN = 0;
    U1MODEbits.UARTEN = 1; //enable the module
    U1STAbits.UTXEN = 1; //enable transmissiond
    U1MODEbits.BRGH = 0;
    U1STAbits.UTXISEL1 = 1;
    U1STAbits.UTXISEL0 = 0;
    U1STAbits.UTXBF = 0;
    U1STAbits.URXDA = 1;
    U1STAbits.OERR = 0;
    U1STAbits.FERR = 0;
    U1STAbits.PERR = 0;
    INTCON1bits.NSTDIS = 0;
    IFS0bits.U1RXIF = 0;
    IEC0bits.U1RXIE = 1;
    IPC2bits.U1RXIP = 7;
}

char UART1_Read(void)
{
    while(!(U1STAbits.URXDA == 1))
    {

    }

    if ((U1STAbits.OERR == 1))
    {
        U1STAbits.OERR = 0;
    }

    return U1RXREG;
}

void UART1_Write(char txData)
{
    while(U1STAbits.UTXBF == 1)
    {

    }

    U1TXREG = txData;    // Write the data byte to the USART.
}


void printString(char *data)
{
    int len = strlen(data);
    int i =0;
    for( i = 0; i<len; i++)
    {
        char txData = *(data + i);
        UART1_Write(txData);
    }
}


void __attribute__((interrupt, no_auto_psv)) _U1RXInterrupt(void) 
{
    //Clear receive interrupt flag
    IFS0bits.U1RXIF = 0;
}


uint8_t data[2];


void pinSetup(void)
{
    //set directions
    TRISBbits.TRISB11 = 0; //TX
    TRISBbits.TRISB10 = 1; //Rx

    //***************************************************************************&&
    __builtin_write_OSCCONL(OSCCON & 0xbf); // unlock PPS

    RPINR18bits.U1RXR = 0x000B;    //RB11->UART1:U1RX
    RPOR5bits.RP10R = 0x0003;    //RB10->UART1:U1TX
    //RPINR0bits.INT1R = 0x0005;    //RB5->EXT_INT:INT1

    __builtin_write_OSCCONL(OSCCON | 0x40); // lock PPS
    //////////////////**********************************************************************&&&&&&&&
}

int main(void)
{
    pinSetup();
    I2C1_init();
    UART1_setup();
    while(true)
    {
       UART1_Write('A');        
    }
}
Mike
  • 4,041
  • 6
  • 20
  • 37
  • 1
    Use your debugger to break the executing code after the first character is sent. Where is the code stuck? Is it stuck in the while UTXBF loop? Or maybe it's stuck in the UART interrupt handler because you're not clearing the interrupt? Or maybe it's stuck in the unhanded exception handler because you haven't defined a UART interrupt handler? – kkrambo Feb 18 '19 at 13:48

2 Answers2

0

Sometimes on initial startup, the UART RX line transits spontaneously causing the Receive buffer to overflow and its associated bit to be set. This disables the UART module until the overrun bit is cleared. Try clearing the receive buffer overflow bit each time you want to send a character and see if it helps

Cerezo
  • 209
  • 2
  • 9
0

The strlen () function returns the number of characters in the string up to the null terminator. But when you write 'A' you have just a single character and not a complete string with a terminator. So try "ABC" e.g.

Mike
  • 4,041
  • 6
  • 20
  • 37