Intro to Peripheral Touch Controller (PTC) Hands-on Project on the ATmega328PB Xplained Mini

Last modified by Microchip on 2023/11/09 08:55

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.

Back to Top

Reference Materials


ATmega328PB Xplained Mini
Evaluation Kit

Atmel® Studio
Integrated Development Environment

Back to Top


Procedure

Atmel Studio

Open Atmel Studio 7

Back to Top

New Project

Go to File>New>Project….

Atmel Studio View:  File>New>Project

Back to Top

QTouch Project

A New Project window appears. From the installed templates section, select GCC C QTouch Executable Project.

New Project window

If you do not see this option, you will need to install QTouch Composer.

Back to Top

Name the Project

Select a name and location for the newly created project.

Back to Top

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.

QTouch Library Project in the QTouch Project Builder page

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.

Sensor Selection page

Click on the Button icon.

Sensor selection Button icon

In the Add Sensors dialogue box, the Number of Buttons is 1 by default, click Add.

Add Sensors dialogue box

In the Add Sensors dialogue box, it is also possible to provide the dimensions of the sensor that is to be displayed on the canvas. These dimensions are intended for visualization only.

The button is added to the QTouch Kit canvas.

Add Sensors dialogue box Button dimensions

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.

Device Selection Window

The selected device appears in the Device Selection tab.

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.

Pin Selection tab

The ATmega328PB Xplained Mini User's Guide contains information on what pins are connected to the touch surfaces (Section 2.3.3). The layout files are also available for the PCB.

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.

Debug Interface Setup tab

QDebug is the touch data protocol used by the QTouch Analyzer. The ATmega328PB Xplained Mini kit connects to the QTouch Analyzer through the Data Gateway Interface (DGI) of the onboard mEDBG device. The mEDBG on the ATmega328PB Xplained Mini kit supports only the UART interface for communicating with QTouch Analyzer. Details of the mEDBG UART connection on the ATmega328PB Xplained Mini are available in Section 2.5.3. of ATmega328PB Xplained Mini User Guide.

In the Debug Interface drop-down list, select UART Interface.

Debug Interface drop down list

UART Port 0 is used for this communication and it is selected by default.

Debug Interface drop down list Port 0 Selected

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.

BSW Channel Properties tab

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.

Tuning Parameter Setup tab

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.

Project Generation tab

Substeps 4, 5, and 6 are optional. If a debug interface is not required and if the default settings are sufficient, go directly from Step 3 – Pin Selection to Step 7 – Project Generation.

QTouch project is successfully created.

Back to Top

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.

In the ATmega328PB Xplained Mini kit, the default fuse settings are such that it operates with the external 16 MHz clock at 5V.

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

Atmel Studio View:  Main.c #define DEF_MCU_CLOCK_PRESCALER 1u

A prescaler setting of "1" sets the clock division factor to 2. Details of various prescaler settings and their corresponding clock division factor can be viewed from the CLKPR register description in the device datasheet.

We have successfully created the QTouch project. The next step is to display the status of the touch button.

Back to Top

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.

/* Configure PB5 as output and set the pin LOW */
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:

if ((p_selfcap_measure_data->measurement_done_touch == 1u))
{
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:

/*Configure PB5 as output and set the pin LOW */
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.

Back to Top

Programming ATmega328PB Xplained Mini Kit

Power the ATmega328PB Xplained Mini kit by connecting a Micro-USB cable.

ATmega328PB Xplained Mini connected by 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.

Pop-up window

Click on Continue and select mEDBG and ISP as Interface.

Select Debugger/Programmer window

Click again on the Start Without Debugging icon or (Ctrl + Alt + F5).

Wait until Atmel Studio 7 completes the programming and the status is ‘Ready’ (at the bottom left corner of the Atmel Studio 7 window).

Atmel Studio View:  Status is ‘Ready’

Back to Top

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.

Back to Top