Introduction
This article guides you from start to finish on how to use the MPLAB® Code Configurator (MCC) mTouch® Sensing Solutions module to create a single touch button on the 8-bit Curiosity Board. It also guides you through the use of the mTouch library API to obtain button information.
1
Create an MPLAB X IDE project for the Curiosity Board.
The Curiosity Board supports a wide range of 8-bit PIC® microcontrollers; in this example, the default PIC16F1619 is used.
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, an 8 MHz system clock is required. 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.
After loading the mTouch module, the available mTouch sensor pin will be shown in the 'Pin Manager'.
Select pin RC1 as the mTouch sensor, which is connected to the mTouch Button on the Curiosity board.
6
Add mTouch button and link to the sensor.
Go to the Buttons configuration view, and click on Create New Button, then the input text box will let you input how many buttons to be created. Input 1 for this project and click Add.
Then, click on the 'Button0' icon to go to the Button Settings view, and select 'Sensor_AN5 sensor' for Button0.
7
Enable Timer Interrupt.
Because the PIC16F1619 uses a software CVD implementation, the Automatic Frequency Adaptation (AFA) requires using a timer interrupt to schedule a CVD scan.
For mTouch library v2.00 or higher, the interrupt will be automatically enabled if AFA is enabled. Please skip this step.
For previous versions, open the 'Interrupt Module' and enable the corresponding timer interrupt; in this example TMR2 is selected as the AFA timer.
If the corresponding timer interrupt is not enabled, there will be a warning issued in the Notification window.
8
Setup I/O pin to control LED.
LED D7 is used to provide visual feedback for the button state.
Select the RC5 pin, which is connected to LED D7, as an output in the 'Pin Manager'.
Go to 'Pin Module', and rename the pin as LED.
9
Generate Code.
Click the Generate button.
10
Call mTouch service in main.c
Open up the generated main.c file, and enable the global and peripheral interrupts by uncommenting the two macros: INTERRUPT_GlobalInterruptEnable() and INTERRUPT_PeripheralInterruptEnable().
Place MTOUCH_Service_Mainloop() in the while loop, as shown below (highlighted):
11
Use mTouch button to control LED D7.
The mTouch button provides an API: bool MTOUCH_Button_isPressed(enum mtouch_button_names button) to obtain the button state.
In this example, there is only one button, so 'Button0' or '0' is provided as the argument for this API. The goal is to turn on the LED D7 (which is active high) when the button is pressed and turn the LED off when the button is released. The application code that you need to write is shown in the highlighted lines below:
12
Program the board and test the code.
Click on the Program icon to program the board.
Once finished, press the mTouch Button and the LED D7 should turn on.