【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 小时前
iOS 知识点 - IAP 是怎样的?
笔记
tingshuo291721 小时前
D006 【模板】并查集
笔记
tingshuo29172 天前
S001 【模板】从前缀函数到KMP应用 字符串匹配 字符串周期
笔记
西岸行者7 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
starlaky7 天前
Django入门笔记
笔记·django
勇气要爆发7 天前
吴恩达《LangChain LLM 应用开发精读笔记》1-Introduction_介绍
笔记·langchain·吴恩达
悠哉悠哉愿意7 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
勇气要爆发7 天前
吴恩达《LangChain LLM 应用开发精读笔记》2-Models, Prompts and Parsers 模型、提示和解析器
android·笔记·langchain
别催小唐敲代码7 天前
嵌入式学习路线
学习
Lester_11017 天前
STM32霍尔传感器输入口设置为复用功能输入口时,还能用GPIO函数直接读取IO的状态吗
stm32·单片机·嵌入式硬件·电机控制