Our Latest News

Touch key detection based on CH32V103 series MCU

CH32V103 series is a 32-bit general-purpose MCU with the core of the Green Barley V3A processor, which is designed based on the RISC-V open source instruction set. On-chip integration of clock security mechanism, multi-level power management, and general-purpose DMA controller. This series has rich peripheral resources such as 1-channel USB2.0 host/device interface, multi-channel 12-bit ADC conversion module, multi-channel TouchKey, multi-group timer, and multi-channel IIC/USART/SPI interface.

This chapter tutorial uses the CH32V103 development board touch button TK1 for touch button detection, and prints the detection information through the serial debug assistant.

This image has an empty alt attribute; its file name is image-137.png

1、Introduction to TKEY and related functions

1、TKEY_V open

The TKEY_V unit detects the internal multiplexing of the channel selection and some register addresses of the ADC module. To use the TKEY_V function, you need to turn on the ADC module (ADON=1) and turn on the ADC clock to access the relevant registers. Then turn on the TKEY_V_CTLR (ADC_CTLR1) register TKENABLE position 1 to turn on the TKEY_V unit function.

Note: Because the sampling channel selection is shared, the ADC and TKEY_V detection functions cannot be used simultaneously.

2、Working principle

Once the TKEY_V function is turned on, the hardware will automatically carry out a periodic sampling and counting conversion process internally, and after completing one conversion, notify the application code to take the data within a fixed time (tDR) to turn on the next conversion, this cyclic process is automatic with TKEY_V on. As shown in the figure, the hardware will provide the pulse source TKCLK for counting internally, and the application software selects the current hardware counting period as 500us or 1ms, when the counting statistics in the cycle is completed internally, the TKIF flag will be generated to notify the application code to read the value of this conversion, the application code needs to take the data within the maximum 43us (tDR), otherwise the next round of conversion will affect the data register content.

poYBAGPYumaAChyyAAEXKNaPXF4322.png

For specific information about CH32V103 touch button detection, please refer to CH32V103 application manual.

2、Hardware Design

The resources used in this chapter tutorial are all internal resources of the development board, no hardware design is required.

3、Software design

TKEY_V detects the touch keys by the principle that the change of capacitance affects the change of internal oscillation frequency and the size of the statistical count. The specific operation process is as follows.

1) Turn on the ADCEN bit of the RCC module to open the TKEY_V register operation permission.

2) Turn on the TKEY_V function, set the ACON bit to 1, and wake up the ADC module. Turn on the TKEY_V unit by setting the TKENABLE bit of ADC_CTLR1 register to 1.

3) Configure the sampling period by operating the CCSEL[2:0] and TKCPS bits of the TKEY_V_CTLR register to select 500us or 1ms period. The unit is internally timed with the AHB clock, so the CCSEL[2:0] field needs to be equal to the current AHB frequency, otherwise it will result in a large or small sampling period.

4) Configure the sampling channel and set the TKEY_V_CHANNEL register, a write operation of this register will trigger the start of a new cycle.

5) Wait for the TKIF flag to set to 1, indicating the completion of a conversion, you can read the TKDR[13:0] count value in the TKEY_V_SDR register. TKSTA indicates whether the current TKDR[13:0] field count value is valid. If TKIEN is set, the TKEY_V (ADC) interrupt will be triggered simultaneously when TKIF=1 to enter the ADC interrupt service function.

6) Repeat steps 3-5 to get the next count value. 3-4 is optional configuration.

According to the above operation steps, write the program as follows.

tkey.h file

pYYBAGPYumaARfQJAACUs-B2rmo684.png

tkey.h file mainly for the definition and function declaration.

tkey.c file

pYYBAGPYumaAUrnAAACtV0rqKXo116.png
poYBAGPYumaAUVbnAAA-hcPjBWc667.png

The tkey.c file mainly performs the initialization configuration and obtains the touch key detection value.

main.c file

pYYBAGPYumaAXlTVAACTlN1uOLA025.png

The main.c file is mainly used for function initialization and printout of the touch button detection count value.

pYYBAGPYumaAalVXAACeMnzUcgo113.png

After the finger touches the touch button, the serial port prints the following details.

poYBAGPYumaAYxKQAACcO5b4tBo049.png

    GET A FREE QUOTE

    FPGA IC & FULL BOM LIST

    We'd love to

    hear from you

    Highlight multiple sections with this eye-catching call to action style.

      Contact Us

      Exhibition Bay South Squre, Fuhai Bao’an Shenzhen China

      • Sales@ebics.com
      • +86.755.27389663