Curiosity Development Board Example

Last modified by Microchip on 2024/06/24 06:29

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.

Top view of Curiosity Board

Back to Top

Reference Materials

Back to Top

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.

Screenshot of mTouch Curiosity Example Project in MPLAB X IDE

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


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 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.

Screenshot of system clock setup in MCC

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.

Selecting nTouch Library in the Device Resources window

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

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


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.

Adding a button to the project in the Sensor/Button/Slider/Proximity tab

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.

Enabling the required timer interrupt - TMR2

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.

Selecting the RC5 pin for the LED in the Pin Manager

Go to Pin Module, and rename the pin as "LED."


Generate Code

Click the Generate button.

Click on "Generate" button to generate the code


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:

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();
    }
}

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:

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
#include "mcc_generated_files/mcc.h"

/*
                         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.

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

Once finished, press the mTouch button and the LED D7 should turn on.

Back to Top