AVR 328pb触摸功能基本介绍和使用

AVR 328pb触摸功能基本介绍和使用


📝ATMEGA328PB-AU外设中带外围触摸控制器(PTC)电容式触摸按钮、滑块和轮子24个自帽通道和144个互帽通道。(ATMEGA328P没有的)

✅PTC-外围触摸控制器

  • 🍃低功耗、高灵敏度、环保坚固的电容式触摸按钮、滑块和轮子
  • 🍃支持从省电睡眠模式唤醒触摸
  • 🍃支持互电容和自电容传感
    • 🌾互电容模式下的144个按钮
    • 🌾自电容模式下的24个按钮
    • 🌾混合匹配互电容和自电容传感器每个电极一个引脚
  • 🌿无外部元件负载补偿电荷传感
    • 🍂寄生电容补偿和增益可调,灵敏度高
    • 🍂传感器的自动校准和重新校准
  • 🌿单次电荷测量
  • 🌿硬件噪声滤波和噪声信号去同步,实现高传导抗扰度.
  • 🌿可选通道更改延迟允许根据需要选择新通道上的建立时间采集
  • 🌿通过acquisition-complete中断降低CPU利用率.

PTC方框图

  • 🌿PTC框图互电容(PTC Block Diagram Mutual-Capacitance)
  • 🌿PTC框图自电容.(PTC Block Diagram Self-Capacitance)
  • 🌿互电容传感器布置
  • 🌿自电容传感器布置

📘PTC软件配置

📑为了访问PTC,用户必须使用Atmel Start QTouch®配置器来配置QTouch Library固件并将其与应用软件链接QTouch Library可用于在单个界面上以多种组合方式实现按钮、滑块和滚轮.

  • 🍁Atmel Studio 7.0触摸功能外设配置界面:

  • 🔱触摸参数:

  • 🔑通过Atmel Start QTouch配置的代码如下:

c 复制代码
#include <atmel_start.h>
#include "touch.h"
extern volatile uint8_t measurement_done_touch;

int main(void)
{
	uint8_t key_status0 = 0;
	uint8_t key_status1 = 0;

	/* Initializes MCU, drivers and middleware */
	atmel_start_init();

	/* Enable interrupts */
	cpu_irq_enable();

	/** If any of the two self-capacitance buttons is touched, the LED is turned ON
	 * When touch is released, the LED is turned OFF
	 */
	while (1) {
		/* Does acquisition and post-processing */
		touch_process();
		if (measurement_done_touch == 1) {
			measurement_done_touch = 0;

			key_status0 = get_sensor_state(0) & 0x80;
			key_status1 = get_sensor_state(1) & 0x80;

			if ((0u != key_status0) || (0u != key_status1))
				LED_set_level(true);
			else
				LED_set_level(false);
		}
	}
}

⛳注意事项

✨在具体使用触摸功能进行测试过程中发现,如果启用了串口打印调试信息,会发现触摸相应后,如果打印调试信息会出现乱码,目前解决的办法就是,在需要通过串口打印调试信息的时候,开启串口,在不需要打印串口调试信息的时候,关闭串口功能。

  • 🔨解决串口输出乱码代码如下:
c 复制代码
#include <atmel_start.h>
#include "touch.h"
#include <stdio.h>


extern volatile uint8_t measurement_done_touch;

int main(void)
{
	uint8_t key_status0 = 0;
	uint8_t key_status1 = 0;
	uint8_t key_status2 = 0;
	uint8_t key_status3 = 0;
	/* Initializes MCU, drivers and middleware */
	atmel_start_init();

	/* Enable interrupts */
	cpu_irq_enable();
	//USART_disable();
	/** If any of the two self-capacitance buttons is touched, the LED is turned ON
	 * When touch is released, the LED is turned OFF
	 */
	while (1) {
		/* Does acquisition and post-processing */
		touch_process();
		if (measurement_done_touch == 1) {
			measurement_done_touch = 0;

			key_status0 = get_sensor_state(0) & 0x80;
			key_status1 = get_sensor_state(1) & 0x80;
			key_status2 = get_sensor_state(2) & 0x80;
			key_status3 = get_sensor_state(3) & 0x80;
			if ((0u != key_status0) || (0u != key_status1) || (0u != key_status2) || (0u != key_status3)){
				LED_set_level(true);
				USART_enable_tx();
				printf("********\r\n");
				printf("PE2:%d,PE3:%d,PE0:%d,PE1:%d\r\n",key_status0,key_status1,key_status2,key_status3);
				printf("Touch Press PE0 - PE3\r\n");
				USART_disable();
				
			}
			else
			{
				LED_set_level(false);
				USART_disable();
			}
		}
	}
}