Dependents: serial_connected_mcu_nucleo rotary_encoder_mbed serial_connected_mcu_nucleo omuni_speed_pid ... more
Fork of rotary_encoder by
このライブラリは以下のプログラムに基いています https://developer.mbed.org/users/gregeric/code/Nucleo_Hello_Encoder/
Diff: rotary_encoder_base/rotary_encoder_base_impl.cpp
- Revision:
- 9:d1e6284a13ab
diff -r 0a217d9f31c7 -r d1e6284a13ab rotary_encoder_base/rotary_encoder_base_impl.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rotary_encoder_base/rotary_encoder_base_impl.cpp Sat May 21 03:19:33 2016 +0000 @@ -0,0 +1,57 @@ +#include "rotary_encoder_base_impl.hpp" +#include "rotary_encoder.hpp" +#include "mbed.h" + +rotary_encoder_base_impl::rotary_encoder_base_impl(TIM_TypeDef* timer_type, + uint32_t encoder_mode, + size_t resolution) : rotary_encoder(resolution) { + timer_handler_.Instance = timer_type; + timer_handler_.Init.Period = max_counts_; + timer_handler_.Init.CounterMode = TIM_COUNTERMODE_UP; + timer_handler_.Init.Prescaler = 0; + timer_handler_.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + + TIM_Encoder_InitTypeDef encoder; + encoder.EncoderMode = encoder_mode; + + encoder.IC1Filter = 0x0F; + encoder.IC1Polarity = TIM_INPUTCHANNELPOLARITY_RISING; + encoder.IC1Prescaler = TIM_ICPSC_DIV4; + encoder.IC1Selection = TIM_ICSELECTION_DIRECTTI; + + encoder.IC2Filter = 0x0F; + encoder.IC2Polarity = TIM_INPUTCHANNELPOLARITY_FALLING; + encoder.IC2Prescaler = TIM_ICPSC_DIV4; + encoder.IC2Selection = TIM_ICSELECTION_DIRECTTI; + + if (HAL_TIM_Encoder_Init(&timer_handler_, &encoder) != HAL_OK) { + error("couldn't init encoder\n"); + } +} + +rotary_encoder_base_impl::~rotary_encoder_base_impl() {} + +int32_t rotary_encoder_base_impl::get_counts() const { + int32_t counts = timer_handler_.Instance->CNT; + + if (counts > (max_counts_ >> 1)) { + return counts - max_counts_; + } + return counts; +} + +void rotary_encoder_base_impl::reset() { + timer_handler_.Instance->CNT = 0; +} + +void rotary_encoder_base_impl::start() { + if(HAL_TIM_Encoder_Start(&timer_handler_, TIM_CHANNEL_1) != HAL_OK) { + error("couldn't start encoder\r\n"); + } +} + +void rotary_encoder_base_impl::stop() { + if(HAL_TIM_Encoder_Stop(&timer_handler_, TIM_CHANNEL_1) != HAL_OK) { + error("couldn't start encoder\r\n"); + } +}