• AVR Freaks

Hot!UART not receiving buffer

Author
AKJ
Starting Member
  • Total Posts : 49
  • Reward points : 0
  • Joined: 2019/05/27 06:55:52
  • Location: 0
  • Status: offline
2019/08/23 04:37:07 (permalink)
0

UART not receiving buffer

Hello, 
i am currently working on an project, in which a pic controller needs to send some strings over UART and receive some response.
I am using the dsPIC33EP512GM706 and wrote this code
Init:

void init(void)
{
     TRISAbits.TRISA4 = 0; // U3TX
     TRISAbits.TRISA9 = 1; // U3RX  
     TRISBbits.TRISB7 = 0; // U3RTS
     TRISCbits.TRISC3 = 1; // U3CTS
 
ANSELAbits.ANSA9 = 0; // UART3 pins to digital
ANSELCbits.ANSC3 = 0;
ANSELAbits.ANSA4 = 0;
ANSELBbits.ANSB7 = 0;
 
RPINR27bits.U3RXR = 0x19; // 0b0011001 // UART3 RXR -> RPI25
RPINR27bits.U3CTSR = 0x33; // 0b0110011 // UART3 CTS -> RPI51
RPOR0bits.RP20R = 0x1B; // 0b011011 UART3 TxD -> RP20
RPOR2bits.RP39R = 0x1C; // 0b011100 UART3 RxD -> RP39
 
// FRCDIV FRC/1; PLLPRE 2; DOZE 1:8; PLLPOST 1:4; DOZEN disabled; ROI disabled;
CLKDIV = 0x3040;
// TUN Center frequency;
OSCTUN = 0x00;
// ROON disabled; ROSEL FOSC; RODIV 0; ROSSLP disabled;
REFOCON = 0x00;
// PLLDIV 50;
PLLFBD = 0x32;
// AD1MD enabled; PWMMD enabled; T3MD enabled; T4MD enabled; T1MD enabled; U2MD enabled; T2MD enabled; U1MD enabled; QEI1MD enabled; SPI2MD enabled; SPI1MD enabled; C2MD enabled; C1MD enabled; DCIMD enabled; T5MD enabled; I2C1MD enabled;
PMD1 = 0x00;
// OC5MD enabled; OC6MD enabled; OC7MD enabled; OC8MD enabled; OC1MD enabled; IC2MD enabled; OC2MD enabled; IC1MD enabled; OC3MD enabled; OC4MD enabled; IC6MD enabled; IC7MD enabled; IC5MD enabled; IC8MD enabled; IC4MD enabled; IC3MD enabled;
PMD2 = 0x00;
// ADC2MD enabled; PMPMD enabled; U3MD enabled; QEI2MD enabled; RTCCMD enabled; CMPMD enabled; T9MD enabled; T8MD enabled; CRCMD enabled; T7MD enabled; I2C2MD enabled; T6MD enabled;
PMD3 = 0x00;
// U4MD enabled; CTMUMD enabled; REFOMD enabled;
PMD4 = 0x00;
// PWM2MD enabled; PWM1MD enabled; PWM4MD enabled; SPI3MD enabled; PWM3MD enabled; PWM6MD enabled; PWM5MD enabled;
PMD6 = 0x00;
// PTGMD enabled; DMA0MD enabled;
PMD7 = 0x00;
// CF no clock failure; NOSC PRI; LPOSCEN disabled; CLKLOCK unlocked; OSWEN Switch is Complete; IOLOCK not-active;
__builtin_write_OSCCONH((uint8_t) (0x02));
__builtin_write_OSCCONL((uint8_t) (0x00));
}

 
// config

#define FP 12000000
#define BAUDRATE 9600
#define BRGVAL ((FP / BAUDRATE) / 16) - 1
#define DELAY_105uS asm volatile ("REPEAT, #4201"); Nop(); // 105uS delay
 
