2016-03-11 12:06 AM
Hello there,
I am trying to configure TIM1 and TIM8 for complementary PWM signals generation (in center aligned mode). It works, but for some reason my maxixmum PWM resolution is not 0xFFFF but 0x3FFF... I am not sure what am I missing. This is my configuration for TIM1 (TIM8 is same):void MX_TIM1_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;
TIM_OC_InitTypeDef sConfigOC;
htim1.Instance = TIM1;
htim1.Init.Prescaler = 0;
htim1.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED1;
htim1.Init.Period = 0xFFFF;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
HAL_TIM_Base_Init(&htim1);
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig);
HAL_TIM_PWM_Init(&htim1);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_ENABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 100;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_ENABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1);
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2);
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3);
}
I have noticed that the faster the PWM frequency is, the less resolution I get. At the moment I am trying to run the PWM with 10kHz frequency and 65535 resolution, but it seems impossible.
I am using STM32F4 with 168 Mhz clock. I would apreciate all help!
2016-03-11 03:45 AM
I'm trying to figure out exactly what the issue is.
Sounds to me that you are basically being constrained by the use of Integer Division, and factoring. Short of changing the laws of math, or the input frequency, you're going to have this problem. Don't understand the merit of 0xFFFF in the 10 KHz case. Period = (168000000 / 10000) - 1; // Input Freq / Output Freq, where terminal count is N-1 ie Period = 16800 - 1; For 16-bit timer both Prescaler and Period values are constrained to 0..65535 Where Prescaler = X -1, and Period = Y - 1; OutFreq = InFreq / (X * Y); // Where X and Y are integers in the range 1..65536 Now in Centre Aligned mode, the count goes up/down within the range, so takes twice as long to go out and back, perhaps accounting for the ''twice shorter'' observation.2016-03-11 03:48 AM
You are right. I havent noticed that the PWM period is the exact same period as the one I set for frequency of the PWM. I thought they are separate and they scale appropriately. Thank you.
2016-03-11 03:52 AM
But the way I understand it, it should take twice LONGER not SHORTER because of the center aligned mode.