|
|
Das EZS Board unterstützt Quadratur Encoder nativ und bietet sie als Timer an. Zu erst muss der Timer richtig registriert werden:
|
|
|
|
|
|
```
|
|
|
int initRotaryEncoderTimer(uint32_t tim, uint32_t portA, uint16_t pinA, uint8_t afA, uint32_t portB, uint16_t pinB, uint8_t afB) {
|
|
|
gpio_mode_setup(portA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, pinA);
|
|
|
gpio_set_af(portA, afA, pinA);
|
|
|
|
|
|
gpio_mode_setup(portB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, pinB);
|
|
|
gpio_set_af(portB, afB, pinB);
|
|
|
|
|
|
timer_reset(tim);
|
|
|
|
|
|
timer_set_mode(tim, TIM_CR1_CKD_CK_INT, //For dead time and filter sampling, not important for now.
|
|
|
TIM_CR1_CMS_EDGE, //TIM_CR1_CMS_EDGE
|
|
|
TIM_CR1_DIR_UP);
|
|
|
|
|
|
timer_set_prescaler(tim, 0);
|
|
|
timer_set_repetition_counter(tim, 0);
|
|
|
timer_enable_preload(tim);
|
|
|
timer_continuous_mode(tim);
|
|
|
|
|
|
timer_slave_set_mode(tim, TIM_SMCR_SMS_EM3); // encoder
|
|
|
|
|
|
timer_set_oc_polarity_high(tim, TIM_OC1);
|
|
|
timer_set_oc_polarity_high(tim, TIM_OC2);
|
|
|
|
|
|
timer_ic_disable(tim, TIM_IC1);
|
|
|
timer_ic_disable(tim, TIM_IC2);
|
|
|
|
|
|
timer_ic_set_input(tim, TIM_IC1, TIM_IC_IN_TI1);
|
|
|
timer_ic_set_input(tim, TIM_IC2, TIM_IC_IN_TI1);
|
|
|
|
|
|
timer_disable_oc_output(tim, TIM_OC1);
|
|
|
timer_disable_oc_output(tim, TIM_OC2);
|
|
|
|
|
|
timer_disable_preload_complementry_enable_bits(tim);
|
|
|
timer_set_period(tim, 0xFFFFFFFF);
|
|
|
|
|
|
timer_enable_counter(tim);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
```
|
|
|
|
|
|
Beispielaufruf:
|
|
|
` initRotaryEncoderTimer(TIM2, GPIOA, GPIO5, GPIO_AF1, GPIOA, GPIO1, GPIO_AF1);`
|
|
|
|
|
|
Dabei muss darauf geachtet werden die richtigen Pins mit den richtigen alternative functions auszuwählen. Eine Übersicht gibt es ab Seite 38 im [Datenblatt](http://www.st.com/content/ccc/resource/technical/document/datasheet/b3/a5/46/3b/b4/e5/4c/85/DM00115249.pdf/files/DM00115249.pdf/jcr:content/translations/en.DM00115249.pdf)
|
|
|
|
|
|
Die aktuelle Position des Drehgebers kann mit einem einfachen Aufruf abgefragt werden:
|
|
|
`timer_get_counter(TIM3);`
|
|
|
|