void UART3_init(void)
{
U3MODEbits.UARTEN = 0; // Disable UART3
U3MODEbits.USIDL = 1; // Continue Operation in Idle Mode
U3MODEbits.IREN = 0; // IrDA Encoder and Decoder are disabled
U3MODEbits.RTSMD = 0; // U3RTS in Flow control mode
U3MODEbits.UEN = 0b10; // U3TX, U3RX, U3CTS, U3RTS are enabled and used
U3MODEbits.WAKE = 0; // Wake-up is disabled
U3MODEbits.LPBACK = 0; // Loopback mode is disabled
U3MODEbits.ABAUD = 0; // Baud rate measurement is disabled or complete
U3MODEbits.URXINV = 1; // Idle state is '0'
U3MODEbits.BRGH = 0; // = BRG generates 16 clocks per bit period (16x baud clock, Standard Speed mode)
U3MODEbits.PDSEL = 0b00; // 8-bit data, no parity
U3MODEbits.STSEL = 0; // 1 stop bit

U3BRG = 0x138; // Baud Rate setting for 9600
U3STAbits.UTXISEL0 = 0; // Transmittion interrupt Mode
U3STAbits.UTXISEL1 = 1; // Interrupt when character is transmitted
U3STAbits.UTXINV = 1; // UxTX Idle state is 0
U3STAbits.UTXBRK= 0; // Sync break transmission is disabled or complete
U3STAbits.URXISEL = 0b11; // Interrupt flag bit is set when the receive buffer is full
U3STAbits.ADDEN = 0; // Address dectect mode is disabled

U3STAbits.RIDLE = 0; //Bit4 *Read Only Bit*
U3STAbits.PERR = 0; //Bit3 *Read Only Bit*
U3STAbits.FERR = 0; //Bit2 *Read Only Bit*
U3STAbits.OERR = 0; //Bit1 *Read Only Bit*
U3STAbits.URXDA = 0; //Bit0 *Read Only Bit*

U3MODEbits.UARTEN = 1;
U3STAbits.UTXEN = 1; // UART3 trasmitter is enabled; U3TX pin is controlled by UART3

DELAY_105uS // Wait at least 105 milliseconds (1/9600) before sending first char
}

 

void UART3_send(uint8_t chr)
{
while (U3STAbits.UTXBF);
U3TXREG = chr;
}
uint8_t UART3_receive(void)
{
while (!(U3STAbits.URXDA == 1));
if ((U3STAbits.OERR == 1))
U3STAbits.OERR = 0;
return U3RXREG;
}
uint8_t UART3_isRxReady(void)
{
return U3STAbits.URXDA;
}
uint8_t UART3_isTxReady(void)
{
return (U3STAbits.TRMT && U3STAbits.UTXEN);
}
void UART3_send_s(char* value)
{
while (*value)
     UART3_send(*value++);
}

I then send a string after the init() and the configuration, like so:

send_s("Hallo World");
uint8_t receive = UART3_receive();

I am supposed to receive 'O' (from a full message "OK"), but i keep receiving different and incorrect values.
Is there something to do to solve this issue?
 
Thanks.
#1

