2611 views|0 replies

821

Posts

0

Resources
The OP
 

【Qinheng Trial】7. TouchKey [Copy link]

 

This experiment uses the touch button function of the ADC module on CH549, and uses the four touch buttons K1, K2, K3, and K4 connected to P00, P01, P02, and P03 on the CH549EVT learning development board to control the four LEDs connected to P22, P23, P24, and P25 to turn on and off respectively.

1. Experimental resources
1. CH549EVT learning development board;
2. Keil v5.28.0.0;
3. CH549 development data summary.rar;
4. WCHISPTool v2.70;
5. CH549EVT other related documents;
6. Qinheng USB to serial port module;
7. Serial port debugging assistant SSCOM51 v5.13.1;

2. Experimental Preparation

1. CH549 touch key. The TouchKey is set on the port shared with the ADC function. CH549 has 16 12-bit ADC ports, distributed on P0[7:0] and P2[7:0], supporting 16 external touch keys. The detection of touch keys utilizes the charging and discharging of capacitors, and is collected and judged through the ADC module.

There is another register related to TouchKey, "TKEY_CTRL", which stores the touch key charging pulse width data. Since only the lower 7 bits are valid, its data range is "0~127". The determination of this value is related to the external capacitance value of the touch key, the operating voltage of the microcontroller, and the operating clock frequency. It should be considered in specific applications. The calculation formula is as follows:

count=(Ckey+Cint)*0.7VDD/ITKEY/(2/Fsys)=(25p+15p)*0.35*5*12M/50u=17
TKEY_CTRL=count > 127 ? 127 : count

The manual also provides detailed instructions for the specific steps of using the touch buttons:

3. This experiment

The serial port (UART0) is also used in the experiment to observe the working status of the keys. Since UART0 has been debugged in the previous experiment, this experiment can directly include the relevant files. The four touch keys K1, K2, K3, and K4 on the CH549EVT are connected to P00, P01, P02, and P03 respectively. These four GPIO ports correspond to AIN8, AIN9, AIN10, and AIN11 of the touch key input channel. The program code for the function implementation is deleted and modified based on the official routine.

The test code is as follows:

void TouchKey_Init( void )//触摸按键初始化
{
	//Touch采样通道设置为高阻输入
	P0_MOD_OC &= 0xF0;                               //P00 P01 P02 P03高阻输入
	P0_DIR_PU &= 0xF0;
	ADC_CFG |= (bADC_EN|bADC_AIN_EN);                //开启ADC模块电源,选择外部通道
	ADC_CFG = ADC_CFG & ~(bADC_CLK0 | bADC_CLK1);    //选择ADC参考时钟750KHz 
	ADC_CHAN = (3<<4);                               //默认选择外部通道0
	ADC_CTRL = bADC_IF;                              //清除ADC转换完成标志,写1清零
	IE_ADC = 1;                                      //开启ADC中断使能
	EA = 1;                                          //开启总中断使能
}

UINT16 TouchKeySelect(UINT8 ch,UINT8 cpw)//触摸按键通道选择
{
	ADC_CHAN = ADC_CHAN & (~MASK_ADC_CHAN) | ch;     //外部通道选择
	GetValueFlag = 0;           //标志位清0
	TKEY_CTRL = cpw;            //充电脉冲宽度配置,仅低7位有效(同时清除bADC_IF,启动一次TouchKey)
	while(GetValueFlag == 0);   //等待采用完成
	while(ADC_CTRL & bTKEY_ACT);//bTKEY_ACT=1时,表示正在给电容充电和ADC测量过程中
	return (ADC_DAT & 0x0FFF);
}

void ADC_ISR(void)  interrupt INT_NO_ADC///触摸按键数据采集中断
{
	if(ADC_CTRL & bADC_IF)
	{
		ADC_CTRL = bADC_IF;               //清除ADC转换完成标志
		GetValueFlag = 1;                 //采样完成标志
	}
}

int main(void)
{
	UINT8 ch;
	UINT16 value;
	UINT16 err;  
	
    SAFE_MOD = 0x55;
	SAFE_MOD = 0xAA;					//进入安全模式
	CLOCK_CFG = 0X85;					//使能内部晶振,Fsys=24MHz

	CLK_Config();
	UART0_Config();
	printf("TouchKey test\n");
	
	LED_Port_Init();
	TouchKey_Init();
	Press_Flag = 0;         //无按键按下
	
	/* 获取按键初值 */
	for(ch = 8; ch!=12; ch++)
	{
		PowerValue[ch] = TouchKeySelect(ch,CPW_Table[ch]);
		printf("%d ",PowerValue[ch] );
	}
	printf("\n");
	
	while(1)
	{
		/* 按键检测 */
		for(ch = 8; ch!=12; ch++)
		{
			value = TouchKeySelect(ch,CPW_Table[ch]);
			err = ABS(PowerValue[ch],value);
			if(err > DOWM_THRESHOLD_VALUE)                //差值大于阈值,认为按下
			{
				if((Press_Flag & (1<<ch)) == 0)               //说明是第一次按下
				{
					printf("ch %d pressed,value:%d\n",(UINT16)ch, value);
					/* 点灯处理 */
					LED_Control(ch-8,1);
				}
				Press_Flag |= (1<<ch);
			}
			else if(err < UP_THRESHOLD_VALUE)             //说明抬起或者未按下
			{
				if(Press_Flag & (1<<ch))                      //刚抬起
				{
					Press_Flag &= ~(1<<ch);
					printf("ch %d up,value:%d\n",(UINT16)ch, value);
					/* 灭灯处理 */
					LED_Control(ch-8,0);
				}
			}
		}
	}
}

IV. Experimental Results

The basic functions are realized.

5. Experimental Summary

Through this experiment, I have a preliminary understanding of the CH549 touch button. Because it is the first time to use the touch button, I have not studied some knowledge points deeply enough and there are still some errors in my understanding. In the experiment, I tried to cover the surface of the onboard four-digit button with a piece of PVC card, the same as a credit card, with a thickness of less than 1mm, but it did not work. I also tried to change the data in "CPW_Table[16]", but it still did not work.

This content is originally created by lising , a user on the EEWORLD forum. If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source

This post is from Domestic Chip Exchange
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list