STM32自动车PWM信号输出

PWM信号输出

脉冲宽度调制模式可以产生一个由TIMx_ARR寄存器确定频率、由TIMx_CCRx寄存器确定占空比的信号。在TIMx_CCMRx寄存器中的OCxM位写入’110’(PWM模式1)或’111’(PWM模式2),能够独立地设置每个OCx输出通道产生一路PWM。必须设置TIMx_CCMRx寄存器OCxPE位以使能相应的预装载寄存器,最后还要设置TIMx_CR1寄存器的ARPE位, (在向上计数或中心对称模式中)使能自动重装载的预装载寄存器。仅当发生一个更新事件的时候,预装载寄存器才能被传送到影子寄存器,因此在计数器开始计数之前,必须通过设置TIMx_EGR寄存器中的UG位来初始化所有的寄存器。OCx的极性可以通过软件在TIMx_CCER寄存器中的CCxP位设置,它可以设置为高电平有效或低电平有效。 TIMx_CCER寄存器中的CCxE位控制OCx输出使能。在PWM模式(模式1或模式2)下,
TIMx_CNT和TIMx_CCRx始终在进行比较, (依据计数器的计数方向)以确 定是否符合TIMx_CCRx≤TIMx_CNT 或者TIMx_CNT≤TIMx_CC。然而为了与OCREF_CLR的功能(在下一个PWM周期之前, ETR信号上的一个外部事件能够清除OCxREF)一致, OCxREF信号只能在下述条件下产生:当比较的结果改变,或当输出比较模式(TIMx_CCMRx寄存器中的OCxM位)从“冻结” (无比较, OCxM=’000’)切换到某个PWM模式(OCxM=’110’或’111’)。这样在运行中可以通过软件强置PWM输出。根据TIMx_CR1寄存器中CMS位的状态,定时器能够产生边沿对齐的PWM信号或中央对齐的PWM信号。

在这里插入图片描述
通用定时器框图
PWM输出模式配置:



/* TIM3 init
function */

void
MX_TIM3_Init(void)

{

  TIM_ClockConfigTypeDef sClockSourceConfig =
{0};

  TIM_MasterConfigTypeDef sMasterConfig = {0};

  TIM_OC_InitTypeDef sConfigOC = {0};

 

  htim3.Instance = TIM3;

  htim3.Init.Prescaler = 72-1;

  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;

  htim3.Init.Period = 20000-1;

  htim3.Init.ClockDivision =
TIM_CLOCKDIVISION_DIV1;

  htim3.Init.AutoReloadPreload =
TIM_AUTORELOAD_PRELOAD_DISABLE;

  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)

  {

    Error_Handler();

  }

  sClockSourceConfig.ClockSource =
TIM_CLOCKSOURCE_INTERNAL;

  if (HAL_TIM_ConfigClockSource(&htim3,
&sClockSourceConfig) != HAL_OK)

  {

    Error_Handler();

  }

  if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)

  {

    Error_Handler();

  }

  sMasterConfig.MasterOutputTrigger =
TIM_TRGO_RESET;

  sMasterConfig.MasterSlaveMode =
TIM_MASTERSLAVEMODE_DISABLE;

  if
(HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) !=
HAL_OK)

  {

    Error_Handler();

  }

  sConfigOC.OCMode = TIM_OCMODE_PWM1;

  sConfigOC.Pulse = 0;

  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

  if (HAL_TIM_PWM_ConfigChannel(&htim3,
&sConfigOC, TIM_CHANNEL_1) != HAL_OK)

  {

    Error_Handler();

  }

  if (HAL_TIM_PWM_ConfigChannel(&htim3,
&sConfigOC, TIM_CHANNEL_2) != HAL_OK)

  {

    Error_Handler();

  }

  if (HAL_TIM_PWM_ConfigChannel(&htim3,
&sConfigOC, TIM_CHANNEL_3) != HAL_OK)

  {

    Error_Handler();

  }

  if (HAL_TIM_PWM_ConfigChannel(&htim3,
&sConfigOC, TIM_CHANNEL_4) != HAL_OK)

  {

    Error_Handler();

  }

  HAL_TIM_MspPostInit(&htim3);

 

}

/* TIM4 init
function */

void
MX_TIM4_Init(void)

{

  TIM_ClockConfigTypeDef sClockSourceConfig =
{0};

  TIM_MasterConfigTypeDef sMasterConfig = {0};

  TIM_OC_InitTypeDef sConfigOC = {0};

 

  htim4.Instance = TIM4;

  htim4.Init.Prescaler = 72-1;

  htim4.Init.CounterMode = TIM_COUNTERMODE_UP;

  htim4.Init.Period = 20000-1;

  htim4.Init.ClockDivision =
TIM_CLOCKDIVISION_DIV1;

  htim4.Init.AutoReloadPreload =
TIM_AUTORELOAD_PRELOAD_DISABLE;

  if (HAL_TIM_Base_Init(&htim4) != HAL_OK)

  {

    Error_Handler();

  }

  sClockSourceConfig.ClockSource =
TIM_CLOCKSOURCE_INTERNAL;

  if (HAL_TIM_ConfigClockSource(&htim4,
&sClockSourceConfig) != HAL_OK)

  {

    Error_Handler();

  }

  if (HAL_TIM_PWM_Init(&htim4) != HAL_OK)

  {

    Error_Handler();

  }

  sMasterConfig.MasterOutputTrigger =
TIM_TRGO_RESET;

  sMasterConfig.MasterSlaveMode =
TIM_MASTERSLAVEMODE_DISABLE;

  if
(HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) !=
HAL_OK)

  {

    Error_Handler();

  }

  sConfigOC.OCMode = TIM_OCMODE_PWM1;

  sConfigOC.Pulse = 0;

  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

  if (HAL_TIM_PWM_ConfigChannel(&htim4,
&sConfigOC, TIM_CHANNEL_1) != HAL_OK)

  {

    Error_Handler();

  }

  if (HAL_TIM_PWM_ConfigChannel(&htim4,
&sConfigOC, TIM_CHANNEL_2) != HAL_OK)

  {

    Error_Handler();

  }

  if (HAL_TIM_PWM_ConfigChannel(&htim4,
&sConfigOC, TIM_CHANNEL_3) != HAL_OK)

  {

    Error_Handler();

  }

  if (HAL_TIM_PWM_ConfigChannel(&htim4,
&sConfigOC, TIM_CHANNEL_4) != HAL_OK)

  {

    Error_Handler();

  }

  HAL_TIM_MspPostInit(&htim4);

 

}

