Microcontroller touch button processing routine

Publisher:脑力激荡Latest update time:2017-08-17 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//File touchbutton.h

#ifndef _TOUCHBUTTON_H_
#define _TOUCHBUTTON_H_
//=========Macro definition============================================ 
#define DEF_WATCH_TKREFER //Watch reference - for testing
        
		
		
//===========================================================		
		
		                
#define V_REFER 1 //AD value exceeds this value, update reference value

#define V_PRESS 7 //AD value - reference. If it is greater than this value, it is considered that the button is pressed - button sensitivity

#define VT_KTc 30 //The number of times to update the reference value


//--------button position definition-----------
#define K_TKON SETB0 //ON/OFF
#define K_TKBL SETB1 //Boiling
#define K_TKPU SETB2 //Water pump
#define K_TKST SETB3 // Constant temperature setting


//#define V_TKQK1 (uint8)5 //Fast addition and subtraction time value


typedef	struct TCHBUT{

    uint8 KTc1; //Update reference value confirmation time
    uint8 KRefer1 ; //Key 1 reference voltage
    uint8 KTc2 ; //Update reference value confirmation time
    uint8 KRefer2 ; //Key 2 reference voltage
 /* uint8 KTc3 ; //Update reference value confirmation time
    uint8 KRefer3 ; //Key 2 reference voltage
    uint8 KTc4 ; //Update reference value confirmation time
    uint8 KRefer4 ; //Key 2 reference voltage

    uint8 KTc5 ; //Update reference value confirmation time
    uint8 KRefer5 ; //Key 2 reference voltage
    uint8 KTc6 ; //Update reference value confirmation time
    uint8 KRefer6 ; //Key 2 reference voltage
    uint8 KTc7 ; //Update reference value confirmation time
    uint8 KRefer7 ; //Key 2 reference voltage
// uint8 KTc8 ; // Update reference value confirmation time
// uint8 KRefer8 ; //Key 2 reference voltage
*/
    uint8 KTVal1 ;
    uint8 KTVal2 ; 
// uint8 KTVal3 ;
// uint8 KTVal4 ; 
           
}tTCHBut ;
extern xdata tTCHBut tCHBut ;

#define KTc1 tCHBut.KTc1
#define		KRefer1			tCHBut.KRefer1
#define		KTc2			tCHBut.KTc2
#define		KRefer2			tCHBut.KRefer2
#define		KTc3			tCHBut.KTc3
#define		KRefer3			tCHBut.KRefer3
#define		KTc4			tCHBut.KTc4
#define		KRefer4			tCHBut.KRefer4

#define		KTc5			tCHBut.KTc5
#define		KRefer5			tCHBut.KRefer5
#define		KTc6			tCHBut.KTc6
#define		KRefer6			tCHBut.KRefer6
#define		KTc7			tCHBut.KTc7
#define		KRefer7			tCHBut.KRefer7
//#define		KTc8			tCHBut.KTc8
//#define		KRefer8			tCHBut.KRefer8

#define		KTVal1			tCHBut.KTVal1
#define KTVal2 tCHBut.KTVal2
#define		KTVal3			tCHBut.KTVal3
#define		KTVal4			tCHBut.KTVal4

//=======Key detection==============
typedef	struct BUTTON2{

		uint8 TKNOW ; //The current status of the keys
		uint8 TCLST ;
// uint8 TKLST1 ;		
// uint8 TKLST2 ;	
// uint8 TKLST3 ;	
		
		uint8 TKSTB ; //button status after debounce
		uint8 TKSTBL ; //button status after last debounce
		
		uint8 TKAVL ; //falling edge valid value
		uint8 TKAVLP ; //Rising edge valid value
				
		uint8 TKeyStatu ; //Read the current value of the key
		
		uint8 TKLONG ; //Long key press count
// uint8 TTconQK ; // fast adjustment count 111 555 10 10 10
}tButton2 ;
extern  tButton2 tKey2 ;

#define		TKNOW				tKey2.TKNOW
#define		TKLST				tKey2.TKLST
#define		TKLST1				tKey2.TKLST1
//#define		KLST2				tKey.TKLST2
//#define		KLST3				tKey.TKLST3
  
#define		TKSTB				tKey2.TKSTB
#define		TKSTBL				tKey2.TKSTBL

