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/
rotary_encoder_base/rotary_encoder_base.cpp@3:65021ea3fae5, 2016-03-31 (annotated)
- Committer:
- inst
- Date:
- Thu Mar 31 04:42:31 2016 +0000
- Revision:
- 3:65021ea3fae5
- Parent:
- 2:4580c3869b7b
- Child:
- 4:d07f58c46a79
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
inst | 0:caf1d0bc4b90 | 1 | #include "rotary_encoder_base.hpp" |
inst | 0:caf1d0bc4b90 | 2 | #include "rotary_encoder.hpp" |
inst | 3:65021ea3fae5 | 3 | #include "mbed.h" |
inst | 0:caf1d0bc4b90 | 4 | |
inst | 2:4580c3869b7b | 5 | namespace mbed_stl { |
inst | 2:4580c3869b7b | 6 | |
inst | 0:caf1d0bc4b90 | 7 | rotary_encoder_base::rotary_encoder_base(TIM_TypeDef* timer_type, |
inst | 0:caf1d0bc4b90 | 8 | uint32_t encoder_mode, |
inst | 0:caf1d0bc4b90 | 9 | size_t resolution) : rotary_encoder(resolution) { |
inst | 3:65021ea3fae5 | 10 | static size_t cnt = 0; |
inst | 3:65021ea3fae5 | 11 | //printf("cnt : %d\n", cnt); |
inst | 0:caf1d0bc4b90 | 12 | |
inst | 3:65021ea3fae5 | 13 | timer_handler_.Instance = timer_type; |
inst | 3:65021ea3fae5 | 14 | timer_handler_.Init.Period = max_counts_; |
inst | 3:65021ea3fae5 | 15 | timer_handler_.Init.CounterMode = TIM_COUNTERMODE_UP; |
inst | 3:65021ea3fae5 | 16 | timer_handler_.Init.Prescaler = 0; |
inst | 3:65021ea3fae5 | 17 | timer_handler_.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
inst | 3:65021ea3fae5 | 18 | |
inst | 3:65021ea3fae5 | 19 | TIM_Encoder_InitTypeDef encoder; |
inst | 0:caf1d0bc4b90 | 20 | encoder.EncoderMode = encoder_mode; |
inst | 3:65021ea3fae5 | 21 | |
inst | 0:caf1d0bc4b90 | 22 | encoder.IC1Filter = 0x0F; |
inst | 0:caf1d0bc4b90 | 23 | encoder.IC1Polarity = TIM_INPUTCHANNELPOLARITY_RISING; |
inst | 0:caf1d0bc4b90 | 24 | encoder.IC1Prescaler = TIM_ICPSC_DIV4; |
inst | 0:caf1d0bc4b90 | 25 | encoder.IC1Selection = TIM_ICSELECTION_DIRECTTI; |
inst | 0:caf1d0bc4b90 | 26 | |
inst | 0:caf1d0bc4b90 | 27 | encoder.IC2Filter = 0x0F; |
inst | 0:caf1d0bc4b90 | 28 | encoder.IC2Polarity = TIM_INPUTCHANNELPOLARITY_FALLING; |
inst | 0:caf1d0bc4b90 | 29 | encoder.IC2Prescaler = TIM_ICPSC_DIV4; |
inst | 0:caf1d0bc4b90 | 30 | encoder.IC2Selection = TIM_ICSELECTION_DIRECTTI; |
inst | 3:65021ea3fae5 | 31 | |
inst | 3:65021ea3fae5 | 32 | //////////////////////////////////////// |
inst | 3:65021ea3fae5 | 33 | //printf("t:%d, e:%d", &timer_handler_, &encoder); |
inst | 3:65021ea3fae5 | 34 | /* |
inst | 3:65021ea3fae5 | 35 | if (HAL_TIM_Encoder_Init(&timer_handler_, &encoder) != HAL_OK) { |
inst | 3:65021ea3fae5 | 36 | error("couldn't init encoder\n"); |
inst | 0:caf1d0bc4b90 | 37 | } |
inst | 3:65021ea3fae5 | 38 | */ |
inst | 3:65021ea3fae5 | 39 | //printf("%d end\n", cnt++); |
inst | 3:65021ea3fae5 | 40 | /////////////////////////////////////////// |
inst | 0:caf1d0bc4b90 | 41 | } |
inst | 0:caf1d0bc4b90 | 42 | |
inst | 3:65021ea3fae5 | 43 | rotary_encoder_base::~rotary_encoder_base() {} |
inst | 3:65021ea3fae5 | 44 | |
inst | 0:caf1d0bc4b90 | 45 | int32_t rotary_encoder_base::get_counts() const { |
inst | 3:65021ea3fae5 | 46 | int32_t counts = timer_handler_.Instance->CNT; |
inst | 0:caf1d0bc4b90 | 47 | |
inst | 0:caf1d0bc4b90 | 48 | if (counts > (max_counts_ >> 1)) { |
inst | 0:caf1d0bc4b90 | 49 | return counts - max_counts_; |
inst | 0:caf1d0bc4b90 | 50 | } |
inst | 0:caf1d0bc4b90 | 51 | return counts; |
inst | 0:caf1d0bc4b90 | 52 | } |
inst | 0:caf1d0bc4b90 | 53 | |
inst | 0:caf1d0bc4b90 | 54 | void rotary_encoder_base::reset() { |
inst | 3:65021ea3fae5 | 55 | timer_handler_.Instance->CNT = 0; |
inst | 0:caf1d0bc4b90 | 56 | } |
inst | 0:caf1d0bc4b90 | 57 | |
inst | 0:caf1d0bc4b90 | 58 | void rotary_encoder_base::start() { |
inst | 3:65021ea3fae5 | 59 | if(HAL_TIM_Encoder_Start(&timer_handler_, TIM_CHANNEL_1) != HAL_OK) { |
inst | 0:caf1d0bc4b90 | 60 | error("couldn't start encoder\r\n"); |
inst | 0:caf1d0bc4b90 | 61 | } |
inst | 0:caf1d0bc4b90 | 62 | } |
inst | 0:caf1d0bc4b90 | 63 | |
inst | 0:caf1d0bc4b90 | 64 | void rotary_encoder_base::stop() { |
inst | 3:65021ea3fae5 | 65 | if(HAL_TIM_Encoder_Stop(&timer_handler_, TIM_CHANNEL_1) != HAL_OK) { |
inst | 0:caf1d0bc4b90 | 66 | error("couldn't start encoder\r\n"); |
inst | 0:caf1d0bc4b90 | 67 | } |
inst | 0:caf1d0bc4b90 | 68 | } |
inst | 2:4580c3869b7b | 69 | |
inst | 2:4580c3869b7b | 70 | } /* namespace mbed_stl */ |