MPLAB Xpress: Altitude Click demo code

0
Some of the links on this blog are affiliated links. READ MORE

The Altitude Click manufactured by MikroElektronika is an altimeter/barometer implemented with an MPL3115A2 sensor from NXP. It provides 20-bit resolution measurements of altitude and atmospheric pressure, with a 20kPa to 110 kPa range. Altitude measurement has a 30cm precision (1.5Pa). It also has an on-board thermometer with a 12-bit resolution. Communication is done via I2C interface, and a programmable interrupt system can be used.

Altitude Click

Altitude Click

In this blog post I will show you how to use the Altitude click with an MPLAB Xpress Evaluation Board, with code in XC8. In this example we read the pressure and temperature and we determine the altitude, and the results are displayed using a terminal program running on the PC (I use putty).

To create the code I will follow the same steps as in my previous SHT1x example, and I will use the MPLAB Xpress Code Configurator to configure the UART and I2C interfaces.

Configuring the microcontroller

So, we go through all the steps required to create a new PIC16F18855 project and we are now in front of the MCC window. First, we configure the system clock to 1MHZ and we set in CONFIG4 register the option “Low voltage programming enable”. Then, from the “Device resources”  area we add the EUSART and MSSP2 peripherals.

Then we configure EUSART: baud rate is set to 19200, and we check “Enable transmit” and “Redirect STDIO to EUSART”. There is nothing special here, this configuration is used in almost all projects that use PC terminal to display messages.

MCC: EUSART configuration

MCC: EUSART configuration

To configure I2C communication we set MSSP2 to I2C master mode, Enable I2C, and we leave everything else to default settings.

MCC: MSSP2 configuration

MCC: MSSP2 configuration

We have configured the peripherals and now it’s time to configure the pin module. In the right pin view window we set EUSART RX to pin RC1 and EUSART TX on pin RC0 by placing the required locks. Then we assign I2C2 SCL line to pin RC4, and I2C2 SDA lline to pin RC3, by clicking on both input and output locks, like in the picture below.

MCC: I2C2 and EUSART pin assignements

MCC: I2C2 and EUSART pin assignements

Back to the pin manager we leave only the TX pin set as output and we uncheck everything else.

MCC: pin configuration

MCC: pin configuration

At this stage we get two warnings: On pin “RC3” TRIS bit is “input” but function “SDA2” requires it to be “output” and On pin “RC4” TRIS bit is “input” but function “SCL2” requires it to be “output”. All you have to do is ignore these warnings, the code will run fine.

I know that ignoring compiler warnings is a big no-no, but this is not the case. The explanation here lies in the way the Peripheral Pin Select (PPS) module works: for I2C2 module we are allowed to have different pins for SDA input and SDA output, and these pins are controlled by different registers. Input pin is controlled by register SSP2DATPPS, while output pin is given by register RC3PPS. Same happens with the SCL2 line, with input pin controlled by SSP2CLKPPS and output pin given by register RC4PPS. For more information see pages 241-250 of PIC16F18855 datasheet Rev.C.

Finally, we go to the Interrupt module and we enable I2C2 interrupts. We get here a notice that we have to enable the global and the peripheral interrupts in the main code – this warning should be respected, or else the code won’t work.

MCC: Interrupt configuration

MCC: Interrupt configuration

With this we are done with MPLAB Xpress Code Configurator and we can click on “Generate” button to create the code and return to MPLAB Xpress window.

Before going any further let’s take a quick look at the pin_manager.c file:

We see here the values for the SSP2DATPPS , SSP2CLKPPS, RC3PPS and RC4PPS registers I mentioned before. If you are curious about what those values mean, look into the PIB16F18855 datasheet, tables 13-2 and 13-3.

1 2 3
Share.

Leave A Reply