STM32 touch key

Time：2021-11-24

This paper will use the capture channel 1 of stm32f207 timer 12 to realize the touch key function, which will be applied to the input capture function. Please see the previous article for details《STM32 input capture function》, refer to the development board of punctual atom for code and circuit design.

01. Touch key principle

Touch RC charging and discharging principle: RC circuit is a circuit composed of resistance R and capacitance C. It is a common circuit in pulse generation and shaping circuit.

Charging process:

The power supply charges the capacitor through the resistor. Since the voltage at both ends of the capacitor is 0 at the beginning, the voltage of the voltage is on the resistor. At this time, the current is large and the charging speed is fast. With the rise of the voltage at both ends of the capacitor, the voltage at both ends of the resistor decreases, the current also decreases, and the charging speed is small. The speed of charging is related to the size of resistance and capacitance. The greater the resistance R, the slower the charging, and the greater the capacitance C, the slower the charging. The constant T (TAO) = RC, which measures the charging speed.

Discharge process:

Capacitor C discharges through resistor R. since the voltage of capacitor at the beginning of discharge is e and the discharge current I = E / R, the current is very large, so the discharge speed is very fast. With the continuous discharge of the capacitor, the voltage of the capacitor also decreases. The current also decreases rapidly. The discharge speed of the capacitor is related to RC. The greater the resistance of R, the slower the discharge speed. The larger the capacitance, the slower the discharge speed.

RC circuit charge discharge formula:

Vt = V0+（V1-V0）*[1-exp(-t/RC)]

V0 is the initial voltage value on the capacitor;

V1 is the voltage value to which the capacitor can be finally charged or placed;

VT is the voltage value on the capacitor at time t.

If V0 is 0, charging starts from 0V. Then the formula is simplified as:

Vt=  V1* [1-exp(-t/RC)]

Conclusion: under the same conditions, the capacitance value C is directly proportional to the time value T. the larger the capacitance, the longer the charging time to reach a critical value.

02. Circuit design

The circuit design is actually a pull-up resistor PCB design, directly a circle, and the bottom grounding copper clad to form stray capacitance. Principle of capacitive touch key R: External capacitor charge discharge resistance.

CS: stray capacitance between TPAD and PCB.

Cx: capacitance between finger and TPAD when finger is pressed.

Switch: capacitor discharge switch, replaced by stm32io port.

03. Code design

Detection capacitance touch key process

① TPAD pin is set to push-pull output and output 0 to realize capacitor discharge to 0.

② TPAD pin is set as floating input (status after IO reset), and the capacitor starts charging.

③ At the same time, turn on the input capture of TPAD pin to start capture.

④ Wait for the charging to complete (charging to the bottom VX, rising edge detected).

⑤ Calculate the charging time.

Touch key initialization

Key reset code

{
GPIO_InitTypeDefGPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_ResetBits(GPIOB,GPIO_Pin_14);

SysCtlDelay(5*(SystemCoreClock/3000));

TIM_ClearITPendingBit(TIM12, TIM_IT_Update|TIM_IT_CC1);
TIM_SetCounter(TIM12,0);

/* Connect TIM pinsto AF9 */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14,GPIO_AF_TIM12);

/* TIM12 channel 1 pin (PB14)configuration */
GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
} Hardware and software open source address:

https://github.com/strongercjd/STM32F207VCT6