Introduction
This article guides you from start to finish on 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. It also guides you through the use of the mTouch library callback Application program interface (API) to obtain touch information.
1
Create an MPLAB X IDE project for the Low-Cost mTouch Eval Board.
The Low-cost mTouch Eval Board uses the PIC16LF1559. This 8-bit microcontroller has two 10-bit ADCs with automated hardware Capacitive Voltage Divider (CVD) modules.
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.
If you don't see the MCC logo as shown above, 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 system clock.
Select '8MHz_HF' for Internal Clock, and enable 'PLL'. This will result in a 32 MHz system clock.
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 the mTouch icon in the Libraries list inside the Device Resources window.
After loading the module, the mTouch icon will appear in the 'Project Resources'.
5
Select mTouch sensor.
| Pin Name | Function |
|---|---|
| RC6 | Proximity Sensor |
| RB4 | Button0 |
| RC1 | Button1 |
| RC0 | Button2 |
| RA2 | Button3 |
| RA5 | Button4 |
| RC4 | Guard |
6
Add mTouch button and link to the sensor.
Go to the Buttons configuration view, click Create New Button, and put "5" into the Number of Button texb box, then click Add
Then, click each button name to go to the Button Settings view and select the corresponding sensor shown in the above table.
7
Add mTouch Proximity and link to the sensor.
Similarly to the process of adding the mTouch button, go to the Proximities configuration view, click Create New Proximity Sensor, keep "1" as the Nuber of Proximity Sensor and click Add.
Then, click the proximity sensor name to go to the Proximity Settings view, and select 'Sensor_AN14' to link to Proximity0.
8
Setup I/O pin to control LED.
| Pin Name | Function |
|---|---|
| RC2 | LED0 |
| RC3 | LED1 |
| RC7 | LED2 |
| RC5 | LED3 |
| RA1 | LED4 |
| RA0 | LED5 |
Use the 'Pin Manager Grid' view to select the LED pins based on the pinout table.
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.
9
Generate Code.
Click on the Generate button.
10
Call mTouch service in main.c
Open up the generated main.c file and place MTOUCH_Service_Mainloop() into the while loop as shown below (highlighted):
Because the PIC16LF1559 has hardware CVD modules, the CVD scan will be automatically triggered by Timer2 so you don't need to enable interrupts for this project.
11
Set up your own callback function
We have seen how to use the polling method to obtain the button/proximity state in the Curiosity 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 below (highlighted):
12
Use 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 complete main.c code is shown below:
13
Program the board
After you connect the board with a programmer (e.g., PICKit™ 3 or MPLAB® ICD 3 In-Circuit Debugger), click the Program icon to program the board.
















