首页 > 编程知识 正文

旋转编码器开关工作原理,旋转编码器 编程

时间:2023-05-04 22:36:11 阅读:216582 作者:1727

typedef struct

{

encoder_phase_state phase_state[KNOB_COUNT];

uint8_t rotate_dir[KNOB_COUNT];

uint8_t tick_count[KNOB_COUNT];

int32_t pos_old[KNOB_COUNT];

int32_t pos_new[KNOB_COUNT];

uint8_t id[KNOB_COUNT];

}knob_struct;

static int32_t knob_read_count(knob_struct *handle, uint8_t axis)

{

const int8_t phase_ab_states[] = { 0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0 };

static int32_t encoder_output[KNOB_COUNT];

static int8_t phase_ab[KNOB_COUNT]; //AB key read out, Previous in the high 2 bits and current in the low two bits;

static bool first_time_run = true;

if(first_time_run == true)

{

first_time_run = false;

for(uint8_t idx = 0; idx < KNOB_COUNT; idx++)

{

encoder_output[idx] = 0x0000;

phase_ab[idx] = 0x00;

}

}

phase_ab[axis] <<= 2; //bit 2..3 now contain the previous AB key read-out;

if(handle->phase_state[axis].phase_a)

{

phase_ab[axis] |= 0x02; //set the 1st bit if A is high now;

}

if(handle->phase_state[axis].phase_b)

{

phase_ab[axis] |= 0x01; //set the 0th bit if B is high;

}

phase_ab[axis] &= 0x0f; //only retain phase_ab' last 4 bits (A_previous, B_previous, A_current, B_current)

encoder_output[axis清新的口红= phase_ab_states[phase_ab[axis]];

handle->pos_new[axis] = encoder_output[axis] >> 1;

return (encoder_output[axis] >> 1);                              //if you want to return absolute steps.

}

这个是我一直用的,主要用于控制面板旋钮操作用的。  1ms 读一次即可。不需要中断,任意一个个IO电平输入即可。

如果是电机的。这种就不合适了。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。