hejun96 发表于 2020-5-20 20:20:35

STM32做主控端,合泰BS83A02A-4做触控按键,按键长按计时问题

最近用合泰的触摸按键BS83A02A-4做触摸按键,主控端STM32F103收到触控端的按键协议那么就解码,现在解码已经解到了,但是按键长按,长按的计时是从松手后自己开始计时,直到计到长按结束的时间,一直没有查到原因void EXTI2_IRQHandler(void)
{
        /*
        if(TIMER_RESET==System_Sta.s.key_timer_flag)
        {
                System_Sta.s.key_timer_flag = TIMER_RUN;
                key_type = COLD_KEY;
        }               
        EXTI_ClearITPendingBit(EXTI_Line2);
        */
        if(READ_COLD_KEY_VALUE)
        {
                cold_detect.sta |= FALLING_MARK;
                cold_detect.val = 0;
        }
        else if(!READ_COLD_KEY_VALUE)
        {
                if(cold_detect.sta & FALLING_MARK)
                {
                        if(cold_detect.sta & LEAD_CODE_MARK)
                        {
                                cold_detect.rec <<= 1;
                                if((cold_detect.val>= 600 && cold_detect.val<=1600))///if((cold_detect.val>=600 && cold_detect.val<=1600))
                                {
                                        cold_detect.rec |= 0;
                                }
                                else if((cold_detect.val>=1700 && cold_detect.val<=2700))///else if((cold_detect.val>=1700 && cold_detect.val<=2700))
                                {
                                        cold_detect.rec |= 1;
                                }
                                cold_detect.cnt++;
                                if(cold_detect.cnt == 2)//传输2位bit位数据
                                {
                                        ///ColdKeyDetectFlag = 1;
                                        cold_detect.keycode = cold_detect.rec;
                                        cold_detect.cnt = 0;
                                        cold_detect.val = 0;
                                        cold_detect.rec = 0;
                                        cold_detect.sta &= ~LEAD_CODE_MARK;
                                        cold_detect.sta |= GET_KEY_MARK;
                                        if(cold_detect.keycode == 0x02)//
                                        {
                                                key_type = COLD_KEY;       
                                               
                                        }
                                        else
                                        {
                                                key_type = OFF_KEY;
                                        }
                                }
                        }
                        else if((cold_detect.val>=4000 && cold_detect.val<=6000))//4000us-6000us ,定时器4配置成10us,定时器4中断中计10次,一次就是1us 1us*4000 = 4000us
                        {
                                cold_detect.sta |= LEAD_CODE_MARK;
                                cold_detect.cnt = 0;
                        }
                        else if((cold_detect.val>=9000 && cold_detect.val<= 11000))
                        {
                                cold_detect.sta |= END_CODE_MARK;
                                cold_detect.sta &= ~GET_KEY_MARK;
                                key_type = OFF_KEY;
                        }       
                }
                cold_detect.sta &= ~FALLING_MARK;
                cold_detect.val = 0;
        }                       
        EXTI_ClearITPendingBit(EXTI_Line2);
}

