本文系笔者近期调试Microchip公司的SAME51J20A单片机的PTC触摸模块的记录贴。
初次体验Microchip的触摸产品,推荐使用官方的MPLAB X ide。配合MCC图形化配置工具,可以方便的添加“触摸对象”,分配触摸引脚,进行触摸调试设置以及灵敏度等多种参数设置。
测试主控:SAME51J20A Curiosity Nano官方开发板
测试触摸单元:自制触摸按键、触摸滑条
在MCC里面配置触摸:
其中,滑条可以具体指定是由几个部分组成:
点击Tune,设置触摸数据串口调试:
连接Touch Library的UART与SERCOM5的UART,这样才能后续在MPLABX IDE中的DATA VISUALIZER里面动态查看触摸反馈数据。
在main.c里面加载触摸检测代码:
#include "touch_example.h" extern char tempStr[50]; void touch_mainloop_example(void) { /* call touch process function */ touch_process(); if (measurement_done_touch == 1u) { measurement_done_touch = 0u; // process touch data touch_status_display(); } } /*============================================================================ void touch_status_display(void) ------------------------------------------------------------------------------ Purpose: Sample code snippet to demonstrate how to check the status of the sensors Input : none Output : none Notes : none ============================================================================*/ void touch_status_display(void) { uint8_t key_status = 0u; uint8_t scroller_status = 0u; uint16_t scroller_position = 0u; key_status = get_sensor_state(0) & KEY_TOUCHED_MASK; if (0u != key_status) { //Touch detect LED0_PA14_Toggle(); sprintf(tempStr, "\r\nTouch Button Round Pressed"); SERCOM5_USART_Write(tempStr, sizeof (tempStr)); } else { //Touch No detect } key_status = get_sensor_state(1) & KEY_TOUCHED_MASK; if (0u != key_status) { //Touch detect sprintf(tempStr, "\r\nTouch Slider Left Pressed"); SERCOM5_USART_Write(tempStr, sizeof (tempStr)); } else { //Touch No detect } key_status = get_sensor_state(2) & KEY_TOUCHED_MASK; if (0u != key_status) { //Touch detect sprintf(tempStr, "\r\nTouch Slider Middle Pressed"); SERCOM5_USART_Write(tempStr, sizeof (tempStr)); } else { //Touch No detect } key_status = get_sensor_state(3) & KEY_TOUCHED_MASK; if (0u != key_status) { //Touch detect sprintf(tempStr, "\r\nTouch Slider Right Pressed"); SERCOM5_USART_Write(tempStr, sizeof (tempStr)); } else { //Touch No detect } scroller_status = get_scroller_state(0); scroller_position = get_scroller_position(0); //Example: 8 bit scroller resolution. Modify as per requirement. scroller_position = scroller_position >> 5u; //LED_OFF if (0u != scroller_status) { switch (scroller_position) { case 0: //LED0_ON break; case 1: //LED1_ON break; case 2: //LED2_ON break; case 3: //LED3_ON break; case 4: //LED4_ON break; case 5: //LED5_ON break; case 6: //LED6_ON break; case 7: //LED7_ON break; default: //LED_OFF break; } } }
编译,烧录。
***************************************************** Currently loaded versions: Application version...........1.31.39 (0x01.0x1f.0x27) Tool pack version .............1.15.838 Target voltage detected Target device ATSAME51J20A found. Device Revision Id = 0x3 Device Id = 0x61810004 Calculating memory ranges for operation... Erasing... The following memory area(s) will be programmed: program memory: start address = 0x0, end address = 0x3fff configuration memory User Id Memory Due to the large memory ranges on this device, only the areas of memory that have been loaded with code (via the build process or loading a hex file) will be read by default. If you wish to read custom ranges, please go to the Memories to Program property page and specify the ranges you want to read. Programming complete
之后打开MPLAB DATA VISUALIZER:
这里注意Serach Path要指定如下文件夹:
点击开始:
按下按键,可以看到delta 94超过设定阈值,成功检测到触摸。
滑条第一个单元:
滑条第二个单元:
滑条第三个单元:
总结:
Microchip的触摸方案在SAME51系列产品上实现起来非常便捷,得益于MPLABX ide丰富强大的生态和代码自动生成功能。可以基于此,进行更丰富的案例应用。