About this Code Example

Note: This is a direct port of Jason Layton's Code Example for the PIC18F25K42 over to the PIC18F26K83.(See Here)

Objective:

The objective of this project is to demonstrate how to use MPLAB Xpress IDE to interface the Bosch BME280 Environmental Sensor on the MikroElektronika Weather Click board using the Serial Peripheral Interface (SPI) module on the PIC18F26K83 device. The goal is to create a PIC powered CuriosityHPC "weather station" that detects environmental humidity, pressure, and temperature from the Bosch BME280 sensor over SPI in real time. The raw data collected over the SPI bus will be arithmetically processed and then transmitted through the UART module to a serial terminal display.

Board Connection / Setup:

The development board used for this example was the Curiosity HPC Development Board (DM164136). The click board used for the environmental sensing was the MikroElektronika Weather Click, and the hardware used for the USB to UART serial converter was the MCP2200 breakout module. The screenshots below show what the board setup should look like once everything has been connected, as well as the MCC pin manager window that shows all of the pins assigned and implemented in this project.

MCC Pin Manager: Grid View:

SPI Module Setup:

The MikroElektronika Weather Click board has an integrated environmental sensing unit (BME280) that can detect temperature, pressure and humidity. The Weather Click Board was designed to communicate with the target microcontroller using SPI or I2C interface, acting as a slave for either protocol. For the purposes of this example the target microcontroller is the PIC18F26K83 which will interface the BME280 environmental sensor on the Weather Click board using the SPI protocol. It is important when configuring the SPI module as a master on the bus to ensure that the SPI mode is matched up across devices on the bus. The SPI slave in this example supports SPI mode ‘00’ (CPOL = CPHA = 0) in respect to the clock polarity and phase settings.

When setting up the PIC18F26K83 (SPI Master) the Clock Polarity (CKP) and Clock Edge (CKE) bits are used to configure the SPI mode. For this example, both bits should be set to zero to match the mode of the SPI slave (CKE = CKP = 0). The slave select interface of the slave device is active-low according to the device datasheet, so when configuring the master device be sure to make the slave select signal active-low as well. Refer to the BME280 Environmental Sensor device datasheet for specifications about SPI write/read protocol and timing. For this example the SPI master (PIC18F26K83) SCK line was programmed onto RB1, the SDI line was tied to RB2, and the SDO line was tied to RB3. The slave select line (CS) was programmed onto pin RA3 and configured to start high since the SPI slave has an active-low slave select. All of these SPI pins were mapped in MCC.

The screenshot below shows the MCC configuration settings used to add and setup the SPI module on the PIC18F26K83 for this application.

*MCC SPI Module Configuration Settings *

UART Module Setup:

The UART module of the PIC18F26K83 is implemented in this example to display the post processed and calculated temperature, pressure, and humidity results in real time via serial port to a PC. In order to implement the UART module in this code example, MCC must be opened and the UART1 module was added to the project. The screenshot below shows the configuration settings used in this project. The UART was configured in Asynchronous 8-bit mode at a baud rate of 9600 and transmission was enabled. In addition to this the "Redirect STDIO to UART" software setting was enabled. The UART RX pin was configured on pin RB4.

UART1 MCC Configuration Settings:

Converting SPI Raw Data to Temperature, Pressure and Humidity:

The SPI module reads back the temperature and pressure as 20-bit signed values, and reads back humidity as a 16-bits signed value. These sensor readings captured by the SPI module are raw data measurements from the BME280 on the Weather Click board, and a calculation/compensation routine must be performed to turn them into readings that make sense. Please note that the BME280 Environmental Sensor has compensation parameters programmed into the device NVM that must be read/stored to be used in post processing of the raw SPI data. Below are the calculation routines that are implemented in this code example to convert the raw SPI temperature, pressure and humidity readings to the values displayed on the terminal output. The calculations needed to convert the raw sensor values to the displayed results are also available in the BME280 Device Datasheet.

UART Terminal Results:

The figure below illustrates what the serial output from the PIC18F26K83 should look like, and the format that the environmental sensor values will be returned in. The application returns temperature(degC), pressure(Pa), and relative humidity. The PIC microcontroller reads the sensor values using the SPI protocol every 500ms, and with every read the device ID is also verified and printed to ensure that a valid SPI connection is being made and that the slave device is working correctly.

Conclusion:

This project provides a code example of interfacing with a more complex SPI slave device that requires full-duplex communication and shows how to send/receive data using the SPI protocol. Please refer to the PIC18F26K83 datasheet or the MikroElekronika Weather Click board product page for more information or specifications. By interfacing the Weather Click board with a PIC18F26K83 using the SPI protocol on the Curiosity HPC Development Board, this code example created a real time weather station that transmits environmental sensor data serially to a PC.