[HAL库学习之路]8.PWM输出

记录自己的STM32的HAL库的学习之路,不定期更新…

一、Pinout

[HAL库学习之路]8.PWM输出_第1张图片

二、Clock Configuration

[HAL库学习之路]8.PWM输出_第2张图片

三、Configuration

[HAL库学习之路]8.PWM输出_第3张图片

四、Generate Code

[HAL库学习之路]8.PWM输出_第4张图片

代码

int main(void)
{

  /* USER CODE BEGIN 1 */
    unsigned int pwmVal = 0;
    unsigned char dir = 1;
  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_TIM3_Init();

  /* USER CODE BEGIN 2 */
    HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_4);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
        HAL_Delay(10);
        if(dir)
            pwmVal++;
        else
            pwmVal--;
        if(pwmVal > 300)
            dir = 0;
        if(pwmVal == 0)
            dir = 1;
        __HAL_TIM_SetCompare(&htim3,TIM_CHANNEL_4,pwmVal);
  }
  /* USER CODE END 3 */

}

你可能感兴趣的:(HAL库学习之路,库,stm32,HAL库)