本帖最后由 aple0807 于 2021-7-30 13:26 编辑
typedef struct
{
uint8_t read_flt_cnt; //采样滤波计数
uint8_t b_first_key; //首个按键
uint16_t found_time_cnt;
uint16_t lost_time_cnt;
uint16_t keep_time_cnt;
uint32_t val;
uint32_t old;
uint32_t edge;
uint32_t found_edge;
uint32_t lost_edge;
uint32_t read;
uint32_t read_old;
} key_type;
/*-------------------------------------------------------------*
* called by timer isr
*--------------------------------------------------------------*/
void key_st_sample(key_type *key, uint8_t flt_sv, uint32_t key_read)
{
if (key_read == key->read_old)
{
key->read_flt_cnt++;
if (key->read_flt_cnt >= flt_sv)
{
key->read = key_read;
}
}
else
{
key->read_flt_cnt = 0;
}
key->read_old = key_read;
}
/*-------------------------------------------------------------*
* called by app
*--------------------------------------------------------------*/
void key_st_handle(key_type *key, uint8_t time_pulse)
{
uint32_t key_now;
key_now = key->read;
key->val = key_now;
key->edge = key->val ^ key->old;
key->found_edge = key->edge & key_now;
key->lost_edge = key->edge & key->old;
if (key_now)
{
key->lost_time_cnt = 0;
if (time_pulse)
{
if (key->found_time_cnt < INT16_MAX)
key->found_time_cnt++;
key->keep_time_cnt++;
}
}
else
{
key->found_time_cnt = 0;
if (time_pulse)
{
if (key->lost_time_cnt < INT16_MAX)
key->lost_time_cnt++;
key->keep_time_cnt++;
}
}
if (key->edge)
key->keep_time_cnt = 0;
if (key_now && (0 == key->old))
{
key->b_first_key = 1;
}
else
{
key->b_first_key = 0;
}
key->old = key_now;
}
我们常用的多键值程序,分ISR采集和应用处理两部分,key_val的每个bit位作为一个按键的状态。
|