Intro to Peripheral Touch Controller (PTC) Hands-on Project on the ATmega328PB Xplained Mini
Objective
The Peripheral Touch Controller (PTC) is a hardware module providing high touch performance while achieving best-in-class noise immunity, moisture tolerance, and faster response time.
The following QTouch® project is created to support the one-touch sensor present on the ATmega328PB Xplained Mini kit. The QTouch area of the ATmega328PB Xplained Mini supports three touch buttons. In this project, a one-touch button (Button "V" in the AVR® logo) will be configured. Once configured, we will write code to use the touch button state to control the onboard LED.
Reference Materials
ATmega328PB Xplained Mini
Evaluation Kit
Atmel® Studio
Integrated Development Environment
Procedure
QTouch Project
A New Project window appears. From the installed templates section, select GCC C QTouch Executable Project.
QTouch Project Builder
Click Create QTouch Library Project on the QTouch Project Builder page. The QTouch Project Builder wizard will now guide you through the steps involved in creating a QTouch project.
The Project Builder Workflow includes seven simple steps with which a QTouch project can be created.
Sensor Selection
This page provides options to add or delete QTouch Sensors.
Click on the Button icon.
In the Add Sensors dialogue box, the Number of Buttons is 1 by default, click Add.
The button is added to the QTouch Kit canvas.
Device Selection
This page allows you to select the required touch technology and device. Click on Device Selection. For this example project, select Self Capacitance technology and choose ATmega328PB. Click OK.
The selected device appears in the Device Selection tab.
Pin Selection
Click on the Pin Selection tab. The QTouch Composer automatically assigns a default set of pins for the configured sensors. Modify the pin selection based on the hardware configuration.
The QTouch button "V" is connected to pin PE3 (PTC pin - Y7). Click on the button that has been added, and then open the command pane to select Y7-PE3 as the Y pin.
Debug Interface
Click on the Debug Interface Setup tab.
Check the Enable QDebug Interface checkbox, which allows live streaming of touch data with QTouch Analyzer.
In the Debug Interface drop-down list, select UART Interface.
UART Port 0 is used for this communication and it is selected by default.
BSW Channel Properties
Click on the BSW Channel Properties tab. In this step, it is possible to specify settings such as Auto-Tune mode, Gain, Prescaler, and Series Resistance for individual channels. The required settings are selected from the drop-down list for each channel.
Default values are sufficient for this project.
Tuning Parameter Setup
Click on the Tuning Parameter Setup tab. In this step, a wide range of tuning parameters related to acquisition, noise countermeasures, and other general parameters are configured. The drop-down list for each parameter provides a list of available settings for that particular parameter. A detailed description of each parameter is provided to better understand their function and the impact it has on touch performance. Scroll down the Tuning Parameters page to view the entire list of parameters and their description.
Default values of tuning parameters are sufficient for this project.
Project Generation
Click on the Project Generation tab. A summary of the project configuration is displayed on this page. Review the summary and click Generate Project.
QTouch project is successfully created.
Ensure MCU operating frequency is 8 MHz
By default, the QTouch Composer project for AVR device is configured to operate at 8 MHz. Configuration of the timer and PTC have been set for 8 MHz operation. This configuration can be modified suitably based on different operating frequencies. However, for this example project, the operating frequency will be set to 8 MHz.
The fuse settings of the connected device can be read from the Device Programming window (Ctrl + Shift + P).
In main.c, modify the macro DEF_MCU_CLOCK_PRESCALER to prescale the operating frequency to 8MHz.
#define DEF_MCU_CLOCK_PRESCALER 1u
We have successfully created the QTouch project. The next step is to display the status of the touch button.
Code to Display Touch Button Status
In this part, the project will be modified to read the sensor status and display the status using the onboard LED of the ATmega328PB Xplained Mini.
The LED is connected to pin 17 - PB5. To configure this as an output pin, add the following code before the while(1) loop inside the main() function in the main.c file.
DDRB = (1u<<PINB5); // Configure PB5 as output
PORTB &=~(1u<<PINB5); // Set PB5 to LOW state
The code will be added inside the while(1) loop. It is necessary to measure the sensor. This is done with the function call touch_sensors_measure(). This will do a measurement on all configured sensors. A call to touch_sensors_measure() is already present in the generated code.
After the function has been executed, a test to see if the measurement has been done needs to be added. This information is available inside the p_selfcap_measure_data struct. The parameter to check is measurement_done_touch. Add an if statement after the call to touch_sensors_measure(); also, clear the parameter after the check:
{
p_selfcap_measure_data->measurement_done_touch = 0u;
}
Now that we know that measurement is done, the sensor state can be checked. This is done by a call to GET_SELFCAP_SENSOR_STATE(0). Based on the status, the LED should be on or off. This code is added inside the newly created if statement. The code should look as shown below:
DDRB = (1u<<PINB5); // Configure PB5 as output
PORTB &=~(1u<<PINB5); // Set PB5 to LOW state
while(1)
{
sleep_enable();
sleep_cpu();
sleep_disable();
touch_sensors_measure();
if ((p_selfcap_measure_data->measurement_done_touch == 1u))
{
p_selfcap_measure_data->measurement_done_touch = 0u;
if(GET_SELFCAP_SENSOR_STATE(0))
{
PORTB |= (1<<PINB5); //LED ON when sensor is touched
}
else
{
PORTB &= ~(1<<PINB5); //LED OFF
}
}
}
Build the solution (F7).
The code to display touch status has been added successfully.
Programming ATmega328PB Xplained Mini Kit
Power the ATmega328PB Xplained Mini kit by connecting a Micro-USB cable.
Program the application by clicking on the Start Without Debugging icon or (Ctrl + Alt + F5).
More information on programming and debugging can be found in section 1.4 - Programming and Debugging of ATmega328PB Xplained Mini User Guide.
A pop-up window may prompt you to select the debugging tool.
Click on Continue and select mEDBG and ISP as Interface.
Click again on the Start Without Debugging icon or (Ctrl + Alt + F5).
Results
The project to view the touch state using LED0 is completed and programmed successfully to the ATmega328PB Xplained Mini kit. Press the touch sensor "V" on the ATmega328PB Xplained Mini kit and observe the LED.