void TIM2_PWMShiftInit_3(TypeDef_Tim* Tim) { TIM_ClockConfigTypeDef sClockSourceConfig = {0}; TIM_OC_InitTypeDef sConfigOC = {0}; TIM_MasterConfigTypeDef sMasterConfig = {0}; GPIO_InitTypeDef GPIO_InitStruct = {0}; Tim->Psc=3; Tim->TimeClock=200000000;// Tim->Frequence=2000;// Tim->Duty=0.5; Tim->DT=2000;// Tim->Arr=Tim->TimeClock/(Tim->Psc+1)/Tim->Frequence/2;// // Tim->CH1Ccr=Tim->Arr-(Tim->Arr*Tim->Duty)-Tim->DT/((Tim->Psc+1)*(1000000000.0f/Tim->TimeClock));// Tim->CH2Ccr=Tim->Arr-(Tim->Arr*Tim->Duty); Tim->Htim.Instance = TIM2; Tim->Htim.Init.Prescaler = Tim->Psc; Tim->Htim.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED3; Tim->Htim.Init.Period = Tim->Arr; Tim->Htim.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; Tim->Htim.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; HAL_TIM_Base_Init(&Tim->Htim); HAL_TIM_Base_Start_IT(&Tim->Htim);// sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; HAL_TIM_ConfigClockSource(&Tim->Htim, &sClockSourceConfig); HAL_TIM_OC_Init(&Tim->Htim); sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; HAL_TIMEx_MasterConfigSynchronization(&Tim->Htim, &sMasterConfig); sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.Pulse = Tim->CH1Ccr; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;// sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; HAL_TIM_OC_ConfigChannel(&Tim->Htim, &sConfigOC, TIM_CHANNEL_3); __HAL_TIM_ENABLE_OCxPRELOAD(&Tim->Htim, TIM_CHANNEL_3); sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.Pulse = Tim->CH2Ccr; sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;// sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; HAL_TIM_OC_ConfigChannel(&Tim->Htim, &sConfigOC, TIM_CHANNEL_4); __HAL_TIM_ENABLE_OCxPRELOAD(&Tim->Htim, TIM_CHANNEL_4); __HAL_RCC_GPIOB_CLK_ENABLE(); /**TIM2 GPIO Configuration PB10 ------> TIM2_CH3 PB11 ------> TIM2_CH4 */ GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); HAL_TIM_PWM_Start(&Tim->Htim, TIM_CHANNEL_3); HAL_TIM_PWM_Start(&Tim->Htim, TIM_CHANNEL_4); } TIM2_PWMShiftInit_3(&MyTim2);是什么意思
时间: 2023-06-09 12:07:31 浏览: 404
通过STM32F103单片机通用定时器TIM2,同时生成频率、占空比可调的4路PWM.
这是一个函数的定义,它用于初始化 TIM2 定时器的 PWM 模式。参数 TypeDef_Tim 是一个结构体类型,它包含了定时器的一些属性,例如预分频器值、时钟频率、PWM 频率、占空比等。在函数体内部,使用了一些 HAL 库函数来配置 TIM2 定时器,包括时钟源配置、通道输出模式配置、主从模式配置以及 GPIO 端口配置等。最后,调用了 HAL_TIM_Base_Init 函数来初始化 TIM2 定时器。
阅读全文