![]() If(HAL_TIM_Encoder_Init(&htim3, &encoderConfig) != HAL_OK) = TIM_CLOCKDIVISION_DIV1 ĮncoderConfig.EncoderMode = TIM_ENCODERMODE_TI12 ĮncoderConfig.IC1Polarity = TIM_ICPOLARITY_RISING ĮncoderConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI ĮncoderConfig.IC1Prescaler = TIM_ICPSC_DIV1 ĮncoderConfig.IC2Polarity = TIM_ICPOLARITY_RISING ĮncoderConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI ĮncoderConfig.IC2Prescaler = TIM_ICPSC_DIV1 ![]() (This code works fine on an STM32F4 Discovery board.) void MX_TIM3_Init(void) If you have both A and B pulses then choose encoder mode TIM_ENCODERMODE_TI12 if only pulse A then TIM_ENCODERMODE_TI1. OS is Linux(Ubuntu 15.04).īest way is to set up a timer in encoder mode to count the pulses of your sensor. If(dutycycle=90)dutycycle=50 //resets to 50% duty cycleĭevelopment board is STM32F407, CubeMX is the source code generator and IDE is Eclipse. HAL_Delay(1000) dutycycle+=10 //1 sec delay and then increases dutycyle by 10% Htim4.Instance->CCR1=dutycycle //setting the duty cycle of PWM signal Speed=(counter1)*(constant) //speed variable, counter 1 related to TIM2 Level=HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_15) //HAL function is called to read pin status HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_1) //Start PWM signal MX_TIM2_Init() //for counting output pulses from PC15 pin * Initialize all configured peripherals */ * Reset of all peripherals, Initializes the Flash interface and the Systick. Int constant=(2*3*1)/20 //roughly circumference/20 slots Volatile GPIO_PinState level // stores input state from PC15, sensor output is connected to PC15 I have doubt about how to use these two timers/counters inside the while loop, which HAL library functions to use here and how to use.Īlso should the code for PWM control and speed calculation be put in same loop. ![]() Sensor output is connected to PC15 and PWM to PD12. ![]() ![]() TIM2'S frequency is same as PWM frequency-10kHz. TIM1's counter period(Auto-reload register) is fixed to 1000 and frequency to 1000Hz. I think, I would need three timers provided by STM32 board: one to provide PWM(TIM4), second(TIM1) to measure the time of (1000msec~1sec) and third timer(TIM2) to measure the number of pulses received from encoder disc in 1 sec. The LM393 based sensor is kept at the right place, so that encoder disc rotates in between the sensing part. I need to measure the speed of a rotating motor fitted to an encoder disc with 20 slots. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
March 2023
Categories |