stm32测两个不同频率方波的相位差hal库代码
时间: 2023-11-09 21:04:59 浏览: 157
以下是使用STM32 HAL库测量两个不同频率方波的相位差的示例代码:
```c
#include "stm32f4xx_hal.h"
#define TIMx TIM2
#define TIMx_CLK_ENABLE() __HAL_RCC_TIM2_CLK_ENABLE()
#define TIMx_IRQn TIM2_IRQn
#define TIMx_IRQHandler TIM2_IRQHandler
#define GPIOx GPIOA
#define GPIO_PIN_CHANNEL1 GPIO_PIN_0
#define GPIO_PIN_CHANNEL2 GPIO_PIN_1
#define GPIO_AF_TIMx GPIO_AF1_TIM2
#define TIMx_FREQ 100000 // TIMx的计数频率,单位为Hz
#define FREQ1 1000 // 第一个方波的频率,单位为Hz
#define FREQ2 500 // 第二个方波的频率,单位为Hz
TIM_HandleTypeDef htimx;
uint32_t counter1 = 0, counter2 = 0;
uint32_t period1 = 0, period2 = 0;
uint32_t phase_diff = 0;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIMx_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIMx_Init();
HAL_TIM_Base_Start_IT(&htimx); // 启动TIMx计数,并启用计数溢出中断
while (1)
{
// 等待计数完成
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1
|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_CHANNEL1|GPIO_PIN_CHANNEL2;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF_TIMx;
HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
}
static void MX_TIMx_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIMx_CLK_ENABLE();
htimx.Instance = TIMx;
htimx.Init.Prescaler = (SystemCoreClock / TIMx_FREQ) - 1;
htimx.Init.CounterMode = TIM_COUNTERMODE_UP;
htimx.Init.Period = (TIMx_FREQ / FREQ1) - 1;
htimx.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_Base_Init(&htimx);
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
HAL_TIM_ConfigClockSource(&htimx, &sClockSourceConfig);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htimx, &sMasterConfig);
TIM_IC_InitTypeDef sConfigIC = {0};
sConfigIC.ICPolarity = TIM_ICPOLARITY_RISING;
sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
sConfigIC.ICFilter = 0;
HAL_TIM_IC_ConfigChannel(&htimx, &sConfigIC, TIM_CHANNEL_1);
sConfigIC.ICPolarity = TIM_ICPOLARITY_RISING;
sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
sConfigIC.ICFilter = 0;
HAL_TIM_IC_ConfigChannel(&htimx, &sConfigIC, TIM_CHANNEL_2);
HAL_TIM_IC_Start_IT(&htimx, TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htimx, TIM_CHANNEL_2);
HAL_NVIC_SetPriority(TIMx_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIMx_IRQn);
}
void TIMx_IRQHandler(void)
{
if (__HAL_TIM_GET_FLAG(&htimx, TIM_FLAG_UPDATE) != RESET)
{
__HAL_TIM_CLEAR_IT(&htimx, TIM_FLAG_UPDATE);
counter1 += htimx.Init.Period + 1;
counter2 += htimx.Init.Period + 1;
}
if (__HAL_TIM_GET_FLAG(&htimx, TIM_FLAG_CC1) != RESET)
{
__HAL_TIM_CLEAR_IT(&htimx, TIM_FLAG_CC1);
period1 = __HAL_TIM_GET_COMPARE(&htimx, TIM_CHANNEL_1) + counter1 - htimx.Instance->CNT;
counter1 = htimx.Instance->CNT;
}
if (__HAL_TIM_GET_FLAG(&htimx, TIM_FLAG_CC2) != RESET)
{
__HAL_TIM_CLEAR_IT(&htimx, TIM_FLAG_CC2);
period2 = __HAL_TIM_GET_COMPARE(&htimx, TIM_CHANNEL_2) + counter2 - htimx.Instance->CNT;
counter2 = htimx.Instance->CNT;
phase_diff = period2 - period1;
}
}
```
在该代码中,TIMx(本例中为TIM2)被配置为计数模式,并且使用了输入捕获功能来测量两个方波的周期,并计算它们之间的相位差。计数频率被设置为100kHz,而第一个方波的频率为1kHz,第二个方波的频率为500Hz。程序使用TIMx的溢出中断和两个输入捕获中断来计数和测量周期。TIMx的计数频率可以根据需求进行调整。
阅读全文