【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 */
相关推荐
吃好睡好便好18 小时前
汪国真的诗歌《假如你不够快乐》摘录
学习
KKei163819 小时前
Flutter for OpenHarmony学习目标追踪应用技术文章
学习·flutter·华为·harmonyos
搁浅小泽19 小时前
万用表测试电子元器件
单片机·嵌入式硬件·可靠性工程师
白小沫19 小时前
解决 TortoiseSVN 文件夹不显示图标的问题(Windows 10/11 通用)
windows·经验分享·笔记
_李小白19 小时前
【Android车载学习笔记】第一天:Android Automotive OS介绍
android·笔记
静小谢20 小时前
sql笔记
spring boot·笔记·sql·mybatis
不会编程的懒洋洋20 小时前
VisionPro 中 卡尺 CogCaliperTool
图像处理·人工智能·笔记·计算机视觉·visionpro·cogcalipertool
hans汉斯20 小时前
力学研究|半主动变刚度吸振器设计及减振机理研究
学习·力学
aini_lovee20 小时前
STM32 上实现 SD 卡读取 JPEG 解码 TFT 显示
开发语言·stm32
你刷碗20 小时前
嵌入式UART printf 数据处理方法
c语言·单片机·嵌入式硬件·arm