首页 > 其他分享 >stm32f429i-Disc PWM生成 + 输入捕获测频率与占空比

stm32f429i-Disc PWM生成 + 输入捕获测频率与占空比

时间:2024-07-11 13:31:19浏览次数:16  
标签:TIM1 void NVIC TIM Disc InitStructure 占空比 GPIO PWM

产生PWM

void My_PWM_Init(void){
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_TIM3);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 推挽复用输出
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
	GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	

	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_Prescaler = 900-1;
	TIM_TimeBaseInitStructure.TIM_Period = 100-1;
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
	
	
	TIM_InternalClockConfig(TIM3);
	TIM_OCInitTypeDef TIM_OCInitStructure;
	TIM_OCStructInit(&TIM_OCInitStructure);
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_Pulse = 99;
	TIM_OC1Init(TIM3, &TIM_OCInitStructure);
	
	TIM_Cmd(TIM3,ENABLE);
}
	
void PWM_SetCompare1(uint16_t plause){
	TIM_SetCompare1(TIM3,plause);
}

void PWM_SetPrescaler(uint16_t prescaler){
	TIM_PrescalerConfig(TIM3, prescaler, TIM_PSCReloadMode_Immediate);
}

输入捕获测量频率和占空比

#include "stm32f4xx.h"                  // Device header

uint8_t first = 1, down = 0, finish = 0;
uint32_t first_cnt = 0, all_cnt = 0, high_cnt = 0;
uint8_t overflow = 0;


void Input_Capture_Init(void){
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
	GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	TIM_InternalClockConfig(TIM1);
	TIM_TimeBaseInitTypeDef TIM_TimBaseInitStructure;
	TIM_TimBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimBaseInitStructure.TIM_Prescaler = 180-1;
	TIM_TimBaseInitStructure.TIM_Period = 65536-1;
	TIM_TimBaseInitStructure.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(TIM1, &TIM_TimBaseInitStructure);
	
	TIM_ICInitTypeDef TIM_ICInitStructure;
	TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
	TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
	TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
	TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
	TIM_ICInitStructure.TIM_ICFilter = 0xF;
	TIM_ICInit(TIM1, &TIM_ICInitStructure);

	TIM_ITConfig(TIM1, TIM_IT_CC1 | TIM_IT_Update, ENABLE);	

	NVIC_InitTypeDef NVIC_InitStructure; 
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);		
    NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn; 	
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;	 
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;	
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;	 
	NVIC_Init(&NVIC_InitStructure);
	
	TIM_Cmd(TIM1, ENABLE);

}

void TIM1_CC_IRQHandler(void)
{
	if(down == 1){
		high_cnt = TIM_GetCapture1(TIM1) - first_cnt + overflow * 65536;
		down = 0;
		TIM_OC1PolarityConfig(TIM1,TIM_ICPolarity_Rising);	// 继续完成上升降沿捕获,测频率
	}
	else{
		if(first == 1){
			TIM_OC1PolarityConfig(TIM1,TIM_ICPolarity_Falling);	// 配置为下降沿捕获,测占空比
			down = 1;
			first_cnt = TIM_GetCapture1(TIM1);
			first = 0;
		}else{
			all_cnt = TIM_GetCapture1(TIM1) - first_cnt + overflow * 65536;
			first = 1;
			finish = 1;
		}
	}
	overflow = 0;
	TIM_ClearITPendingBit(TIM1, TIM_IT_CC1); //清除中断标志
}

void TIM1_UP_TIM10_IRQHandler(void){
	overflow ++;
	TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
}

uint8_t isFinished(void){
	return finish;
}

uint32_t Get_ALLN(void){
	return all_cnt;
}

uint32_t Get_HighN(void){
	return high_cnt;
}

main()中while循环语句:
注意,stm32f429i-Disc用于生成基准频率的TIM1定时器频率为180MHZ,在经过TIM_Prescaler = 180-1 的分频后基准频率变为1MHZ,此为Frequency中1000000的来源。

	while(1){
		uint32_t Frequency = 0;
		uint32_t Duty = 0;
		if(isFinished()){
			uint32_t ALLN = Get_ALLN();
			uint32_t HIGHN = Get_HighN();
			Frequency = (1000000)/(uint32_t)ALLN;
			Duty = Get_HighN()*100 / (uint16_t)Get_ALLN();
		}
	}

标签:TIM1,void,NVIC,TIM,Disc,InitStructure,占空比,GPIO,PWM
From: https://blog.csdn.net/qq_39683145/article/details/140349157

