STM32F103C8T6使用TIM3和TIM4实现呼吸灯

1、背景

使用TIM3和TIM4,分别输出一个PWM波形,PWM的占空比随时间变化,去驱动你外接的一个LED以及最小开发板上已焊接的LED(固定接在 PC13 GPIO端口),实现2个 LED呼吸灯的效果。

2、什么是PWD

PWM一般指脉冲宽度调制。

PWM是Pulse Width Modulation 的缩写,中文意思就是脉冲宽度调制,简称脉宽调制。它是利用微处理器的数字输出来对模拟电路进行控制的一种非常有效的技术,其控制简单、灵活和动态响应好等优点而成为电力电子技术最广泛应用的控制方式。

其应用领域包括测量,通信,功率控制与变换,电动机控制、伺服控制、调光、开关电源,甚至某些音频放大器,因此学习PWM具有十分重要的现实意义。

扩展资料

PWM应用包括焊接、家用电器、电机驱动器、个人电子产品、逆变器、电动车、不间断电源、太阳能发电、音频放大器和汽车加热器。一般而言,在使用开关电源且无需复杂高级拓扑结构的情况下,建议使用简单的通用PWM控制器。

在许多应用中使用合理的PWM控制器有助于缩短开发时间,而且还可以借助一系列可靠的设计来增强对电源设计的信心。PWM的最后一个优点是它可以用于多个设计,这样采购团队便可大批量采购元件。

通用PWM控制器的生产工艺从双极技术发展到如今的Bi-CMOS技术,其关键设计参数得到了大幅改进。

3、创建工程

①、选择自己所用的芯片

②、配置RCC

在这里插入图片描述

③、配置SYS

在这里插入图片描述

④、设置TIM3和TIM4

① 选Internal Clock(内部时钟)
② 通道1选择:PWM Generation CH1(PWM输出通道1)
③ Prtscaler (定时器分频系数) : 71
④ Counter Mode(计数模式):Up(向上计数模式)
⑤ Counter Period(自动重装载值) : 500
⑥ CKD(时钟分频因子) :No Division (不分频 )

设置TIM3
在这里插入图片描述
设置TIM4,与设置TIM3的相同,这里就略过了

5、设置时钟

在这里插入图片描述

6、项目设置

在这里插入图片描述
在这里插入图片描述

4、代码编写

①、占空比变量

在main.c文件中编写占空比代码。

uint16_t pwm=0; 

②、TIM3和TIM4代码编写

在main函数中添加如下代码

HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1); 
HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_1); 

③、在while循环中添加代码

//每隔50毫秒,占空比加10,如果超过500(也就是PWM周期),自动变成0.
	  while (pwm< 500)
	  {
		  pwm++;
		  __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_1, pwm);  
          __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_1, pwm);  			
		  HAL_Delay(1);
	  }
	  while (pwm)
	  {
		  pwm--;
		  __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_1, pwm);    
       __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_1, pwm);  
			
		  HAL_Delay(1);
	  }
	  HAL_Delay(200);

所以整个main.c文件为

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 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 */
uint16_t pwm=0; 
/* 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 */

  /* 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();
  MX_TIM4_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */
	HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1); 
	HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_1); 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
		while (pwm< 500)
	  {
		  pwm++;
		  __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_1, pwm);  
    __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_1, pwm);  			
		  HAL_Delay(1);
	  }
	  while (pwm)
	  {
		  pwm--;
		  __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_1, pwm);    
       __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_1, pwm);  
			
		  HAL_Delay(1);
	  }
	  HAL_Delay(200);

    /* 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_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 */

5、接线

3V3 —> 3V3
GND —> GND
RXD —> A9
TXD —> A10
LED灯短脚 —>A6
LED灯长脚 —> 3V3
PB6 —> PC13

在这里插入图片描述

6、结果

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值