当前位置:网站首页>STM32 - capacitive touch button experiment
STM32 - capacitive touch button experiment
2022-07-27 14:27:00 【Three sloths】
One 、 Principle of capacitive touch button
RC Principle of circuit charging and discharging :
R Is the resistance ,CX For capacitance , Press button capacitance CX Start charging , Up to and V1 identical .
RC The charge discharge formula of the circuit :Vt=V0+(V1-V0)*[1-exp(-t/RC)]
V0 Is the initial voltage on the capacitor ;V1 It is the voltage value that the capacitor can be charged or put into ;Vt by t The voltage on the capacitor at any moment ;
When V0=0, The formula is reduced to :Vt=V1*[1-exp(-t/RC)]

That is, under the same conditions , Capacitance value C With time t Proportional relation , The greater the capacitance , The longer the charge reaches a certain threshold .( Zero state response )
Schematic diagram of capacitive touch button :

R: External capacitor, charge discharge resistor ;Cs:TPAD and PCB The stray capacitance between the two ;Cx: When the fingers are pressed , Fingers and TPAD Form capacitance between . switch : Capacitor discharge switch , from STM32IO Oral substitution ;
Principle analysis : Without finger touch , There is only one capacitor in the circuit Cs, And the charging and discharging time is fixed . When your fingers touch LOGO when , Fingers and the ground will form a capacitance Cx,Cx And Cs parallel connection , The total capacitance increases . According to the principle of zero state response ( When different capacitors reach the same voltage , The larger the capacitance, the longer it takes ), Compared with not touching before , The time for the capacitor to reach the same voltage increases , The system therefore detects a touch .
The process of detecting capacitance and touching buttons :
1、TPAD The pin is set to push-pull output , Output 0, Discharge the capacitor to 0.
2、TPAD The pin is set to float input (IO The state after reset ), The capacitor begins to discharge .
3、 At the same time open TPAD The input capture of the pin starts to capture .
4、 Wait for the charge to complete ( Charging to the end Vx, Rising edge detected ).
5、 Calculate the charging time .
notes : When not pressed , Charging time is T1(default). Press down TPAD, The capacitance increases , So the charging time is T2. We can judge whether to press... By detecting the charging and discharging time . If T2-T1 Greater than a certain value , It can be judged that a key is pressed .
Two 、 Programming ideas
Important functions :
1、void TPAD_Reset(void) function : Reset TPAD
Set up IO The port is push-pull output 0, Capacitor discharge . Wait until the discharge is complete , Set to float input , To start charging . At the same time, put the... Of the counter CNT Set to 0.
2、TPAD_Get_Val() function : Get a capture value ( Get charging time )
Reset TPAD, Wait for the rising edge to capture , After capture , Get the value of the timer , Calculate the charging time .
3、TPAD_Get_MaxVal() function :
Multiple calls TPAD_Get_Val() Function to get the charging time , Get the maximum value .
4、TPAD_Init() function : initialization TPAD
After the system starts , Initialize input capture . First 10 Secondary call TPAD_Get_Val() Function to obtain 10 Time of first charge , Then get the middle N(N=8 perhaps 6) The average of times , As the default value of charging time when no capacitor touch key is pressed tpad_default_val.
5、TPAD_Scan() function : scanning TPAD
call TPAD_Get_MaxVal() Function to obtain the maximum charging time in multiple charging , And tpad_default_val Compare , If it is greater than a certain value , It is considered that there is touch action .
6、void TIM5_CH2_Cap_Init(u16 arr,u16 psc) function : Input capture channel initialization
You can use any timer ,M3 Timer 5,M4 Timer 2.