void
HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)

{

 

  GPIO_InitTypeDef GPIO_InitStruct = {0};

  if(tim_baseHandle->Instance==TIM1)

  {

  /* USER CODE BEGIN TIM1_MspInit 0 */

 

  /* USER CODE END TIM1_MspInit 0 */

    /* TIM1 clock enable */

    __HAL_RCC_TIM1_CLK_ENABLE();

  

    __HAL_RCC_GPIOA_CLK_ENABLE();

    /**TIM1 GPIO Configuration    

    PA8    
------> TIM1_CH1

    PA9    
------> TIM1_CH2

    PA10    
------> TIM1_CH3

    PA11    
------> TIM1_CH4 

    */

    GPIO_InitStruct.Pin =
GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11;

    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 

    /* TIM1 interrupt Init */

    HAL_NVIC_SetPriority(TIM1_CC_IRQn, 0, 0);

    HAL_NVIC_EnableIRQ(TIM1_CC_IRQn);

  /* USER CODE BEGIN TIM1_MspInit 1 */

 

  /* USER CODE END TIM1_MspInit 1 */

  }

  else if(tim_baseHandle->Instance==TIM2)

  {

  /* USER CODE BEGIN TIM2_MspInit 0 */

 

  /* USER CODE END TIM2_MspInit 0 */

    /* TIM2 clock enable */

    __HAL_RCC_TIM2_CLK_ENABLE();

  

    __HAL_RCC_GPIOB_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();

    /**TIM2 GPIO Configuration    

    PB10    
------> TIM2_CH3

    PB11    
------> TIM2_CH4

    PA15    
------> TIM2_CH1

    PB3    
------> TIM2_CH2 

    */

    GPIO_InitStruct.Pin =
GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_3;

    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 

    GPIO_InitStruct.Pin = GPIO_PIN_15;

    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 

    __HAL_AFIO_REMAP_TIM2_ENABLE();

 

    /* TIM2 interrupt Init */

    HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);

    HAL_NVIC_EnableIRQ(TIM2_IRQn);

  /* USER CODE BEGIN TIM2_MspInit 1 */

 

  /* USER CODE END TIM2_MspInit 1 */

  }

  else if(tim_baseHandle->Instance==TIM3)

  {

  /* USER CODE BEGIN TIM3_MspInit 0 */

 

  /* USER CODE END TIM3_MspInit 0 */

    /* TIM3 clock enable */

    __HAL_RCC_TIM3_CLK_ENABLE();

  /* USER CODE BEGIN TIM3_MspInit 1 */

 

  /* USER CODE END TIM3_MspInit 1 */

  }

  else if(tim_baseHandle->Instance==TIM4)

  {

  /* USER CODE BEGIN TIM4_MspInit 0 */

 

  /* USER CODE END TIM4_MspInit 0 */

    /* TIM4 clock enable */

    __HAL_RCC_TIM4_CLK_ENABLE();

  /* USER CODE BEGIN TIM4_MspInit 1 */

 

  /* USER CODE END TIM4_MspInit 1 */

  }

}

void
HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)

{

 

  GPIO_InitTypeDef GPIO_InitStruct = {0};

  if(timHandle->Instance==TIM3)

  {

  /* USER CODE BEGIN TIM3_MspPostInit 0 */

 

  /* USER CODE END TIM3_MspPostInit 0 */

    __HAL_RCC_GPIOA_CLK_ENABLE();

    __HAL_RCC_GPIOB_CLK_ENABLE();

    /**TIM3 GPIO Configuration    

    PA6    
------> TIM3_CH1

    PA7    
------> TIM3_CH2

    PB0    
------> TIM3_CH3

    PB1    
------> TIM3_CH4 

    */

    GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Speed =
GPIO_SPEED_FREQ_LOW;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 

    GPIO_InitStruct.Pin =
GPIO_PIN_0|GPIO_PIN_1;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Speed =
GPIO_SPEED_FREQ_LOW;

    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 

  /* USER CODE BEGIN TIM3_MspPostInit 1 */

 

  /* USER CODE END TIM3_MspPostInit 1 */

  }

  else if(timHandle->Instance==TIM4)

  {

  /* USER CODE BEGIN TIM4_MspPostInit 0 */

 

  /* USER CODE END TIM4_MspPostInit 0 */

  

    __HAL_RCC_GPIOB_CLK_ENABLE();

    /**TIM4 GPIO Configuration    

    PB6    
------> TIM4_CH1

    PB7    
------> TIM4_CH2

    PB8    
------> TIM4_CH3

    PB9    
------> TIM4_CH4 

    */

    GPIO_InitStruct.Pin =
GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Speed =
GPIO_SPEED_FREQ_LOW;

    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 

  /* USER CODE BEGIN TIM4_MspPostInit 1 */

 

  /* USER CODE END TIM4_MspPostInit 1 */

  }

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值