MPLAB Xpress: XC8 code for SHT1x click board

Teodor Costachioiu
Mar 3, 2016 · 7 min read

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 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.

We start by opening and we login into our account to use all the features of MPLAB Xpress, Then, we click on File — New Project.

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

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

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.

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 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).

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.

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

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.

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.

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”.

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:

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:

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:

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

// get/set SDA_pin aliases
#define SDA_pin_TRIS TRISC3
#define SDA_pin_LAT LATC3
#define SDA_pin_PORT RC3
#define SDA_pin_WPU WPUC3
#define SDA_pin_ANS ANSC3
#define SDA_pin_SetHigh() do { LATC3 = 1; } while(0)
#define SDA_pin_SetLow() do { LATC3 = 0; } while(0)
#define SDA_pin_Toggle() do { LATC3 = ~LATC3; } while(0)
#define SDA_pin_GetValue() RC3
#define SDA_pin_SetDigitalInput() do { TRISC3 = 1; } while(0)
#define SDA_pin_SetDigitalOutput() do { TRISC3 = 0; } while(0)

#define SDA_pin_SetPullup() do { WPUC3 = 1; } while(0)
#define SDA_pin_ResetPullup() do { WPUC3 = 0; } while(0)
#define SDA_pin_SetAnalogMode() do { ANSC3 = 1; } while(0)
#define SDA_pin_SetDigitalMode() do { ANSC3 = 0; } while(0)

// get/set SCL_pin aliases
#define SCL_pin_TRIS TRISC4
#define SCL_pin_LAT LATC4
#define SCL_pin_PORT RC4
#define SCL_pin_WPU WPUC4
#define SCL_pin_ANS ANSC4
#define SCL_pin_SetHigh() do { LATC4 = 1; } while(0)
#define SCL_pin_SetLow() do { LATC4 = 0; } while(0)
#define SCL_pin_Toggle() do { LATC4 = ~LATC4; } while(0)
#define SCL_pin_GetValue() RC4
#define SCL_pin_SetDigitalInput() do { TRISC4 = 1; } while(0)
#define SCL_pin_SetDigitalOutput() do { TRISC4 = 0; } while(0)

#define SCL_pin_SetPullup() do { WPUC4 = 1; } while(0)
#define SCL_pin_ResetPullup() do { WPUC4 = 0; } while(0)
#define SCL_pin_SetAnalogMode() do { ANSC4 = 1; } while(0)
#define SCL_pin_SetDigitalMode() do { ANSC4 = 0; } while(0)

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:
  • When receiving data:
  • Delays: note the two underscores at the beginning of delay function in XC8. Easy to make typos here.
  • The value defined as RESET in MikroC was renamed SHT_RESET as it overlapped the XC8 RESET.

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

Originally published at on March 3, 2016. Moved to Medium on April 28, 2020.


DIY electronics projects and more

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store