Sending ADCC Data via Bluetooth with RN41 module and MPLAB Xpress Board

 Objective

This project uses an RN41 Bluetooth® module interfaced with an Xpress development board. The RN41 module used in this project is part of a MikroElektronika Bluetooth® Click Board. The output of the Xpress Module's on-board potentiometer is connected to the ADCC. That signal is then sent to the RN41 serially via a EUSART connection. This provides a complete foundation for sending analog information via Bluetooth communication, helping you get up and running with your next remote sensor application.

 Materials

Hardware Tools (Optional)

Tool About Purchase
Xpress-50px.png
MPLAB® Xpress
Development Board

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
MPLAB® Xpress
Cloud Integrated Development Environment

Exercise Files

File Download
Installation
Instructions
Windows Linux Mac OSX
Project and Source Files

Additional Files

Files

Additional Resources:

 Procedure

1

Create a new project

Create a new project in MPLAB Xpress for a PIC16F1855 using the MPLAB Xpress Development Board called analogReadSerialWrite.

Instructions are below if this is your first project.

2

Open the MPLAB Code Configurator (MCC).

Instructions are below if this is your first project.

3

Set the Clock and Clock Divider

When you open the MCC window, it will open to the System Module window. Increase the HF Internal Clock to 32_Mhz and decrease the Clock Divider to 1. Check your screen compared to the window below:

figure1.JPG

4

Setting the EUSART

The Bluetooth module will communicate with the PIC16F18855 via serial communication. Therefore, we will take advantage of the EUSART Peripheral to send the RN41 initialization commands serially. Choose EUSART from the Device Resources menu:

figure2.JPG

Once the window is open, a few settings need to be changed.

  1. Check the Enable Transmit box to enable the PIC16F18855 to send information out across the EUSART serial line.
  2. Check the Enable EUSART Interrupts because interrupts will be used in the written portion of this program to help with the communication timing between the PIC16F18855 and the RN41.
  3. Check the Redirect STDIO to USART box. This allows the use of the Standard IO library which makes code writing an easier process by adding additional functions such as printf, which is used heavily in this program.
  4. Increase the Baud Rate to 115200 because this is the speed of communication for the RN41 (found on page 7 here).
  5. Check the Enable Continuous Receive because the PIC will be receiving more than one piece of information from the RN41.
figure3.JPG

5

ADCC setup

As mentioned in the introduction, this program will take data from an analog potentiometer and feed it to the Bluetooth module. Therefore, we will also need an ADCC module to convert the analog potentiometer values to digital signals. Find the ADCC under Device Resources:

figure4.JPG

We need to change a few settings in the ADCC.

  1. Change the Clock Source to FRC, which will allow the ADCC to run even if the CPU is in sleep mode.
  2. Change the Result Alignment to right.

Because the information coming from the ADCC is 10-bits, it must use two 8-bit registers. This changes the order of the output information from the ADCC to right alignment, changing the output values from the left most 10 bits of a 16 bit value to the right 10 most bits. What this means for you is that your program can expect values from 0 to 1023 (2^0 to 2^10) as opposed to values from 2^7 to 2^16, which will be much more confusing to deal with. Check your finished window with the one below:

figure5.JPG

6

Set the Analog Input Pin

You may notice that we only changed two things in the ADCC window, but three places were circled. The third place highlights an I/O pin name change, specifically the ADCC input channel connected to the potentiometer. To update this, navigate to the Pin Module under Project Resources:

figure6.JPG

First, close the locks in the pin configurator window as shown. To close a lock, simply click the open lock. The lock will turn green to highlight the pin is selected.

figure7.JPG

To make the user written program easier to write, we will also change the names of a few pins by changing the Custom Name in the pin configurator window:

figure8.JPG

7

Generating Code

Once you have finished the preliminary setup, choose the Generate option at the top of the page to generate your settings and import them into the MPLAB Xpress. To check that everything uploaded, check your Project box under the Header Files:

figure9.JPG

You will notice that there are two additional files here: RN41.h and RN41.c. The additional files are part of the Project and Source Files download listed earlier. These two files include a series of functions written to setup all the preliminary settings necessary to communicate with the Bluetooth module. The Bluetooth module is controlled by serial commands sent from the PIC16F18855, and therefore these commands must be added into the project.

8

Linking the MCC generated code to your application in main.c

Next, the following code needs to be entered into the main.c file that was generated by the MCC. Double click main.c to make sure you are in the correct file. You can also check by looking at which tab is highlighted in the top of the main window:

figure11.JPG

One word of caution, if the delay functions increase above ~50 μs, it will cause the program to get stuck and stop working. This is due to the byte transfer speed (115200 kps).

Once you have completed writing the program, then it is time to program the microcontroller. Choose the Make and Generate Code button at the top of the IDE window to generate a .hex file.

Figure%2012.JPG

Then, drag this into your Xpress board that shows up as a USB mass storage device to your computer:

Figure%2013.png

 Results

When you have completed this process, it’s time to test it. This project demostrates this project working with a free Android App called Bluetooth Terminal: https://play.google.com/store/apps/details?id=nextprototypes.bluetoothterminal&hl=en

This app allows a user to connect the RN41 to a phone and see the data being sent. Here are the results in a screen shot:

figure13.png

As you can see, the RN41 is sending the current 10-bit value coming from the ADCC; to change this value, turn the on-board potentiometer.

 Conclusions

Now that you have become successful with the Bluetooth chip at an introductory level, you are at the jumping off point for more interesting things. The potentiometer is only one of a myriad of analog sensors that can be used to bring physical world information into the digital world. Use the base of information covered in this tutorial to send other analog information, such as the temperature or weather, into the digital world over greater distances provided by Bluetooth communication. This is a great stepping off point for development in the Internet of Things, or simply if you do not want wires connecting things over long or complicated distances.

© 2016 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.