According to the plan, the second part should be the touch key routine analysis. This routine should be in the program package path: \EXAM\TouchKey\ First, the manual introduces it like this:
- 17.3 Touch-Key function capacitance detection steps: (1) Set bTKC_2MS and bTKC_CHAN2~bTKC_CHAN0 in the TKEY_CTRL register, select the cycle and input channel. The selected input channel, its GPIO pin must be set to high-impedance input mode, or open-drain output mode and in the output 1 state (equivalent to high-impedance input), Pn_DIR_PU[x]=0. (2) Clear bTKC_IF and enable interrupt IE_TKEY to wait for the timer interrupt, or enter the interrupt program by actively querying bTKC_IF. (3) After the capacitance detection of the current channel is completed, bTKC_IF will be automatically set to request an interrupt, and the preparation stage for the next cycle will be entered at the same time, and the TKEY_DAT data will be kept unchanged for about 87uS. (4) Enter the interrupt program, first read the capacitance data of the current channel from TKEY_DAT, and mask the highest bit bTKD_CHG. This data is a relative value, inversely proportional to the capacitance. When the touch key is pressed, the data is smaller than when it is not pressed. (5) Set bTKC_2MS and bTKC_CHAN2~bTKC_CHAN0 in the TKEY_CTRL register to select the next input channel. This write operation will automatically clear bTKC_IF and end the interrupt request. (6) Compare the TKEY_DAT data read in step (4) with the previously saved data when there is no key on the channel to determine whether the capacitance has changed and whether a key has been pressed. (7) The interrupt returns, and when the capacitance detection of the next channel is completed, it will go to step (3).
复制代码The manual also says: Touch-Key: 6-channel capacitance detection, supports up to 15 touch keys, and supports independent timing interrupts. It can be seen that having an independent timer is much more convenient. Let's analyze the example below: First, the port is initialized. It is initially a high-impedance input without pull-up
- P1_DIR_PU &= ~channel; P1_MOD_OC &= ~channel;
复制代码This manual explains: Pn_MOD_OC Pn_DIR_PU Working mode description 0 0 High-impedance input mode, the pin has no pull-up resistor and is set to 2MS timing:
Then, the average of the five samples of the channel to be measured is used as the initial value
- for ( i = 0; i < TOUCH_NUM; i++ ) { sum = 0; j = SAMPLE_TIMES; TK_SelectChannel( i ); // TKEY_CTRL |= TK_Code[i]; while( j-- ) { OverTime = 0; while( ( TKEY_CTRL & bTKC_IF ) == 0 ) { if( ++OverTime == 0 ) { return FAIL; } } sum += TKEY_DAT; /* */ } Key_FreeBuf[i] = sum / SAMPLE_TIMES; printf( "Key_FreeBuf[%d]=%d\t", (UINT16)(i), (UINT16)Key_FreeBuf[i] ); }
复制代码The main program only determines which channel the key action occurred:
- while ( 1 ) { // if( TK_Measure() != SUCCESS ) /* For Query Mode. */ // { // printf("ERROR\n"); // } if( Touch_IN != 0 ) { if( Touch_IN & CH0 ) { printf("CH0 is pressed.\n"); } if( Touch_IN & CH1 ) { printf("CH1 is pressed.\n"); } if( Touch_IN & CH2 ) { void TK_int_ISR( void ) interrupt INT_NO_TKEY using 1 { static UINT8 ch = 0; UINT16 KeyData; KeyData = TKEY_DAT; if( KeyData < ( Key_FreeBuf[ch] - TH_VALUE ) ) { Touch_IN |= 1 << ( TK_Code[ch] - 1 ); } // printf( "ch[%d]=%d\t", (UINT16)(TK_Code[ch] - 1), (UINT16)KeyData ); if( ++ch >= TOUCH_NUM ) { // printf("\n"); ch = 0; } TK_SelectChannel( ch ); }
复制代码The entire channel selection procedure is as follows:
- UINT8 TK_SelectChannel( UINT8 ch ) { if ( ch <= TOUCH_NUM ) { TKEY_CTRL = ( TKEY_CTRL & 0XF8) | TK_Code[ch]; return SUCCESS; } return FAIL; }
复制代码It can be seen that capacitive touch is an action generated by comparing the capacitance with the initial value each time it changes, so do not place anything on the touch button when the power is just turned on. The following is a screenshot of the operation:
This content is originally created by EEWORLD forum user ddllxxrr. If you want to reprint or use it for commercial purposes, you must obtain the author’s consent and indicate the source