[STM32 和 PWM 输出 结合 proteus 仿真]

PWM 是脉冲宽度调制的缩写,那么他就是 通过调节占空比的变化来调节能量,信号的变化

占空比 就是脉冲处于较高电压的时间占整个脉冲周期的百分比

如图 高电平 是T1

一个脉冲周期所占的时间周期为T

电路图的布置 示波器有4个通道 我们选择一个就好

这个是示波器,由于是仿真 我们通过PWM 来看 不是很清楚 借助示波器更好的帮助我们来观察

下面进行cubemx配置

1 先进行定时器的配置 TIM2 (因为我们前面选择了 PA0 )

2 再选择 P G C

代码 fun.c

cs 复制代码
#include "bsp_system.h"

#define PWM_MAX_VALUE 99U

void led_show(void)
{
     uint8_t pwm_direction = 0;
     uint8_t pwm_started = 0;
     uint16_t pwm_num = 0;

    if (!pwm_started)
    {
        HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
        pwm_started = 1;
    }

    if (pwm_direction)
    {
        if (pwm_num > 0)
        {
            pwm_num--;
        }
        else
        {
            pwm_direction = 0;
        }
    }
    else
    {
        if (pwm_num < PWM_MAX_VALUE)
        {
            pwm_num++;
        }
        else
        {
            pwm_direction = 1;
        }
    }

    __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, pwm_num);
    OLED_ShowNum(1, 2, pwm_num, 4);
    HAL_Delay(1);
}

void main_pros(void)
{
    led_show();
}

main函数

cs 复制代码
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2026 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 "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "bsp_system.h"
/* 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 */
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_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_TIM2_Init();
  /* USER CODE BEGIN 2 */
  OLED_Init();

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */

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

    /* USER CODE BEGIN 3 */
  }
  /* 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_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  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_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != 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 */
相关推荐
lhbweilai9 小时前
STM32 ADC模拟数字转换(hal库)
stm32·单片机·嵌入式硬件
坏柠9 小时前
通信协议入门:单片机怎样把一个数字发送出去?
单片机·嵌入式硬件
小僧景贤11 小时前
STM32 工程文件深度解析(Keil‑MDK + STM32CubeIDE)
stm32·单片机·stm32cubeide
国科安芯11 小时前
低轨卫星姿态与轨道控制系统中高可靠MCU的选型研究——基于AS32S601的抗辐照性能试验数据分析
分布式·科技·单片机·嵌入式硬件·系统架构
zcmodeltech14 小时前
工业生产线沙盘模型多段灯带时序控制系统设计:基于STM32与Modbus RTU的连铸-热轧-镀锌全流程联动方案
stm32·单片机·嵌入式硬件·物联网·制造·嵌入式实时数据库·多分类
小僧景贤16 小时前
STM32F407 时钟系统超详解
stm32·单片机·嵌入式硬件
西城微科方案开发16 小时前
胎压计方案开发
单片机·嵌入式硬件
JckOLF04Z16 小时前
自动开机调用迅雷下载数据库备份,完成后自动关机
数据库·单片机·嵌入式硬件
国科安芯17 小时前
卫星电推进系统阀门驱动控制中的宽温域高可靠MCU应用研究——基于AS32S601电气特性与辐照试验数据的工程分析
单片机·嵌入式硬件·fpga开发·云计算·信息与通信·卫星·抗辐射芯片
210Brian18 小时前
STM32学习笔记(四)OLED显示屏与Keil调试
笔记·stm32·学习