STM32F429 >> 14. TIM_(二)_高级定时器 (Code)

44 篇文章 8 订阅
34 篇文章 8 订阅

基本定时器输出PWM 信号

在该例程中,我将PWM 信号输出管脚连接至LED 管脚,以此观察PWM 信号输出是否正常。
注意:若要对LED 管脚进行初始化配置,对应LED 不能设置为熄灭,否则PWM 信号不能点亮LED

bsp_tim_advanced.h

/**
  ******************************************************************************
  * @file    bsp_tim_advanced.h
  * @author  Waao
  * @version V1.0.0
  * @date    30-Jan-2019
  * @brief   This file contains some board support package's definitions for the advanced TIM.
  *            
  ******************************************************************************
  * @attention
  *
  * None
	*
  ******************************************************************************
  */

#ifndef __BSP_TIM_ADVANCED_H_
#define	__BSP_TIM_ADVANCED_H_

#include <stm32f4xx_tim.h>



extern u16 Channel_Pulse;

//============== TIM ================
#define Specified_TIM_							TIM8
#define TIM_CLK											RCC_APB2Periph_TIM8

//============== GPIO ================
#define TIM_OCPWM_GPIO_PORT					GPIOC
#define TIM_OCPWM_GPIO_PIN					GPIO_Pin_6
#define TIM_OCPWM_GPIO_CLK					RCC_AHB1Periph_GPIOC
#define TIM_OCPWM_GPIO_PINSOURCE		GPIO_PinSource6
#define TIM_OCPWM_GPIO_AF						GPIO_AF_TIM8

#define TIM_OCNPWM_GPIO_PORT				GPIOA
#define TIM_OCNPWM_GPIO_PIN					GPIO_Pin_5
#define TIM_OCNPWM_GPIO_CLK					RCC_AHB1Periph_GPIOA
#define TIM_OCNPWM_GPIO_PINSOURCE		GPIO_PinSource5
#define TIM_OCNPWM_GPIO_AF					GPIO_AF_TIM8

#define TIM_BKIN_GPIO_PORT					GPIOA
#define TIM_BKIN_GPIO_PIN						GPIO_Pin_6
#define TIM_BKIN_GPIO_CLK						RCC_AHB1Periph_GPIOA
#define TIM_BKIN_GPIO_PINSOURCE			GPIO_PinSource6
#define TIM_BKIN_GPIO_AF						GPIO_AF_TIM8


void TIM_GPIO_Config(void);
void TIM_Config(void);


#endif

bsp_tim_advanced.c

/**
  ******************************************************************************
  * @file    bsp_tim_advanced.c
  * @author  Waao
  * @version V1.0.0
  * @date    30-Jan-2019
  * @brief   This file contains some board support package's functions for the TIM.
  *            
  ******************************************************************************
  * @attention
  *
  * None
	*
  ******************************************************************************
  */

#include <bsp_tim_advanced.h>


u16 Channel_Pulse = 130;

/**
  * @brief  Initialize the gpio.
  * @param  None  
  * @retval None
  */
void TIM_GPIO_Config(void)
{
	GPIO_InitTypeDef OCPWM_GPIO_InitStructure;
	GPIO_InitTypeDef OCNPWM_GPIO_InitStructure;
	GPIO_InitTypeDef BKIN_GPIO_InitStructure;
	
	//============= OCPWM ===============
	RCC_AHB1PeriphClockCmd(TIM_OCPWM_GPIO_CLK, ENABLE);
	
	OCPWM_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	OCPWM_GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	OCPWM_GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	OCPWM_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	OCPWM_GPIO_InitStructure.GPIO_Pin = TIM_OCPWM_GPIO_PIN;
	
	GPIO_PinAFConfig(TIM_OCPWM_GPIO_PORT, TIM_OCPWM_GPIO_PINSOURCE, TIM_OCPWM_GPIO_AF);
	
	GPIO_Init(TIM_OCPWM_GPIO_PORT, &OCPWM_GPIO_InitStructure);
	
	//============= OCNPWM ===============
	RCC_AHB1PeriphClockCmd(TIM_OCNPWM_GPIO_CLK, ENABLE);
	
	OCNPWM_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	OCNPWM_GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	OCNPWM_GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	OCNPWM_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	OCNPWM_GPIO_InitStructure.GPIO_Pin = TIM_OCNPWM_GPIO_PIN;
	
	GPIO_PinAFConfig(TIM_OCNPWM_GPIO_PORT, TIM_OCNPWM_GPIO_PINSOURCE, TIM_OCNPWM_GPIO_AF);
	
	GPIO_Init(TIM_OCNPWM_GPIO_PORT, &OCNPWM_GPIO_InitStructure);
	
	//============= BKIN ===============
	RCC_AHB1PeriphClockCmd(TIM_BKIN_GPIO_CLK, ENABLE);
	
	BKIN_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	BKIN_GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	BKIN_GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	BKIN_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	BKIN_GPIO_InitStructure.GPIO_Pin = TIM_BKIN_GPIO_PIN;
	
	GPIO_PinAFConfig(TIM_BKIN_GPIO_PORT, TIM_BKIN_GPIO_PINSOURCE, TIM_BKIN_GPIO_AF);
	
	GPIO_Init(TIM_BKIN_GPIO_PORT, &BKIN_GPIO_InitStructure);
}



