MPLAB Xpress: XC8 code for SHT1x click board

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

In this project I will show how to use the SHT1x click board from MikroElektronika with an MPLAB Xpress Evaluation Board, with code written in XC8 and step by step instructions.

As the name suggests, the SHT1x click board is a breakout board for the Sensirion SHT11 temperature and humidity sensor. The board is compatible with the mikroBUS specification, which is a proprietary standard developed by MikroElektronika for their expansion boards. Nowadays the click board format is no longer used by MikroElektronika alone, you will find click sockets on Microchip’s Curiosity board and Xpress Evaluation board, and there is a plethora of adapter shields that allows other hardware platforms such as Arduino, BeagleBone  or Raspberry Pi to name only a few.

A nice thing regarding the click boards is that they come with code examples too. Just check http://libstock.mikroe.com/ and you might be lucky to find the code you need. In today’s example I have no XC8 code, but there is a MikroC for PIC demo code, which I plan to modify to work on XC8.

This project assumes that you already have an Microchip account, and that you have installed Microchip Code Configurator.

Creating a new project in MPLAB Xpress

We start by opening https://mplabxpress.microchip.com/mplabcloud/ide and we login into our account to use all the features of MPLAB Xpress, Then, we click on File – New Project.

New project dialog in MPLAB Xpress

New project dialog in MPLAB Xpress

A new window opens, and we have to choose Microchip Embedded – Standalone Project, then we click on the “Next” button.

Creating a Standalone Project in MPLAB Xpress

Creating a Standalone Project in MPLAB Xpress

We then choose our microcontroller: PIC16F18855, as this is the microcontroller on the MPLAB Xpress board, and again we click the “Next” button.

MPLAB Xpress: choosing the microcontroller

MPLAB Xpress: choosing the microcontroller

At this point we have to give a name to our project. Let’s say SHT1x_Click_Demo. Please observe the underscores in the project name: spaces and other special characters are not allowed, and if you get a “Project name contains illegal characters” error then you can’t go any further.

Naming your project

Naming your project

After we click on finish this wizard terminates, and we are now in MPLAB Xpress main screen. From here we click on the MPLAB Xpress code configurator icon (the one with three blue hexagons).

MPLAB Xpress main screen: Starting MCC

MPLAB Xpress main screen: Starting MCC

 

MPLAB Xpress Code Configurator: setting up PIC16F18855

In this project we assume that you have installed MPLAB Xpress Code configurator, and that everything works fine. All you have to do is to go to step 3 and copy the token to clipboard by clicking on the rightmost green icon (the one with the files).

MPLAB Xpress: starting MCC

MPLAB Xpress: starting MCC

Then we minimize the browser and we start MPLAB Xpress Code Configurator by clicking its desktop icon. Be careful: don’t close the browser, or you won’t be able to insert the code generated by MCC into your project.

We are now in the main screen of MPLAB Xpress Configurator. The microcontroller is shown in SOIC28 package, but I want it to look exactly as on the Xpress board, so I will change the package to QFN28. This is just a cosmetic change, it does not produce other effects.

MPLAB Xpress Code Configurator

MPLAB Xpress Code Configurator

Then, we configure the microcontroller: on the System Module, on the Easy Setup tab we leave everything as it is for a 1MHz system clock, with the watchdog timer disabled:

  • Oscillator select: HFINTOSC
  • HF Internal Clock: 4MHz
  • Clock divider: 4
  • Watchdog Timer Enable: WDT disabled, SWDTEN is ignored
MPLAB Xpress Code Configurator: Easy Setup

MPLAB Xpress Code Configurator: Easy Setup

We go then to the Registers tab, where we go to Register: CONFIG4 and we change:

  • LVP: Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.
Enabling Low Voltage programming

Enabling Low Voltage programming

The above setting is mandatory, as the programmer onboard the MPLAB Xpress Evaluation Board works only in Low Voltage programming mode.

With this setting of the System Module is completed, we can set up UART. This is required as this projects shows temperature and humidity on the PC, using a terminal program such as Putty or TerraTerm. To achieve this we go to “Device Resources” area, we go to “Peripherals” and we double click on EUSART.

