[分享] 【NUCLEO-WB09KE评测】3、PWM控制LD3亮度

sujingliang   2024-9-25 10:47 楼主

目标

        STM3WB09xE有3个通用定时器(TIM2,TIM16,TIM17)

        Each general-purpose timer can be used to generate PWM outputs

        本次测试,利用TIM2实现PWM输出到通道3(PB2),控制LD3亮度。

一、配置

1、时钟配置

2.png

 

2、TIM2

3.png

 

选择CHANNEL3:PWM Generation CH3

Prescaler:63

Period:499

主频:64MHZ

64000000/(64*500)=2000hz

占空比:300/500=60%

3、GPIO

4.png

PB2的修改

二、程序

1、TIM2初始化

static void MX_TIM2_Init(void)
{

  /* USER CODE BEGIN TIM2_Init 0 */

  /* USER CODE END TIM2_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_OC_InitTypeDef sConfigOC = {0};

  /* USER CODE BEGIN TIM2_Init 1 */

  /* USER CODE END TIM2_Init 1 */
  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 63;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 499;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 300;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM2_Init 2 */

  /* USER CODE END TIM2_Init 2 */
  HAL_TIM_MspPostInit(&htim2);

}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(htim->Instance==TIM2)
  {
  /* USER CODE BEGIN TIM2_MspPostInit 0 */

  /* USER CODE END TIM2_MspPostInit 0 */

    __HAL_RCC_GPIOB_CLK_ENABLE();
    /**TIM2 GPIO Configuration
    PB2     ------> TIM2_CH3
    */
    GPIO_InitStruct.Pin = GPIO_PIN_2;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF3_TIM2;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /* USER CODE BEGIN TIM2_MspPostInit 1 */

  /* USER CODE END TIM2_MspPostInit 1 */
  }

}

2、修改TIM2 CH3占空比

//设置 TIM 通道 3 的占空比
//compare:比较值
void TIM_SetTIM2Compare3(uint32_t compare)
{
	//TIM2->CCR3=compare;
	__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, compare); // compare为新的占空比值

}

3、main中增加HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);//开启 PWM 通道 3

  MX_TIM2_Init();
  HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);//开启 PWM 通道 3

4、main while(1)中增加

调整占空比参数,改变占空比调节LD3亮度

  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		HAL_Delay(10);
		if(dir)led3pwmval++; //dir==1 led0pwmval 递增
		else led3pwmval--; //dir==0 led0pwmval 递减
		if(led3pwmval>300)dir=0; //led0pwmval 到达 300 后,方向为递减
		if(led3pwmval==0)dir=1; //led0pwmval 递减到 0 后,方向改为递增
		printf("led3pwmval:%d\r\n",led3pwmval);
		TIM_SetTIM2Compare3(led3pwmval);
		
  }

三、运行效果

WeChat_20240925104528

 

 

 

回复评论 (2)

STM3WB09xE有3个通用定时器(TIM2,TIM16,TIM17)这个应该和普通芯片的控制模式是一样的,不过相对来说还是定时器比较少

在爱好的道路上不断前进,在生活的迷雾中播撒光引
点赞  2024-9-25 13:13

赞!

点赞  2024-9-25 19:01
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复