/**
  * @brief  Initialize the advanced TIM.
  * @param  None  
  * @retval None
  */
void TIM_Config(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBase_InitStructure;
	TIM_OCInitTypeDef TIM_OC_InitStructure;
	TIM_BDTRInitTypeDef TIM_BDTR_InitStructure;
	
	RCC_APB2PeriphClockCmd(TIM_CLK, ENABLE);
	
	//=================== TIM_TimeBase_InitStructure =================
	TIM_TimeBase_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBase_InitStructure.TIM_Prescaler = 1800-1;
	TIM_TimeBase_InitStructure.TIM_Period = 1024-1;
	TIM_TimeBase_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBase_InitStructure.TIM_RepetitionCounter = 0;
	
	TIM_TimeBaseInit(Specified_TIM_, &TIM_TimeBase_InitStructure);
	
	//====================== TIM_OC_InitStructure ====================
	TIM_OC_InitStructure.TIM_OCMode = TIM_OCMode_PWM1;
	TIM_OC_InitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OC_InitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
	TIM_OC_InitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
	TIM_OC_InitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
	TIM_OC_InitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
	TIM_OC_InitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
	TIM_OC_InitStructure.TIM_Pulse = Channel_Pulse;
	
	TIM_OC1Init(Specified_TIM_, &TIM_OC_InitStructure);
	TIM_OC1PreloadConfig(Specified_TIM_, TIM_OCPreload_Enable);
	
	//===================== TIM_BDTR_InitStructure ===================
	TIM_BDTR_InitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
	TIM_BDTR_InitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
	TIM_BDTR_InitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
	TIM_BDTR_InitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
	TIM_BDTR_InitStructure.TIM_Break = TIM_Break_Enable;
	TIM_BDTR_InitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;
	TIM_BDTR_InitStructure.TIM_DeadTime = 11;
	
	TIM_BDTRConfig(Specified_TIM_, &TIM_BDTR_InitStructure);
	
	
	TIM_Cmd(Specified_TIM_, ENABLE);
	TIM_CtrlPWMOutputs(Specified_TIM_, ENABLE);
}

main.c

#include <bsp_tim_advanced.h>
#include <bsp_key.h>
#include <bsp_systick.h>


int main(void)
{
	u32 TEMP = 10;
	
	TIM_GPIO_Config();
	TIM_Config();
	SysTick_Init();
	
	while(1)
	{
		Delay(1200);
		Channel_Pulse -= TEMP;
		TIM_SetCompare1(Specified_TIM_, Channel_Pulse);
		
		if(Channel_Pulse <= 0)
		{
			while(Channel_Pulse <= 1200)
			{
				Delay(1200);
				Channel_Pulse += TEMP;
				TIM_SetCompare1(Specified_TIM_, Channel_Pulse);
			}
		}
	}
}

通用计时器输出PWM 信号(按键可控PWM 占空比)

在该例程中,我将PWM 信号输出管脚连接至LED 管脚,以此观察PWM 信号输出是否正常。
注意:若要对LED 管脚进行初始化配置,对应LED 不能设置为熄灭,否则PWM 信号不能点亮LED