Device Resources: EUSART

Device Resources: EUSART

In the EUSART configuration we check “Enable EUSART” and “Enable Transmit” and we set the baudrate to 19200. We also check “Redirect STDIO to EUSART”.

EUSART configuration

EUSART configuration

Finally, we must configure the I/O pins. The SHT1x click board uses pins RC3 and RC4 for communication. Please observe that although these are the usual pins for I2C communication, the SHT1x sensor uses its own protocol and we later will have to write the communication functions ourselves. Then we have to configure the serial communication by setting pin RC1 as TX and pin RC0 as RX.

The settings we do are:

  • set pin RC4 as output; define SCL_pin as an alias for RC4
  • set pin RC3 as input; define SDA_pin
  • set pin RC1 as RX
  • set pin RC0 as TX.

In the lower-right area of the MPLAB Xpress Code Configurator we can see the pins, with little open locks that can be used to configure the pins. Those locks should be set as in the figure below:

Setting up I/O pins

Setting up I/O pins

Now, on the upper-left area on MCC we go to “Project Resources”, “System” and we click on “Pin module”. In the pin module window we perform the following settings:

Pin settings - part 2

Pin settings – part 2

All analog checkboxes must be unchecked. All WPU checkboxes must be unchecked (there are already pull-up resistors on the click board). Pin RC3 receives custom name “SDA_pin”. Pin RC4 is “SCL pin”. Pins RC0 and RC4 are set as outputs, the rest as inputs.

With this settings we finished our work in the MPLAB Xpress Code Configurator and we click on the “Generate”button. After a few warnings we have to receive the message that the code was successfully uploaded to MPLAB Xpress IDE.

Now we can close MPLAB Xpress Code Configurator and  we can return to the browser, where wee see that some new files were added to our project:

MPLAB Xpress Code Configurator generated files

MPLAB Xpress Code Configurator generated files

Reading temperature and humidity

The following code lines in the pin_manager.h file are particularly important for communication with the SHT1x sensor:

To comunicate with the SHT1x sensor one  must change SDA_pin as an output to send data to the sensor, and to change it as an input to receive data. This is done by the SDA_pin_SetDigitalInput() and SDA_pin_SetDigitalOutput() functions.

Setting SDA_pin to a value of logical “1” is done by SDA_pin_SetHigh(), while setting SDA_pin to a value of logical “0” is done by SDA_pin_SetLow(). Similarly, SCL_pin_SetHigh() and SCL_pin_SetLow() are used to generate the clock signal for SHT1x communication.

Reading the state of the SDA_pin is done by the SDA_pin_GetValue() function.

With the above considerations, here’s the listing of main.c code:

One might observe that the above code is heavily inspired from the MikroC for PIC code example, with minor code changes required by the different compiler:

  • When sending data to the SHT1x sensor:
    • SDA_dir = 1; becomes SDA_pin_SetDigitalInput();
    • SDA_dir = 0; becomes SDA_pin_SetDigitalOutput();
    • SDA_pin = 0; becomes SDA_pin_SetLow();
    • SDA_pin = 1; becomes SDA_pin_SetHigh();
    • SCL_pin = 0; becomes SCL_pin_SetLow();
    • SCL_pin = 1; becomes SCL_pin_SetHigh();
  • When receiving data:
    • if (SDA_pin == 1) becomes if (SDA_pin_GetValue() == 1)
  • Delays: note the two underscores at the beginning of delay function in XC8. Easy to make typos here.
    • Delay_uS(1); becomes __delay_us(1);
    • Delay_mS(1); becomes __delay_ms(1);
  • The value defined as RESET in MikroC was renamed SHT_RESET as it overlapped the XC8 RESET.

That’s all. You can download the project files here. Just upload the code, open a terminal program, connect it to the corresponding COM port with a baud rate and 19200 and watch the result:

SHT1x click demo in Putty

SHT1x click demo in Putty

Share.

Leave A Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.