STM32之PWM

PWM

PWM,英文名Pulse Width Modulation,是脉冲宽度调制缩写,它是通过对一系列脉冲的宽度进行调制,等效出所需要的波形(包含形状以及幅值),对模拟信号电平进行数字编码,也就是说通过调节占空比的变化来调节信号、能量等的变化,占空比就是指在一个周期内,信号处于有效电平的时间占据整个信号周期的百分比。

  • PWM是脉冲宽度调制。

  • 有效电平持续的时间占整个周期的百分比称为占空比。

PWM的输出模式

可以修改TIMx_CCRx寄存器的值来修改占空比。

PWM模式1

在向上计数时,一旦CNT

STM32之PWM_第1张图片

在向下计数时,一旦CNT>CCRx 时输出为无效电平,否则为有效电平。

STM32之PWM_第2张图片

PWM模式2

在向上计数时,一旦CNT

STM32之PWM_第3张图片

在向下计数时,一旦CNT>CCRx 时输出为有效电平,否则为无效电平。

STM32之PWM_第4张图片

STM32F103C8T6的PWM资源

  • 高级定时器(TIM1):7路的PWM

  • 通用定时器(TIM2~TIM4):每个定时器各4路的PWM

PWM的周期和频率

周期是频率的倒数,如驱动sg90舵机时PWM信号的频率大概为50HZ,即周期为20ms(Tout,也就是定时时间)

STM32之PWM_第5张图片

PWM实现呼吸灯

利用调节PWM的占空比大小来实现呼吸灯,PWM周期为0.5ms即频率为2000HZ

使用STM32CubeMX创建工程

配置SYS

STM32之PWM_第6张图片

配置RCC

STM32之PWM_第7张图片

STM32之PWM_第8张图片

配置PWM

翻看使用手册,查看LED1使用哪个PWM

STM32之PWM_第9张图片

选择定时器4,打开时钟来源选择中间时钟,选择通道三输出PWM

STM32之PWM_第10张图片

配置定时方式,定时时间为0.5ms(即PSC为71,ARR为499),PWM的相关信息(选择PWM模式1)

  • 由于需要手动改变PWM的占空比,所以设置为0

  • 由于点亮LED1的有效电平为低电平,所以PWM的极性选择LOW

STM32之PWM_第11张图片

STM32之PWM_第12张图片

配置工程名称、工程路径

STM32之PWM_第13张图片

选择固件库

STM32之PWM_第14张图片

生成工程

STM32之PWM_第15张图片

main函数编写

  1. 在main函数中打开某个PWM(HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_3);)

  1. 修改某个PWM的占空比,即修改TIMx_CCRx寄存器的值(__HAL_TIM_SetCompare(&htim4,TIM_CHANNEL_3,pwmVal);)

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

    uint16_t pwmVal = 0;  //占空比大小(CCRx的大小)
    uint8_t direction = 1;  //呼吸灯方向: 1. 越来越亮   2. 越来越暗
    
  /* 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_TIM4_Init();
  /* USER CODE BEGIN 2 */

    //初始化之后,打开引脚PB8的PWM,即定时器4通道三的PWM
    HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_3);

  /* USER CODE END 2 */

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

    /* USER CODE BEGIN 3 */
        
        HAL_Delay(3);  //控制呼吸灯呼吸速率
        //控制占空比大小,即修改CCRx的大小
        if(direction){
            pwmVal++;
        }else{
            pwmVal--;
        }
        //设置了ARR为499,因此每计数500为一个周期
        if(pwmVal > 500){ 
            direction = 0;  //改变呼吸灯方向
        }else if(pwmVal == 0){
            direction = 1;  //改变呼吸灯方向
        }
        //修改定时器4通道三的PWM的占空比
        __HAL_TIM_SetCompare(&htim4,TIM_CHANNEL_3,pwmVal);
        
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

你可能感兴趣的:(STM32,stm32,单片机,嵌入式硬件)