Introduction to PTC on AVR

Last modified by Microchip on 2023/11/10 11:09

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

Materials

Procedure

Atmel Studio

Open Atmel Studio 7


New Project

Go to File > New > Project….

NewProject.png


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. 


Name the Project

Select a name and location for the newly created 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.

QTouchProjectBuilder.png

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.

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


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


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.


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

USARTInterface.png

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

Port0.png


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.

Step5.png

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.

Step6.png

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.

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.


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

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.


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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*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).
Code to display touch status has been added successfully.


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