Search code examples
embeddeduartdspic

dsPIC33EP128MC202 UART receiver doesn't work


I wrote the code for UART communication. TX is working fine, but RX is not working. I have searched a lot but found no solution.

I am transmitting character x to PC with time interval, and I am able see the data. But when transmits data pic is not receiving any thing.

Below are pins used for uart

//PGEC1/AN4/C1IN1+/RPI34/RB2   for receiver
 RPINR18bits.U1RXR = 34;  

//RP36/RB4   for transmitting data
RPOR1bits.RP36R = 1; 

I am able transmit data to pc but PIC doesn't receive any charector from pc. Thanks in advance

Here is my code. DSPIC33EP128MC202

    /*
 * File:   newmainXC16.c
 * Author:
 *
 * Created on 27 December, 2017, 4:21 PM
 */

// FICD
#pragma config ICS = PGD3               // ICD Communication Channel Select bits (Communicate on PGEC3 and PGED3)
#pragma config JTAGEN = OFF             // JTAG Enable bit (JTAG is disabled)

// FPOR
#pragma config ALTI2C1 = OFF            // Alternate I2C1 pins (I2C1 mapped to SDA1/SCL1 pins)
#pragma config ALTI2C2 = OFF            // Alternate I2C2 pins (I2C2 mapped to SDA2/SCL2 pins)
#pragma config WDTWIN = WIN25           // Watchdog Window Select bits (WDT Window is 25% of WDT period)

// FWDT
#pragma config WDTPOST = PS32768        // Watchdog Timer Postscaler bits (1:32,768)
#pragma config WDTPRE = PR128           // Watchdog Timer Prescaler bit (1:128)
#pragma config PLLKEN = ON              // PLL Lock Enable bit (Clock switch to PLL source will wait until the PLL lock signal is valid.)
#pragma config WINDIS = OFF             // Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = OFF             // Watchdog Timer Enable bit (Watchdog timer enabled/disabled by user software)

// FOSC
#pragma config POSCMD = NONE            // Primary Oscillator Mode Select bits (Primary Oscillator disabled)
#pragma config OSCIOFNC = ON           // OSC2 Pin Function bit (OSC2 is clock output)
#pragma config IOL1WAY = ON             // Peripheral pin select configuration (Allow only one reconfiguration)
#pragma config FCKSM = CSDCMD           // Clock Switching Mode bits (Both Clock switching and Fail-safe Clock Monitor are disabled)

// FOSCSEL
#pragma config FNOSC = FRCPLL           // Oscillator Source Selection (Fast RC Oscillator with divide-by-N with PLL module (FRCPLL) )
#pragma config PWMLOCK = ON             // PWM Lock Enable bit (Certain PWM registers may only be written after key sequence)
#pragma config IESO = ON                // Two-speed Oscillator Start-up Enable bit (Start up device with FRC, then switch to user-selected oscillator source)

// FGS
#pragma config GWRP = OFF               // General Segment Write-Protect bit (General Segment may be written)
#pragma config GCP = OFF                // General Segment Code-Protect bit (General Segment Code protect is Disabled)


#include "xc.h"
#include <stdint.h>
#include <p33EP128MC202.h>


#define FP 60000000
#define BAUDRATE 115200
#define BRGVAL ((FP/BAUDRATE)/16)-1
#define DELAY_100uS asm volatile ("REPEAT, #5400"); Nop(); // 100uS delay


int main(void)
{
    uint8_t c;
    int i,j,a,b;

    // Configure Oscillator to operate the device at 60Mhz
    // Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
    // Fosc= 8M*60/(2*2)=120Mhz for 8M input clock  == 8*60/4 = 120/2 = 60

 CLKDIVbits.PLLPOST=0;       // PLLPOST (N1) 0=/2 
 while(!OSCCONbits.LOCK);    // wait for PLL ready
    PLLFBD = 58;                        // M=60
    CLKDIVbits.PLLPOST = 0;             // N1=2
    CLKDIVbits.PLLPRE = 0;              // N2=2
    OSCTUN = 0;                         // Tune FRC oscillator, if FRC is used

    /*Initialize the Ports */
    TRISA = 0x00;
    LATA = 0x0000;
    PORTA = 0x0000;
    ANSELAbits.ANSA1 = 0;
    ANSELAbits.ANSA0 = 0;


    __builtin_write_OSCCONL(OSCCON & ~(1<<6));
    RPINR18bits.U1RXR = 34;
    RPOR1bits.RP36R = 1;
    __builtin_write_OSCCONL(OSCCON | (1<<6));

    U1MODEbits.STSEL = 0; 
    U1MODEbits.PDSEL = 0; 
    U1MODEbits.ABAUD = 0; 
    U1MODEbits.BRGH = 0;
    U1BRG = 30;
    U1MODEbits.UARTEN = 1; 
    IEC0bits.U1TXIE = 0;

    U1STAbits.UTXEN = 1;
    U1STAbits.URXISEL = 0; 



    DELAY_100uS;
    DELAY_100uS;
    DELAY_100uS;
    U1TXREG = 'X';

    char ReceivedChar;

  while(1){

    //If data is received send data to TX  
    if(U1STAbits.URXDA == 1) {
        ReceivedChar = U1RXREG + 2; 
        U1TXREG = 10;
        U1TXREG = 13;
    }
    for(i=0;i<1000;i++){
          a = a + 1;
          for(j=0;j<2000;j++){
              b = b  + 1;
          }
    }
    //LED Blink code for programme check
    if(c == 0){
          c = 1;
    }else{
          c = 0;
    }
    LATAbits.LATA0 = c; 
    LATAbits.LATA1 = c; 
    U1TXREG = 'x';
    U1TXREG = 10;
    U1TXREG = 13;
  }

}


 void __attribute__((interrupt, no_auto_psv)) _U1TXInterrupt( void ) 
 { 
    IFS0bits.U1TXIF = 0; // Clear TX Interrupt flag 
 } 
 void __attribute__((interrupt, no_auto_psv)) _U1RXInterrupt( void ) 
 { 
     IFS0bits.U1RXIF = 0; // Clear RX Interrupt flag 
 //cntr++; 
    U1TXREG='a'; 
    U1TXREG = 10;
      U1TXREG = 13;

 } 

Solution

  • I looked into the code, It works fine for me.

    Please check the connection with your peripherals and their pins. If there is an extra connection to that pin remove it and try it again.