tim.h

/**
  ******************************************************************************
  * @file    tim.h
  * @author  Waao
  * @version V1.0.0
  * @date    1-Feb-2019
  * @brief   This file contains some board support package's definition for the TIM.
  *            
  ******************************************************************************
  * @attention
  *
  * None
	*
  ******************************************************************************
  */
#ifndef __TIM_H_
#define __TIM_H_


#include <stm32f4xx_tim.h>


extern __IO u32 Channel_Pulse;


//============= General_TIM =============
#define Specified_General_TIM_          			TIM2
#define General_TIM_CLK          							RCC_APB1Periph_TIM2


//================ GPIO =================
#define General_TIM_OCPWM_GPIO_PORT						GPIOA
#define General_TIM_OCPWM_GPIO_PIN						GPIO_Pin_5
#define General_TIM_OCPWM_GPIO_PINSOURCE			GPIO_PinSource5
#define General_TIM_OCPWM_GPIO_AF							GPIO_AF_TIM2
#define General_TIM_OCPWM_GPIO_CLK						RCC_AHB1Periph_GPIOA



void All_TIM_Config(void);

#endif

tim.c

/**
  ******************************************************************************
  * @file    tim.c
  * @author  Waao
  * @version V1.0.0
  * @date    1-Feb-2019
  * @brief   This file contains some board support package's functions for the TIM.
  *            
  ******************************************************************************
  * @attention
  *
  * None
	*
  ******************************************************************************
  */

#include "tim.h"


__IO u32 Channel_Pulse = 1300;

/**
  * @brief  Initialize the General_TIM GPIO.
  * @param  None  
  * @retval None
  */
void General_GPIO_Config(void)
{
	GPIO_InitTypeDef GENERAL_GPIO_InitStructure;
	
	RCC_AHB1PeriphClockCmd(General_TIM_OCPWM_GPIO_CLK, ENABLE);
	
	GENERAL_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GENERAL_GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GENERAL_GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GENERAL_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GENERAL_GPIO_InitStructure.GPIO_Pin = General_TIM_OCPWM_GPIO_PIN;
	
	GPIO_PinAFConfig(General_TIM_OCPWM_GPIO_PORT, General_TIM_OCPWM_GPIO_PINSOURCE, General_TIM_OCPWM_GPIO_AF);
	
	GPIO_Init(General_TIM_OCPWM_GPIO_PORT, &GENERAL_GPIO_InitStructure);
}


/**
  * @brief  Initialize the General_TIM.
  * @param  None  
  * @retval None
  */
void General_TIM_Config(void)
{
	TIM_TimeBaseInitTypeDef General_TIM_TimeBase_InitStructure;
	TIM_OCInitTypeDef General_TIM_OC_InitStructure;
	
	RCC_APB1PeriphClockCmd(General_TIM_CLK, ENABLE);
	
	//=========== General_TIM_TimeBase_InitStructure ===========
	General_TIM_TimeBase_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	General_TIM_TimeBase_InitStructure.TIM_Prescaler = 1800-1;
	General_TIM_TimeBase_InitStructure.TIM_Period = 1024-1;
	General_TIM_TimeBase_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;

	TIM_TimeBaseInit(Specified_General_TIM_, &General_TIM_TimeBase_InitStructure);
	
	//============== General_TIM_OC_InitStructure ==============
	General_TIM_OC_InitStructure.TIM_OCMode = TIM_OCMode_PWM1;
	General_TIM_OC_InitStructure.TIM_OutputState = TIM_OutputState_Enable;
	General_TIM_OC_InitStructure.TIM_Pulse = Channel_Pulse;
	General_TIM_OC_InitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
	General_TIM_OC_InitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

	TIM_OC1Init(Specified_General_TIM_, &General_TIM_OC_InitStructure);
	TIM_OC1PreloadConfig(Specified_General_TIM_, Channel_Pulse);
	
	TIM_Cmd(Specified_General_TIM_, ENABLE);
}


/**
  * @brief  Initialize the All_TIM.
  * @param  None  
  * @retval None
  */
void All_TIM_Config(void)
{
	General_GPIO_Config();
	General_TIM_Config();
}