#define		TKAVL				tKey2.TKAVL
#define TKAVLP tKey2.TKAVLP

#define		TKeyStatu		tKey2.TKeyStatu

#define		TKLONG				tKey2.TKLONG
#define		TTconQK			tKey2.TTconQK

#define V_TKLOV1 200 //1S confirms long press
#define V_TKLOV2 20//15 //150MS to set the key flag
 
//=========================================
typedef struct KFlag2 //Key processing flag definition
{
		uint8 Flg1: 1 ;
		uint8 Flg2: 1 ;
		uint8 Flg3: 1 ;
		uint8 Flg4: 1 ;
		uint8 Flg5: 1 ;
		uint8 Flg6: 1 ;
		uint8 Flg7: 1 ;
		uint8 Flg8: 1 ;	
		
}tKFlg2 ;
external tKFlg2 KF2 ;
//#define F_TRdKeyOK KF2.Flg1 //Read key is valid
#define F_TKLO KF2.Flg2 //Long press is effective

//#define	  F_TKQK1			KF2.Flg4
//#define	  F_TKQK2			KF2.Flg5
#define	  F_TKEYOK			KF2.Flg6	


//===========================================


//================================
extern void ReadTKRefer(uint8 CH_AD,uint8 *a) ;

extern void CheckTouch1(void) ;
extern void CheckTouch2(void) ;

extern  void CheckTKey(void) ;


#endif

//File touchbutton.c

#include "global.h"
#include "touchbutton.h"

//#include "touchbutton.h"
/***********************************************
Touch PWM 500K
When the button is pressed, the voltage value increases 

***********************************************/

tButton2 tKey2 ;

tKFlg2 KF2 ;

xdata tTCHBut tCHBut ;


