It is based on https://developer.mbed.org/users/gregeric/code/Nucleo_Hello_Encoder/

Committer:
inst
Date:
Sat Feb 06 14:24:56 2016 +0000
Revision:
0:caf1d0bc4b90
Child:
2:4580c3869b7b
many classes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
inst 0:caf1d0bc4b90 1 #include "mbed.h"
inst 0:caf1d0bc4b90 2 #include "rotary_encoder_abz_phase.hpp"
inst 0:caf1d0bc4b90 3 #include "rotary_encoder_base.hpp"
inst 0:caf1d0bc4b90 4
inst 0:caf1d0bc4b90 5 rotary_encoder_abz_phase::rotary_encoder_abz_phase(TIM_TypeDef* timer_type, PinName z_pin, size_t resolution) :
inst 0:caf1d0bc4b90 6 rotary_encoder_base(timer_type, TIM_ENCODERMODE_TI12, resolution),
inst 0:caf1d0bc4b90 7 z_phase_intr_(z_pin),
inst 0:caf1d0bc4b90 8 counts_in_prev_intr_(0) {
inst 0:caf1d0bc4b90 9 z_phase_intr_.fall(this, &rotary_encoder_abz_phase::intr_z_phase_first);
inst 0:caf1d0bc4b90 10 }
inst 0:caf1d0bc4b90 11
inst 0:caf1d0bc4b90 12 int32_t rotary_encoder_abz_phase::get_counts() const {
inst 0:caf1d0bc4b90 13 int32_t counts = timer_handler_->Instance->CNT;
inst 0:caf1d0bc4b90 14
inst 0:caf1d0bc4b90 15 if (counts > (max_counts_ >> 1)) {
inst 0:caf1d0bc4b90 16 return counts - max_counts_;
inst 0:caf1d0bc4b90 17 }
inst 0:caf1d0bc4b90 18 return counts;
inst 0:caf1d0bc4b90 19 }
inst 0:caf1d0bc4b90 20
inst 0:caf1d0bc4b90 21 void rotary_encoder_abz_phase::intr_z_phase_first() {
inst 0:caf1d0bc4b90 22 counts_in_prev_intr_ = timer_handler_->Instance->CNT;
inst 0:caf1d0bc4b90 23 z_phase_intr_.fall(this, &rotary_encoder_abz_phase::intr_z_phase);
inst 0:caf1d0bc4b90 24 }
inst 0:caf1d0bc4b90 25
inst 0:caf1d0bc4b90 26 void rotary_encoder_abz_phase::intr_z_phase() {
inst 0:caf1d0bc4b90 27 uint32_t counts = timer_handler_->Instance->CNT;
inst 0:caf1d0bc4b90 28 // Z相の割り込みが入る時は、前回とのカウントの差は分解能で割り切れるはず
inst 0:caf1d0bc4b90 29 int64_t error = (counts - counts_in_prev_intr_) % resolution_;
inst 0:caf1d0bc4b90 30 timer_handler_->Instance->CNT -= error;
inst 0:caf1d0bc4b90 31
inst 0:caf1d0bc4b90 32 counts_in_prev_intr_ = counts;
inst 0:caf1d0bc4b90 33 }
inst 0:caf1d0bc4b90 34
inst 0:caf1d0bc4b90 35 void rotary_encoder_abz_phase::reset() {
inst 0:caf1d0bc4b90 36 counts_in_prev_intr_ = 0;
inst 0:caf1d0bc4b90 37 timer_handler_->Instance->CNT = 0;
inst 0:caf1d0bc4b90 38 }