相关文章

  • 腾讯云COS插件入驻Discuz!x
    Discuz!Discuz!平台,由一群高擎互联网人在倾情支持,他们来自于腾讯Discuz!创业团队成员以及优秀的开发者。在中国互联网风云变迁中,Discuz!20多年间为300万企业及站长赋能,秉承“开放、连接、共赢”的精神,倡导与生态伙伴及开发者共建健康可持续的Discuz!品牌合作模式,突破以往需求......
  • FD-Align: Feature Discrimination Alignment for Fine-tuning Pre-Trained Models in
    文章汇总动机CLIP注意图更关注背景,全面微调后的CLIP关注在了非显著特征的地方。FD-Align注意图倾向于关注标签相关的信息。解决办法总损失有两个损失函数组成:对VisualEncoder进行微调冻结CLIP的文本编码器g......
  • Linux驱动之利用STM32、设备树、pwm子系统实现风扇的分级调控
    Linux驱动之利用STM32、设备树、pwm子系统实现风扇的分级调控系统:Linux5.10.61开发板:STM32mp157a硬件:风扇一、首先我们需要对PWM和定时器(TIM)的联系简单的做一下了解,具体详细的PWM原理可见PWM原理PWM频率与占空比详解。​PWM(脉宽调制)和TIM(定时器)在嵌入式系统中有着......
  • 零基础STM32单片机编程入门(八)定时器PWM输入实战含源码视频
    文章目录一.概要二.PWM输入框架图三.CubeMX配置一个PWM输入例程1.硬件准备2.创建工程3.调试四.CubeMX工程源代码下载五.讲解视频链接地址六.小结一.概要脉冲宽度调制(PWM),是英文“PulseWidthModulation”的缩写,简称脉宽调制,是利用单片机数字输出(1或0)来对外部模拟......
  • TMS320F28377D学习日志:day4基于ePWM的呼吸灯
    文章目录前言一、TMS320F28377D的ePWM介绍1.1PWM的复用引脚1.2PWM模块的组成1.3TB模块1.4CC模块1.5AQ模块二、程序介绍前言28377D具有24路PWM输出引脚,14路高分辨率的HRPWM。ePWM是对PWM的加强型。其中一个ePWM通道有2个PWM输出引脚EPWMxA、EPWMxB,可以用于配置......
  • WS2812b彩灯(DMA+PWM)
    一.WS2812B简介        WS2812B是一种数字可编程的LED灯条,可以使用单个数据线进行通讯控制LED灯的颜色和亮度。每个WS2812B都有一个唯一的地址,可以通过单个数据线进行级联。二.WS2812B参数简介 三.WS2812B数据通讯简介 1.级联电路2.数据传输    ......
  • 离散傅里叶变换(Discrete Fourier Transform,DFT)
    离散傅里叶变换(DiscreteFourierTransform,DFT)是信号分析中的一种基本方法,它将离散时序信号从时间域变换到频率域,是傅里叶变换在时域和频域都呈现离散的形式。以下是关于离散傅里叶变换的详细介绍:一、定义与物理意义定义:离散傅里叶变换是指将离散时间信号(即时间域上的离......
  • 基于STM32F103C8T6的同步电机驱动-PWM驱动代码以及SVPWM的实现
    基于STM32F103C8T6的同步电机驱动-PWM驱动代码以及SVPWM的实现本系列文章:基于STM32F103C8T6的同步电机驱动-CubeMX配置与IQmath调用基于STM32F103C8T6的同步电机驱动-PWM驱动代码以及SVPWM的实现一、PWM驱动代码的编写在系列文章的第一篇中已经对主定时器TIM1进行了P......
  • STM32-PWM配置流程及作用
    STM32F407ZET6单片机配置PWM(脉冲宽度调制)PWM配置流程STM32F407ZET6单片机配置PWM的流程通常包括以下几个步骤:时钟使能:首先,需要使能定时器和相关IO口的时钟。这通常通过调用RCC(ResetandClockControl)相关的函数来实现,比如RCC_APB1PeriphClockCmd()用于使能APB1总线上的定时......
  • 【PWM太阳能控制器调光方案】5V升24V 6A大电流无频闪PWM调光升压恒流LED驱动芯片FP720
    目前led灯带产品应用越来越广泛,在节能环保的社会快速发展驱动下,太阳能照明产品越来越多。但是现有技术中,有些空间狭小,又长又窄的环境空间不适用于现有的太阳能灯使用,现有技术中的太阳能灯照明效果不佳且不能够实现智能化照明;因此迫切地需要重新设计一款新的太阳能led灯带以......