Hot!UART Rx Line Issue

Author
MasterBlaster
New Member
  • Total Posts : 9
  • Reward points : 0
  • Joined: 2018/01/14 02:53:47
  • Location: 0
  • Status: offline
2018/09/23 02:26:54 (permalink)
0

UART Rx Line Issue

I am trying to build a very basic communication between PIC32MX274F256D and a laptop through a 3.3v usb to ttl converter.
The Transmission of data from PIC32 to serial terminal works fine. The Reception of data from serial terminal on the other hand, does not work at all. No interrupts are being used.
  • I have enabled both Tx and Rx in U1CON register.
  • The baud-rate is correct (since I receive correct data in terminal at 115200)
  • Connections are correct
  • Data are being sent correctly from the converter to PIC Rx pin (tested with oscilloscope)
  • I have configured the peripheral pin selection rather...correctly
When I connected the ICD3 debugger though, I noticed that inside the UART receive function, U1STAbits.URXDA flag,that triggers when data is available in Receive buffer, is never triggered and the program stays at that "while" checking loop for ever. Why is this happening since data can be seen on the Rx pin of the microcontroller?
 
I attach the UART receive function, UART configuration and the main.c file in case you notice something that I don't.
 
Thank you for your time
post edited by MasterBlaster - 2018/09/23 02:33:05
#1