//#define LKEY_ENB //Long key enable
//***************************************
// Function name: ReadTKRefer
// Function: Read touch button reference
// Input parameters: CH_AD button AD channel 
// Export parameter: *a corresponds to the AD reference value of the key
//***************************************
void ReadTKRefer(uint8 CH_AD,uint8 *a)
{
	uint8	R_Save[3];
	uint8	R_Save2[3];		
	uint16 R_SaveSum = 0 ;
    uint8 Tcon = 0 ;
    uint8 Tmp1 ;
    uint8 i ;

    for(i=40;i>0;i--)
    {
        AdcSwitch(CH_AD); //Read button 1 voltage
        Tmp1 = ADDH ;
    
        RMov3Data_Byte(R_Save,Tmp1) ;			
    	Tmp1 = C3MidVal(R_Save); //The maximum number of digits to be processed is 3, and the median value is taken for processing

        RMov3Data_Byte(R_Save2,Tmp1) ;			
    	Tmp1 = C3MidVal(R_Save2); //The maximum number of digits to be processed is 3, and the median value is taken for processing
        	
        R_SaveSum += Tmp1 ;
    
        Tcon++ ;
        if(Tcon >= (1<<3))
        {
            *a = R_SaveSum>>3 ; 
            Tcon = 0 ;
            R_SaveSum = 0 ;    
        }
    }
}
//***************************************
// Function name: CheckTouch1
// Function: Detect touch buttons
// Entry parameters: None
//Export parameters: None
//***************************************
void CheckTouch1(void) 
{
    uint8 a[3];
    uint8 Tmp1 ;
    uint8 TmpAD ; //temporary reference value

    AdcSwitch(V_ADCH_tch1); //Read button voltage
    a[0] = ADDH ;
    _nop_();_nop_();_nop_();_nop_();_nop_();
    AdcSwitch(V_ADCH_tch1); //Read button voltage
    a[1] = ADDH ;
    _nop_();_nop_();_nop_();_nop_();_nop_();
    AdcSwitch(V_ADCH_tch1); //Read button voltage
    a[2] = ADDH ;

   //----The following is the middle value of three AD values-----
    TmpAD = C3MidVal(a) ;

#ifdef	DEF_WATCH_TKREFER
    KTVal1 = TmpAD ; //Current key AD value -- used for testing ^^^^^^^^^^^^^^^^^^^^^^
#endif
    if(!(TKSTB & SETB0)) //This button is in the lifted state at this time --Change the reference value
    {
        if(TmpAD < KRefer1)
        {
            Tmp1 = KRefer1 - TmpAD ;
        }
        else
        {
            Tmp1 = TmpAD - KRefer1 ;
        }
        
        if(Tmp1 >= V_REFER) //Update reference threshold value
        {
            KTc1 -- ;
            if(KTc1 == 0)
            {
                KTc1 = VT_KTc ;

                if(KRefer1 == TmpAD) //KRefer1 = TmpAD ; 
                {}
                else if(KRefer1 < TmpAD)
                {
                    KRefer1 ++; //Update reference, add or subtract 1 each time     
                }
                else
                {
                    KRefer1 -- ;
                }
                    
            }    
        }
        else
        {
            KTc1 = VT_KTc ;    
        }        
    }
    else //if(TKSTB & SETB0) //This button is currently pressed
    {
        KTc1 = VT_KTc ;
    }
    
    Tmp1 = KRefer1 + V_PRESS ;
    if(TmpAD >= Tmp1) // greater than the reference value, it is considered pressed
    {
        TKeyStatu |= SETB0 ;
    }
    else
    {
        TKeyStatu &= ~SETB0 ;       
    }              
}
//***************************************
// Function name: CheckTouch2
// Function: Detect touch buttons
// Entry parameters: None
//Export parameters: None
//***************************************
void CheckTouch2(void) 
{
    uint8 a[3];
    uint8 Tmp1 ;
    uint8 TmpAD ; //temporary reference value

    AdcSwitch(V_ADCH_tch2); //Read button voltage
    a[0] = ADDH ;
    _nop_();_nop_();_nop_();_nop_();_nop_();
    AdcSwitch(V_ADCH_tch2); //Read button voltage
    a[1] = ADDH ;
    _nop_();_nop_();_nop_();_nop_();_nop_();
    AdcSwitch(V_ADCH_tch2); //Read button voltage
    a[2] = ADDH ;

   //----The following is the middle value of three AD values-----
    TmpAD = C3MidVal(a) ;

#ifdef	DEF_WATCH_TKREFER
    KTVal2 = TmpAD ; //Current key AD value -- used for testing ^^^^^^^^^^^^^^^^^^^^^^
#endif
    if(!(TKSTB & SETB1)) //This button is in the lifted state at this time --Change the reference value
    {
        if(TmpAD < KRefer2)
        {
            Tmp1 = KRefer2 - TmpAD ;
        }
        else
        {
            Tmp1 = TmpAD - KRefer2 ;
        }
        
        if(Tmp1 >= V_REFER) //Update reference threshold value
        {
            KTc2 -- ;
            if(KTc2 == 0)
            {
                KTc2 = VT_KTc ;
                if(KRefer2 == TmpAD) //KRefer1 = TmpAD ; 
                {}
                else if(KRefer2 < TmpAD)
                {
                    KRefer2 ++ ; // Update reference, add or subtract 1 each time     
                }
                else
                {
                    KRefer2 -- ;
                }     
            }    
        }
        else
        {
            KTc2 = VT_KTc ;    
        }        
    }
    else //if(TKSTB & SETB0) //This button is currently pressed
    {
        KTc2 = VT_KTc ;
    }
    
    Tmp1 = KRefer2 + V_PRESS ;
    if(TmpAD >= Tmp1) // greater than the reference value, it is considered pressed
    {
        TKeyStatu |= SETB1 ;
    }
    else
    {
        TKeyStatu &= ~SETB1 ;       
    }              
}


//File maic.c
//======Simple Routine============

//*************************************
// Function name: Init_TKey
// Function: Read the reference value of the touch button
// Entry parameters: None
//Export parameters: None
//***************************************
void Init_TKey(void) //
{
    uint8 i ;

    for(i=250;i>0;i--) //Wait for about 250MS to let the touch voltage stabilize
    {
        Nopt(250) ;
        Nopt(250) ;
        Nopt(250) ;
        Nopt(250) ;
        Nopt(250) ;
    }

    ReadTKRefer(V_ADCH_tch1,&KRefer1) ;
    ReadTKRefer(V_ADCH_tch2,&KRefer2) ;
    ReadTKRefer(V_ADCH_tch3,&KRefer3) ;
    ReadTKRefer(V_ADCH_tch4,&KRefer4) ;

}
//--------------------
void CmdTK1(void) 
{			
    if(!(TKAVL & K_TKON)) return ;
    TKAVL &= ~K_TKON ;

	//Processing button 1 pressing program
}
//--------------------
void CmdTK2(void) 
{			
    if(!(TKAVL & K_TKBL)) return ;
    TKAVL &= ~K_TKBL ;

	//Processing button 2 pressing program
}
//--------------------------------------
void main(void)
{

    Init_TKey() ; //Read the reference value of the touch key after power on

	while(1)
	{
		if(F_20MS) //20MS runs once
		{
			F_20MS = 0 ;


		    CheckTouch1(); //Detect key 1 and update reference value
		    CheckTouch2(); //Detect key 2 and update reference value

			CheckTKey() ;

			CmdTK1(); //Process key 1 
			CmdTK2(); //Process key 2

		}
		
	}

}


