Skip to content

Commit 0b43571

Browse files
authored
Merge pull request #938 from cheyao/tkey
Add Tkey captive touch example for v203
2 parents 9b78dd6 + caeadd1 commit 0b43571

4 files changed

Lines changed: 126 additions & 0 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
all : flash
2+
3+
TARGET:= cap_touch_tkey
4+
TARGET_MCU:= CH32V203
5+
TARGET_MCU_PACKAGE:= CH32V203F8
6+
include ../../ch32fun/ch32fun.mk
7+
8+
flash : cv_flash
9+
clean : cv_clean
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Captive Touch using Tkey peripheral
2+
3+
This example shows how to use the Tkey peripheral to sample captive buttons.
4+
5+
It assumes that there are leds on pins PA4 and PB11, and 4 touch pads on PB1, PB0, PA7 and PA5. (This was made for my [comu](https://github.com/cheyao/comu) pcb, for pad size reference)
6+
7+
It turns on the left led when PB1 touched, off when PB0 touched. Same for right led and PA7 and PA5.
8+
9+
----
10+
11+
The Tkey peripheral returns a value between 0 and 4096. On my board when pressed the value drops from ~4096 to ~1000, but this is probably dependant on pad size.
12+
13+
You can also adjust the charge time and sampling delay with the IDATAR1 and RDATAR registers.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include "ch32fun.h"
2+
3+
// 4096 = max
4+
// <1024 = pressed
5+
// -> 2560 = threshold
6+
#define THRESHOLD 2560
7+
8+
// Captive keys (left to right):
9+
// A9 (PB1)
10+
// A8 (PB0)
11+
// A7 (PA7)
12+
// A5 (PA5)
13+
#define KEY_L1 9
14+
#define KEY_L2 8
15+
#define KEY_R2 7
16+
#define KEY_R1 5
17+
18+
// Our clock runs at 8*18=144MHz
19+
// PB1 CLK is 72MHz, PB2 is 144MHz
20+
uint16_t sample_touch( const uint8_t key )
21+
{
22+
// Select converted channel
23+
ADC1->RSQR3 = key;
24+
25+
TKey1->IDATAR1 = 0x10; // CHGOFFSET
26+
TKey1->RDATAR = 0x8; // ACT_DCG (How long delay to sample)
27+
while ( !( ADC1->STATR & ADC_FLAG_EOC ) );
28+
29+
return TKey1->RDATAR;
30+
}
31+
32+
int main()
33+
{
34+
SystemInit();
35+
36+
// We dont use funGpioInitAll() as we also want to init ADC1
37+
RCC->APB2PCENR |= ( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
38+
RCC_APB2Periph_ADC1 );
39+
RCC->CFGR0 |= ( 0b11 << 14 ); // Max 14MHz. PCLK2 is 144Mhz by default so /8
40+
41+
// PA4 is left led, PB11 is right
42+
funPinMode( PA4, GPIO_Speed_10MHz | GPIO_CNF_OUT_PP );
43+
funPinMode( PB11, GPIO_Speed_10MHz | GPIO_CNF_OUT_PP );
44+
45+
// Tkeys
46+
funPinMode( PB1, GPIO_CFGLR_IN_ANALOG );
47+
funPinMode( PB0, GPIO_CFGLR_IN_ANALOG );
48+
funPinMode( PA5, GPIO_CFGLR_IN_ANALOG );
49+
funPinMode( PA7, GPIO_CFGLR_IN_ANALOG );
50+
51+
// Reset ADC
52+
RCC->APB2PRSTR |= RCC_APB2Periph_ADC1;
53+
RCC->APB2PRSTR &= ~RCC_APB2Periph_ADC1;
54+
55+
// Configure sampling times for channels
56+
TKey1->SAMPTR2 = ( ADC_SampleTime_7Cycles5 << ( 3 * KEY_L1 ) ) | ( ADC_SampleTime_7Cycles5 << ( 3 * KEY_L2 ) ) |
57+
( ADC_SampleTime_7Cycles5 << ( 3 * KEY_R2 ) ) | ( ADC_SampleTime_7Cycles5 << ( 3 * KEY_R1 ) );
58+
59+
// Always one channel at a time
60+
ADC1->RSQR1 = ( 0 << 20 );
61+
ADC1->RSQR2 = 0;
62+
ADC1->CTLR2 |= ADC_ADON;
63+
64+
// The ADC calibration isn't really needed
65+
// But we include it just in case
66+
67+
// Reset calibration
68+
ADC1->CTLR2 |= ADC_RSTCAL;
69+
while ( ADC1->CTLR2 & ADC_RSTCAL );
70+
71+
// Calibrate
72+
ADC1->CTLR2 |= ADC_CAL;
73+
while ( ADC1->CTLR2 & ADC_CAL );
74+
75+
TKey1->CTLR1 |= ADC_BUFEN | ( 1 << 24 ); // Enable TKey
76+
while ( 1 )
77+
{
78+
// Left pressed
79+
if ( sample_touch( KEY_L1 ) < THRESHOLD )
80+
{
81+
funDigitalWrite( PB11, FUN_LOW );
82+
}
83+
if ( sample_touch( KEY_L2 ) < THRESHOLD )
84+
{
85+
funDigitalWrite( PB11, FUN_HIGH );
86+
}
87+
88+
// Right pressed
89+
if ( sample_touch( KEY_R1 ) < THRESHOLD )
90+
{
91+
funDigitalWrite( PA4, FUN_LOW );
92+
}
93+
if ( sample_touch( KEY_R2 ) < THRESHOLD )
94+
{
95+
funDigitalWrite( PA4, FUN_HIGH );
96+
}
97+
98+
Delay_Ms( 10 );
99+
}
100+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#ifndef _FUNCONFIG_H
2+
#define _FUNCONFIG_H
3+
4+
#endif

0 commit comments

Comments
 (0)