6 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 9624
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: UART Rx Line Issue 2018/09/23 02:33:24 (permalink)
    4 (1)
    MasterBlaster
    ...
    • I have configured the peripheral pin selection rather...correctly

    I can't see your source files.
    The most likely error is you have not switched the Rx pin from analog to digital mode.
     
    #2
    MasterBlaster
    New Member
    • Total Posts : 9
    • Reward points : 0
    • Joined: 2018/01/14 02:53:47
    • Location: 0
    • Status: offline
    Re: UART Rx Line Issue 2018/09/23 03:33:36 (permalink)
    0
    Hi, I will write the code here :
     

     pinout map
    #define GetSystemClock()          48000000UL                                    /**< system clock at 48MHz*/
    #define GetPeripheralClock()      GetSystemClock()                              /**< peripherals clock at 48MHz*/
    #define UART_BaudRate()           115200UL                                      /**< BaudRate for Serial */  
        
    #define INPUT   1
    #define OUTPUT  0
    #define HIGH    1
    #define LOW     0
    #define TRUE    1
    #define FALSE   0
        
    #define LED_PIN                 TRISAbits.TRISA0
    #define LED_ON()                LATAbits.LATA0 = FALSE
    #define LED_OFF()               LATAbits.LATA0 = TRUE
        
    #define USB_ID_PIN              TRISBbits.TRISB14
    #define USB_ID_STATE            LATBbits.LATB14
        
    #define I2C_SDA_PIN             TRISBbits.TRISB8
    #define I2C_SDA_STATE           LATBbits.LATB8
    #define I2C_SCL_PIN             TRISBbits.TRISB9                                
    #define I2C_SCL_STATE           LATBbits.LATB8                                  

    #define SPI_MOSI_PIN            TRISAbits.TRISA1       
    #define SPI_MOSI_STATE          LATAbits.LATA1
    #define SPI_MISO_PIN            TRISCbits.TRISC1       
    #define SPI_MISO_STATE          LATCbits.LATC1
    #define SPI_SCK_PIN             TRISBbits.TRISB15   
    #define SPI_SCK_STATE           LATBbits.LATB15
        
    #define FLASH_CS_PIN            TRISAbits.TRISA7
    #define FLASH_CS_STATE          LATAbits.LATA7
        
    #define UART_Tx_PIN             TRISBbits.TRISB3
    #define UART_Tx_STATE           LATBbits.LATB3
    #define UART_Rx_PIN             TRISBbits.TRISB2
    #define UART_Rx_STATE           LATBbits.LATB2
        
    #define LORA_CS_PIN             TRISAbits.TRISA10  
    #define LORA_CS_STATE           LATAbits.LATA10
    #define LORA_RESET_PIN          TRISCbits.TRISC2
    #define LORA_RESET_STATE        LATCbits.LATC2

    #define CHGR_CEN_PIN            TRISCbits.TRISC3   
    #define CHGR_CEN_STATE          LATCbits.LATC3
    #define CHGR_SYSOFF_PIN         TRISAbits.TRISA9
    #define CHGR_SYSOFF_STATE       LATAbits.LATA9
    #define CHGR_PGOOD_PIN          TRISCbits.TRISC4
    #define CHGR_PGOOD_STATE        LATCbits.LATC4   
    #define CHGR_CHARGING_PIN       TRISCbits.TRISC5
    #define CHGR_CHARGING_STATE     LATCbits.LATC5
        
    #define CHGR    
    #define CHGR_ENABLE()           LATCbits.LATC3 = 0
    #define CHGR_DISABLE()          LATCbits.LATC3 = 1
    #define CHGR_SYSOFF_ENABLE()    LATAbits.LATA9 = 1
    #define CHGR_SYSOFF_DISABLE()   LATAbits.LATA9 = 0
        
    #define PPG_INTERRUPT_PIN       TRISBbits.TRISB5  
    #define PPG_INTERRUPT_STATE     LATBbits.LATB5
    #define PPG_RED_DRV_PIN         TRISBbits.TRISB13
    #define PPG_IR_DRV_PIN          TRISCbits.TRISC6
        
    #define IMU_INTERRUPT_PIN       TRISBbits.TRISB7
    #define IMU_INTERRUPT_STATE     LATBbits.LATB7
       
     
     
    //===================================
    void uart_Setup(){
        
        U1BRGbits.BRG = ( GetPeripheralClock()/( 16*115200 ) )-1; // 25 --> for 115200 bps
        U1MODEbits.CLKSEL = 0x00; // PBCLK is being selected
        U1MODEbits.BRGH = 0x00; // Standard speed mode
        U1MODEbits.UEN = 0x00; // Only Tx/Rx pins are used
        U1MODEbits.PDSEL = 0x00; // 8 bit data, no parity
        U1MODEbits.STSEL = 0x00; // 1 Stop Bit
        U1MODEbits.RXINV = 0;
        U1STAbits.UTXEN = 0x01; // Enable UART Transmitter
        U1STAbits.URXEN = 0x01; // Enable UART Receiver
        U1MODEbits.ON = TRUE;
    }


    //=====================================================

    uint8_t uart_RcvByte(void){
        bool check = 0;
        do{
            check = U1STAbits.URXDA;
        }
        while(check == 0);
            
        return U1RXREG;
    }

    //=====================================================

    // DEVCFG3
    #pragma config AI2C1 = OFF // Alternate I/O Select for I2C1 (I2C1 uses the ASDA1/ASCL1 pins)
    #pragma config AI2C2 = OFF // Alternate I/O Select for I2C2 (I2C2 uses the ASDA2/ASCL2 pins)
    #pragma config PMDL1WAY = OFF // Peripheral Module Disable Configuration (Allow multiple reconfigurations)
    #pragma config IOL1WAY = OFF // Peripheral Pin Select Configuration (Allow multiple reconfigurations)
    #pragma config FUSBIDIO = OFF // USB USID Selection (Controlled by the USB Module)

    // DEVCFG2
    #pragma config FPLLIDIV = DIV_4 // PLL Input Divider (2x Divider)
    #pragma config FPLLMUL = MUL_24 // PLL Multiplier (24x Multiplier)
    #pragma config FPLLICLK = PLL_POSC // System PLL Input Clock Selection (POSC is input to the System PLL)
    #pragma config UPLLIDIV = DIV_4 // USB PLL Input Divider (4x Divider)
    #pragma config UPLLEN = ON // USB PLL Enable (USB PLL Enabled)
    #pragma config FPLLODIV = DIV_2 // System PLL Output Clock Divider (PLL Divide by 2)
    #pragma config BOREN = OFF // Brown-Out Reset (BOR) Enable (Enable BOR)
    #pragma config DSBOREN = OFF // Deep Sleep BOR Enable (Enable ZPBOR during Deep Sleep Mode)
    #pragma config DSWDTPS = DSPS32 // Deep Sleep Watchdog Timer Postscaler (1:2^36)
    #pragma config DSWDTOSC = SOSC // Deep Sleep WDT Reference Clock Selection (Select SOSC as DSWDT Reference Clock)
    #pragma config DSWDTEN = OFF // Deep Sleep Watchdog Timer Enable (Disable DSWDT during Deep Sleep Mode)
    #pragma config FDSEN = OFF // Deep Sleep Enable (Disable DSEN bit in DSCON)

    // DEVCFG1
    #pragma config FNOSC = SPLL // Oscillator Selection Bits (Primary Osc (HS,EC, XT))
    #pragma config FSOSCEN = ON // Secondary Oscillator Enable (Enabled)
    #pragma config IESO = ON // Internal/External Switch Over (Enabled)
    #pragma config POSCMOD = HS // Primary Oscillator Configuration (HS osc mode)
    #pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled)
    #pragma config FPBDIV = DIV_1 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/1)
    #pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
    #pragma config WDTPS = PS16384 // Watchdog Timer Postscaler (1:16384)
    #pragma config WDTSPGM = ON // Watchdog Timer Stop During Flash Programming (Watchdog Timer stops during Flash programming)
    #pragma config WINDIS = OFF // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode)
    #pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
    #pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size (Window Size is 25%)

    // DEVCFG0
    #pragma config JTAGEN = OFF // JTAG Enable (JTAG Disabled)
    #pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select (Communicate on PGEC2/PGED2)
    #pragma config PWP = OFF // Program Flash Write Protect (Disable)
    #pragma config SMCLR = MCLR_NORM // Soft Master Clear Enable (MCLR pin generates a normal system Reset)
    #pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled)
    #pragma config CP = OFF // Code Protect (Protection Disabled)

    /*
     *
     */
    int main(void) {
        
        system_config_performance(GetSystemClock(), 0);
        
        //===============================PERIPHERAL PIN SELECT==============================================
        // Configure PIC32 pins as outputs/inputs
        SPI_MISO_PIN = INPUT; /**< Set RC1 pin as input for the MISO2 pin*/ // TRISCSET = _TRISC_TRISC1_MASK;
        SPI_MOSI_PIN = OUTPUT; /**< Set RA1 pin as output for the MOSI2 pin*/ // TRISACLR = _TRISA_TRISA1_MASK;
        SPI_SCK_PIN = OUTPUT; /**< Set RB14 pin as output for the SCK2 pin*/ // TRISBCLR = _TRISB_TRISB14_MASK;
        
    // UART_Tx_PIN = OUTPUT; /**< Set RB3 pin as output for the UART1 Tx pin*/ // TRISBCLR = _TRISB_TRISB3_MASK;
    // UART_Rx_PIN = INPUT; /**< Set RB2 pin as input for the UART1 Rx pin*/ // TRISBSET = _TRISB_TRISB2_MASK;
        
        FLASH_CS_PIN = OUTPUT; /**< Set RA7 pin as output for the flash enable pin*/ // TRISACLR = _TRISA_TRISA7_MASK;
        LORA_CS_PIN = OUTPUT; /**< Set RA10 pin as output for the LoRa enable pin*/ // TRISACLR = _TRISA_TRISA10_MASK;
        CHGR_CEN_PIN = OUTPUT; /**< Set RC3 pin as output for the charger enable pin*/ // TRISCCLR = _TRISC_TRISC3_MASK;
        
        // I2C_SDA_PIN = INPUT; /**< Set RB8 pin as input for the SDA1 pin*/
        // I2C_SCL_PIN = INPUT; /**< Set RB9 pin as output for the SCL1 pin*/
        
        
        uint32_t dmaVal, irqVal;
        unlock_OSCCON(&irqVal, &dmaVal);
        OSCCONbits.UFRCEN = 0; /**< Disable Internal RC Oscillator*/
        CFGCONbits.IOLOCK = 0; /**< Pin Select Unlock bit*/
        
        // Select the peripheral pins (PPS) for UART, SPI, I2C protocols
        U1RXRbits.U1RXR = 0x04; /**< Place Rx of UART1 to pin RB2*/
        RPB3Rbits.RPB3R = 0x01; /**< Place Tx of UART1 to pin RB3*/
     
        //SDI2Rbits.SDI2R = 0x06; /**< Place MISO pin of SPI2 to pin RC1*/
        //RPA1Rbits.RPA1R = 0x04; /**< Place MOSI pin of SPI2 to pin RA1*/
        // SCK1 is placed by default to RB15 pin when the SPI module is enabled
        
        // SDA1 is placed by default to RB9 pin when the I2C1 module is enabled
        // SCL1 is placed by default to RB8 pin when the I2C1 module is enabled
        

        
        CFGCONbits.IOLOCK = 1; /**<Pin Select lock bit*/
        lock_OSCCON(irqVal, dmaVal);
        

        
        //====================Peripheral setup=======================
        
        unsigned char k = 0;
        
        LED_PIN = OUTPUT;
        LED_OFF();
        
        
        uart_Setup();
        delay_us(100000);
        I2C_Master_Setup();

        uart_SndString("Give password\n");
         k = uart_RcvByte();

        if(k == 'S')
           uart_SndString("Correct password\n");
        else
           uart_SndString("Wrong password\n");

        
        while(1){
            
     

            LED_ON();
            _CP0_SET_COUNT(0);
            while(_CP0_GET_COUNT()< 23994230);
            LED_OFF();
            _CP0_SET_COUNT(0);
            while(_CP0_GET_COUNT()< 5000000);
           
           
        }
        return (EXIT_SUCCESS);
    }

    post edited by MasterBlaster - 2018/09/23 03:37:57
    #3
    MasterBlaster
    New Member
    • Total Posts : 9
    • Reward points : 0
    • Joined: 2018/01/14 02:53:47
    • Location: 0
    • Status: offline
    Re: UART Rx Line Issue 2018/09/23 03:53:03 (permalink)
    0
    It worked!!!  Thank you sir you saved my day.
    So my question is this. Do analog pins have priority over re-mappable pins and pins that are used
     for other peripherals in general? For example SPI, I2C, UART.
     
    Again thank you!Smile: Smile
     
    #4
    qhb
    Superb Member
    • Total Posts : 9624
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: UART Rx Line Issue 2018/09/23 03:59:33 (permalink)
    0
    MasterBlaster
    ... Do analog pins have priority over re-mappable pins and pins that are used
     for other peripherals in general?

    Yes. That is a setting on the hardware pin itself.
    This is all documented in the datasheet.
     
     
    #5
    MasterBlaster
    New Member
    • Total Posts : 9
    • Reward points : 0
    • Joined: 2018/01/14 02:53:47
    • Location: 0
    • Status: offline
    Re: UART Rx Line Issue 2018/09/23 04:19:45 (permalink)
    0
    I found it in p.154 table 12-4 of the datasheet
     
    Many Thanks!
    #6
    pboddie
    Starting Member
    • Total Posts : 29
    • Reward points : 0
    • Joined: 2017/05/09 06:22:45
    • Location: 0
    • Status: offline
    Re: UART Rx Line Issue 2018/10/16 14:15:44 (permalink)
    0
    To save people having to look at "the datasheet", the problem was probably solved by setting TRISB<2> (making RB2 an input) and clearing ANSELB<2> (making RB2 digital). It is worth reminding people unfamiliar with the peripheral pin select functionality that configuring pins for peripheral usage doesn't set these general pin characteristics.
    #7
    Jump to:
    © 2019 APG vNext Commercial Version 4.5