main.c

#include <tim.h>
#include <bsp_led.h>
#include <bsp_key.h>


u32 TEMP = 50;

int main(void)
{
	KEY_GPIO_Config();
	All_TIM_Config();
	
	while(1)
	{
		if(Key_Scan(KEY1_GPIO_PORT, KEY1_PIN) == KEY_ON)
		{
			if(Channel_Pulse < 1000)
				Channel_Pulse += TEMP;
			else
				Channel_Pulse = 1000;
			TIM_SetCompare1(Specified_General_TIM_, Channel_Pulse);
		}
		if(Key_Scan(KEY2_GPIO_PORT, KEY2_PIN) == KEY_ON)
		{
			if(Channel_Pulse > 0)
				Channel_Pulse -= TEMP;
			else
				Channel_Pulse = 0;
			TIM_SetCompare1(Specified_General_TIM_, Channel_Pulse);
		}
	}
}

PWM 捕捉(通用定时器输出PWM,高级定时器捕捉PWM)

在该示例中,我们可用按键实现对通用定时器输出的PWM 信号的调整,然后通过高级定时器进行PWM 捕捉计算其占空比并输出。

tim.h

/**
  ******************************************************************************
  * @file    tim.h
  * @author  Waao
  * @version V1.0.0
  * @date    1-Feb-2019
  * @brief   This file contains some board support package's definition for the TIM.
  *            
  ******************************************************************************
  * @attention
  *
  * None
	*
  ******************************************************************************
  */
#ifndef __TIM_H_
#define __TIM_H_


#include <stm32f4xx_tim.h>


extern __IO u32 Channel_Pulse;
extern float Duty_Ratio;


//===================== General_TIM =====================
#define Specified_General_TIM_          			TIM2
#define General_TIM_CLK          							RCC_APB1Periph_TIM2

//========== General_TIM_GPIO ==========
#define General_TIM_OCPWM_GPIO_PORT						GPIOA
#define General_TIM_OCPWM_GPIO_PIN						GPIO_Pin_5
#define General_TIM_OCPWM_GPIO_PINSOURCE			GPIO_PinSource5
#define General_TIM_OCPWM_GPIO_AF							GPIO_AF_TIM2
#define General_TIM_OCPWM_GPIO_CLK						RCC_AHB1Periph_GPIOA


//==================== Advanced_TIM =====================
#define Specified_Advanced_TIM_          			TIM8
#define Advanced_TIM_CLK          						RCC_APB2Periph_TIM8

//============ ICPWM_GPIO ==============
#define Advanced_TIM_ICPWM_GPIO_PORT					GPIOC
#define Advanced_TIM_ICPWM_GPIO_PIN						GPIO_Pin_6
#define Advanced_TIM_ICPWM_GPIO_PINSOURCE			GPIO_PinSource6
#define Advanced_TIM_ICPWM_GPIO_AF						GPIO_AF_TIM8
#define Advanced_TIM_ICPWM_GPIO_CLK						RCC_AHB1Periph_GPIOC

//=============== NVIC =================
#define Advanced_TIM_IRQChannel								TIM8_CC_IRQn
#define Advanced_TIM_IRQHandler								TIM8_CC_IRQHandler


void All_TIM_Config(void);

#endif

tim.c

/**
  ******************************************************************************
  * @file    tim.c
  * @author  Waao
  * @version V1.0.0
  * @date    1-Feb-2019
  * @brief   This file contains some board support package's functions for the TIM.
  *            
  ******************************************************************************
  * @attention
  *
  * None
	*
  ******************************************************************************
  */
#include "tim.h"


__IO u32 Channel_Pulse = 3000-1;
float Duty_Ratio = 0;


/**
  * @brief  Initialize the General_TIM GPIO.
  * @param  None  
  * @retval None
  */
