mirror of
https://github.com/IcedRooibos/py32f0-template.git
synced 2025-10-29 00:42:06 -07:00
64 lines
1.7 KiB
C
64 lines
1.7 KiB
C
#include "encoder.h"
|
|
|
|
TIM_HandleTypeDef htim3;
|
|
|
|
static uint16_t newCount;
|
|
static uint16_t prevCount;
|
|
|
|
void Encoder_Init(void) {
|
|
HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL);
|
|
}
|
|
|
|
uint16_t Encoder_Read() {
|
|
uint16_t val = __HAL_TIM_GET_COUNTER(&htim3);
|
|
return val >> 1;
|
|
}
|
|
|
|
Encoder_Status Encoder_Get_Status() {
|
|
newCount = Encoder_Read();
|
|
if (newCount != prevCount) {
|
|
if (newCount > prevCount) {
|
|
prevCount = newCount;
|
|
return Incremented;
|
|
} else {
|
|
prevCount = newCount;
|
|
return Decremented;
|
|
}
|
|
}
|
|
return Neutral;
|
|
}
|
|
|
|
/**
|
|
* @brief TIM3 Initialization Function (Encoder Mode)
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
void Encoder_Config(void)
|
|
{
|
|
TIM_Encoder_InitTypeDef sConfig = {0};
|
|
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
|
|
|
htim3.Instance = TIM3;
|
|
htim3.Init.Prescaler = 0;
|
|
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
|
|
htim3.Init.Period = 65535;
|
|
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
|
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
|
sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
|
|
sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
|
|
sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
|
|
sConfig.IC1Prescaler = TIM_ICPSC_DIV1; // Capture performed each time an edge is detected on the capture input
|
|
sConfig.IC1Filter = 0;
|
|
sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
|
|
sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
|
|
sConfig.IC2Prescaler = TIM_ICPSC_DIV1; // Capture performed each time an edge is detected on the capture input
|
|
sConfig.IC2Filter = 0;
|
|
|
|
HAL_TIM_Encoder_Init(&htim3, &sConfig);
|
|
|
|
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
|
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
|
|
|
HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);
|
|
}
|