|
说明:
TIM1作为主控机,配置为PWM模式,通过更新事件作为TIM3和TIM4的触发源。
TIM3和TIM4作为从机,配置为PWM模式,输入触发源是ITR0,由于使用了门控功能,TIM4和TIM3的启动停止全部由TIM1来控制。
TIM1 频率 = TIM1 counter clock / (TIM1_Period + 1) = 781.250 KHz
占空比 = TIM1_CCR1 / (TIM1_ARR + 1) = 50%
TIM3频率 = (TIM1 frequency)/ ((TIM3 period +1)* (TIM1_Repetition_Counter+1)) = 52.083 KHz
占空比 = TIM3_CCR1/(TIM3_ARR + 1) = 33.3%
TIM4频率 = (TIM1 frequency)/ ((TIM4 period +1)* (Repetition_Counter+1)) = 78.125 KHz
占空比 = TIM4_CCR1/(TIM4_ARR + 1) = 50%
框图:
代码:
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Timer1 handler declaration: Master */
- TIM_HandleTypeDef TimMasterHandle;
- /* Timer3 handler declaration: Slave1 */
- TIM_HandleTypeDef TimSlave1Handle;
- /* Timer4 handler declaration: Slave2 */
- TIM_HandleTypeDef TimSlave2Handle;
- /* Output compare structure */
- TIM_OC_InitTypeDef sOCConfig;
- /* Master configuration structure */
- TIM_MasterConfigTypeDef sMasterConfig;
- /* Slave configuration structure */
- TIM_SlaveConfigTypeDef sSlaveConfig;
- /* Private function prototypes -----------------------------------------------*/
- static void SystemClock_Config(void);
- static void Error_Handler(void);
- static void CPU_CACHE_Enable(void);
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Main program.
- * @param None
- * @retval None
- */
- int main(void)
- {
- /* Enable the CPU Cache */
- CPU_CACHE_Enable();
- /* STM32H7xx HAL library initialization:
- - Systick timer is configured by default as source of time base, but user
- can eventually implement his proper time base source (a general purpose
- timer for example or other time source), keeping in mind that Time base
- duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
- handled in milliseconds basis.
- - Set NVIC Group Priority to 4
- - Low Level Initialization
- */
- HAL_Init();
- /* Configure the system clock to 400 MHz */
- SystemClock_Config();
- /* Configure LED3 */
- BSP_LED_Init(LED3);
- /* Timers Configuration */
- /* ---------------------------------------------------------------------------
- TIM1 and Timers(TIM3 and TIM4) synchronisation in parallel mode.
- 1/TIM1 is configured as Master Timer:
- - PWM Mode is used
- - The TIM1 Update event is used as Trigger Output
- 2/TIM3 and TIM4 are slaves for TIM1,
- - PWM Mode is used
- - The ITR0(TIM1) is used as input trigger for both slaves
- - Gated mode is used, so starts and stops of slaves counters
- are controlled by the Master trigger output signal(update event).
- In this example TIM1 input clock (TIM1CLK) is set to APB2 clock DIV2 (PCLK2),
- since APB2 prescaler is different from 1.
- TIM1CLK = 2 * APB2 clock
- => TIM1CLK = 2*PCLK2
- The TIM1 counter clock is equal to 2*PCLK2 = 200 MHz.
- The Master Timer TIM1 is running at:
- TIM1 frequency = TIM1 counter clock / (TIM1_Period + 1) = 781.250 KHz
- TIM1_Period = (TIM1 counter clock / TIM1 frequency) - 1 = 182
- and the duty cycle is equal to: TIM1_CCR1/(TIM1_ARR + 1) = 50%
- The TIM3 is running at:
- (TIM1 frequency)/ ((TIM3 period +1)* (Repetition_Counter+1)) = 52.083 KHz and
- a duty cycle equal to TIM3_CCR1/(TIM3_ARR + 1) = 33.3%
- The TIM4 is running at:
- (TIM1 frequency)/ ((TIM4 period +1)* (Repetition_Counter+1)) = 78.125 KHz and
- a duty cycle equal to TIM4_CCR1/(TIM4_ARR + 1) = 50%
- Note:
- SystemCoreClock variable holds HCLK frequency and is defined in SystemClock_Config().
- Each time the core clock (HCLK) changes, user had to update SystemCoreClock
- variable value. Otherwise, any configuration based on this variable will be incorrect.
- This variable is updated in three ways:
- 1) by calling CMSIS function SystemCoreClockUpdate()
- 2) by calling HAL API function HAL_RCC_GetSysClockFreq()
- 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
- --------------------------------------------------------------------------- */
- /* Set Timers instance */
- TimMasterHandle.Instance = TIM1;
- TimSlave1Handle.Instance = TIM3;
- TimSlave2Handle.Instance = TIM4;
- /*====================== Master configuration : TIM1 =======================*/
- /* Initialize TIM1 peripheral in PWM mode*/
- TimMasterHandle.Init.Period = 255;
- TimMasterHandle.Init.Prescaler = 0;
- TimMasterHandle.Init.ClockDivision = 0;
- TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
- TimMasterHandle.Init.RepetitionCounter = 4;
- if (HAL_TIM_PWM_Init(&TimMasterHandle) != HAL_OK)
- {
- /* Initialization Error */
- Error_Handler();
- }
- /* Configure the PWM_channel_1 */
- sOCConfig.OCMode = TIM_OCMODE_PWM1;
- sOCConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
- sOCConfig.Pulse = 128;
- sOCConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
- sOCConfig.OCFastMode = TIM_OCFAST_DISABLE;
- sOCConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
- sOCConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
- if (HAL_TIM_PWM_ConfigChannel(&TimMasterHandle, &sOCConfig, TIM_CHANNEL_1) != HAL_OK)
- {
- /* Configuration Error */
- Error_Handler();
- }
- /* Configure TIM1 as master & use the update event as Trigger Output (TRGO) */
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
- if (HAL_TIMEx_MasterConfigSynchronization(&TimMasterHandle, &sMasterConfig) != HAL_OK)
- {
- /* Configuration Error */
- Error_Handler();
- }
- /*================== End of Master configuration : TIM1 ====================*/
- /*====================== Slave1 configuration : TIM3 =======================*/
- /* Initialize TIM3 peripheral in PWM mode*/
- TimSlave1Handle.Init.Period = 2;
- TimSlave1Handle.Init.Prescaler = 0;
- TimSlave1Handle.Init.ClockDivision = 0;
- TimSlave1Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
- TimSlave1Handle.Init.RepetitionCounter = 0;
- if (HAL_TIM_PWM_Init(&TimSlave1Handle) != HAL_OK)
- {
- /* Initialization Error */
- Error_Handler();
- }
- /* Configure the PWM_channel_1 */
- sOCConfig.OCMode = TIM_OCMODE_PWM1;
- sOCConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
- sOCConfig.Pulse = 1;
- if (HAL_TIM_PWM_ConfigChannel(&TimSlave1Handle, &sOCConfig, TIM_CHANNEL_1) != HAL_OK)
- {
- /* Configuration Error */
- Error_Handler();
- }
- /* Configure TIM3 in Gated slave mode &
- use the Internal Trigger 0 (ITR0) as trigger source */
- sSlaveConfig.SlaveMode = TIM_SLAVEMODE_GATED;
- sSlaveConfig.InputTrigger = TIM_TS_ITR0;
- sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_NONINVERTED;
- sSlaveConfig.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1;
- sSlaveConfig.TriggerFilter = 0;
- if (HAL_TIM_SlaveConfigSynchronization(&TimSlave1Handle, &sSlaveConfig) != HAL_OK)
- {
- /* Configuration Error */
- Error_Handler();
- }
- /*================== End of Slave1 configuration : TIM3 ====================*/
- /*====================== Slave2 configuration : TIM4 =======================*/
- /* Initialize TIM4 peripheral in PWM mode*/
- TimSlave2Handle.Init.Period = 1;
- TimSlave2Handle.Init.Prescaler = 0;
- TimSlave2Handle.Init.ClockDivision = 0;
- TimSlave2Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
- TimSlave2Handle.Init.RepetitionCounter = 0;
- if (HAL_TIM_PWM_Init(&TimSlave2Handle) != HAL_OK)
- {
- /* Initialization Error */
- Error_Handler();
- }
- /* Configure the PWM_channel_1 */
- sOCConfig.OCMode = TIM_OCMODE_PWM1;
- sOCConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
- sOCConfig.Pulse = 1;
- if (HAL_TIM_PWM_ConfigChannel(&TimSlave2Handle, &sOCConfig, TIM_CHANNEL_1) != HAL_OK)
- {
- /* Configuration Error */
- Error_Handler();
- }
- /* Configure TIM3 in Gated slave mode &
- use the Internal Trigger 0 (ITR0) as trigger source */
- sSlaveConfig.SlaveMode = TIM_SLAVEMODE_GATED;
- sSlaveConfig.InputTrigger = TIM_TS_ITR0;
- if (HAL_TIM_SlaveConfigSynchronization(&TimSlave2Handle, &sSlaveConfig) != HAL_OK)
- {
- /* Configuration Error */
- Error_Handler();
- }
- /*================== End of Slave2 configuration : TIM4 ====================*/
- /* Start Master PWM generation */
- if (HAL_TIM_PWM_Start(&TimMasterHandle, TIM_CHANNEL_1) != HAL_OK)
- {
- /* PWM generation Error */
- Error_Handler();
- }
- /* Start Slave1 PWM generation */
- if (HAL_TIM_PWM_Start(&TimSlave1Handle, TIM_CHANNEL_1) != HAL_OK)
- {
- /* PWM generation Error */
- Error_Handler();
- }
- /* Start Slave2 PWM generation */
- if (HAL_TIM_PWM_Start(&TimSlave2Handle, TIM_CHANNEL_1) != HAL_OK)
- {
- /* PWM generation Error */
- Error_Handler();
- }
- while (1)
- {
- }
- }
复制代码
|
|