你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

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

[复制链接]
踮起脚摘苹果 提问时间:2020-5-20 20:20 /
最近用合泰的触摸按键BS83A02A-4做触摸按键,主控端STM32F103收到触控端的按键协议那么就解码,现在解码已经解到了,但是按键长按,长按的计时是从松手后自己开始计时,直到计到长按结束的时间,一直没有查到原因
  1. void EXTI2_IRQHandler(void)
  2. {
  3.         /*
  4.         if(TIMER_RESET==System_Sta.s.key_timer_flag)
  5.         {
  6.                 System_Sta.s.key_timer_flag = TIMER_RUN;
  7.                 key_type = COLD_KEY;
  8.         }               
  9.         EXTI_ClearITPendingBit(EXTI_Line2);
  10.         */
  11.         if(READ_COLD_KEY_VALUE)
  12.         {
  13.                 cold_detect.sta |= FALLING_MARK;
  14.                 cold_detect.val = 0;
  15.         }
  16.         else if(!READ_COLD_KEY_VALUE)
  17.         {
  18.                 if(cold_detect.sta & FALLING_MARK)
  19.                 {
  20.                         if(cold_detect.sta & LEAD_CODE_MARK)
  21.                         {
  22.                                 cold_detect.rec <<= 1;
  23.                                 if((cold_detect.val>= 600 && cold_detect.val<=1600))///if((cold_detect.val>=600 && cold_detect.val<=1600))
  24.                                 {
  25.                                         cold_detect.rec |= 0;
  26.                                 }
  27.                                 else if((cold_detect.val>=1700 && cold_detect.val<=2700))///else if((cold_detect.val>=1700 && cold_detect.val<=2700))
  28.                                 {
  29.                                         cold_detect.rec |= 1;
  30.                                 }
  31.                                 cold_detect.cnt++;
  32.                                 if(cold_detect.cnt == 2)//传输2位bit位数据
  33.                                 {
  34.                                         ///ColdKeyDetectFlag = 1;
  35.                                         cold_detect.keycode = cold_detect.rec;
  36.                                         cold_detect.cnt = 0;
  37.                                         cold_detect.val = 0;
  38.                                         cold_detect.rec = 0;
  39.                                         cold_detect.sta &= ~LEAD_CODE_MARK;
  40.                                         cold_detect.sta |= GET_KEY_MARK;
  41.                                         if(cold_detect.keycode == 0x02)//
  42.                                         {
  43.                                                 key_type = COLD_KEY;       
  44.                                                
  45.                                         }
  46.                                         else
  47.                                         {
  48.                                                 key_type = OFF_KEY;
  49.                                         }
  50.                                 }
  51.                         }
  52.                         else if((cold_detect.val>=4000 && cold_detect.val<=6000))//4000us-6000us ,定时器4配置成10us,定时器4中断中计10次,一次就是1us 1us*4000 = 4000us
  53.                         {
  54.                                 cold_detect.sta |= LEAD_CODE_MARK;
  55.                                 cold_detect.cnt = 0;
  56.                         }
  57.                         else if((cold_detect.val>=9000 && cold_detect.val<= 11000))
  58.                         {
  59.                                 cold_detect.sta |= END_CODE_MARK;
  60.                                 cold_detect.sta &= ~GET_KEY_MARK;
  61.                                 key_type = OFF_KEY;
  62.                         }       
  63.                 }
  64.                 cold_detect.sta &= ~FALLING_MARK;
  65.                 cold_detect.val = 0;
  66.         }                       
  67.         EXTI_ClearITPendingBit(EXTI_Line2);
  68. }

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

  85.         if(key_type == CHILD_KEY)///if(READ_CHILD_KEY_VALUE)//读取童锁键值
  86.         {
  87.                 if(gTick - ChildKeyPressTick >= 9*1000)//按键按下时间超过9*1000ms
  88.                 {
  89.                         ChildKeyLongPressStatus = 1;
  90.                 }
  91.         }
  92.         else
  93.         {
  94.                 ChildKeyLongPressStatus = 0;
  95.                 ChildKeyPressTick = gTick;
  96.         }
  97.        
  98.        
  99.                
  100. /*               
  101.        
  102.         if(ChildKeyLongPressStatus == 1 && READ_COLD_KEY_VALUE == 0 && READ_HOT_KEY_VALUE == 0)//长按童锁键自排水状态
  103.         {
  104.                 KeyLongPressEnterCleanSelfFlag = 1;
  105.                
  106.                 System_Sta.s.Vent_Flag = FLAG_OFF;//自排气标志位关闭
  107.                
  108.                 ChildKeyLongPressStatus = 0;//按下后清空原状态.
  109.                 child_key_press_tick = gTick;//童锁按下定时器计时
  110.                 if(key_type == CHILD_KEY)
  111.                 {
  112.                         ChildKeyChange = !ChildKeyChange;
  113.                         key_type = OFF_KEY;
  114.                        Trans_dat_append(STATE_23);//整机自清洗指令
  115.                                        
  116.                         System_Sta.s.clean_self_cmd = FLAG_ON;               
  117.                        
  118.                        
  119.                        
  120.                                                                
  121.                        
  122.                         //加蜂鸣器时间判断是否进入此循环,进入排水状态,蜂鸣器响2声
  123.                                        
  124.                         System_Sta.s.beep_timer_flag = TIMER_RUN;
  125.                         Block_Timer(50);//阻塞时间50ms
  126.                         System_Sta.s.beep_timer_flag = TIMER_RESET;
  127.                         Block_Timer(200);//阻塞时间200ms
  128.                         System_Sta.s.beep_timer_flag = TIMER_RUN;
  129.                         Block_Timer(50);//阻塞时间50ms
  130.                         System_Sta.s.beep_timer_flag = TIMER_RESET;
  131.                        
  132.                        
  133.                         //把定时器的标志位结束
  134.                         System_Sta.s.beep_timer_flag = TIMER_END;
  135.                        
  136.                                
  137.                          System_Data.flash_water_flag[0]='0';
  138.                          System_Data.flash_water_flag[1]='1';//开始排水
  139.                          Trans_dat_append(STATE_23);
  140.                                
  141.                        
  142.                                
  143.                        
  144.                                                        
  145.                        

  146. //                        else//不进入自排水状态,蜂鸣器响3声
  147. //                        {
  148. //                               
  149. //                               
  150. //                                System_Sta.s.clean_self_cmd = FLAG_OFF;//未收到自排水命令
  151. //                               
  152. //                                System_Sta.s.beep_timer_flag = TIMER_RUN;
  153. //                                Block_Timer(50);//阻塞时间50ms
  154. //                                System_Sta.s.beep_timer_flag = TIMER_RESET;
  155. //                                Block_Timer(200);//阻塞时间200ms
  156. //                                System_Sta.s.beep_timer_flag = TIMER_RUN;
  157. //                                Block_Timer(50);//阻塞时间50ms
  158. //                                System_Sta.s.beep_timer_flag = TIMER_RESET;
  159. //                                Block_Timer(200);//阻塞时间200ms
  160. //                                System_Sta.s.beep_timer_flag = TIMER_RUN;
  161. //                                Block_Timer(50);//阻塞时间50ms
  162. //                                System_Sta.s.beep_timer_flag = TIMER_RESET;
  163. //                               
  164. //                               
  165. //                                 
  166. //                                System_Data.flash_water_flag[0]='0';
  167. //                                System_Data.flash_water_flag[0]='2';//停止排水
  168. //                                Trans_dat_append(STATE_23);
  169. //                               
  170. //                               
  171. //                                System_Sta.s.drain_close_flag = 1;
  172. //                                ChildKeyChange = 0;
  173. //                        }                       
  174.                 }                       
  175.         }
  176. */                                               
  177.   #if 1
  178.   //增加复位功能=>冷水键长按标志位+热水键长按标志位
  179.   
  180.        
  181.        
  182.         if(key_type == HOT_KEY)///if(READ_HOT_KEY_VALUE)
  183.         {
  184.           if(gTick-HotKeyHoldPressTick >= 9 * 1000 && \
  185.                                 gTick-HotKeyHoldPressTick <= 11 * 1000)//
  186.                 {
  187.                         HotKeyLongPressStatus = HOT_KEY_AND_COLD_KEY_SOFT_RESET;//热水键和冷水键同时长按10s复位
  188.                 }
  189.                 else if(gTick-HotKeyHoldPressTick >= 15 * 1000)
  190.                 {                       
  191.                         HotKeyLongPressStatus = HOT_TUBE1_TOGGLE;//单独长按热水键1min打开加热
  192.                 }
  193.         }
  194.         else
  195.         {
  196.                 HotKeyLongPressStatus = 0;
  197.                 HotKeyHoldPressTick = gTick;
  198.         }
  199.        
  200.         if(key_type == COLD_KEY)///if(READ_COLD_KEY_VALUE)
  201.         {
  202.                 if(gTick - ColdKeyHoldPressTick >= 9*1000)//“冷键”按下时间超过9*1000ms
  203.                 {
  204.                         ColdKeyLongPressStatus = 1;
  205.                 }
  206.                
  207.         }
  208.         else
  209.         {
  210.                 ColdKeyLongPressStatus = 0;
  211.                 ColdKeyHoldPressTick = gTick;
  212.         }       

  213.        
  214.         if(ColdKeyLongPressStatus==1 && HOT_KEY_AND_COLD_KEY_SOFT_RESET == HotKeyLongPressStatus)//同时按下冷水键和热水键
  215.         {        
  216.                 Save_Data.s.self_clean_flag = 0;
  217.                 i2cWriteBuffer1(AT24C02_ADDR,AT24C02_REG,sizeof(SAVE_DATA_UNION),(u8*)&Save_Data);/*写入数据*/               
  218.                 for(int i=0;i<sizeof(STA_UNION);i++)
  219.                 {
  220.                         System_Sta.sta[i] = 0;
  221.                 }
  222.                
  223.                 memset((void *)(&System_Data),0,sizeof(DATA_STRUCT));
  224.                 //开启蜂鸣器
  225.                 if(TIMER_RESET == System_Sta.s.beep_timer_flag)
  226.                 {
  227.                         System_Sta.s.beep_timer_flag = TIMER_RUN;
  228.                 }
  229.                 System_Data.flash_water_flag[0]='0';
  230.                 System_Data.flash_water_flag[1]='2';/*停止清洗*/
  231.                 Trans_dat_append(STATE_23);
  232.                 while(1);
  233.         }

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

  235.         //static unsigned char VentBuzzerWorkFlag = 0;
  236.         if(ColdKeyLongPressStatus==1 && ChildKeyLongPressStatus==1)   //按下冷水键和童锁键10s处于排气过程(问题点)
  237.         {                                               
  238.                 System_Sta.s.clean_self_cmd = FLAG_OFF;//自排水标志位turn off               
  239.                 vent_flag = 1;               
  240.                 ColdKeyLongPressStatus = ChildKeyLongPressStatus = 0;       
  241.                 System_Sta.s.Vent_Flag = FLAG_ON;//自排气标志位打开
  242.                
  243. //                System_Data.Cold_Water_G_Tick = 0;//
  244. //                System_Sta.s.Cold_Water_G_Flag = FLAG_OFF;
  245. //                       
  246. //                       
  247. //                System_Data.Normal_Water_H_Tick = 0;               
  248. //                System_Sta.s.Normal_Water_H_Flag = FLAG_OFF;
  249.                
  250. //                System_Data.cold_water_time += System_Data.cold_water_flag_tick/1000;
  251. //                System_Data.cold_water_flag_tick = FLAG_OFF;
  252. //                System_Sta.s.cold_water_timer_flag = TIMER_RESET;
  253. //                System_Sta.s.cold_water_on_flag = FLAG_OFF;               
  254.                
  255. //                System_Data.Normal_Water_H_Off_Time += System_Data.Normal_Water_H_Tick/1000;
  256. //                System_Data.Normal_Water_H_Tick = FLAG_OFF;
  257. //                System_Sta.s.normal_water_timer_flag = TIMER_RESET;
  258. //                System_Sta.s.normal_water_on_flag = FLAG_OFF;
  259.                
  260.                
  261. //                i2cWriteBuffer1(AT24C02_ADDR,AT24C02_REG,sizeof(SAVE_DATA_UNION),(u8*)&Save_Data);/*写入数据*/
  262.        
  263.                
  264.                
  265. //                for (int i = 0; i < sizeof(STA_UNION); i++)
  266. //                {
  267. //                        System_Sta.sta[i] = 0;
  268. //                }
  269. //               
  270. //                memset((void *)(&System_Data),0,sizeof(DATA_STRUCT));
  271.                 if(0 == VentBuzzerWorkFlag)
  272.                 {
  273.                         System_Sta.s.beep_timer_flag = TIMER_RUN;
  274.                         Block_Timer(50);//阻塞时间50ms
  275.                         System_Sta.s.beep_timer_flag = TIMER_RESET;
  276.                         Block_Timer(200);//阻塞时间200ms
  277.                         System_Sta.s.beep_timer_flag = TIMER_RUN;
  278.                         Block_Timer(50);//阻塞时间50ms
  279.                         System_Sta.s.beep_timer_flag = TIMER_RESET;
  280.                         Block_Timer(200);//阻塞时间200ms
  281.                         System_Sta.s.beep_timer_flag = TIMER_RUN;
  282.                         Block_Timer(50);//阻塞时间50ms
  283.                         System_Sta.s.beep_timer_flag = TIMER_RESET;
  284.                                                
  285.                         /* 蜂鸣器的定时器标志位关闭 */
  286.                         System_Sta.s.beep_timer_flag = TIMER_END;               
  287.                 }               
  288.                 VentBuzzerWorkFlag = 1;               
  289.                 /* 把定时器的标志位结束        */
  290.                 //System_Sta.s.timer_flag = TIMER_END;
  291.                
  292.                 System_Data.flash_water_flag[0]='0';
  293.                 System_Data.flash_water_flag[1]='2';/*停止清洗*/
  294.                 Trans_dat_append(STATE_23);//整机自清洗指令
  295.                 Vent_Work();
  296.                 /*
  297.                 while(vent_flag)
  298.                 {
  299.                         IWDG_Feed();//reload
  300.                 };
  301.                 */
  302.         }                       
  303.         /*       
  304.         else
  305.         {
  306.                 VentBuzzerWorkFlag = 0;
  307.         }
  308.         */                 
  309.         if(HOT_TUBE1_TOGGLE == HotKeyLongPressStatus && 0 == READ_COLD_KEY_VALUE)//只按下热水键长按       
  310.         {
  311.                 if(key_type == HOT_KEY)
  312.                 {
  313.                         HotKeyChange = !HotKeyChange;//hot change
  314.                         key_type = OFF_KEY;
  315.                         if (1 == HotKeyChange)///if(System_Sta.s.hot_close_flag == 1)
  316.                         {
  317.                                 if(Bit_RESET==Read_WaterShortagDetection())
  318.                                 {
  319.                                         System_Data.fixed_state[1] = '1';

  320.                                         ///System_Sta.s.hot_close_flag = 0;
  321.                                         System_Data.ntc1_setting_temperature = 92;//原值为92
  322.                                         if (Save_Data.s.machine_type != DY_100B)
  323.                                         {
  324.                                                 System_Data.ntc2_setting_temperature = 92;//0;
  325.                                         }
  326.                                         else
  327.                                         {
  328.                                                 System_Data.ntc2_setting_temperature = 0;
  329.                                         }
  330.                                         // u8 tem[2] = {0};
  331.                                         // tem[0] = System_Data.ntc1_setting_temperature;
  332.                                         // tem[1] = System_Data.ntc2_setting_temperature;
  333.                                         // i2cWriteBuffer1(AT24C02_ADDR, AT24C02_REG_TEM, sizeof(tem), (u8*)&tem);/*写入数据*/
  334.                                         u8 tem[2] = {0};
  335.                                         tem[0] = System_Data.ntc1_setting_temperature;
  336.                                         tem[1] = System_Data.ntc2_setting_temperature;
  337.                                         i2cWriteBuffer1(AT24C02_ADDR, AT24C02_REG_TEM, sizeof(tem), (u8*)&tem);/*写入数据*/
  338.                                         Trans_dat_append(STATE_12);
  339.                                        
  340.                                         System_Sta.s.beep_timer_flag = TIMER_RUN;
  341.                                         Block_Timer(50);//阻塞时间50ms
  342.                                         System_Sta.s.beep_timer_flag = TIMER_RESET;
  343.                                         Block_Timer(200);//阻塞时间200ms
  344.                                         System_Sta.s.beep_timer_flag = TIMER_RUN;
  345.                                         Block_Timer(50);//阻塞时间50ms
  346.                                         System_Sta.s.beep_timer_flag = TIMER_RESET;
  347.                                         // GPIO_WriteBit(GPIOC, GPIO_Pin_12, 1);
  348.                                         // GPIO_WriteBit(GPIOA, GPIO_Pin_13, 1);
  349.                                         System_Sta.s.to_heat1_cmd_flag = FLAG_ON;
  350.                                         System_Sta.s.to_heat2_cmd_flag = FLAG_ON;
  351.                                         Trans_dat_append(STATE_03);
  352.                                 }
  353.                                 else
  354.                                 {       
  355.                                         System_Sta.s.beep_timer_flag = TIMER_RUN;
  356.                                         Block_Timer(50);
  357.                                         System_Sta.s.beep_timer_flag = TIMER_RESET;
  358.                                         Block_Timer(200);
  359.                                         System_Sta.s.beep_timer_flag = TIMER_RUN;
  360.                                         Block_Timer(50);
  361.                                         System_Sta.s.beep_timer_flag = TIMER_RESET;
  362.                                         Block_Timer(200);
  363.                                         System_Sta.s.beep_timer_flag = TIMER_RUN;
  364.                                         Block_Timer(50);
  365.                                         System_Sta.s.beep_timer_flag = TIMER_RESET;
  366.                                         HotKeyChange = 0;//hot_key change
  367.                                 }
  368.                         }                  
  369.                         else
  370.                         {
  371.                                 //GPIO_WriteBit(GPIOC, GPIO_Pin_12, 0);
  372.                                 // GPIO_WriteBit(GPIOA, GPIO_Pin_13, 0);
  373.                                
  374.                                 System_Data.ntc1_setting_temperature = 0;
  375.                                 if (Save_Data.s.machine_type == DY_100B)
  376.                                 {
  377.                                         System_Data.ntc2_setting_temperature = 0;
  378.                                 }
  379.                                 else
  380.                                 {
  381.                                         System_Data.ntc2_setting_temperature = 0;
  382.                                 }
  383.                                 u8 tem[2] = {0};
  384.                                 tem[0] = System_Data.ntc1_setting_temperature;
  385.                                 tem[1] = System_Data.ntc2_setting_temperature;
  386.                                 i2cWriteBuffer1(AT24C02_ADDR, AT24C02_REG_TEM, sizeof(tem), (u8*)&tem);/*写入数据*/
  387.                                 Trans_dat_append(STATE_12);
  388.                                
  389.                                  
  390.                                 System_Data.fixed_state[1] = '0';
  391.                                 System_Sta.s.beep_timer_flag = TIMER_RUN;
  392.                                 Block_Timer(50);
  393.                                 System_Sta.s.beep_timer_flag = TIMER_RESET;
  394.                                 Set_HotTube_1(Bit_RESET);
  395.                                 Set_HotTube_2(Bit_RESET);                               
  396.                                 ///System_Sta.s.hot_close_flag = 1;
  397.                                 Trans_dat_append(STATE_03);
  398.                         }
  399.                 }
  400.         }
  401.         if(ColdKeyLongPressStatus == 1 && READ_HOT_KEY_VALUE == 0)//只按下冷水键长按       
  402.         {
  403.                 if(key_type == COLD_KEY)
  404.                 {
  405.                         ColdKeyChange = !ColdKeyChange;//冷键翻转
  406.                         //GPIO_WriteBit(GPIOA, GPIO_Pin_14, ColdKeyChange);
  407.                         key_type = OFF_KEY;
  408.                         Trans_dat_append(STATE_02);
  409.                         if(ColdKeyChange)
  410.                         {
  411.                                 System_Data.fixed_state[2] = '1';
  412.                                 Set_IceTube(Bit_SET);
  413.                                 System_Sta.s.beep_timer_flag = TIMER_RUN;
  414.                                 Block_Timer(50);
  415.                                 System_Sta.s.beep_timer_flag = TIMER_RESET;
  416.                                 Block_Timer(200);
  417.                                 System_Sta.s.beep_timer_flag = TIMER_RUN;
  418.                                 Block_Timer(50);
  419.                                 System_Sta.s.beep_timer_flag = TIMER_RESET;
  420.                         }
  421.                         else
  422.                         {
  423.                                 System_Data.fixed_state[2] = '0';
  424.                                 Set_IceTube(Bit_RESET);
  425.                                 System_Sta.s.beep_timer_flag = TIMER_RUN;
  426.                                 Block_Timer(50);
  427.                                 System_Sta.s.beep_timer_flag = TIMER_RESET;
  428.                         }
  429.                         Trans_dat_append(STATE_02);
  430.                 }
  431.         }
  432.         /*两个热罐在95℃以上一直被设置,设置相应的GPIO口,也就是不让两个热罐动作*/       
  433.         if(Bit_SET==Read_WaterShortagDetection()||System_Data.ntc1_temperature>=95||System_Data.ntc2_temperature>=95)
  434.         {
  435.                 GPIO_WriteBit(GPIOC, GPIO_Pin_12, 0);//hot tube 1
  436.                 GPIO_WriteBit(GPIOA, GPIO_Pin_13, 0);//hot tube 2
  437.                 HotKeyChange = 0;
  438.         }
  439. #endif
复制代码



收藏 评论2 发布时间:2020-5-20 20:20

举报

2个回答
陌路夕颜 回答时间:2020-5-21 10:25:18
一般有长按和短按存在的时候,按键按下后开始计时,在长按计时未到时,检测到按键松开,认为是短按,否则认为是长按。你在捋捋计时的逻辑吧

评分

参与人数 1蝴蝶豆 +3 收起 理由
STMCU + 3

查看全部评分

butterflyspring 回答时间:2020-5-27 10:47:03
同意楼上观点,一般长按键都是从按下开始计时,持续监测按下的时间,超过一定时间后判定为长按键。当然这些还需要结合触控器的产品说明来实现的

评分

参与人数 1蝴蝶豆 +2 收起 理由
STMCU + 2

查看全部评分

所属标签

相似问题

关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版