Low-Cost mTouch® Evaluation Kit Example
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.
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.
Open MCC in MPLAB X IDE
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.
Configure the System Clock
Select 8MHz_HF for Internal Clock, and check Software PLL Enabled. 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 on the mTouch button in the Libraries list inside the Device Resources window.
After loading the module, the mTouch button will appear in the Project Resources pane.
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 Name | Function |
---|---|
RC6 | Proximity Sensor |
RB4 | Button0 |
RC1 | Button1 |
RC0 | Button2 |
RA2 | Button3 |
RA5 | Button4 |
RC4 | Guard |
Table 1
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.
Then, click each button name to go to the Button Settings view and select the corresponding sensor shown in Table 1.
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.
Then, click the proximity sensor name to go to the Proximity Settings view, and select Sensor_AN14 : RC6 / AN14 to link to Proximity0.
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.
After enabling debugging, the EUSART module will be automatically added to the project and should appear in the project resources window.
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.
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 Name | Function |
---|---|
RC2 | LED0 |
RC3 | LED1 |
RC7 | LED2 |
RC5 | LED3 |
RA1 | LED4 |
RA0 | LED5 |
Table 2
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.
Generate Code
Click on 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 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).
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
{
// 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();
}
}
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):
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
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();
}
}
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:
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
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();
}
}
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.
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.
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.
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.
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.