[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 */
相关推荐
12.=0.23 分钟前
【stm32_9】RTOS的概念、种类对比,FressRTOS的概述、FressRTOS的源码结构、FressRTOS的源码移植
stm32·单片机·嵌入式硬件
Y多了个想法38 分钟前
RK3576 android14 I2C总线,硬件I2C 与 GPIO模拟I2C 比对
经验分享·嵌入式硬件·i2c·rk·rk3576
小+不通文墨1 小时前
树莓派4b-wiringpi库的安装和使用
驱动开发·经验分享·笔记·嵌入式硬件·学习
小麦嵌入式1 小时前
FPGA入门(三):3-8 译码器 仿真波形解读
stm32·单片机·嵌入式硬件·mcu·fpga开发·硬件工程
Elihuss2 小时前
关于RK3506 的MCU软复位后跑不起问题
linux·单片机·嵌入式硬件
fengfuyao9853 小时前
GRBL 1.1 移植到 STM32 (HAL库)
stm32·单片机·嵌入式硬件
biyezuopinvip3 小时前
基于STC89C51单片机的多波形信号发生器设计与Proteus仿真
单片机·proteus·课程设计·proteus仿真·基于stc89c51单片机的·多波形·信号发生器设计
无人装备硬件开发爱好者3 小时前
STM32G474 驱动 1.54 寸三色电子墨水屏实现贪吃蛇游戏完整指南
stm32·嵌入式硬件·游戏
山木嵌入式3 小时前
FreeRTOS任务创建全解析:动态/静态创建+实战案例+参数深度剖析
stm32·freertos
项目題供诗3 小时前
STM32-定时器定时中断&定时器外部时钟(十一)
stm32·单片机·嵌入式硬件