<div class="blockcode"><blockquote>/*按键工作*/
///static char HotKeyChange = 0,ColdKeyChange = 0, ChildKeyChange = 0;//hot change ,cold change 设置翻转
void Key_Work(void)
{
//        static KEY_TYPE key_type_temp = OFF_KEY;
        ///static unsigned long ColdKeyHoldPressTick=0;
        ///static unsigned long HotKeyHoldPressTick=0;
        static unsigned long ChildKeyPressTick=0;
        static unsigned long ResetKeyPressTick = 0;
        static char HotKeyLongPressStatus = 0;// status hot
        static char ColdKeyLongPressStatus = 0;//status cold
        static char ChildKeyLongPressStatus = 0;//童锁
        static char ResetStatus = 0;
        static char HotKeyChange = 0,ColdKeyChange = 0, ChildKeyChange = 0;//hot change ,cold change 设置翻转
        static unsigned char KeyLongPressEnterCleanSelfFlag = 0;//在自排水处用来作按键判断的进入标志位
        static unsigned char VentBuzzerWorkFlag = 0;//用来判断蜂鸣器在排气状态下会不会响?或者是一直响?

        if(key_type == CHILD_KEY)///if(READ_CHILD_KEY_VALUE)//读取童锁键值
        {
                if(gTick - ChildKeyPressTick >= 9*1000)//按键按下时间超过9*1000ms
                {
                        ChildKeyLongPressStatus = 1;
                }
        }
        else
        {
                ChildKeyLongPressStatus = 0;
                ChildKeyPressTick = gTick;
        }
       
       
               
/*               
       
        if(ChildKeyLongPressStatus == 1 && READ_COLD_KEY_VALUE == 0 && READ_HOT_KEY_VALUE == 0)//长按童锁键自排水状态
        {
                KeyLongPressEnterCleanSelfFlag = 1;
               
                System_Sta.s.Vent_Flag = FLAG_OFF;//自排气标志位关闭
               
                ChildKeyLongPressStatus = 0;//按下后清空原状态.
                child_key_press_tick = gTick;//童锁按下定时器计时
                if(key_type == CHILD_KEY)
                {
                        ChildKeyChange = !ChildKeyChange;
                        key_type = OFF_KEY;
                     Trans_dat_append(STATE_23);//整机自清洗指令
                                       
                        System_Sta.s.clean_self_cmd = FLAG_ON;               
                       
                       
                       
                                                               
                       
                        //加蜂鸣器时间判断是否进入此循环,进入排水状态,蜂鸣器响2声
                                       
                        System_Sta.s.beep_timer_flag = TIMER_RUN;
                        Block_Timer(50);//阻塞时间50ms
                        System_Sta.s.beep_timer_flag = TIMER_RESET;
                        Block_Timer(200);//阻塞时间200ms
                        System_Sta.s.beep_timer_flag = TIMER_RUN;
                        Block_Timer(50);//阻塞时间50ms
                        System_Sta.s.beep_timer_flag = TIMER_RESET;
                       
                       
                        //把定时器的标志位结束
                        System_Sta.s.beep_timer_flag = TIMER_END;
                       
                               
                       System_Data.flash_water_flag='0';
                       System_Data.flash_water_flag='1';//开始排水
                       Trans_dat_append(STATE_23);
                               
                       
                               
                       
                                                       
                       

//                        else//不进入自排水状态,蜂鸣器响3声
//                        {
//                               
//                               
//                                System_Sta.s.clean_self_cmd = FLAG_OFF;//未收到自排水命令
//                               
//                                System_Sta.s.beep_timer_flag = TIMER_RUN;
//                                Block_Timer(50);//阻塞时间50ms
//                                System_Sta.s.beep_timer_flag = TIMER_RESET;
//                                Block_Timer(200);//阻塞时间200ms
//                                System_Sta.s.beep_timer_flag = TIMER_RUN;
//                                Block_Timer(50);//阻塞时间50ms
//                                System_Sta.s.beep_timer_flag = TIMER_RESET;
//                                Block_Timer(200);//阻塞时间200ms
//                                System_Sta.s.beep_timer_flag = TIMER_RUN;
//                                Block_Timer(50);//阻塞时间50ms
//                                System_Sta.s.beep_timer_flag = TIMER_RESET;
//                               
//                               
//                               
//                                System_Data.flash_water_flag='0';
//                                System_Data.flash_water_flag='2';//停止排水
//                                Trans_dat_append(STATE_23);
//                               
//                               
//                                System_Sta.s.drain_close_flag = 1;
//                                ChildKeyChange = 0;
//                        }                       
                }                       
        }
*/                                               
#if 1
//增加复位功能=>冷水键长按标志位+热水键长按标志位

       
       
        if(key_type == HOT_KEY)///if(READ_HOT_KEY_VALUE)
        {
          if(gTick-HotKeyHoldPressTick >= 9 * 1000 && \
                                gTick-HotKeyHoldPressTick <= 11 * 1000)//
                {
                        HotKeyLongPressStatus = HOT_KEY_AND_COLD_KEY_SOFT_RESET;//热水键和冷水键同时长按10s复位
                }
                else if(gTick-HotKeyHoldPressTick >= 15 * 1000)
                {                       
                        HotKeyLongPressStatus = HOT_TUBE1_TOGGLE;//单独长按热水键1min打开加热
                }
        }
        else
        {
                HotKeyLongPressStatus = 0;
                HotKeyHoldPressTick = gTick;
        }
       
        if(key_type == COLD_KEY)///if(READ_COLD_KEY_VALUE)
        {
                if(gTick - ColdKeyHoldPressTick >= 9*1000)//“冷键”按下时间超过9*1000ms
                {
                        ColdKeyLongPressStatus = 1;
                }
               
        }
        else
        {
                ColdKeyLongPressStatus = 0;
                ColdKeyHoldPressTick = gTick;
        }       

       
        if(ColdKeyLongPressStatus==1 && HOT_KEY_AND_COLD_KEY_SOFT_RESET == HotKeyLongPressStatus)//同时按下冷水键和热水键
        {      
                Save_Data.s.self_clean_flag = 0;
                i2cWriteBuffer1(AT24C02_ADDR,AT24C02_REG,sizeof(SAVE_DATA_UNION),(u8*)&Save_Data);/*写入数据*/               
                for(int i=0;i<sizeof(STA_UNION);i++)
                {
                        System_Sta.sta = 0;
                }
               
                memset((void *)(&System_Data),0,sizeof(DATA_STRUCT));
                //开启蜂鸣器
                if(TIMER_RESET == System_Sta.s.beep_timer_flag)
                {
                        System_Sta.s.beep_timer_flag = TIMER_RUN;
                }
                System_Data.flash_water_flag='0';
                System_Data.flash_water_flag='2';/*停止清洗*/
                Trans_dat_append(STATE_23);
                while(1);
        }

        static unsigned char vent_flag = 0;//判断自排气状态的进入标志位          

        //static unsigned char VentBuzzerWorkFlag = 0;
        if(ColdKeyLongPressStatus==1 && ChildKeyLongPressStatus==1)   //按下冷水键和童锁键10s处于排气过程(问题点)
        {                                               
                System_Sta.s.clean_self_cmd = FLAG_OFF;//自排水标志位turn off               
                vent_flag = 1;               
                ColdKeyLongPressStatus = ChildKeyLongPressStatus = 0;       
                System_Sta.s.Vent_Flag = FLAG_ON;//自排气标志位打开
               
//                System_Data.Cold_Water_G_Tick = 0;//
//                System_Sta.s.Cold_Water_G_Flag = FLAG_OFF;
//                       
//                       
//                System_Data.Normal_Water_H_Tick = 0;               
//                System_Sta.s.Normal_Water_H_Flag = FLAG_OFF;
               
//                System_Data.cold_water_time += System_Data.cold_water_flag_tick/1000;
//                System_Data.cold_water_flag_tick = FLAG_OFF;
//                System_Sta.s.cold_water_timer_flag = TIMER_RESET;
//                System_Sta.s.cold_water_on_flag = FLAG_OFF;               
               
//                System_Data.Normal_Water_H_Off_Time += System_Data.Normal_Water_H_Tick/1000;
//                System_Data.Normal_Water_H_Tick = FLAG_OFF;
//                System_Sta.s.normal_water_timer_flag = TIMER_RESET;
//                System_Sta.s.normal_water_on_flag = FLAG_OFF;
               
               
//                i2cWriteBuffer1(AT24C02_ADDR,AT24C02_REG,sizeof(SAVE_DATA_UNION),(u8*)&Save_Data);/*写入数据*/
       
               
               
//                for (int i = 0; i < sizeof(STA_UNION); i++)
//                {
//                        System_Sta.sta = 0;
//                }
//               
//                memset((void *)(&System_Data),0,sizeof(DATA_STRUCT));
                if(0 == VentBuzzerWorkFlag)
                {
                        System_Sta.s.beep_timer_flag = TIMER_RUN;
                        Block_Timer(50);//阻塞时间50ms
                        System_Sta.s.beep_timer_flag = TIMER_RESET;
                        Block_Timer(200);//阻塞时间200ms
                        System_Sta.s.beep_timer_flag = TIMER_RUN;
                        Block_Timer(50);//阻塞时间50ms
                        System_Sta.s.beep_timer_flag = TIMER_RESET;
                        Block_Timer(200);//阻塞时间200ms
                        System_Sta.s.beep_timer_flag = TIMER_RUN;
                        Block_Timer(50);//阻塞时间50ms
                        System_Sta.s.beep_timer_flag = TIMER_RESET;
                                               
                        /* 蜂鸣器的定时器标志位关闭 */
                        System_Sta.s.beep_timer_flag = TIMER_END;               
                }               
                VentBuzzerWorkFlag = 1;               
                /* 把定时器的标志位结束        */
                //System_Sta.s.timer_flag = TIMER_END;
               
                System_Data.flash_water_flag='0';
                System_Data.flash_water_flag='2';/*停止清洗*/
                Trans_dat_append(STATE_23);//整机自清洗指令
                Vent_Work();
                /*
                while(vent_flag)
                {
                        IWDG_Feed();//reload
                };
                */
        }                       
        /*       
        else
        {
                VentBuzzerWorkFlag = 0;
        }
        */                 
        if(HOT_TUBE1_TOGGLE == HotKeyLongPressStatus && 0 == READ_COLD_KEY_VALUE)//只按下热水键长按       
        {
                if(key_type == HOT_KEY)
                {
                        HotKeyChange = !HotKeyChange;//hot change
                        key_type = OFF_KEY;
                        if (1 == HotKeyChange)///if(System_Sta.s.hot_close_flag == 1)
                        {
                                if(Bit_RESET==Read_WaterShortagDetection())
                                {
                                        System_Data.fixed_state = '1';

                                        ///System_Sta.s.hot_close_flag = 0;
                                        System_Data.ntc1_setting_temperature = 92;//原值为92
                                        if (Save_Data.s.machine_type != DY_100B)
                                        {
                                                System_Data.ntc2_setting_temperature = 92;//0;
                                        }
                                        else
                                        {
                                                System_Data.ntc2_setting_temperature = 0;
                                        }
                                        // u8 tem = {0};
                                        // tem = System_Data.ntc1_setting_temperature;
                                        // tem = System_Data.ntc2_setting_temperature;
                                        // i2cWriteBuffer1(AT24C02_ADDR, AT24C02_REG_TEM, sizeof(tem), (u8*)&tem);/*写入数据*/
                                        u8 tem = {0};
                                        tem = System_Data.ntc1_setting_temperature;
                                        tem = System_Data.ntc2_setting_temperature;
                                        i2cWriteBuffer1(AT24C02_ADDR, AT24C02_REG_TEM, sizeof(tem), (u8*)&tem);/*写入数据*/
                                        Trans_dat_append(STATE_12);
                                       
                                        System_Sta.s.beep_timer_flag = TIMER_RUN;
                                        Block_Timer(50);//阻塞时间50ms
                                        System_Sta.s.beep_timer_flag = TIMER_RESET;
                                        Block_Timer(200);//阻塞时间200ms
                                        System_Sta.s.beep_timer_flag = TIMER_RUN;
                                        Block_Timer(50);//阻塞时间50ms
                                        System_Sta.s.beep_timer_flag = TIMER_RESET;
                                        // GPIO_WriteBit(GPIOC, GPIO_Pin_12, 1);
                                        // GPIO_WriteBit(GPIOA, GPIO_Pin_13, 1);
                                        System_Sta.s.to_heat1_cmd_flag = FLAG_ON;
                                        System_Sta.s.to_heat2_cmd_flag = FLAG_ON;
                                        Trans_dat_append(STATE_03);
                                }
                                else
                                {       
                                        System_Sta.s.beep_timer_flag = TIMER_RUN;
                                        Block_Timer(50);
                                        System_Sta.s.beep_timer_flag = TIMER_RESET;
                                        Block_Timer(200);
                                        System_Sta.s.beep_timer_flag = TIMER_RUN;
                                        Block_Timer(50);
                                        System_Sta.s.beep_timer_flag = TIMER_RESET;
                                        Block_Timer(200);
                                        System_Sta.s.beep_timer_flag = TIMER_RUN;
                                        Block_Timer(50);
                                        System_Sta.s.beep_timer_flag = TIMER_RESET;
                                        HotKeyChange = 0;//hot_key change
                                }
                        }                  
                        else
                        {
                                //GPIO_WriteBit(GPIOC, GPIO_Pin_12, 0);
                                // GPIO_WriteBit(GPIOA, GPIO_Pin_13, 0);
                               
                                System_Data.ntc1_setting_temperature = 0;
                                if (Save_Data.s.machine_type == DY_100B)
                                {
                                        System_Data.ntc2_setting_temperature = 0;
                                }
                                else
                                {
                                        System_Data.ntc2_setting_temperature = 0;
                                }
                                u8 tem = {0};
                                tem = System_Data.ntc1_setting_temperature;
                                tem = System_Data.ntc2_setting_temperature;
                                i2cWriteBuffer1(AT24C02_ADDR, AT24C02_REG_TEM, sizeof(tem), (u8*)&tem);/*写入数据*/
                                Trans_dat_append(STATE_12);
                               
                               
                                System_Data.fixed_state = '0';
                                System_Sta.s.beep_timer_flag = TIMER_RUN;
                                Block_Timer(50);
                                System_Sta.s.beep_timer_flag = TIMER_RESET;
                                Set_HotTube_1(Bit_RESET);
                                Set_HotTube_2(Bit_RESET);                               
                                ///System_Sta.s.hot_close_flag = 1;
                                Trans_dat_append(STATE_03);
                        }
                }
        }
        if(ColdKeyLongPressStatus == 1 && READ_HOT_KEY_VALUE == 0)//只按下冷水键长按       
        {
                if(key_type == COLD_KEY)
                {
                        ColdKeyChange = !ColdKeyChange;//冷键翻转
                        //GPIO_WriteBit(GPIOA, GPIO_Pin_14, ColdKeyChange);
                        key_type = OFF_KEY;
                        Trans_dat_append(STATE_02);
                        if(ColdKeyChange)
                        {
                                System_Data.fixed_state = '1';
                                Set_IceTube(Bit_SET);
                                System_Sta.s.beep_timer_flag = TIMER_RUN;
                                Block_Timer(50);
                                System_Sta.s.beep_timer_flag = TIMER_RESET;
                                Block_Timer(200);
                                System_Sta.s.beep_timer_flag = TIMER_RUN;
                                Block_Timer(50);
                                System_Sta.s.beep_timer_flag = TIMER_RESET;
                        }
                        else
                        {
                                System_Data.fixed_state = '0';
                                Set_IceTube(Bit_RESET);
                                System_Sta.s.beep_timer_flag = TIMER_RUN;
                                Block_Timer(50);
                                System_Sta.s.beep_timer_flag = TIMER_RESET;
                        }
                        Trans_dat_append(STATE_02);
                }
        }
        /*两个热罐在95℃以上一直被设置,设置相应的GPIO口,也就是不让两个热罐动作*/       
        if(Bit_SET==Read_WaterShortagDetection()||System_Data.ntc1_temperature>=95||System_Data.ntc2_temperature>=95)
        {
                GPIO_WriteBit(GPIOC, GPIO_Pin_12, 0);//hot tube 1
                GPIO_WriteBit(GPIOA, GPIO_Pin_13, 0);//hot tube 2
                HotKeyChange = 0;
        }
#endif


陌路夕颜 发表于 2020-5-21 10:25:18

一般有长按和短按存在的时候,按键按下后开始计时,在长按计时未到时,检测到按键松开,认为是短按,否则认为是长按。你在捋捋计时的逻辑吧

butterflyspring 发表于 2020-5-27 10:47:03

同意楼上观点,一般长按键都是从按下开始计时,持续监测按下的时间,超过一定时间后判定为长按键。当然这些还需要结合触控器的产品说明来实现的
页: [1]
查看完整版本: STM32做主控端,合泰BS83A02A-4做触控按键,按键长按计时问题