This article guides you from start to finish on how to use the 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.
Create an MPLAB® X 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 article.
Open MCC in MPLAB X IDE.
If you don't see the MCC logo as shown above, please check if you have installed the MCC plugin. More information on MCC installation can be found here.
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.
Load mTouch Module.
Double click 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.
Select mTouch sensor.
Add mTouch button and link to the sensor.
Go to the Buttons configuration view, and click Create New Button five times to create five buttons.
Then, click each buton name to go to the Button Settings view, and select the corresponding sensor shown in the above table.
Add mTouch Proximity and link to the sensor.
Similarly to the process of adding the mTouch button, go to the Proximities configuration view, and click Create New Proximity Sensor.
Then, click the proximity sensor name to go to the Proximity Settings view, and select Sensor_AN14 to link to the Proximity0.
Setup I/O pin to control LED.
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 low to turn off the LEDs.
Click the Generate button.
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 proejct.
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.
To use the API for mTouch button and proximity, mtouch_button.h and mtouch_proximity.h must be included in main.c. We then write the press/release callback functions for button and proximity sensor, and finally pass these function pointers to the setter functions for each event as shown below (highlighted):
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:
Program the board
After you connect the board with a programmer (e.g., PICKit 3 or ICD3), click the Program icon to program the board.