当前位置:网站首页>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
边栏推荐
- 开源版思源怎么私有部署
- Download address of each version of libtorch
- PROFINET 模拟器使用教程
- Interview eight part essay · TCP protocol
- Secondary spanning tree [template]
- Utnet hybrid transformer for medical image segmentation
- HDU4565 So Easy! [matrix multiplication] [derivation]
- @Repository详解
- Good architecture is evolved, not designed
- va_ List usage summary
猜你喜欢

线程知识总结
![[luogu_p4820] [national training team] stack [mathematics] [physics] [harmonic progression]](/img/79/67399e6ce1908e38dc000e4b13a7ea.png)
[luogu_p4820] [national training team] stack [mathematics] [physics] [harmonic progression]

Chinese character style transfer --- antagonistic discriminative domain adaptation (L1)

面向流行性疾病科普的用户问题理解与答案内容组织

Dako held a meeting for the biological IPO: the annual revenue was 837million, and Wu Qingjun and his daughter were the actual controllers

Architecture - the sublimation of MVC

Charles tutorial

Slam overview Reading Note 6: slam research based on image semantics: application-oriented solutions for autonomous navigation of mobile robots 2020

codeforces 1708E - DFS Trees

汉字风格迁移篇---对抗性区分域适应(L1)Adversarial Discriminative Domain Adaptation
随机推荐
One of yolox improvements: add CBAM, Se, ECA attention mechanism
How to make computers have public IP
基于RoBERTa-wwm动态融合模型的中文电子病历命名实体识别
log4j2 jdbc appender
Secondary spanning tree [template]
"Game engine light in light out" 4.1 unity shader and OpenGL shader
基于预训练模型的多标签专利分类研究
arduino+ZE08-CH2O甲醛模块,输出甲醛含量
【多线程的相关内容】
HDU4565 So Easy!【矩阵连乘】【推导】
Slam overview Reading Note 6: slam research based on image semantics: application-oriented solutions for autonomous navigation of mobile robots 2020
How to view revenue and expenditure by bookkeeping software
codeforces 1708E - DFS Trees
递归方法实现最大公约数
Research on automatic classification of electronic medical records based on unbalanced data
Navicate报错access violation at address 00000000
SLAM综述阅读笔记六:基于图像语义的SLAM调研:移动机器人自主导航面向应用的解决方案 2020
Leetcode · daily question · 592. fraction addition and subtraction · simulation
【论文精读】Grounded Language-Image Pre-training(GLIP)
Advanced MySQL III. storage engine