Dependents:   serial_connected_mcu_nucleo rotary_encoder_mbed serial_connected_mcu_nucleo omuni_speed_pid ... more

Fork of rotary_encoder by tarou yamada

このライブラリは以下のプログラムに基いています https://developer.mbed.org/users/gregeric/code/Nucleo_Hello_Encoder/

Committer:
inst
Date:
Fri Apr 15 08:53:57 2016 +0000
Revision:
5:817222abfd86
Parent:
4:d07f58c46a79

        

Who changed what in which revision?

UserRevisionLine numberNew 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 4:d07f58c46a79 10
inst 4:d07f58c46a79 11 // todo:delete debug
inst 4:d07f58c46a79 12 encoder_mode = TIM_ENCODERMODE_TI12;
inst 4:d07f58c46a79 13 resolution = 400;
inst 4:d07f58c46a79 14
inst 3:65021ea3fae5 15 timer_handler_.Instance = timer_type;
inst 3:65021ea3fae5 16 timer_handler_.Init.Period = max_counts_;
inst 3:65021ea3fae5 17 timer_handler_.Init.CounterMode = TIM_COUNTERMODE_UP;
inst 3:65021ea3fae5 18 timer_handler_.Init.Prescaler = 0;
inst 3:65021ea3fae5 19 timer_handler_.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
inst 3:65021ea3fae5 20
inst 3:65021ea3fae5 21 TIM_Encoder_InitTypeDef encoder;
inst 0:caf1d0bc4b90 22 encoder.EncoderMode = encoder_mode;
inst 3:65021ea3fae5 23
inst 0:caf1d0bc4b90 24 encoder.IC1Filter = 0x0F;
inst 0:caf1d0bc4b90 25 encoder.IC1Polarity = TIM_INPUTCHANNELPOLARITY_RISING;
inst 0:caf1d0bc4b90 26 encoder.IC1Prescaler = TIM_ICPSC_DIV4;
inst 0:caf1d0bc4b90 27 encoder.IC1Selection = TIM_ICSELECTION_DIRECTTI;
inst 0:caf1d0bc4b90 28
inst 0:caf1d0bc4b90 29 encoder.IC2Filter = 0x0F;
inst 0:caf1d0bc4b90 30 encoder.IC2Polarity = TIM_INPUTCHANNELPOLARITY_FALLING;
inst 0:caf1d0bc4b90 31 encoder.IC2Prescaler = TIM_ICPSC_DIV4;
inst 0:caf1d0bc4b90 32 encoder.IC2Selection = TIM_ICSELECTION_DIRECTTI;
inst 3:65021ea3fae5 33
inst 3:65021ea3fae5 34 ////////////////////////////////////////
inst 3:65021ea3fae5 35 //printf("t:%d, e:%d", &timer_handler_, &encoder);
inst 3:65021ea3fae5 36 if (HAL_TIM_Encoder_Init(&timer_handler_, &encoder) != HAL_OK) {
inst 3:65021ea3fae5 37 error("couldn't init encoder\n");
inst 0:caf1d0bc4b90 38 }
inst 3:65021ea3fae5 39 //printf("%d end\n", cnt++);
inst 3:65021ea3fae5 40 ///////////////////////////////////////////
inst 5:817222abfd86 41
inst 5:817222abfd86 42 reset();
inst 0:caf1d0bc4b90 43 }
inst 0:caf1d0bc4b90 44
inst 3:65021ea3fae5 45 rotary_encoder_base::~rotary_encoder_base() {}
inst 3:65021ea3fae5 46
inst 0:caf1d0bc4b90 47 int32_t rotary_encoder_base::get_counts() const {
inst 3:65021ea3fae5 48 int32_t counts = timer_handler_.Instance->CNT;
inst 0:caf1d0bc4b90 49
inst 0:caf1d0bc4b90 50 if (counts > (max_counts_ >> 1)) {
inst 0:caf1d0bc4b90 51 return counts - max_counts_;
inst 0:caf1d0bc4b90 52 }
inst 0:caf1d0bc4b90 53 return counts;
inst 0:caf1d0bc4b90 54 }
inst 0:caf1d0bc4b90 55
inst 0:caf1d0bc4b90 56 void rotary_encoder_base::reset() {
inst 3:65021ea3fae5 57 timer_handler_.Instance->CNT = 0;
inst 0:caf1d0bc4b90 58 }
inst 0:caf1d0bc4b90 59
inst 0:caf1d0bc4b90 60 void rotary_encoder_base::start() {
inst 3:65021ea3fae5 61 if(HAL_TIM_Encoder_Start(&timer_handler_, TIM_CHANNEL_1) != HAL_OK) {
inst 0:caf1d0bc4b90 62 error("couldn't start encoder\r\n");
inst 0:caf1d0bc4b90 63 }
inst 0:caf1d0bc4b90 64 }
inst 0:caf1d0bc4b90 65
inst 0:caf1d0bc4b90 66 void rotary_encoder_base::stop() {
inst 3:65021ea3fae5 67 if(HAL_TIM_Encoder_Stop(&timer_handler_, TIM_CHANNEL_1) != HAL_OK) {
inst 0:caf1d0bc4b90 68 error("couldn't start encoder\r\n");
inst 0:caf1d0bc4b90 69 }
inst 0:caf1d0bc4b90 70 }
inst 2:4580c3869b7b 71
inst 2:4580c3869b7b 72 } /* namespace mbed_stl */