void General_GPIO_Config(void)
{
	GPIO_InitTypeDef GENERAL_GPIO_InitStructure;
	
	RCC_AHB1PeriphClockCmd(General_TIM_OCPWM_GPIO_CLK, ENABLE);
	
	GENERAL_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GENERAL_GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GENERAL_GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GENERAL_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GENERAL_GPIO_InitStructure.GPIO_Pin = General_TIM_OCPWM_GPIO_PIN;
	
	GPIO_PinAFConfig(General_TIM_OCPWM_GPIO_PORT, General_TIM_OCPWM_GPIO_PINSOURCE, General_TIM_OCPWM_GPIO_AF);
	
	GPIO_Init(General_TIM_OCPWM_GPIO_PORT, &GENERAL_GPIO_InitStructure);
}


/**
  * @brief  Initialize the General_TIM.
	*					This general TIM is used to output the PWM.
  * @param  None  
  * @retval None
  */
void General_TIM_Config(void)
{
	TIM_TimeBaseInitTypeDef General_TIM_TimeBase_InitStructure;
	TIM_OCInitTypeDef General_TIM_OC_InitStructure;
	
	RCC_APB1PeriphClockCmd(General_TIM_CLK, ENABLE);
	
	//=========== General_TIM_TimeBase_InitStructure ===========
	General_TIM_TimeBase_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	General_TIM_TimeBase_InitStructure.TIM_Prescaler = 900-1;
	General_TIM_TimeBase_InitStructure.TIM_Period = 10000-1;
	General_TIM_TimeBase_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;

	TIM_TimeBaseInit(Specified_General_TIM_, &General_TIM_TimeBase_InitStructure);
	
	//============== General_TIM_OC_InitStructure ==============
	General_TIM_OC_InitStructure.TIM_OCMode = TIM_OCMode_PWM1;
	General_TIM_OC_InitStructure.TIM_OutputState = TIM_OutputState_Enable;
	General_TIM_OC_InitStructure.TIM_Pulse = Channel_Pulse;
	General_TIM_OC_InitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
//	General_TIM_OC_InitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

	TIM_OC1Init(Specified_General_TIM_, &General_TIM_OC_InitStructure);
//	TIM_OC1PreloadConfig(Specified_General_TIM_, Channel_Pulse);
	
	TIM_Cmd(Specified_General_TIM_, ENABLE);
}


/**
  * @brief  Initialize the Advanced_TIM GPIO.
  * @param  None  
  * @retval None
  */
void Advanced_GPIO_Config(void)
{
	GPIO_InitTypeDef Advanced_IC_GPIO_InitStructure;
	
	RCC_AHB1PeriphClockCmd(Advanced_TIM_ICPWM_GPIO_CLK, ENABLE);
	
	//=============== IC =================
	Advanced_IC_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	Advanced_IC_GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	Advanced_IC_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	Advanced_IC_GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	Advanced_IC_GPIO_InitStructure.GPIO_Pin = Advanced_TIM_ICPWM_GPIO_PIN;
	
	GPIO_PinAFConfig(Advanced_TIM_ICPWM_GPIO_PORT, Advanced_TIM_ICPWM_GPIO_PINSOURCE, Advanced_TIM_ICPWM_GPIO_AF);
	GPIO_Init(Advanced_TIM_ICPWM_GPIO_PORT, &Advanced_IC_GPIO_InitStructure);
}


/**
  * @brief  Initialize the Advanced_TIM.
	*					This advanced TIM is used to compute the duty ratio of the input PWM.
  * @param  None  
  * @retval None
  */
