Intro to PTC Hands-On Project

 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 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, 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 on-board LED.

 Materials

Hardware Tools

Tool About Purchase
ATmega328PB-XplainedMini-50px.png
ATmega328PB Xplained Mini
Evaluation Kit

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
swtool-28px.png
Atmel® Studio
Integrated Development Environment

 Procedure

1

Atmel Studio

Open Atmel Studio 7

2

New Project

Go to File>New>Project….

NewProject.png

3

QTouch Project

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

QTouchExecutableProject.png

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

4

Name the Project

Select a name and location for the newly created project.

5

QTouch Project Builder

Click Create QTouch Library Project in the QTouch Project Builder page. The QTouch Project Builder wizard will now guide you through the steps involved in creating a QTouch project.

QTouchProjectBuilder.png

The Project Builder Workflow includes seven simple steps with which a QTouch project can be created.

1

Step 1 - Sensor Selection

This page provides options to add or delete QTouch Sensors.

Step1.png

Click on the Button icon.

Button.png

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

AddSensors.png

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.

ButtonAdded.png

2

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

Step2.png

The selected device appears in the Device Selection tab.

DeviceSelection.png

3

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

Step3.png

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.

4

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

Step4.png

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

USARTInterface.png

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

Port0.png

5

Step 5 - 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 the Series Resistance for individual channels. The required settings are selected from the drop-down list for each channel.

Step5.png

Default values are sufficient for this project.

6

Step 6 – 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.

Step6.png

Default values of tuning parameters are sufficient for this project.

7

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

Step7.png

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

6

Ensure MCU operating frequency is 8MHz

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

Fuse settings of the connected device can be read from 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

Clock.png

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

7

Code to Display Touch Button Status

In this part, the project will be modified to read sensor status and display the status using the on-board LED of the ATmega328PB Xplained Mini.

The LED is connected to pin 17 - PB5. To configure this as 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 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 a 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:

Build the solution (F7).
Code to display touch status has been added successfully.

8

Programming ATmega328PB Xplained Mini Kit

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

USBPower.png

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

SelectDebugTool.png

Click on Continue and select mEDBG and ISP as Interface.

Interface.png

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 Atmel Studio 7 window).

Ready.png

 Results

The project to view touch state using LED0 is completed and programmed successfully to the ATmega328PB Xplained Mini kit. Press the touch sensor "V" on ATmega328PB Xplained Mini kit and observe the LED.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.