Low-Cost mTouch® Evaluation Kit Example

Introduction

This article explains how to use the MPLAB® Code Configurator (MCC) mTouch® Sensing Solutions module to create one proximity sensor and five touch buttons on the Low-Cost mTouch Evaluation board. You will learn how to set up and use Data Visualizer, a useful tool for tuning a touch application. This article also guides you through the mTouch library callback Application Program Interface (API) to obtain touch information.

mtouch_kit.png

 Materials

Hardware

Software

 Procedure

1

Create an MPLAB X IDE Project for the Low-Cost mTouch Eval Board

The Low-Cost mTouch Eval Board uses thePIC16LF1559. This 8-bit microcontroller has two 10-bit Analog-to-Digital Converters (ADCs) with automated hardware Capacitive Voltage Divider (CVD) modules.

mtouch_kit_example.png

If you are not familiar with MPLAB X IDE, please visit the"MPLAB® X IDE" Developer Help page.

2

Open MCC in MPLAB X IDE

openMCC.png

If you don't see the MCC logo, please check that you have installed the MCC plugin. More information on MCC installation can be found on the "Install MPLAB Code Configurator (MCC)" page.

3

Configure the System Clock

Select8MHz_HF for Internal Clock, and checkSoftware PLL Enabled. This will result in a 32 MHz system clock.

mtouch_kit_fosc.png

To ensure the performance of mTouch button/proximity, the system clock is required to be at least 8 MHz. If you select a system clock slower than 8 MHz, the mTouch module will generate a warning in the notification window.

4

Load mTouch Module

Double click on themTouch button in theLibraries list inside theDevice Resources window.

curiosity_deviceResource.png

After loading the module, themTouch button will appear in theProject Resources pane.

curiosity_projectResource.png

5

Select mTouch Sensor

After loading the mTouch module, all available mTouch sensor pins will be shown in thePin Manager pane. You will need to select the physical sensors and guard based on the sensor pinout information in Table 1.
Pin NameFunction
RC6Proximity Sensor
RB4Button0
RC1Button1
RC0Button2
RA2Button3
RA5Button4
RC4Guard
Table 1
mtouch_kit_selectSensor_2.png

6

Add the mTouch Button and Link to the Sensor

Go to theButtons configuration view, clickCreate New Button. Type "5" into theNumber of Button text box, then clickAdd.

mtouch_kit_createButton_2.png

Then, click each button name to go to theButton Settings view and select the corresponding sensor shown in Table 1.

mtouch_kit_linkSensor_2.png

7

Add mTouch Proximity and Link to the Sensor

Similar to the process of adding the mTouch button, go to theProximities configuration view, clickCreate New Proximity Sensor, keep "1" as theNumber of Proximity Sensor and clickAdd.

mtouch_kit_createProximity_2.png

Then, click the proximity sensor name to go to theProximity Settings view, and selectSensor_AN14 : RC6 / AN14 to link to Proximity0.

mtouch_kit_linkSensor2_2.png

8

Enable Debugging with Data Visualizer and Configure EUSART Module

Go to theDebug tab within the mTouch module. Check theEnable Debug box and selectData Visualizer as the debug method.

8_DebugConfig.png

After enabling debugging, the EUSART module will be automatically added to the project and should appear in the project resources window.

8_EUSARTmodule.png

Check that the RX and TX pins are properly selected in thePin Manager: Grid View.

8_PinManager.png

Click on theEUSART module in the Project Resources window and set the baud rate to57600.

8_eusartConfig.png

9

Setup I/O Pin to Control LED

There are six LEDs on this board and we want to use them to indicate the proximity sensor and button state. The pinout is shown in Table 2:
Pin NameFunction
RC2LED0
RC3LED1
RC7LED2
RC5LED3
RA1LED4
RA0LED5
Table 2

Use thePin Manager: Grid View to select the LED pins based on the pinout table.

mtouch_kit_selectLEDpin.png

Go to the Pin Module and rename the pin as "LEDx". Because the LEDs are active low, we need to setup the pin starting high to turn off the LEDs.

mtouch_kit_renameLED.png

10

Generate Code

Click on theGenerate button.

curiosity_generate.png

11

Call mTouch Service inmain.c

Open up the generatedmain.c file and placeMTOUCH_Service_Mainloop() into the while loop as shown in the following code block (highlighted).

Because the PIC16LF1559 has hardware CVD modules, the CVD scan will be automatically triggered by Timer2, so interrupts are not required for touch functionality alone. However, you will need to enable interrupts since the EUSART module uses them. This is done by removing the comments from the interrupt enable functions as shown in the following code block (highlighted).

12

Set Up Your Own Callback Function

We have seen how to use the polling method to obtain the button/proximity state in theCuriosity Board example. In this example, we will use callback functions. The mTouch library allows you to set your own callback function when a press/release event happens by passing the function pointer to the setter function.

First, we implement the press/release callback functions for button and proximity sensor and then pass these function pointers to the setter functions for each event as shown in the following code block (highlighted):

13

Use the mTouch Button and Proximity to Control LEDs

Once you have setup the callback functions, you will need to implement the logic to control the LEDs based on the event from different proximity/button. The completemain.c code is shown in the following code block:

14

Program the Board

After you connect the board with a programmer (e.g., PICKit™ 4 or MPLAB® ICD 4 In-Circuit Debugger), click theProgram button to program the board.

mtouch_kit_programBoard.png

15

Connect to Data Visualizer

Connect the MCP2221 Breakout Module to the PICkit™ serial header on the mTouch Eval Board and connect to your PC with USB.

15_MCP_connected.png

OpenData Visualizer and select the appropriate COM port in the drop-down box in theSerial Port Control Panel window. Then, click on theAutodetect protocols link to select the folder within the project where the protocol files are located.

15_dvCOMselect.png

The protocol files should be located in the mtouch folder within the mcc_generated_files folder. You can check this by locating the files of typeDB, DS, and SC.

15_SelectPath.png

Now, click theConnect button. When a successful connection is established, you will see the live touch data appear in themTouch Data Visualizer window. This data can be useful for debugging and tuning a touch project.

15_buttondata.png
15_proximitydata.png