【stm32】hal库学习笔记--定时器输出PWM波

【stm32】hal库学习笔记--定时器输出PWM波

PWM波原理

输出比较

输入捕获

驱动函数

定时器驱动函数

PWM波驱动函数

定时器基本不使用DMA方式

定时器中断处理通用函数

复制代码
HAL_TIM_IRQHandler

实验一:输出固定占空比PWM波

时钟树配置

PF9 改为tim14CH1
tim14配置

开启tim14全局中断 更改中断优先级

复制代码
  /* USER CODE BEGIN 2 */
  lcd_init();
  HAL_TIM_Base_Start_IT(&htim14);
  HAL_TIM_PWM_Start_IT(&htim14, TIM_CHANNEL_1);
  /* USER CODE END 2 */

输出可变占空比PWM波

编写tim.c程序

复制代码
/* USER CODE BEGIN 0 */
uint8_t pulseChangeDirection = 1;
uint16_t pulseWidth = 50;
/* USER CODE END 0 */

void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim) {
	if (htim->Instance != TIM14) {
		return;
	}
	if (pulseChangeDirection) {
		pulseWidth++;
		if (pulseWidth >= 95) {
			pulseWidth = 95;
			pulseChangeDirection = 0;
		}
	} else {
		pulseWidth--;
		if (pulseWidth <= 5) {
			pulseWidth = 5;
			pulseChangeDirection = 1;
		}
	}
	__HAL_TIM_SET_COMPARE(&htim14, TIM_CHANNEL_1, pulseWidth);
}

实验二:输出比较

定时器配置

测量PWM波的脉宽和周期

时钟树配置
定时器tim14设置


定时器tim9设置

打开tim9中断

程序编写

复制代码
/* USER CODE BEGIN Includes */
#include "lcd.h"
#include "keyled.h"
#include <stdio.h>
/* USER CODE END Includes */

  /* USER CODE BEGIN 2 */
  lcd_init();
  lcd_show_str(0, 0,   16, "Demo10_3:PWM Input", RED);
  lcd_show_str(0, 20,  16, "TIM14 generate PWM on PF9(LED1)", RED);
  lcd_show_str(0, 40,  16, "TIM9 measure PWM on PE5", RED);
  lcd_show_str(0, 60,  16, "Please connect PE5 and PF9 by line", RED);
  lcd_show_str(0, 80,  16, "[1]KeyLeft to decrease pulse width", RED);
  lcd_show_str(0, 100, 16, "[2]KeyRight to increase pulse width", RED);

  HAL_TIM_Base_Start(&htim14);
  HAL_TIM_Base_Start(&htim9);
  HAL_TIM_IC_Start_IT(&htim9, TIM_CHANNEL_1);
  HAL_TIM_IC_Start_IT(&htim9, TIM_CHANNEL_2);
  HAL_TIMEx_PWMN_Start(&htim14, TIM_CHANNEL_1);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  KEYS curKey = ScanPressedKey(KEY_WAIT_ALWAYS);
	  uint32_t CCR = __HAL_TIM_GET_COMPARE(&htim14, TIM_CHANNEL_1);
	  if (curKey == KEY_LEFT) {
		  __HAL_TIM_SET_COMPARE(&htim14, TIM_CHANNEL_1, CCR - 5);
	  } else if (curKey == KEY_RIGHT) {
		  __HAL_TIM_SET_COMPARE(&htim14, TIM_CHANNEL_1, CCR + 5);
	  }
	  HAL_Delay(300);
    /* USER CODE END WHILE */

/* USER CODE BEGIN 4 */
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) {
	uint16_t IC1_Width = __HAL_TIM_GET_COMPARE(&htim9, TIM_CHANNEL_1);
	uint16_t IC2_Pulse = __HAL_TIM_GET_COMPARE(&htim9, TIM_CHANNEL_2);
	if ((IC1_Width == 0) || (IC2_Pulse == 0)) {
		return;
	}
	char str[40];
	sprintf(str, "PWM width = %d", IC1_Width);
	lcd_show_str(0, 120, 16, str, RED);
	sprintf(str, "Pulse width = %d", IC2_Pulse);
	lcd_show_str(0, 140, 16, str, RED);

}
/* USER CODE END 4 */
相关推荐
旅僧11 分钟前
机械臂学习笔记(更新中)
笔记·学习
qingwufeiyang_53024 分钟前
Python学习笔记3-项目实战-AI应用
笔记·学习
智者知已应修善业32 分钟前
【proteus 74160实现模60计数器模41计数器】2024-5-27
驱动开发·经验分享·笔记·硬件架构·proteus·硬件工程
-To be number.wan39 分钟前
计算机组成原理 | 虚拟存储器
学习·计算机组成原理
墨白曦煜40 分钟前
算法实战笔记:空间换时间的黑魔法——单调栈全景解析(十一)
java·笔记·算法
问心无愧051344 分钟前
ctf show web入门157 158
前端·笔记
暖馒1 小时前
WPF-Prism学习入门步骤记录
学习·wpf
张海森-1688201 小时前
库里搜索函数 api接口__grep命令
单片机
MartinYeung51 小时前
[论文学习]透过增强式 Few-Shot Learning 实现高效 PII 从大型语言模型中提取
人工智能·学习·语言模型
闪闪发亮的小星星1 小时前
STK-03-通信卫星方向最常遇到的场景
笔记