diff --git a/source/test/pwm/main.c b/source/test/pwm/main.c index 0f7789a..225fba0 100644 --- a/source/test/pwm/main.c +++ b/source/test/pwm/main.c @@ -30,7 +30,8 @@ static TIM_HandleTypeDef timer_handle = { static TIM_IC_InitTypeDef input_capture_init = { .ICPrescaler = TIM_ICPSC_DIV1, .ICFilter = 0, - .ICPolarity = TIM_ICPOLARITY_FALLING, +// .ICPolarity = TIM_ICPOLARITY_FALLING, + .ICPolarity = TIM_ICPOLARITY_BOTHEDGE, .ICSelection = TIM_ICSELECTION_INDIRECTTI, }; @@ -64,9 +65,7 @@ static const struct driver gpio_b10 = { &__gpio_b10, }; -static uint32_t ch3 = 0, ch4 = 0; -static int period = 0, pulse = 0; - +static int period = 0, period_start = 0, pulse = 0, pulse_start = 0; void config_input(void) { @@ -77,13 +76,8 @@ void config_input(void) /* cfg timer */ HAL_TIM_IC_Init(&timer_handle); HAL_TIM_IC_ConfigChannel(&timer_handle, &input_capture_init, TIM_CHANNEL_4); - input_capture_init.ICPolarity = TIM_ICPOLARITY_RISING; - input_capture_init.ICSelection = TIM_ICSELECTION_DIRECTTI; - HAL_TIM_IC_ConfigChannel(&timer_handle, &input_capture_init, TIM_CHANNEL_3); - HAL_TIM_SlaveConfigSynchronization(&timer_handle, &slave_config); HAL_TIM_IC_Start_IT(&timer_handle, TIM_CHANNEL_4); - HAL_TIM_IC_Start_IT(&timer_handle, TIM_CHANNEL_3); HAL_NVIC_EnableIRQ(TIM2_IRQn); } @@ -109,18 +103,14 @@ int main(void) drv_ioctl(&pwm_3, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); drv_ioctl(&pwm_4, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); sleep_ms(100); - if((period >= 0) && (pulse >= 0)) { -// sprintf(print_buffer, "period: %u,\tpulse: %u\r\n", period, pulse); -// drv_write(&uart_1, print_buffer, strlen(print_buffer)); - float dist = 84000.0 / pulse; - dist = 343.0 / dist; - dist /= 2.0; - dist *= 100; // m -> cm - int d = (int)dist; - sprintf(print_buffer, "distance: %d cm\r\n", d); - drv_write(&uart_1, print_buffer, strlen(print_buffer)); - } + float dist = 84000.0 / pulse; + dist = 343.0 / dist; + dist /= 2.0; + dist *= 100; // m -> cm + int d = (int)dist; + sprintf(print_buffer, "distance: %d cm\r\n", d); + drv_write(&uart_1, print_buffer, strlen(print_buffer)); } for(unsigned int duty = 98; duty > 0; duty--) { drv_ioctl(&pwm_1, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); @@ -128,18 +118,14 @@ int main(void) drv_ioctl(&pwm_3, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); drv_ioctl(&pwm_4, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty); sleep_ms(100); - if((period >= 0) && (pulse >= 0)) { -// sprintf(print_buffer, "period: %u,\tpulse: %u\r\n", period, pulse); -// drv_write(&uart_1, print_buffer, strlen(print_buffer)); - float dist = 84000.0 / pulse; - dist = 343.0 / dist; - dist /= 2.0; - dist *= 100; // m -> cm - int d = (int)dist; - sprintf(print_buffer, "distance: %d cm\r\n", d); - drv_write(&uart_1, print_buffer, strlen(print_buffer)); - } + float dist = 84000.0 / pulse; + dist = 343.0 / dist; + dist /= 2.0; + dist *= 100; // m -> cm + int d = (int)dist; + sprintf(print_buffer, "distance: %d cm\r\n", d); + drv_write(&uart_1, print_buffer, strlen(print_buffer)); } } @@ -151,51 +137,27 @@ void TIM2_IRQHandler(void) { enter_isr(); TIM_HandleTypeDef *htim = &timer_handle; - uint32_t tmpreg = 0U; - if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC1) != RESET) { - if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC1) !=RESET) { - __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC1); - /* Input capture event */ - if((htim->Instance->CCMR1 & TIM_CCMR1_CC1S) != 0x00U) { - tmpreg = htim->Instance->CCR1; - } - } - } - /* Capture compare 2 event */ - if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC2) != RESET) { - if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC2) !=RESET) { - __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC2); - /* Input capture event */ - if((htim->Instance->CCMR1 & TIM_CCMR1_CC2S) != 0x00U) { - tmpreg = htim->Instance->CCR2; - } - } - } - /* Capture compare 3 event */ - if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC3) != RESET) { - if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC3) !=RESET) { - __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC3); - /* Input capture event */ - if((htim->Instance->CCMR2 & TIM_CCMR2_CC3S) != 0x00U) { - ch3 = htim->Instance->CCR3; - } - } - } /* Capture compare 4 event */ if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC4) != RESET) { if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC4) !=RESET) { __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC4); /* Input capture event */ if((htim->Instance->CCMR2 & TIM_CCMR2_CC4S) != 0x00U) { - uint32_t ch4_old = ch4; - ch4 = htim->Instance->CCR4; - period = ch4 - ch4_old; - pulse = ch4 - ch3; + uint32_t ch4 = htim->Instance->CCR4; + if(GPIO_PIN_SET == HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_10)) { + if(ch4 > period_start) + period = ch4 - period_start; + period_start = ch4; + pulse_start = ch4; + } + else { + if(ch4 > pulse_start) + pulse = ch4 - pulse_start; + } } } } - tmpreg = tmpreg; exit_isr(); }