void Advanced_TIM_Config(void)
{
	TIM_TimeBaseInitTypeDef TimeBase_InitStructure;
	TIM_ICInitTypeDef IC_InitStructure;
	
	RCC_APB2PeriphClockCmd(Advanced_TIM_CLK, ENABLE);
	
	//============= TIM_IC1InitTypeDef =============
	TimeBase_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TimeBase_InitStructure.TIM_Prescaler = 1800-1;
	TimeBase_InitStructure.TIM_Period = 0xFFFF-1;
	TimeBase_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TimeBase_InitStructure.TIM_RepetitionCounter = 0;
	
	TIM_TimeBaseInit(Specified_Advanced_TIM_, &TimeBase_InitStructure);
	
	//============= TIM_ICInitTypeDef =============
	IC_InitStructure.TIM_Channel = TIM_Channel_1;
	IC_InitStructure.TIM_ICFilter = 0;
	IC_InitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
	IC_InitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
	IC_InitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
	
	TIM_ICInit(Specified_Advanced_TIM_, &IC_InitStructure);
	TIM_PWMIConfig(Specified_Advanced_TIM_, &IC_InitStructure);
	
	/* Because we just need to configure one capture channel, another channel will configure itself, 
	 * so we needn't configure the falling triggered capture channel.
	 */
//	IC_InitStructure.TIM_Channel = TIM_Channel_2;
//	IC_InitStructure.TIM_ICFilter = 0;
//	IC_InitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
//	IC_InitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
//	IC_InitStructure.TIM_ICSelection = TIM_ICSelection_IndirectTI;
//	
//	//TIM_ICInit(Specified_Advanced_TIM_, &IC_InitStructure);
//	TIM_PWMIConfig(Specified_Advanced_TIM_, &IC_InitStructure);

	//=============================================
	
	/* The channel compute the cycle should be configured the triggered signal.
	 */
	TIM_SelectInputTrigger(Specified_Advanced_TIM_, TIM_TS_TI1FP1);
	TIM_SelectSlaveMode(Specified_Advanced_TIM_, TIM_SlaveMode_Reset);
	TIM_SelectMasterSlaveMode(Specified_Advanced_TIM_, TIM_MasterSlaveMode_Enable);
	
	TIM_Cmd(Specified_Advanced_TIM_, ENABLE);
	
	TIM_ITConfig(Specified_Advanced_TIM_, TIM_IT_CC1, ENABLE);
}


/**
  * @brief  Initialize the Advanced_TIM_NVIC.
  * @param  None  
  * @retval None
  */
void Advanced_TIM_NVIC_Config(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;;
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
	
	NVIC_InitStructure.NVIC_IRQChannel = Advanced_TIM_IRQChannel;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	
	NVIC_Init(&NVIC_InitStructure);
}


/**
  * @brief  Initialize the All_TIM.
  * @param  None  
  * @retval None
  */
void All_TIM_Config(void)
{
	General_GPIO_Config();
	General_TIM_Config();
	Advanced_GPIO_Config();
	Advanced_TIM_Config();
	Advanced_TIM_NVIC_Config();
}

stm32f4xx_it.c


/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_it.h"
#include <bsp_led.h>
#include <bsp_usart.h>
#include "tim.h"

	
/**
  * @brief  This function handles Advanced_TIM_IRQHandler exception.
  * @param  None
  * @retval None
  */
void Advanced_TIM_IRQHandler(void)
{
	float TIME_IC1 = 0, TIME_IC2 = 0;

	TIM_ClearITPendingBit(Specified_Advanced_TIM_, TIM_IT_CC1);
 	
	TIME_IC1 = TIM_GetCapture1(Specified_Advanced_TIM_);
	TIME_IC2 = TIM_GetCapture2(Specified_Advanced_TIM_);
	
	Duty_Ratio = (float)(TIME_IC2 * 100) / TIME_IC1;
	printf("\nThe TIME_IC1 is %f, the TIME_IC2 is %f", TIME_IC1, TIME_IC2);
	printf("\nThe duty ratio is %.2f%%", Duty_Ratio);
//	printf("\nThe duty ratio is");
}


main.c

#include <tim.h>
#include <bsp_led.h>
#include <bsp_key.h>
#include <bsp_usart.h>


u32 TEMP = 200;

int main(void)
{
	USART_GPIO_Config();
	USART1_Config();
	KEY_GPIO_Config();
	All_TIM_Config();
	
	printf("\nWhy so serious ?\n  The game just begin.");
	while(1)
	{
		if(Key_Scan(KEY1_GPIO_PORT, KEY1_PIN) == KEY_ON)
		{
			if(Channel_Pulse < 10000)
				Channel_Pulse += TEMP;
			else
				Channel_Pulse = 10000;
			TIM_SetCompare1(Specified_General_TIM_, Channel_Pulse);
		}
		if(Key_Scan(KEY2_GPIO_PORT, KEY2_PIN) == KEY_ON)
		{
			if(Channel_Pulse > 0)
				Channel_Pulse -= TEMP;
			else
				Channel_Pulse = 0;
			TIM_SetCompare1(Specified_General_TIM_, Channel_Pulse);
		}
	}
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诺亚方包

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值