RA2E1开发板在显要位置设置了两个触摸按钮,分别为TS10(P109),TS11(P110)。
触摸按钮检测原理参考:
电路板制作时都会在表面上覆盖一层绝缘层,用于防腐蚀和绝缘,所以实际电路板设计时情况如 图。电路板最上层是绝缘材料,下面一层是导电铜箔,我们根据电路走线情况设计并决定铜箔的形状,再下面一层一 般是FR-4板材。金属感应片与地信号之间有绝缘材料隔着,整个可以等效为一个电容Cx。一般在设计时候,把金属感应片设计成方便手指触摸大小。

在电路板未上电时,可以认为电容Cx是没有电荷的,在上电时,在电阻作用下,电容Cx就会有一个充电过程,直到电容充满,即Vc电压值为3.3V,这个充电过程的时间长短受到电阻R阻值和电容Cx容值的直接影响。但是在我们选择合适电阻R并焊接固定到电路板上后,这个充电时间就基本上不会变了,因为此时电阻R已经是固定的,电容Cx在无外界明显干扰情况下基本上也是保持不变的。
现在,我们来看看当我们用手指触摸时会是怎样一个情况?如图,当我们用手指触摸时,金属感应片除了与地信号形成一个等效电容Cx外,还会与手指形成一个Cs等效电容。

此时整个电容按键可以容纳的电荷数量就比没有手指触摸时要多了,可以看成是Cx和Cs叠加的效果。在相同的电阻R情况下,因为电容容值增大了,导致需要更长的充电时间。也就是这个充电时间变长使得我们区分有无手指触摸,也就是电容按键是否被按下。
现在最主要的任务就是测量充电时间。充电过程可以看出是一个信号从低电平变成高电平的过程,现在就是要求出这个变化过程的时间。我们可以利用定时器输入捕获功能计算充电时间,即设置TIMx_CH为定时器输入捕获模式通道。这样先测量得到无触摸时的充电时间作为比较基准,然后再定时循环测量充电时间与无触摸时的充电时间作比较,如果超过一定的阈值就认为是有手指触摸。
下图为Vc跟随时间变化情况,可以看出在无触摸情况下,电压变化较快;而在有触摸时,总的电容量增大了,电压变化缓慢一些。

为测量充电时间,我们需要设置定时器输入捕获功能为上升沿触发,图中VH就是被触发上升沿的电压值,也是RA6M5认为是高电平的最低电压值,大 约为1.8V。t1和t2可以通过定时器捕获/比较寄存器获取得到。
回到案例本身。
先设置P103,P104为输出。

其次设置Touch Stack。

在CTSU属性设置Enable
同时设置TS10(P109),TS11(P110)。

接下来利用瑞萨QE工具进行触摸调试。

碰到的问题是QE菜单没Captouch。
原来是要额外下载Captouch软件包:
https://www.renesas.cn/zh/software-tool/qe-capacitive-touch-development-assistance-tool-capacitive-touch-sensors#downloads



安装完毕后就有Captouch功能了。
接下来就可以进行触摸调试了。具体步骤按指引一步步来即可。


调试好之后。
在hal_entry.c加入语句:
externvoidqe_touch_main(void);
qe_touch_main();

在qe_touch_sample.c加入语句:
if (FSP_SUCCESS == err)
{
if(button_status==0)
{
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_03, BSP_IO_LEVEL_HIGH);
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_04, BSP_IO_LEVEL_LOW);
;
}
else
{
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_04, BSP_IO_LEVEL_HIGH);
R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_03, BSP_IO_LEVEL_LOW);
}
最后下载调试,即可实现触摸按钮操作LED灯关闭。
