working pwm input capture
This commit is contained in:
parent
2df23ab567
commit
48f60f0f73
@ -30,7 +30,8 @@ static TIM_HandleTypeDef timer_handle = {
|
|||||||
static TIM_IC_InitTypeDef input_capture_init = {
|
static TIM_IC_InitTypeDef input_capture_init = {
|
||||||
.ICPrescaler = TIM_ICPSC_DIV1,
|
.ICPrescaler = TIM_ICPSC_DIV1,
|
||||||
.ICFilter = 0,
|
.ICFilter = 0,
|
||||||
.ICPolarity = TIM_ICPOLARITY_FALLING,
|
// .ICPolarity = TIM_ICPOLARITY_FALLING,
|
||||||
|
.ICPolarity = TIM_ICPOLARITY_BOTHEDGE,
|
||||||
.ICSelection = TIM_ICSELECTION_INDIRECTTI,
|
.ICSelection = TIM_ICSELECTION_INDIRECTTI,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -64,9 +65,7 @@ static const struct driver gpio_b10 = {
|
|||||||
&__gpio_b10,
|
&__gpio_b10,
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint32_t ch3 = 0, ch4 = 0;
|
static int period = 0, period_start = 0, pulse = 0, pulse_start = 0;
|
||||||
static int period = 0, pulse = 0;
|
|
||||||
|
|
||||||
|
|
||||||
void config_input(void)
|
void config_input(void)
|
||||||
{
|
{
|
||||||
@ -77,13 +76,8 @@ void config_input(void)
|
|||||||
/* cfg timer */
|
/* cfg timer */
|
||||||
HAL_TIM_IC_Init(&timer_handle);
|
HAL_TIM_IC_Init(&timer_handle);
|
||||||
HAL_TIM_IC_ConfigChannel(&timer_handle, &input_capture_init, TIM_CHANNEL_4);
|
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_SlaveConfigSynchronization(&timer_handle, &slave_config);
|
||||||
HAL_TIM_IC_Start_IT(&timer_handle, TIM_CHANNEL_4);
|
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);
|
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_3, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty);
|
||||||
drv_ioctl(&pwm_4, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty);
|
drv_ioctl(&pwm_4, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty);
|
||||||
sleep_ms(100);
|
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;
|
float dist = 84000.0 / pulse;
|
||||||
dist = 343.0 / dist;
|
dist = 343.0 / dist;
|
||||||
dist /= 2.0;
|
dist /= 2.0;
|
||||||
dist *= 100; // m -> cm
|
dist *= 100; // m -> cm
|
||||||
int d = (int)dist;
|
int d = (int)dist;
|
||||||
sprintf(print_buffer, "distance: %d cm\r\n", d);
|
sprintf(print_buffer, "distance: %d cm\r\n", d);
|
||||||
drv_write(&uart_1, print_buffer, strlen(print_buffer));
|
drv_write(&uart_1, print_buffer, strlen(print_buffer));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
for(unsigned int duty = 98; duty > 0; duty--) {
|
for(unsigned int duty = 98; duty > 0; duty--) {
|
||||||
drv_ioctl(&pwm_1, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&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_3, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty);
|
||||||
drv_ioctl(&pwm_4, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty);
|
drv_ioctl(&pwm_4, IOCTL_PWM_SET_DUTY_CYCLE, (const void *)&duty);
|
||||||
sleep_ms(100);
|
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;
|
float dist = 84000.0 / pulse;
|
||||||
dist = 343.0 / dist;
|
dist = 343.0 / dist;
|
||||||
dist /= 2.0;
|
dist /= 2.0;
|
||||||
dist *= 100; // m -> cm
|
dist *= 100; // m -> cm
|
||||||
int d = (int)dist;
|
int d = (int)dist;
|
||||||
sprintf(print_buffer, "distance: %d cm\r\n", d);
|
sprintf(print_buffer, "distance: %d cm\r\n", d);
|
||||||
drv_write(&uart_1, print_buffer, strlen(print_buffer));
|
drv_write(&uart_1, print_buffer, strlen(print_buffer));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,51 +137,27 @@ void TIM2_IRQHandler(void)
|
|||||||
{
|
{
|
||||||
enter_isr();
|
enter_isr();
|
||||||
TIM_HandleTypeDef *htim = &timer_handle;
|
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 */
|
/* Capture compare 4 event */
|
||||||
if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC4) != RESET) {
|
if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC4) != RESET) {
|
||||||
if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC4) !=RESET) {
|
if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC4) !=RESET) {
|
||||||
__HAL_TIM_CLEAR_IT(htim, TIM_IT_CC4);
|
__HAL_TIM_CLEAR_IT(htim, TIM_IT_CC4);
|
||||||
/* Input capture event */
|
/* Input capture event */
|
||||||
if((htim->Instance->CCMR2 & TIM_CCMR2_CC4S) != 0x00U) {
|
if((htim->Instance->CCMR2 & TIM_CCMR2_CC4S) != 0x00U) {
|
||||||
uint32_t ch4_old = ch4;
|
uint32_t ch4 = htim->Instance->CCR4;
|
||||||
ch4 = htim->Instance->CCR4;
|
if(GPIO_PIN_SET == HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_10)) {
|
||||||
period = ch4 - ch4_old;
|
if(ch4 > period_start)
|
||||||
pulse = ch4 - ch3;
|
period = ch4 - period_start;
|
||||||
|
period_start = ch4;
|
||||||
|
pulse_start = ch4;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if(ch4 > pulse_start)
|
||||||
|
pulse = ch4 - pulse_start;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tmpreg = tmpreg;
|
|
||||||
|
|
||||||
exit_isr();
|
exit_isr();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user