CAP1xxx Library User's Guide for MCC Turnkey Touch Library from Microchip
Introduction
The CAP1xxx library module will generate the low-level driver code to access the CAP1xxx register. The example code is also provided, showing you how to use the driver in application code.
The CAP1xxx library module is found under the Turnkey Touch library inside the Device Resources window in MPLAB® Code Configurator (MCC). Select the CAP1xxx library module and the I2C module will be added automatically.
Driver Configuration
There are several configuration options for the CAP1xxx driver code, as shown below.
Select Device
Choose the device that the host controller is communicating with.
Device Address
By default, all the CAP1xxx devices use 0x28 as the 7-bit I2C address. Some devices allow assigning different I2C addresses.
Generate Example
You have the option to generate an example code file that shows how to use the CAP1xxx driver in the application code.
I2C
This enables the MCC I2C driver for the device.
ALERT
The CAP1xxx has an ALERT pin to notify the host if an event happened, for example, touch, release, noise, etc.
If the ALERT pin is enabled, you will need to assign a pin on the host to read the ALERT state.
RESET
Some of the CAP1xxx devices feature a RESET pin which is active low.
WAKE
Some of the CAP1xxx devices feature a WAKE pin to wake up the device from Sleep mode. More information can be found on the CAP1xxx datasheet.
Select I2C Master
If the host has multiple I2C peripherals, you can choose the one to talk to the CAP1xxx device.
Generated Files
The module will generate the CAP1xxx driver and application example code (optional).
- cap1xxx_app.c, cap1xxx_app.h: low-level driver that accesses the register.
- cap11xx.h/cap12xx.h: full register description of the CAP1xxx device.
- cap1xxx_example.c, cap1xxx_example.h: example of how to use the driver in application code.
Code Example
Here are some examples using the driver code (PIC32MM as the host):
Use the example code directly
The example function provides a quick start to verify if the hardware is functional.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/cap1xxx_example.h"
int main(void)
{
// initialize the device
SYSTEM_Initialize();
// the CAP1xxx device needs at least 200ms at power up to respond the I2C command
// this is a user-defined function to delay at least 200ms before excute CAP1xxx initialization.
my_delay_function();
CAP1xxx_initializationExample();
while (1)
{
CAP1xxx_handleGeneralStatusExample();
CAP1xxx_readSensorStatusExample();
}
return 1;
}
Use the driver code
Once the design is functional, you can use the driver code to integrate with your application.
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
#include "mcc_generated_files/mcc.h"
// sensor initial configuration
const uint8_t myCAP1xxxConfig[][2] =
{ // Address Value
{ 0x00, 0x00 }, // Main Control
{ 0x1F, 0x2F }, // Sensitivity Control
{ 0x20, 0x20 }, // General Configuration
{ 0x21, 0xFF }, // Sensor Input Enable
{ 0x22, 0xA4 }, // Sensor Input Configuration
{ 0x23, 0x07 }, // Sensor Input Configuration 2
{ 0x24, 0x39 }, // Averaging and Sampling Config
{ 0x26, 0x00 }, // Calibration Activate
{ 0x27, 0xFF }, // Interrupt Enable
{ 0x28, 0xFF }, // Repeat Rate Enable
};
int main(void)
{
// initialize the device
SYSTEM_Initialize();
// the CAP1xxx device needs at least 200ms at power up to respond the I2C command
// this is a user-defined function to delay at least 200ms before excute CAP1xxx initialization
my_delay_function();
// initialize the CAP1xxx device
CAP_init(myCAP1xxxConfig,sizeof(myCAP1xxxConfig)/2);
// wait until all the sensor is calibrated.
while(!CAP_isCalibrated());
while (1)
{
CAP_GENERAL_STATUSbits_t CAP_current_status;
// read out the general status of the CAP1xxx device
CAP_current_status.value = CAP_getGeneralStatus();
if(CAP_current_status.TOUCH)
{
// Touch Event
CAP_SENSOR_INPUT_STATUSbits_t CAP_sensor_input_status;
// Read out sensor status to check which sensor is pressed
CAP_sensor_input_status.value = CAP_getSensorStatus();
printf("Sensor Status = %d \r\n",CAP_sensor_input_status.value);
if (CAP_sensor_input_status.CS1)
{
// application process if CS1 is pressed
LED_SetHigh();
}
else
{
// application process if CS1 is released
LED_SetLow();
}
}
}
return 1;
}
}
Touch Tuning
Microchip recommends using the CAP1xxx GUI for intuitive and efficient tuning.
Application note AN2034 provides guidelines on touch performance tuning.