我利用stm32L4芯片内部DAC产生正弦波,再利用timer的PWM产生方波,可是利用示波器观察发现,两个波形不同步,开发库是HAL库,求解决方案,已经困扰我两个星期了。。。。我之前利用stm32F1的标准库产生的波形是同步的。。。部分代码如下:timer配置:
void TIM1_Config(void)
{
TimHandle.Instance = TIM1;
TimHandle.Init.Prescaler = uhPrescalerValue1;
TimHandle.Init.Period = PERIOD_VALUE1;
TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
TimHandle.Init.RepetitionCounter = 0;
if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
sConfig.OCMode = TIM_OCMODE_PWM1;
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfig.OCFastMode = TIM_OCFAST_DISABLE;
sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfig.Pulse = PULSE1_VALUE1;
if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
{
/* PWM Generation Error */
Error_Handler();
}
}
DAC时钟配置:
void TIM6_Config(void)
{
TIM_MasterConfigTypeDef sMasterConfig;
htim.Instance = TIM6;
htim.Init.Period = PERIOD_VALUE6;
htim.Init.Prescaler = uhPrescalerValue2;
htim.Init.ClockDivision = 0;
htim.Init.CounterMode = TIM_COUNTERMODE_UP;
htim.Init.RepetitionCounter = 0;
HAL_TIM_Base_Init(&htim);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;//TIM_TRGO_ENABLE;//
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim, &sMasterConfig);
HAL_TIM_Base_Start(&htim);
}
|