#define TPAD_ARR_MAX_VAL 0XFFFFFFFF
vu16 tpad_default_val=0;
u8 TPAD_Init(u8 psc)
{
u16 buf[10];
u16 temp;
u8 i,j;
TIM2_CH1_Cap_Init(TPAD_ARR_MAX_VAL,psc-1);
for(i=0;i<10;i++){
buf[i]=TPAD_Get_Val();
delay_ms(10);
}
for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{
if(buf[i]>buf[j])
{
temp=buf[i];
buf[i]=buf[j];
buf[j]=temp;
}
}
}
temp=0;
for(i=2;i<8;i++) temp+=buf[i];
tpad_default_val=temp/6;
printf("tpad_default_val:%d\r\n",tpad_default_val);
if(tpad_default_val>TPAD_ARR_MAX_VAL/2) return 1;
return 0;
}
void TPAD_Reset(void)
{
GPIO_InitTypeDef GPIO_InitABC;
GPIO_InitABC.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitABC.GPIO_OType=GPIO_OType_PP;
GPIO_InitABC.GPIO_Pin=GPIO_Pin_5;
GPIO_InitABC.GPIO_PuPd=GPIO_PuPd_DOWN;
GPIO_InitABC.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_InitABC);
GPIO_ResetBits(GPIOA,GPIO_Pin_5);
delay_ms(5);
TIM_ClearITPendingBit(TIM2,TIM_IT_CC1|TIM_IT_Update);
TIM_SetCounter(TIM2,0);
GPIO_InitABC.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitABC.GPIO_OType=GPIO_OType_PP;
GPIO_InitABC.GPIO_Pin=GPIO_Pin_5;
GPIO_InitABC.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_InitABC.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_InitABC);
}
u16 TPAD_Get_Val(void)
{
TPAD_Reset();
while(TIM_GetFlagStatus(TIM2,TIM_IT_CC1)==RESET)
{
if(TIM_GetCounter(TIM2)>TPAD_ARR_MAX_VAL-500)return TIM_GetCounter(TIM2);
}
return TIM_GetCapture1(TIM2);
}
u16 TPAD_Get_MaxVal(u8 n)
{
u16 temp=0;
u16 res=0;
while(n--)
{
if(temp>res) res=temp;
}
return res;
}
#define TPAD_GATE_VAL 100
u8 TPAD_Scan(u8 mode)
{
static u8 keyen=0;
u8 res=0;
u8 sample=3;
u16 rval;
if(mode)
{
sample=6;
keyen=0;
}
rval=TPAD_Get_MaxVal(sample);
if(rval>(tpad_default_val+TPAD_GATE_VAL)&&rval<(10*tpad_default_val))
{
if((keyen==0)&&(rval>(tpad_default_val+TPAD_GATE_VAL)))
{
res=1;
}
keyen=3;
}
if(keyen)keyen--;
return res;
}
void TIM2_CH1_Cap_Init(u32 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitABC;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitABC;
TIM_ICInitTypeDef TIM_ICInitABC;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_TIM2);
GPIO_InitABC.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitABC.GPIO_OType=GPIO_OType_PP;
GPIO_InitABC.GPIO_Pin=GPIO_Pin_5;
GPIO_InitABC.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_InitABC.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_InitABC);
TIM_TimeBaseInitABC.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitABC.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitABC.TIM_Period=arr;
TIM_TimeBaseInitABC.TIM_Prescaler=psc;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitABC);
TIM_ICInitABC.TIM_Channel=TIM_Channel_1;
TIM_ICInitABC.TIM_ICFilter=0X00;
TIM_ICInitABC.TIM_ICPolarity=TIM_ICPolarity_Rising;
TIM_ICInitABC.TIM_ICPrescaler=TIM_ICPSC_DIV1;
TIM_ICInitABC.TIM_ICSelection=TIM_ICSelection_DirectTI;
TIM_ICInit(TIM2,&TIM_ICInitABC);
TIM_Cmd(TIM2,ENABLE);
}
int main(void)
{
u8 t=0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_ms(168);
uart_init(115200);
LED_Init();
TPAD_Init(8);
while(1)
{
if(TPAD_Scan(0))
{
LED1=!LED1;
}
t++;
if(t==15)
{
t=0;
LED0=!LED0;
}
delay_ms(10);
}
}
STM32 Capacitive touch experiment
边栏推荐
- 连接ResourceManager 失败
- STM32——电容触摸按键实验
- Recursive method to realize the greatest common divisor
- C#测量工具示意图
- Some key information about Max animation (shift+v)
- Flat die cutting machine
- Leetcode · daily question · 592. fraction addition and subtraction · simulation
- va_ List usage summary
- Getting started for beginners: build your own blog with WordPress
- Shell编程规范与变量
猜你喜欢

Shell编程规范与变量

PROFINET 模拟器使用教程
![[luogu_p4556] [Vani has an appointment] tail in rainy days / [template] segment tree merging](/img/e3/c2b3d45c6a0d1f7ff0b8b7bccf2106.png)
[luogu_p4556] [Vani has an appointment] tail in rainy days / [template] segment tree merging

Named entity recognition of Chinese electronic medical records based on Roberta WwM dynamic fusion model
How to solve cache avalanche, breakdown and penetration problems

架构——MVC的升华

Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading

基于在线问诊记录的抑郁症病患群组划分与特征分析

Import the virtual machine officially made by Kali Linux into Oracle VirtualBox

面向流行性疾病科普的用户问题理解与答案内容组织
随机推荐
Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
va_list 使用总结
用命令如何返回上级目录
JS 疫情宅在家,学习不能停,七千字长文助你彻底弄懂原型与原型链
[training day3] delete [simulation]
解气!哈工大被禁用MATLAB后,国产工业软件霸气回击
基于GEC6818开发板的相册
基于在线问诊记录的抑郁症病患群组划分与特征分析
在Oracle VirtualBox中导入Kali Linux官方制作的虚拟机
Carla notes (04) - client and world (create client, connect world, batch object, set weather, set lights, world snapshots)
[training day3] section [greed] [two points]
网上券商APP开户安全有保障吗?
Failed to connect to ResourceManager
初学者入门:使用WordPress搭建一个专属自己的博客
C#测量工具示意图
[intensive reading of papers] grounded language image pre training (glip)
Lighting 5g in the lighthouse factory, Ningde era is the first to explore the way made in China
windows10 安装Sql Server 2019
this指向问题,闭包以及递归
DVWA全级别通关教程