PIC32MX and RN52 bluetooth using MPLAB Harmony

Interfacing and Communication between PIC332MX and RN52 bluetooth module using MPLAB Harmony

In this PIC32MX and RN52 bluetooth using MPLAB Harmony tutorial you will learn how to interface and send command from PIC32MX to RN52 bluetooth module.

PIC32MX is one of the 32 bit PIC family products. RN52 is a popular Bluetooth module used for audio streaming, handset and handfree bluetooth application.

The following is about how to interface PIC32MX and RN52 and send command from PIC32 to RN52. It uses the USART peripheral of the PIC32MX and the UART hardware of the RN52. The connection is such that the TX and RX of PIC32MX is crossed connected to RX and TX of the RN52. The connection of RX and TX of the devices is same as in the earlier tutorial Control and Configure RN52 Bluetooth using Arduino.

But in case of PIC32MX, one has to configure which of the available USART peripheral to use. This can be done using MPLAB Harmony easily. The MPLAB Harmony Configurator(MHC) has a graphical interface that can be used to select which of the pins to assign for TX and RX. In this tutorial the USART 1 TX and RX was used. The following shows the Pin Diagram tab in MHC where the TX and RX pins were selected.

USART pin selection in MHC

Note that you have to first set the clock and the USART driver to be used before you can assign the pins in the Pin Diagram above.

In this example tutorial, the USART driver was selected in polling mode(not interrupt), Dynamic Driver was selected in non-blocking mode. The system clock and the peripheral bus clock frequency was set to 80MHz and external clock of 12MHz was used.

Once you have generated the code, MHC automatically generates the USART initialization data structure and system related usart object.

The USART initialization data structure is found in the system_init.c file. The data structure is as follows.

The system related usart object called drvUsart0 is created in the system_definations.h file as below.

To use this drvUsart0 system object handle in application development we use the sysObj.drvUsart0 dot notation

The USART initialization data structure tells various features and settings of the USART peripheral we opted using the MHC.

The next step is to create a USART driver handle to a client and pass the USART initialization data structure using the DRV_USART_OPEN() function .

First we have to create a USART handle for application client. This is done in the app.h file. In the app.h look for the APP_DATA data structure and use the DRV_HANDLE to create a handle called usart_handle as shown in the following code.

Once we have created this handle called usart_handle we can use the DRV_USART_OPEN() function to pass the USART initialization data structure as system object called sysObj.drvUsart0 to open a client of the handle.

This is to be written in the app.c file inside the APP_Initialize() function.

Note that drvUsart0 is system handle to USART which is auto generated and auto named. That is the name drvUsart0 was not user declared. This drvUsart0 is placed inside the SYSTEM_OBJECTS data structure in the system_definations.h automatically. Then to use it you have to SYSTEM_OBJECTS object called sysObj.

Optionally you can just delete drvUsart0 and declare you own system related usart handle in the app.c file as follows,


And then you could have just opened the client handle like in the following way.

In similar manner the client handle called usart_handle was declared and named by us in the app.h file inside the APP_DATA structure. We could have directly declared and written in the app.c file as follows.

DRV_HANDLE usart_handle;

And then we could have opened the client handle as follows.

So in this way you can avoid the dot notation when using MPLAB Harmony.

The next step is to develop the application. The application is mainly developed in the app.c. The APP_Tasks() function in the app.c is the finite state machine where the application code is implemented. The states and state variables for the application are declared in the app.h file.

Consider first what the application must do. In this example we want our application to change the name of the RN52 bluetooth module.The following are the states for our application.

State 1:

For this we need to first enter into the command mode. To enter into the command mode we can use a switch. When the switch is switch to high, we will send a high signal to a transistor switch which will output low signal and this low signal is connected to the GPIO9 pin of the RN52. And to know visually that the command mode has bee issued we can turn on a LED.

State 2:

Then once we are in the command mode we have to issue the command to change the name. The name can be changed by sending sn,harmony\r via USART to the RN52.

State 3:

After we have sent the change name command we have to reboot the RN52. The command to reboot is r,1\r

Then from state 2 we can go back to state 1.

The following is code in app.c

The supporting variables are in app.h as under.

Now the following shows the changed name in the terminal.

PIC32MX and RN52 bluetooth using MPLAB Harmony

For basics we recommend you to look at our basic RN52 Bluetooth Tutorial which teaches how to power on the RN52 and how to communicate with RN52 via terminal. For PIC32 the basic tutorial is Simplest MPLAB Harmony Project with PIC32 microcontroller.

Leave a Reply

Your email address will not be published. Required fields are marked *