Curiosity Development Board Example
Objective
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 Development Board. It also guides you through the use of the mTouch library API to obtain button information.
Reference Materials
- Part Number: DM164137 Curiosity Development Board
- MPLAB X IDE
- MPLAB XC Compiler
- MPLAB Code Configurator
Procedure
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.
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.
Configure System Clock
Select 8MHz_HF for Internal Clock, and check the PLL Enabled box. 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.
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.
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.
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 are 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.
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.
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."
Generate Code
Click the Generate button.
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:
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();
}
}
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:
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
/*
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();
while (1)
{
// Add your application code
if(MTOUCH_Service_Mainloop())
{
if(MTOUCH_Button_isPressed(Button0))
LED_SetHigh();
else
LED_SetLow();
}
}
}
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.