//The circuit reference is as follows. PAN2 is the AD detection port. Take this figure as an example. When a button is pressed, the voltage of the PAN2 port will increase.

Microcontroller touch button processing routine 0


Keywords:MCU Reference address:Microcontroller touch button processing routine

Previous article:Summary of low power consumption design of single chip microcomputer
Next article:How does MCU cooperate with microcontroller for simulation?

Recommended ReadingLatest update time:2024-11-17 02:00

Design of driving circuit for electric power steering system based on single chip microcomputer
    1 Basic structure and working principle of EPS system     The Electric Power Steering (EPS) system is the future direction of the steering system. The system directly provides steering assistance by the electric motor, and has the characteristics of simple adjustment, flexible installation, and can provide steerin
[Microcontroller]
Design of driving circuit for electric power steering system based on single chip microcomputer
PIC microcontroller CCS C language (#TYPE usage)
#TYPE Syntax: #type standard-type=size #type default=area standard-type is one of short, int, long, or a user-defined size; size is 1, 8, 16 or 32; area is a storage area defined by #TYPE using the typemod command. Purpose: By default, the compiler treats short as 1 bit, int as 8 bits, and long as 16 bits. T
[Microcontroller]
Design of intelligent window control system based on single chip microcomputer
  I. Introduction     With the continuous development of electronic technology , many household electrical appliances such as color TVs, refrigerators, air conditioners, etc. have been labeled as intelligent, which has contributed to improving people's quality of life. But unfortunately, the eyes of the room, the wi
[Microcontroller]
Design of intelligent window control system based on single chip microcomputer
STC12C MCU power-down mode
 POF: It determines whether it is a power-on reset, interrupt reset or watchdog reset; PD: When set to 1, it enters power down mode and can be awakened by external interrupt low level trigger or falling edge trigger interrupt mode, or by starting the power-down wake-up dedicated timer. The wake-up pins are: INT
[Microcontroller]
STC12C MCU power-down mode
Design of emergency power inverter circuit based on single chip microcomputer and SPWM control
0 Introduction With the development of society, the more information and modernization it has, the more it relies on electricity. Sudden power outages will affect people's normal life order and learning, especially for important loads in production and life. Once the power supply is interrupted, it will cause signifi
[Microcontroller]
Design of emergency power inverter circuit based on single chip microcomputer and SPWM control
Design of self-powered overcurrent relay based on single chip microcomputer
introduction Since the appearance of the inductive overcurrent relay in 1901 , the development of relay protection technology has experienced electromagnetic protection, transistor protection, integrated circuit protection and the widely used microcomputer protection technology. The resear
[Industrial Control]
Design of self-powered overcurrent relay based on single chip microcomputer
Use ST MCU internal reference voltage to monitor power supply voltage and other
During the development process using ST MCU, someone asked if there is a way to monitor the power supply voltage in a simpler way if the power supply voltage is changing, or if other AD input voltages to be tested can be detected when the power supply voltage fluctuates. Here I would like to share with you a method.
[Microcontroller]
Use ST MCU internal reference voltage to monitor power supply voltage and other
Design of human-computer interaction system based on single chip microcomputer and FPGA
In the development of modern instruments, human-computer interaction functions are playing an irreplaceable role. Instruments with friendly human-computer interaction interfaces will be easier to operate and use, thereby improving work efficiency. Liquid crystal displays (LCDs) have the characteristics of low power
[Embedded]
Design of human-computer interaction system based on single chip microcomputer and FPGA
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号