|
@@ -5,16 +5,19 @@
|
|
|
|
|
|
#define PWM_IN_TIM_FREQ 1000000
|
|
#define PWM_IN_TIM_FREQ 1000000
|
|
|
|
|
|
|
|
+#define PWM_FILTER_THR 16
|
|
|
|
+
|
|
static TIM_HandleTypeDef TimHandle;
|
|
static TIM_HandleTypeDef TimHandle;
|
|
static TIM_IC_InitTypeDef sConfig;
|
|
static TIM_IC_InitTypeDef sConfig;
|
|
static TIM_SlaveConfigTypeDef sSlaveConfig;
|
|
static TIM_SlaveConfigTypeDef sSlaveConfig;
|
|
|
|
|
|
static uint16_t prescaler;
|
|
static uint16_t prescaler;
|
|
-static uint32_t tim_freq;
|
|
|
|
|
|
|
|
__IO uint32_t cap_value_1 = 0; // Captured Value
|
|
__IO uint32_t cap_value_1 = 0; // Captured Value
|
|
-__IO uint32_t uwDutyCycle = 0; // Duty Cycle Value
|
|
|
|
-__IO uint32_t uwFrequency = 0; // Frequency Value
|
|
|
|
|
|
+__IO uint32_t duty = 0; // Duty Cycle Value
|
|
|
|
+__IO uint32_t freq = 0; // Frequency Value
|
|
|
|
+
|
|
|
|
+uint8_t btn_pressed = 0;
|
|
|
|
|
|
//
|
|
//
|
|
void tim_pwm_in_init(void)
|
|
void tim_pwm_in_init(void)
|
|
@@ -39,13 +42,12 @@ void tim_pwm_in_init(void)
|
|
|
|
|
|
TimHandle.Instance = TIM3;
|
|
TimHandle.Instance = TIM3;
|
|
TimHandle.Init.Period = 0xFFFF;
|
|
TimHandle.Init.Period = 0xFFFF;
|
|
- TimHandle.Init.Prescaler = 47;
|
|
|
|
|
|
+ TimHandle.Init.Prescaler = prescaler;
|
|
TimHandle.Init.ClockDivision = 0;
|
|
TimHandle.Init.ClockDivision = 0;
|
|
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
|
|
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
|
|
TimHandle.Init.RepetitionCounter = 0;
|
|
TimHandle.Init.RepetitionCounter = 0;
|
|
TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
|
TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
|
HAL_TIM_IC_Init(&TimHandle);
|
|
HAL_TIM_IC_Init(&TimHandle);
|
|
- //HAL_TIM_Base_Init(&TimHandle);
|
|
|
|
|
|
|
|
sConfig.ICPrescaler = TIM_ICPSC_DIV1;
|
|
sConfig.ICPrescaler = TIM_ICPSC_DIV1;
|
|
sConfig.ICFilter = 0;
|
|
sConfig.ICFilter = 0;
|
|
@@ -73,47 +75,65 @@ void tim_pwm_in_init(void)
|
|
//
|
|
//
|
|
void tim_print_out_pwm(void)
|
|
void tim_print_out_pwm(void)
|
|
{
|
|
{
|
|
- printf("Captured Value %u\r\n", uwIC2Value);
|
|
|
|
- printf("Duty Cycle Value %u\r\n", uwDutyCycle);
|
|
|
|
- printf("Frequency Value %u\r\n\n", uwFrequency);
|
|
|
|
|
|
+ printf("Captured Value %u\r\n", cap_value_1);
|
|
|
|
+ printf("Duty Cycle Value %u\r\n", duty);
|
|
|
|
+ printf("Frequency Value %u\r\n\n", freq);
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
|
|
+//
|
|
void TIM3_IRQHandler(void)
|
|
void TIM3_IRQHandler(void)
|
|
{
|
|
{
|
|
HAL_TIM_IRQHandler(&TimHandle);
|
|
HAL_TIM_IRQHandler(&TimHandle);
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
|
|
+//
|
|
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
|
|
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
|
|
{
|
|
{
|
|
- static uint32_t cnt = 0;
|
|
|
|
|
|
+ static uint8_t bt_pressed_flag = 0;
|
|
|
|
+ static uint8_t filter_cnt = 0;
|
|
|
|
|
|
- cnt++;
|
|
|
|
-
|
|
|
|
-#if 1
|
|
|
|
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
|
|
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
|
|
{
|
|
{
|
|
- // Get the Input Capture value
|
|
|
|
- uwIC2Value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
|
|
|
|
|
|
+ cap_value_1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
|
|
|
|
|
|
- if (uwIC2Value != 0)
|
|
|
|
|
|
+ if (cap_value_1 != 0)
|
|
{
|
|
{
|
|
- // Duty cycle computation
|
|
|
|
- //uwDutyCycle = ((HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2)) * 100) / uwIC2Value;
|
|
|
|
-
|
|
|
|
- uwDutyCycle = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
|
|
|
|
-
|
|
|
|
- // uwFrequency computation
|
|
|
|
- //TIM3 counter clock = (RCC_Clocks.HCLK_Frequency)
|
|
|
|
- uwFrequency = tim_freq/uwIC2Value;
|
|
|
|
|
|
+ // частота ШИМ в герцах (для контроля)
|
|
|
|
+ freq = PWM_IN_TIM_FREQ/cap_value_1;
|
|
|
|
+
|
|
|
|
+ duty = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
|
|
|
|
+
|
|
|
|
+ if (duty > 1600) {
|
|
|
|
+ filter_cnt++;
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ bt_pressed_flag = 0;
|
|
|
|
+ filter_cnt = 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Выполняется условие фильтра и было ожатие кнопки
|
|
|
|
+ if ((filter_cnt == PWM_FILTER_THR) && (bt_pressed_flag == 0)){
|
|
|
|
+ bt_pressed_flag = 1;
|
|
|
|
+ btn_pressed = true;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- uwDutyCycle = 0;
|
|
|
|
- uwFrequency = 0;
|
|
|
|
|
|
+ duty = 0;
|
|
|
|
+ freq = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-#endif
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+bool get_button(void)
|
|
|
|
+{
|
|
|
|
+ return btn_pressed;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+//
|
|
|
|
+void set_button(bool state)
|
|
|
|
+{
|
|
|
|
+ btn_pressed = state;
|
|
|
|
+}
|