3.4 Application Example Using MPLAB Harmony v2 PLIBs

There are significant differences between MPLAB Harmony v2 and MPLAB Harmony v3 PLIBs, therefore porting from an MPLAB Harmony v2 PLIB-based application to an MPLAB Harmony v3 PLIB-based application is not straight forward. The porting steps will vary based on the MPLAB Harmony modules used by the application.

A simple example is printing a Hello World message on the computer console. This is shown to compare the application development steps using the MPLAB Harmony v2 PLIB APIs against the MPLAB Harmony v3 PLIB APIs.

To create an application using the MPLAB Harmony v2 PLIBs, follow these steps:

  1. Create the MPLAB Harmony v2 project.
  2. Configure using the MHC.
    1. Configure the UART pin: Go to the Pin Setting window of the MHC and configure the UART Transmit pin.
  3. Generate the code using the MHC.
  4. Update app.c.
    1. The following code shows the initialization code required to develop the application. It initializes the UART peripheral.
      UART_Initialize ()
      {
          uint32_t clockSource;
      
          /* Disable the USART module to configure it*/
          PLIB_USART_Disable (USART_ID_6);
      
          /* Set the line control mode */
          PLIB_USART_LineControlModeSelect(USART_ID_6, USART_8N1);
      
          /* We set the receive interrupt mode to receive an interrupt whenever FIFO
             is not empty */
          PLIB_USART_InitializeOperation(USART_ID_6,USART_RECEIVE_FIFO_ONE_CHAR,USART_TRANSMIT_FIFO_IDLE,USART_ENABLE_TX_RX_USED);
      
          /* Get the USART clock source value*/
          clockSource = SYS_CLK_PeripheralFrequencyGet ( CLK_BUS_PERIPHERAL_1 );
      
          /* Set the baud rate and enable the USART */
          PLIB_USART_BaudSetAndEnable(USART_ID_6, clockSource, 9600);  /*Desired Baud rate value*/
      }
      
      // *****************************************************************************
      // *****************************************************************************
      // Section: Application Initialization and State Machine Functions
      // *****************************************************************************
      // *****************************************************************************
      
      /*******************************************************************************
        Function:
          void APP_Initialize ( void )
      
        Remarks:
          See prototype in app.h.
       */
      
      void APP_Initialize ( void )
      {   
          UART_Initialize ();
      }
      Note: In MPLAB Harmony v2, the PLIB for the UART peripheral is named as USART, not UART. All APIs have a prefix of PLIB_USART and not PLIB_UART.
    2. The following code has the application logic to show the message on the console.
      uint8_t count = 0;
      uint8_t consoleMsg[] = "Hello World\n\r";
      
      void APP_Tasks ( void )
      {
          if (count < sizeof(consoleMsg))
          {
              /* Wait till TX buffer is available */
              while(PLIB_USART_TransmitterBufferIsFull(USART_ID_6));
              /* Send one byte */
              PLIB_USART_TransmitterByteSend(USART_ID_6, consoleMsg[count]);
              count++;
          }
      }