Search code examples
mplabbaud-rateusartpic32

PIC32MZ2048ECH144 USART- Junk characters transmitted


I am new to MPLAB X Harmony framework and also in working with microcontrollers. I am working on PIC32MZ2048ECH144. I wanted to transmit a simple string using USART and see it displayed in RealTerm terminal.(I have tried HyperTerminal also.) Whatever string I send, I see only junk characters being displayed. When I browsed for the solution for this problem of junk characters being displayed, there were suggestions to check for the baud rate. I have set the baud rate to be 9600 in MPLab Harmony Configurator(Options -> Harmony Framework configuration -> Drivers -> USART -> USART Driver Instance 0 -> Baud Rate -> 9600). So I used the following line in app.c to explicitly set the baud rate.(PBCLK is 100MHz). But no luck! PLIB_USART_BaudRateSet(USART_ID_2, 100000000 ,9600); The code for app.c file:

    /*******************************************************************************
     Start of File
     */
    const char *string1 = "*** UART Interrupt-driven Application Example ***\r\n";
    const char *string2 = "*** Type some characters and observe the LED turn ON ***\r\n";


    APP_DATA appData = 
    {
    };

    APP_DRV_OBJECTS appDrvObject;

    void APP_Initialize ( void )
    {
        appData.state = USART_ENABLE;
        appData.InterruptFlag = false;
    }

    bool WriteString(void)
    {
        if(*appData.stringPointer == '\0')
        {
            return true;
        }

        while (PLIB_USART_TransmitterIsEmpty(USART_ID_1))
        {
            PLIB_USART_TransmitterByteSend(USART_ID_1, *appData.stringPointer);
            appData.stringPointer++;
            if(*appData.stringPointer == '\0')
            {
                return true;
            }
        }
        return false;
    }

    bool PutCharacter(const char character)
    {
        if(PLIB_USART_TransmitterIsEmpty(USART_ID_1))
        {
            PLIB_USART_TransmitterByteSend(USART_ID_1, character);
            return true;
        }
        else
            return false;
    }

    void APP_Tasks ( void )
    {   
        /* check the application state*/
        switch ( appData.state )
        {
            case USART_ENABLE:
                /* Enable the UART module*/
                PLIB_USART_BaudRateSet(USART_ID_1, 100000000 ,9600);
                PLIB_USART_Enable(USART_ID_1);
                appData.stringPointer = string1;
                appData.state =  USART_TRANSMIT_FIRST_STRING;
                break;

            case USART_TRANSMIT_FIRST_STRING:
                if(true == WriteString())
                {
                    appData.state = USART_TRANSMIT_SECOND_STRING;
                    appData.stringPointer = string2;
                }
                break;

            case USART_TRANSMIT_SECOND_STRING:
                if(true == WriteString())
                {
                    appData.state = USART_RECEIVE_DONE;
                }
                break;

            case USART_RECEIVE_DONE:
                if (appData.InterruptFlag)
                {
                    if(true == PutCharacter(appData.data))
                    {
                      appData.InterruptFlag = false;   
                    }
                }
                break;

            default:

                while (1);

        }

    } 

    /*******************************************************************************
     End of File
     */

I am sorry that I cannot attach the image of the output I receive in RealTerm as I do not have enough points. I have no clue where else the problem could be that gives the mismatch of baud rate. Any hints or help would be of great help. Thanks in advance. Kindly apologize me for any mistakes in the post.


Solution

  • you are correct that it is most likely BAUD rate, but just to be sure how is the USART hooked to the computer? Do you have a translator chip since the computer is expecting +-5V? As for the BAUD, check your clocking scheme and know that PBCLK is sometimes DIV_2 of the SYSCLOCK. There is a great clocking schematic in the Harmony framework to double check your clocking and CONFIG pragmas.