Air001 高级定时器输入捕获功能测量脉宽和频率
- ✨Air001只有1个16位高级定时器,经实际测试发现,通道1用于输入捕获功能失效,不确定是否是IO引脚存在问题还是硬件bug,折腾了好久,最后切换到通道2使用,就可以捕获到了,其余通道3和通道4没有做有效性测试。
- 🌿对于ARM 32位的M0+内核单片机,同STM32同内核架构的单片机,在定时器的使用上都一样,代码基本可以照搬。
- 🔰本文主要是将STM32代码移植到该芯片工程中,验证功能。
- 🌿测试了采用高级定时器通道1和通道2,复位模式,测量脉宽信号,发现也是不行的,在STM32f030上使用都正常的。《STM32 HAL库定时器输入捕获SlaveMode脉宽测量》
- 🔖Air001价格比较便宜,硬件上还是有很多暗坑或设计缺陷的,在选择移植切换方案时,最好提前做好功能验证。
void MX_TIM1_Init(void)
{TIM_ClockConfigTypeDef sClockSourceConfig = {0};TIM_MasterConfigTypeDef sMasterConfig = {0};TIM_IC_InitTypeDef sConfigIC = {0};htim1.Instance = TIM1;htim1.Init.Prescaler = 23;htim1.Init.CounterMode = TIM_COUNTERMODE_UP;htim1.Init.Period = 65535;htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;htim1.Init.RepetitionCounter = 0;htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;if (HAL_TIM_Base_Init(&htim1) != HAL_OK){Error_Handler();}sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK){Error_Handler();}if (HAL_TIM_IC_Init(&htim1) != HAL_OK){Error_Handler();}sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK){Error_Handler();}sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;sConfigIC.ICFilter = 0;if (HAL_TIM_IC_ConfigChannel(&htim1, &sConfigIC, TIM_CHANNEL_2) != HAL_OK){Error_Handler();}if (HAL_TIM_Base_Start_IT(&htim1) != HAL_OK){Error_Handler();}
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{GPIO_InitTypeDef GPIO_InitStruct = {0};if(tim_baseHandle->Instance==TIM1){__HAL_RCC_TIM1_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();GPIO_InitStruct.Pin = GPIO_PIN_3;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);HAL_NVIC_SetPriority(TIM1_BRK_UP_TRG_COM_IRQn, 2, 0);HAL_NVIC_EnableIRQ(TIM1_BRK_UP_TRG_COM_IRQn);HAL_NVIC_SetPriority(TIM1_CC_IRQn, 1, 0);HAL_NVIC_EnableIRQ(TIM1_CC_IRQn);}if(tim_baseHandle->Instance==TIM16){__HAL_RCC_TIM16_CLK_ENABLE();}
}
- 🌿系统时钟频率配置(本例程测试用于内部时钟,频率24MHz)
void SystemClock_Config(uint32_t HSICLKSource_SET)
{RCC_OscInitTypeDef RCC_OscInitStruct = {0};RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1; RCC_OscInitStruct.HSICalibrationValue = HSICLKSource_SET;if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {Error_Handler();}RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1;RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {Error_Handler();}
}
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef* htim)
{static uint8_t RisingEdge_count = 0;if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2){__HAL_TIM_CLEAR_IT(&htim1, TIM_IT_CC2);if(capture_flag & 0x40) {capture_flag &= 0x3F; value2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); __HAL_TIM_DISABLE(htim); __HAL_TIM_SET_COUNTER(htim, value2); TIM_RESET_CAPTUREPOLARITY(htim, TIM_CHANNEL_2); TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_2, TIM_ICPOLARITY_RISING); __HAL_TIM_ENABLE(htim); }else {capture_flag |= 0x40; RisingEdge_count++;if((RisingEdge_count % 2 == 0)) {value3 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); capture_flag |= 0x80; Pulse_Width = (value2 + OverflowCount_high * TIM1_Period_Value - value1 + 7)/2; PWM_Period_ARR[PWM_Period_CNT++] = value3 + OverflowCount_high * TIM1_Period_Value + OverflowCount_low * TIM1_Period_Value - value1 - 3;if(PWM_Period_CNT == 5){PWM_Period = findMostFrequentNum(PWM_Period_ARR, 5)/2;PWM_Period_CNT = 0;}OverflowCount_high = OverflowCount_low = 0;}else {capture_flag &= 0x7F; value1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2); }__HAL_TIM_DISABLE(htim);__HAL_TIM_SET_COUNTER(htim, value1); TIM_RESET_CAPTUREPOLARITY(htim, TIM_CHANNEL_2); TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_2, TIM_ICPOLARITY_FALLING); __HAL_TIM_ENABLE(htim); }}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim)
{__HAL_TIM_CLEAR_IT(&htim1, TIM_IT_UPDATE);if((capture_flag & 0X80) == 0) {if(capture_flag & 0x40) {OverflowCount_high++;}else {OverflowCount_low++;}}else {OverflowCount_high = 0;OverflowCount_low = 0;}
}
volatile uint32_t ARR_Value, PWM_f;
volatile char capture_flag = 0;
volatile uint8_t OverflowCount_high = 0;
volatile uint8_t OverflowCount_low = 0;
volatile uint32_t value1, value2, value3;
volatile uint32_t Pulse_Width = 0;
volatile uint32_t PWM_Period = 0;
uint32_t PWM_Period_ARR[5] = {0};
uint8_t PWM_Period_CNT = 0;
int main(void)
{uint32_t TimerUART;uint8_t USART_TX_Buff[40] = {0};HAL_Init();SystemClock_Config(RCC_HSICALIBRATION_24MHz);APP_GpioConfig();HAL_TIM_Base_MspInit(&htim1);MX_TIM1_Init();MX_TIM16_Init();MX_USARTx_UART_Init();
HAL_TIM_PWM_Start(&htim16, TIM_CHANNEL_1); HAL_TIM_Base_Start_IT(&htim1);
__HAL_TIM_URS_ENABLE(&htim1); __HAL_TIM_CLEAR_IT(&htim1, TIM_IT_UPDATE);__HAL_TIM_ENABLE_IT(&htim1, TIM_IT_UPDATE);HAL_TIM_IC_Start_IT(&htim1, TIM_CHANNEL_2);TimerUART = HAL_GetTick();printf("Air001 SysClockFreq:%d \r\n", HAL_RCC_GetSysClockFreq());while(1){
if((HAL_GetTick() - TimerUART) > 1000){HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_1);PWM_f = 1000000l / PWM_Period;sprintf((char*)USART_TX_Buff, "Pulse_Width:%dus,PWM_Period:%d,PWM_f:%dHz", Pulse_Width, PWM_Period, PWM_f);printf("%s \n", USART_TX_Buff);memset((char*)USART_TX_Buff, '\0', strlen((char*)USART_TX_Buff)); TimerUART = HAL_GetTick();}}
}
- 🌿当被测量信号频率越高时,误差就越大。
- 🌿被测信号频率比较低时,测量结果:
📚测试工程
链接:https:
提取码:93ev