Skip to main content

<alp/counter.h> — Counters and Encoders

Generic up/down counter API plus quadrature-encoder mode.

Counter

#include <alp/counter.h>

alp_counter_t *cnt = alp_counter_open(&(alp_counter_config_t){
.counter_id = ALP_E1M_COUNTER0, /* 0..3 */
});

alp_counter_start(cnt);
uint32_t value;
alp_counter_get_value(cnt, &value);
alp_counter_close(cnt);

Alarms

void on_alarm(alp_counter_t *c, uint32_t ticks, void *user) { /* fired */ }

alp_counter_set_alarm(cnt, /* ticks_from_now */ 1000000, on_alarm, NULL);

Quadrature encoder

alp_qenc_t *qenc = alp_qenc_open(&(alp_qenc_config_t){
.encoder_id = ALP_E1M_ENC0, /* 0..ALP_E1M_ENC_COUNT-1 (= 4) */
.pulses_per_rev = 24, /* mechanical resolution, informational */
});

int32_t position;
alp_qenc_get_position(qenc, &position);

/* Reset accumulated count back to 0: alp_qenc_reset_position(qenc); */

alp_qenc_close(qenc);

The decoder hardware runs continuously — alp_qenc_open() just gives you a handle to query the accumulated count. position is signed: sign carries direction since the last reset, magnitude is the raw pulse count.

On the E1M-X V2N family the four quadrature encoders are owned by the GD32 supervisor; the host driver routes alp_qenc_* through <alp/chips/gd32g553.h> without the app having to change anything.

See also

Questions about this page? Discuss in Community Forum