19 Replies Related Threads

    ric
    Super Member
    • Total Posts : 23545
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: UART not receiving buffer 2019/08/23 04:58:51 (permalink)
    0
    Are you receiving "Hallo World" correctly?
     
    (Note, correct spelling is "Hello" )
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    AKJ
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2019/05/27 06:55:52
    • Location: 0
    • Status: offline
    Re: UART not receiving buffer 2019/08/23 05:14:37 (permalink)
    0
    ric,
     
    "hallo" in some languages = "hello" in English.
    I can't receive more than 2 characters back and also, the value that i receive, differ each time.
     
    Thanks
    #3
    ric
    Super Member
    • Total Posts : 23545
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: UART not receiving buffer 2019/08/23 05:24:58 (permalink)
    0
    AKJ
    "hallo" in some languages = "hello" in English.

    Is "World" spelt like that on those languages? ;)
     

    I can't receive more than 2 characters back and also, the value that i receive, differ each time.

    How exactly are you testing this?
    Does the other end wait until the complete "Hallo World" has been received before it sends anything?
    If it is sending at the same time as you, then you WILL lose characters. You'd have to set up a receive interrupt service to save into a FIFO buffer if you want to send and receive at the same time.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3233
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: UART not receiving buffer 2019/08/23 05:34:38 (permalink)
    0
    "send_s("Hallo World");"

    Use the stdio library.

    "puts("Hallo World\r");"

    The stdout function can be re-directed to use any UART.

    Override ,"puts" to:

    puts(int Console, const char *String);
    "Console" could be uart 1 - x or even an LCD.
    You could also set a global variable instead of override.
    int __attribute__((__weak__, __section__(".libc.puts"))) puts(int Console, const char *String){
    switch(Console){
    case 1 : //uart 1 - 4, LCD etc...
        while(!*S){
            ;insert driver code
           }
    }

    post edited by Gort2015 - 2019/08/23 05:36:32

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #5
    AKJ
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2019/05/27 06:55:52
    • Location: 0
    • Status: offline
    Re: UART not receiving buffer 2019/08/23 05:40:35 (permalink)
    0
    ric,
     
    "Is "World" spelt like that on those languages? ;)" haha nice.
    How do i set a receive interrupt routine?
     
    Thanks.
    #6
    AKJ
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2019/05/27 06:55:52
    • Location: 0
    • Status: offline
    Re: UART not receiving buffer 2019/08/23 06:10:45 (permalink)
    0
    Gort2015,
     
    is there a way to write my own function from scratch?
     
    Thanks.
    #7
    AKJ
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2019/05/27 06:55:52
    • Location: 0
    • Status: offline
    Re: UART not receiving buffer 2019/08/23 06:58:47 (permalink)
    0
    Moreover, according to the debugger, the Baud Rate is stuck at 77 even though, i am setting it to 312, after calculating.
    Is my clock speed too low? I am using the internal oscilllator which is clocked at 24 MHz.
    #8
    AKJ
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2019/05/27 06:55:52
    • Location: 0
    • Status: offline
    Re: UART not receiving buffer 2019/08/23 08:12:38 (permalink)
    0
    Gort2015,
     
    i tried this, but still nothing in the buffer:

    char buffer2[10];
    void __attribute__((__interrupt__)) _U3RXInterrupt(void)
    {
    IFS5bits.U3RXIF = 0;
    buffer2[0] = U3RXREG;
    }

    #9
    rodims
    Super Member
    • Total Posts : 1515
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: offline
    Re: UART not receiving buffer 2019/08/23 09:48:09 (permalink)
    0
    If you want to use an interrupt function for receiving you need to configure/enable interrupts.
    If you modified your "config section" from the first post, you need to post it again here.
    buffer2[0] = U3RXREG;
    Your are overwriting previously received bytes with the following ones. 
    It's ok if you only want to check whether your interrupt gets invoked.  
    The logic for receiving and handling must be added.
    #10
    Aussie Susan
    Super Member
    • Total Posts : 3612
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: UART not receiving buffer 2019/08/25 19:26:02 (permalink)
    4 (1)
    If you are having problems getting the UART to work in a basic (polling) manner, then introducing interrupts is just going to add another layer of complexity and problems.
    You really need to tell us more about how you are interacting with the 'other end' of the serial connection by answering some of the other questions in this thread.
    Susan
    #11
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3233
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: UART not receiving buffer 2019/08/25 20:15:18 (permalink)
    0
    Be back soon with a solution.
    post edited by Gort2015 - 2019/08/25 20:19:33

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #12
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3233
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: UART not receiving buffer 2019/08/25 21:35:38 (permalink)
    0
    Try this:
    #define __DIF__ (U2MODE-U1MODE)
    int __attribute__((__weak__, __section__(".libc.write"))) write(const void *Buffer, int Len){
         //global int F exists elsewhere;
        volatile unsigned int *tx=&U1TXREG+__STDOUTF__*__DIF__;
        volatile unsigned int *sta=&U1STA+__STDOUTF__*__DIF__;
        
        switch(__STDOUTF__){
            //uart
            case 1: //uart 1
            case 2: //uart 2
            case 3: //uart 3
                for(int x=0;x<Len;x++){
                    while(!(*sta&_U1STA_TRMT_MASK));
                    *tx=*(char*)Buffer++;
                }
                break;
            case 4:
                //lcd
                break;
        }
        return Len;
    }

    int __STDOUTF__;  //global stdout

    I have not tested it, just kick it around.
    setf(SEL_UART1);
    puts("Hello World\r");
    setf(SEL_UART2);
    puts("Hello World\r");
    setf(SEL_LCD);
    puts("Hello World\r");

     

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #13
    AKJ
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2019/05/27 06:55:52
    • Location: 0
    • Status: offline
    Re: UART not receiving buffer 2019/08/27 02:00:00 (permalink)
    0
    Hello,
     
    thank you for your answers. I am currently testing the hardware. The issue could be a hardware problem, as even without the UART receiving device connected, a breakPoint placed in the RX Interrupt function gets hit, after sending some values over UART. 
    I have connected some pins to my multimeter, and even though these pins are turned to digital, LAT = 0 and set to output, i get 0.5 - 0.10 V of power.
     
    Is this normal?
     
    Thanks
    #14
    rodims
    Super Member
    • Total Posts : 1515
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: offline
    Re: UART not receiving buffer 2019/08/27 04:54:43 (permalink)
    0
    i tried this, but still nothing in the buffer ...
     
    ... The issue could be a hardware problem, as even without the UART receiving device connected, a breakPoint placed in the RX Interrupt function gets hit

     
    Which issue ?? First your interrupt function was not invoked and now it is. You WANT the interrupt function to be invoked, right ? You did not respond to Susan,- but may she is right and interrupts are complicating things too much for now.
    If you let your inputs (RX) floating, you should expect things like receiving some rubbish on the UART. And don't worry about the voltage level of your outputs, it's normal enhough. From your description there is no reason to assume an hardware problem with your PIC.
    But you should explain in detail what is the device you are trying to communicate with.
    How did you prove that this device receives your "Hallo World" correctly ? Is it a PC ?
     
    But anyway: which code are you using now ? If you change your code you need to post it again here, at least if you expect help.
    #15
    AKJ
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2019/05/27 06:55:52
    • Location: 0
    • Status: offline
    Re: UART not receiving buffer 2019/08/28 07:22:22 (permalink)
    0
    Hello,
     
    i apologize for the late reply. The reasons for it was to give you this time, something concrete. (The controller is supposed to be talking to a modem)
     
    I tried to debug this issue doing the following:
     
    0. Write a Python script to read Serial messages on the command -> works (because of the next line)
    1. Send serial message with arduino Uno to a computer -> works
    2. Send serial message with ESP32 to computer -> works
    3. Connect ESP32 to the Tx/Rx pins of the Arduino uno (no script uploaded on it, so to use the Serial ports with interference), i can see the messages from the ESP32 -> works
    4. Open up, Mplab Code generator to test a simple UART example. Configure the controller using the easy setup offered by MCC. Connect the PIC controller to the arduino Ports as done with the ESP32. Send 'a' from the PIC. But i receive something like so "b'\x02\xfc\x03\xfc\x03\xfc\x03\xfc\x03\xfc\x03\xfc\x03\xfc\x03\xfc\x03\xfc\x03\xfc\x03\xfc\x02\xfc\x03\xfc\x03\xfc\x3\xfc'", when the arduino->ESP32 was sending "b\'a"
     
    At least, i now know that the UART connection of my PIC was incorrect and know where to focus my effort.
    Do you have any idea what i might be doing wrong here?
     
    P.S: Codes on the next comment.
     
    Thanks.
    #16
    AKJ
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2019/05/27 06:55:52
    • Location: 0
    • Status: offline
    Re: UART not receiving buffer 2019/08/28 07:25:10 (permalink)
    0
    Serial read:

    import serial
    print("Serial Running with 9600 Baud Rate")
    with serial.Serial('/dev/ttyUSB0', 9600, timeout=10) as ser:
    while True:
    print(ser.readline(30), end='\n')
    print('\n')

     
     
    UART1

    /**
    UART1 Generated Driver File
    @Company
    Microchip Technology Inc.
    @File Name
    uart1.c
    @Summary
    This is the generated driver implementation file for the UART1 driver using Foundation Services Library
    @Description
    This header file provides implementations for driver APIs for UART1.
    Generation Information :
    Product Revision : Foundation Services Library - pic24-dspic-pic32mm : v1.26
    Device : dsPIC33EP512GM706
    The generated drivers are tested against the following:
    Compiler : XC16 1.30
    MPLAB : MPLAB X 3.45
    */
    /*
    (c) 2016 Microchip Technology Inc. and its subsidiaries. You may use this
    software and any derivatives exclusively with Microchip products.
    THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
    EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
    WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
    PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
    WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
    IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
    INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
    WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
    BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
    FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
    ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
    THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
    MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
    TERMS.
    */
    /**
    Section: Included Files
    */
    #include <xc.h>
    #include "uart1.h"
    /**
    Section: UART1 APIs
    */
    void UART1_Initialize(void)
    {
    /**
    Set the UART1 module to the options selected in the user interface.
    Make sure to set LAT bit corresponding to TxPin as high before UART initialization
    */
    // STSEL 1; IREN disabled; PDSEL 8O; UARTEN enabled; RTSMD disabled; USIDL disabled; WAKE disabled; ABAUD disabled; LPBACK disabled; BRGH enabled; URXINV disabled; UEN TX_RX;
    U1MODE = (0x800C & ~(1<<15)); // disabling UARTEN bit
    // UTXISEL0 TX_ONE_CHAR; UTXINV disabled; OERR NO_ERROR_cleared; URXISEL RX_ONE_CHAR; UTXBRK COMPLETED; UTXEN disabled; ADDEN disabled;
    U1STA = 0x0;
    // BaudRate = 9600; Frequency = 12000000 Hz; BRG 312;
    U1BRG = 0x138;

    U1MODEbits.UARTEN = 1; // enabling UARTEN bit
    U1STAbits.UTXEN = 1;
    }
    uint8_t UART1_Read(void)
    {
    while(!(U1STAbits.URXDA == 1))
    {
    }
    if ((U1STAbits.OERR == 1))
    {
    U1STAbits.OERR = 0;
    }
    return U1RXREG;
    }
    void UART1_Write(uint8_t txData)
    {
    while(U1STAbits.UTXBF == 1)
    {
    }
    U1TXREG = txData; // Write the data byte to the USART.
    }
    bool UART1_IsRxReady(void)
    {
    return U1STAbits.URXDA;
    }
    bool UART1_IsTxReady(void)
    {
    return (U1STAbits.TRMT && U1STAbits.UTXEN );
    }
    bool UART1_IsTxDone(void)
    {
    return U1STAbits.TRMT;
    }
    /* !!! Deprecated API - This function may not be supported in a future release !!! */
    UART1_STATUS __attribute__((deprecated)) UART1_StatusGet (void)
    {
    return U1STA;
    }
    /* !!! Deprecated API - This function may not be supported in a future release !!! */
    bool __attribute__((deprecated)) UART1_DataReady(void)
    {
    return UART1_IsRxReady();
    }
    /* !!! Deprecated API - This function may not be supported in a future release !!! */
    bool __attribute__((deprecated)) UART1_is_tx_ready(void)
    {
    return UART1_IsTxReady();
    }
    /* !!! Deprecated API - This function may not be supported in a future release !!! */
    bool __attribute__((deprecated)) UART1_is_rx_ready(void)
    {
    return UART1_IsRxReady();
    }
    /* !!! Deprecated API - This function may not be supported in a future release !!! */
    bool __attribute__((deprecated)) UART1_is_tx_done(void)
    {
    return UART1_IsTxDone();
    }

     
     
    Pins Config:

    /**
    PIN MANAGER Generated Driver File
    @Company:
    Microchip Technology Inc.
    @File Name:
    pin_manager.c
    @Summary:
    This is the generated manager file for the PIC24 / dsPIC33 / PIC32MM MCUs device. This manager
    configures the pins direction, initial state, analog setting.
    The peripheral pin select, PPS, configuration is also handled by this manager.
    @Description:
    This source file provides implementations for PIN MANAGER.
    Generation Information :
    Product Revision : PIC24 / dsPIC33 / PIC32MM MCUs - 1.135.0
    Device : dsPIC33EP512GM706
    The generated drivers are tested against the following:
    Compiler : XC16 v1.36b
    MPLAB : MPLAB X v5.20
    */
    /*
    (c) 2019 Microchip Technology Inc. and its subsidiaries. You may use this
    software and any derivatives exclusively with Microchip products.
    THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
    EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
    WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
    PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
    WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
    IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
    INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
    WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
    BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
    FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
    ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
    THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
    MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
    TERMS.
    */

    /**
    Section: Includes
    */
    #include <xc.h>
    #include <stdio.h>
    #include "pin_manager.h"
    /**
    Section: File specific functions
    */
    /**
    Section: Driver Interface Function Definitions
    */
    void PIN_MANAGER_Initialize (void)
    {
    /****************************************************************************
    * Setting the Output Latch SFR(s)
    ***************************************************************************/
    LATA = 0x0010;
    LATB = 0x0000;
    LATC = 0x0000;
    LATD = 0x0000;
    LATE = 0x0000;
    LATF = 0x0000;
    LATG = 0x0000;
    /****************************************************************************
    * Setting the GPIO Direction SFR(s)
    ***************************************************************************/
    TRISA = 0x1F83;
    TRISB = 0xFFFF;
    TRISC = 0x3FFF;
    TRISD = 0x0160;
    TRISE = 0xF000;
    TRISF = 0x0003;
    TRISG = 0x03C0;
    /****************************************************************************
    * Setting the Weak Pull Up and Weak Pull Down SFR(s)
    ***************************************************************************/
    CNPDA = 0x0000;
    CNPDB = 0x0000;
    CNPDC = 0x0000;
    CNPDD = 0x0000;
    CNPDE = 0x0000;
    CNPDF = 0x0000;
    CNPDG = 0x0000;
    CNPUA = 0x0000;
    CNPUB = 0x0000;
    CNPUC = 0x0000;
    CNPUD = 0x0000;
    CNPUE = 0x0000;
    CNPUF = 0x0000;
    CNPUG = 0x0000;
    /****************************************************************************
    * Setting the Open Drain SFR(s)
    ***************************************************************************/
    ODCA = 0x0000;
    ODCB = 0x0000;
    ODCC = 0x0000;
    ODCD = 0x0000;
    ODCE = 0x0000;
    ODCF = 0x0000;
    ODCG = 0x0000;
    /****************************************************************************
    * Setting the Analog/Digital Configuration SFR(s)
    ***************************************************************************/
    ANSELA = 0x1803;
    ANSELB = 0x0283;
    ANSELC = 0x0C3F;
    ANSELE = 0xF000;
    ANSELG = 0x03C0;

    /****************************************************************************
    * Set the PPS
    ***************************************************************************/
    __builtin_write_OSCCONL(OSCCON & 0xbf); // unlock PPS
    RPOR0bits.RP20R = 0x0001; //RA4->UART1:U1TX
    RPINR18bits.U1RXR = 0x0019; //RA9->UART1:U1RX
    __builtin_write_OSCCONL(OSCCON | 0x40); // lock PPS
    }

     
    main.c
     

     
    #include "mcc_generated_files/system.h"
    #include "mcc_generated_files/uart1.h"
    /*
    Main application
    */
    int main(void)
    {
    // initialize the device
    SYSTEM_Initialize();
    while (1)
    {
    UART1_Write('a');
    int counter = 0;
    for (counter = 0; counter < 10000; counter++);
    }
    return 1;
    }

     
    #17
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 3233
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: UART not receiving buffer 2019/08/28 12:50:11 (permalink)
    0
    1. configure uart, coms and pins - Read the ref. docs not MCC with magic numbers and inserted docs.
    2. #include <stdio.h>
    3. puts("Hello World\r"); not UART1_Write('a');
     
     
     
     
     
     
     
     

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #18
    AKJ
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2019/05/27 06:55:52
    • Location: 0
    • Status: offline
    Re: UART not receiving buffer 2019/08/29 19:01:58 (permalink)
    0
    Gort2015,
     
    1. i did read the full documentation of the UART protocol for the controller i am using. The first codes posted above were all made by hand (the UART functions got some inspiration from the MCC functions).
     
    2. 3. I did try that but nothing happened. Is the 'puts' automatically connected to the UART1?
     
    Thanks 
    #19
    AKJ
    Starting Member
    • Total Posts : 49
    • Reward points : 0
    • Joined: 2019/05/27 06:55:52
    • Location: 0
    • Status: offline
    Re: UART not receiving buffer 2019/08/30 02:23:22 (permalink)
    5 (1)
    Hello to everyone,
     
    I managed to solve it!
    CSI: I noticed that i was getting serial values read on my terminal, coming from the controller even though its power source was turned off. Moreover, i couldn't even get those junk serial data on the command line sent from the controller, when the debugger/programmer was unplugged.
     
    Then it strucked me, that i was working on a controller that has many oscillators and that to debug, i had set the controller to use the primary oscillators, which means its clock speed was coming from the OSCs pins. I suppose that i never actually thought about oscillators working on Arduinos and ESP controllers, as  they only have a single one and  they don't allow debugging.
     
    To solve this issue. I simply switched to the internal oscillators and voila, i finally solved this one week old issue.
     
    Thanks for your help guys.
    #20
    Jump to:
    © 2019 APG vNext Commercial Version 4.5