Low-Cost mTouch® Evaluation Kit Example

Last modified by Microchip on 2023/11/10 11:18

Objective

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.

Low Cost mTouch Evaluation Kit

Materials

Hardware Tools

Software Tools

Procedure

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 Analog-to-Digital Converters (ADCs) with automated hardware Capacitive Voltage Divider (CVD) modules.

Project Dashboard view of "low_cost_mTouch_mcc_example" project

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

Back to Top


Open MCC in MPLAB X IDE

MPLAB X toolbar showing the MCC button to open the tool

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 "MPLAB Code Configurator (MCC)" page.

Back to Top


Configure the System Clock

Select 8MHz_HF for Internal Clock, and check Software PLL Enabled. This will result in a 32 MHz system clock.

Screenshot of system clock setup in MCC

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.

Back to Top


Load mTouch Module

Double click on the mTouch button in the Libraries list inside the Device Resources window.

Selecting nTouch Library in the Device Resources window

After loading the module, the mTouch button will appear in the Project Resources pane.

After adding the mTouch Library to the project, it shows up in the Project Resources window

Back to Top


Select mTouch Sensor

After loading the mTouch module, all available mTouch sensor pins will be shown in the Pin 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

The mTouch pins show up in the Pin Manager Grid View when the library is added to project, pins from Table 1 are selected

Back to Top


Add the mTouch Button and Link to the Sensor

Go to the Buttons configuration view, click Create New Button. Type "5" into the Number of Button text box, then click Add.

On Common Button Settings tab add 5 buttons

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

Select each Button and link to the sensor from Table 1

Back to Top


Add mTouch Proximity and Link to the Sensor

Similar to the process of adding the mTouch button, go to the Proximities configuration view, click Create New Proximity Sensor, keep "1" as the Number of Proximity Sensor and click Add.

Create a Proximity Sensor from the Proximities tab

Then, click the proximity sensor name to go to the Proximity Settings view, and select Sensor_AN14 : RC6 / AN14 to link to Proximity0.

Link Proximity Sensor to the AN14/RC6 pin as called out in Table 1

Back to Top


Enable Debugging with Data Visualizer and Configure EUSART Module

Go to the Debug tab within the mTouch module. Check the Enable Debug box and select Data Visualizer as the debug method.

Enable debugging with the Data Visualizer from the Debug tab of mTouch GUI

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

EUSART module showing up in the Project Resources after enabling debugging

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

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

Click on the EUSART module in the Project Resources window and set the baud rate to 57600.

Click on the EUSART module in the Project Resources window and set the baud rate to 57600

Back to Top


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 the Pin Manager: Grid View to select the LED pins based on the pinout table.

In the Pin Manager Grid View, select pins for driving LEDs from Table 2

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.

Changing the pin names to "LEDx" per Table 2 in the Pin Module

Back to Top


Generate Code

Click on the Generate button.

Click on "Generate" button to generate the code

Back to Top


Call mTouch Service in main.c

Open up the generated main.c file and place MTOUCH_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).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void main(void)
{
   // initialize the device
   SYSTEM_Initialize();

   // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
   // Use the following macros to:

   // Enable the Global Interrupts
   INTERRUPT_GlobalInterruptEnable();

   // Enable the Peripheral Interrupts
   INTERRUPT_PeripheralInterruptEnable();

   // Disable the Global Interrupts
   //INTERRUPT_GlobalInterruptDisable();

   // Disable the Peripheral Interrupts
   //INTERRUPT_PeripheralInterruptDisable();

   while (1)
    {
       // Add your application code
       MTOUCH_Service_Mainloop();
    }
}

Back to Top


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 the 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):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "mcc_generated_files/mcc.h"

void processButtonTouch(enum mtouch_button_names button)
{
}

void processButtonRelease(enum mtouch_button_names button)
{
}

void processProximityActivate(enum mtouch_proximity_names prox)
{
}

void processProximityNotActivate(enum mtouch_proximity_names prox)
{
}

/*
                         Main application
 */

void main(void)
{
   // initialize the device
   SYSTEM_Initialize();

   // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
   // Use the following macros to:

   // Enable the Global Interrupts
   INTERRUPT_GlobalInterruptEnable();

   // Enable the Peripheral Interrupts
   INTERRUPT_PeripheralInterruptEnable();

   // Disable the Global Interrupts
   //INTERRUPT_GlobalInterruptDisable();

   // Disable the Peripheral Interrupts
   //INTERRUPT_PeripheralInterruptDisable();

    MTOUCH_Button_SetPressedCallback(processButtonTouch);
    MTOUCH_Button_SetNotPressedCallback(processButtonRelease);
    MTOUCH_Proximity_SetActivatedCallback(processProximityActivate);
    MTOUCH_Proximity_SetNotActivatedCallback(processProximityNotActivate);

   while (1)
    {
       // Add your application code
       MTOUCH_Service_Mainloop();
    }
}

Back to Top


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 complete main.c code is shown in the following code block:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "mcc_generated_files/mcc.h"

void processButtonTouch(enum mtouch_button_names button)
{
   switch(button)
    {
       case Button0: LED1_SetLow();break;
       case Button1: LED2_SetLow();break;
       case Button2: LED3_SetLow();break;
       case Button3: LED4_SetLow();break;
       case Button4: LED5_SetLow();break;
       default: break;
    }
}

void processButtonRelease(enum mtouch_button_names button)
{
   switch(button)
    {
       case Button0: LED1_SetHigh();break;
       case Button1: LED2_SetHigh();break;
       case Button2: LED3_SetHigh();break;
       case Button3: LED4_SetHigh();break;
       case Button4: LED5_SetHigh();break;
       default: break;
    }
}

void processProximityActivate(enum mtouch_proximity_names prox)
{
   if(prox == Proximity0)
        LED0_SetLow();
}

void processProximityNotActivate(enum mtouch_proximity_names prox)
{
   if(prox == Proximity0)
        LED0_SetHigh();
}

/*
                         Main application
 */

void main(void)
{
   // initialize the device
   SYSTEM_Initialize();

   // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
   // Use the following macros to:

   // Enable the Global Interrupts
   INTERRUPT_GlobalInterruptEnable();

   // Enable the Peripheral Interrupts
   INTERRUPT_PeripheralInterruptEnable();

   // Disable the Global Interrupts
   //INTERRUPT_GlobalInterruptDisable();

   // Disable the Peripheral Interrupts
   //INTERRUPT_PeripheralInterruptDisable();

    MTOUCH_Button_SetPressedCallback(processButtonTouch);
    MTOUCH_Button_SetNotPressedCallback(processButtonRelease);
    MTOUCH_Proximity_SetActivatedCallback(processProximityActivate);
    MTOUCH_Proximity_SetNotActivatedCallback(processProximityNotActivate);

   while (1)
    {
       // Add your application code
       MTOUCH_Service_Mainloop();
    }
}

Back to Top


Program the Board

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

Screenshot of the tool bar with the "Program" button highlighted. Click to build the project and program the board

Back to Top


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.

mTouch kit with all boards connected

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

Select the appropriate COM port for the Data Visualizer

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 type DB, DS, and SC.

Select the path for the protocol files in the mcc_generated_files

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

Live Button Touch data in the Data Visualizer window

Live Proximity data in the Data Visualizer window

Back to Top