Electronza
Published in

Electronza

MPLAB Xpress: Proximity Click code

More than two weeks have passed since I published my Altitude Click code. If you don’t have an MPLAB Xpress Development board yet it’s time to order one: the part number is DM164140. On the Microchip Direct site you’ll find this little development board with an estimated ship date of 1 June 2016. Some other big distributors have announced it too: you can find it on Farnell (order code 2543062), Mouser (part no 579-DM164140) and TME.

OK, enough with the hardware. Time for another MPLAB Xpress project: a demo code for the Proximity click board from MikroElektronika.

Proximity Click

This little click board carries one VCNL4010 proximity sensor from Vishay, plus all the additional components required for its use. It’s one of the few MikroElektronika click boards that can be configured for 5V operation — so you can use it also with 5V microcontrollers. However, as the PIC167F18855 is a 3.3V microcontroller I’ll just leave it on its factory setting.

The VCNL4010 combines ambient light detection and proximity detection in one single chip. Ambient light is measured in 16-bit precision on a range of 0.25 lx to 16 klx, with 100 Hz and 120 Hz flicker noise rejection. Proximity measurements are also returned on 16 bit, with the distance range from 0 to 20cm.

Interfacing with the microcontroller is done via I2C, plus an interrupt pin. VCNL4010 has a number of 16 internal registers, of which register #0 is used to configure the operating mode and also as a flag register for completing a measurement. Register #1 stores the device version (current version is 0x21).

Parameters for ambient light measurement are stored in registers #4, while ambient measurement results are stored in registers #5 and #6.

Proximity measurements are configured via registers #2 (proximity measurement rate), #3 (IR LED current) and register #15 (proximity modulator timing adjustments). Results are stored in registers #7 and #8.

VCNL4010 has also an interrupt system, which can be configured to issue an interrupt when specific thresholds are reached. There is a 16-bit low threshold level stored in registers #10 and #11, and a high threshold level stored in registers #12 and #13.

Interrupts are configured via register #9, with the interrupts status stored in register #14.

The hardware of this project is just as simple as placing the Proximity click in the provided mikroBUS socket of the MPLAB Xpress development board.

As for the software, it follows the same configuration steps as in the Altitude Click project, and it uses the same I2C functions I have described there. Thus, I won’t repeat it here.

Although the VCNL4010 has an auto-increment function that allows reading the data from two consecutive registers in one single I2C transaction, this would require a dedicated I2C function. As this project just reads proximity or ambient data and sends it to PC via USB-UART, the slowest part of the code is the UART communication, I see no point in optimizing I2C communication. So, I will just read the ambient and proximity data in two separate transactions, one for the MSB and one for the LSB.

Again, I see no point in performing periodic measurements, as this mode will gather more data that I display with ease on the UART terminal. Instead, I will configure the VCNL4010 to perform measurements on-demand, and in the software, I will poll the corresponding flag in the CONFIG register to determine when a measurement was completed.

To keep things simple I will also disable the interrupt system.

Proximity click C code

To make the code more human-readable I have created a header file containing some definitions of the VCNL4010 address, registers and function names.

The code listing is:

Some explanations regarding the code

The code works in the following way

First, we configure the sensor by writing the appropriate values in the configuration registers and we display the configuration values.

We then enter an infinite loop where we measure first the proximity value. If the proximity value is below a given threshold (here the value is 2200) this means that we have nothing in the measuring range of the sensor. In this case, we measure and display the ambient value.

If the proximity measurement is above the set threshold, then we have something in front of the sensor, and we display the proximity value.

The functions used to perform ambient and proximity measurements differ only by the name of the registers used, so I will explain only the proximity measurement procedure:

uint16_t update_proxy(void)
{
// This is done by setting COMMAND_PROX_ON_DEMAND bit in control register
I2C2_write (VCNL40x0_ADDRESS, REGISTER_COMMAND, COMMAND_PROX_ON_DEMAND);
uint8_t proxy_rdy = 0;
uint16_t proxy_d = 0;
// Then we poll the COMMAND_ALS_DATA_READY bit in comand register
while (proxy_rdy ==0)
{
I2C2_read (VCNL40x0_ADDRESS, REGISTER_COMMAND, &rd_data);
rd_data &= 0x20; // mask all other bits in config register
if (rd_data == 0x20)
proxy_rdy = 1;
else
__delay_ms(100);
}

// We have a valid measurement, let's read it from registers
// Ambient is stored in registers 85 and 86
I2C2_read(VCNL40x0_ADDRESS, REGISTER_PROX_VALUE_MSB, &rd_data);
proxy_d = rd_data << 8;
I2C2_read(VCNL40x0_ADDRESS, REGISTER_PROX_VALUE_LSB, &rd_data);
proxy_d |= rd_data;
return proxy_d;
}

In this function, we first set the prox_en (bit 1) of the CONFIG register #0. This initiates a proximity measurement. When the measurement is completed the prox_data_rdy (bit 5) of the CONFIG register #0 will be set. So, the functions poll this bit until it goes to logical 1.

At that point, the data is ready to be read, so we read the MSB from register #7 and LSB from register #8. The two values are combined and the resulting proximity is returned.

The same process is followed to perform an ambient light measurement, but the measurement is initiated by setting bit 2 of the config register, and bit 6 is polled. Results are in registers #5 and #6.

As usual, I have submitted this code on the MPLAB Xpress Examples site.

Originally published at https://electronza.com on May 12, 2016. Moved to Medium on May 5, 2020.

--

--

--

Arduino, ESP8266, PIC, PIC32 projects and other electronics stuff

Recommended from Medium

How to root Walton walpad 8x

Root android phone

Monthly Update — April 2022

Tutorial Fuzzy Logic Mamdani for Arduino

Tutorial Fuzzy Logic Mamdani for Arduino

How to Resolve QuickBooks Error 3140? | +1–888–485–0289

!P.D.F D.o.w.n.l.o.a.d Nim in Action Full PDF Online

Python Dictionary

Graceful shutdown using Simple Sytems Manager and Terraform on AWS

Top libraries every Java developer should know

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
Teodor Costăchioiu

Teodor Costăchioiu

More from Medium

Month 2 — Cruise Mode

Multi Threading

CoreDump: Provable Security, Oauth

Somebody Called for a Cleaner: Refactoring