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

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-03-10 07:12:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-10 07:12:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-10 07:12:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-10 07:12:02       20 阅读

热门阅读

  1. 微信小程序常用标签

    2024-03-10 07:12:02       30 阅读
  2. Rust 读写csv文件

    2024-03-10 07:12:02       24 阅读
  3. 计算机网络-网络应用服务器(四)

    2024-03-10 07:12:02       23 阅读
  4. 达梦审计功能

    2024-03-10 07:12:02       24 阅读
  5. Basis运维日常检查清单- Checklist

    2024-03-10 07:12:02       17 阅读
  6. 面向对象——设计模式

    2024-03-10 07:12:02       25 阅读
  7. CentOS 7.9 安装 openGauss 2.1.0 之剥茧抽丝

    2024-03-10 07:12:02       18 阅读
  8. webpack5基础--12_开发服务器&自动化

    2024-03-10 07:12:02       22 阅读
  9. [BUG]Windows安装WSL报错

    2024-03-10 07:12:02       24 阅读