打印
[STM32C0]

NUCLEO-C092RC开发板pwm+timer+控制呼吸

[复制链接]
71|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
采用工具进行相关的配置,选择好对应的封装?

这里开启了tim1,tim2对应的通道,同时进行相关参数的配置
根据时钟树的显示,这里面不再对时钟的频率进行修改,采用默认方式。

那么下面看看对应的关键配置,因为tim1是高级计时器,所以参数比较多,但是这里用到的比较有限

同理,tim2是一个普通计时器,只要简单正确配置就可以了
那么看看关键代码:
/* USER CODE BEGIN 2 */
  // Start the PWM channels for both LEDs
  // Note: Use the correct Timer handle and Channel defined by your configuration
  if (HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2) != HAL_OK) // For LED on PC9
  {
    Error_Handler();
  }
  if (HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1) != HAL_OK) // For LED on PA5
  {
    Error_Handler();
  }


  // --- Variables for breathing effect ---
  // Set PWM_MAX_VALUE to your timer's ARR (Auto-Reload Register) value
  // Assuming ARR was set to 999 for both timers in CubeMX for a 0-1000 range
  uint32_t pwm_max_value = 999;
  uint32_t brightness = 0;
  int8_t step = 5; // How much to change brightness each step
  uint8_t delay_ms = 10; // Delay between steps, controls breathing speed


  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
          // --- Fade In ---
              for (brightness = 0; brightness <= pwm_max_value; brightness += step)
              {
                // Clamp brightness to max value in case step overshoots
                if (brightness > pwm_max_value) {
                    brightness = pwm_max_value;
                }

                // Set the PWM duty cycle (Compare value) for both LEDs simultaneously
                __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, brightness); // LED on PC9
                __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, brightness); // LED on PA5

                HAL_Delay(delay_ms); // Adjust delay for breathing speed
              }

               // Ensure brightness is exactly max value after loop
              __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, pwm_max_value);
              __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, pwm_max_value);
              HAL_Delay(delay_ms * 10); // Optional: Pause briefly at full brightness


              // --- Fade Out ---
              // Start slightly below max to avoid issues with unsigned wrap-around if step doesn't divide evenly
              for (brightness = pwm_max_value; brightness > 0; )
              {
                 // Decrement first, then check bounds
                 if (brightness <= step) { // Prevent underflow for unsigned int
                      brightness = 0;
                 } else {
                     brightness -= step;
                 }

                // Set the PWM duty cycle (Compare value) for both LEDs simultaneously
                __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, brightness); // LED on PC9
                __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, brightness); // LED on PA5

                HAL_Delay(delay_ms); // Adjust delay for breathing speed
              }

              // Ensure brightness is exactly 0 after loop
              __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, 0);
              __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 0);
              HAL_Delay(delay_ms * 10); // Optional: Pause briefly when off

              /* USER CODE END 3 */
            }
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */


并且构建成功:
继续新的探索

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

46

主题

317

帖子

0

粉丝