当前位置: 首页 > article >正文

国产航顺HK32F030M:定时器计数/PWM输出/输出翻转/输入捕获

定时器计数

/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "hk32f030m.h"
#include "hk32f030m_gpio.h"
#include <stdio.h>
#include "stdarg.h"

uint16_t CCR1_Val = 5000;
uint16_t CCR2_Val = 2500;
uint16_t CCR3_Val = 1250;
uint16_t CCR4_Val = 625;
uint16_t PrescalerValue = 0;

void RCC_Configuration(void);
void GPIO_Configuration(void);
void TIM_Config(void);
void GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin);
void TIM6_IRQHandler(void);

int main(void)
  /* Infinite loop */
{
	RCC_Configuration();
	GPIO_Configuration();
	TIM_Config();
	
  while (1)
  {
  }
}
/*配置时钟*/
void RCC_Configuration(void)
{
	RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE );
	RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE );
	RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOC, ENABLE );
	RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOD, ENABLE );
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6 , ENABLE);
}
/*配置GPIO*/
void GPIO_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	//GPIOA,PA1,PA2,PA3
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);	
	//GPIOD,PD4
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
}
/*配置TIMER*/
void TIM_Config(void)
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	// 预分频器值 // SystemCoreClock = 48M,计数器时钟counter clock = 1 MHz
//  PrescalerValue = (uint16_t) (SystemCoreClock  / 1000000) - 1;// 32MHz
	PrescalerValue = (uint16_t) (SystemCoreClock  / 10000) - 1;// 3200

	/* Enable the TIM1 gloabal Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0; // 中断优先级
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
	
  /* Time Base configuration */
	// 公式:Tout= ((arr+1)*(psc+1))/Tclk
	// 1s = ((10000+1)*(3200+1))/32000000
	// 0.5s = ((5000+1)*(3200+1))/32000000
  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;// 预分频系数 3200
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;// 计数模式:向上计数
  TIM_TimeBaseStructure.TIM_Period = 5000; // 自动重载值 10000,计数模式为向上计数时,定时器从0开始计数,计数超过到arr时触发定时中断服务函数
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

  TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);
	 /* TIM Interrupts enable */
  TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);
  /* TIM1 counter enable */
  TIM_Cmd(TIM6, ENABLE);
}
/*配置IO翻转*/
void GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
  /* Check the parameters */
  assert_param(IS_GPIO_PIN(GPIO_Pin));

  GPIOx->ODR ^= GPIO_Pin; // 输出翻转 
}

uint16_t capture = 0;

void TIM6_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET)
  {
    TIM_ClearITPendingBit(TIM6, TIM_IT_Update); //每次进入中断都要清空中断标志,否则主函数将无法正常执行

    /* PA1 toggling with frequency = 50 Hz */
		GPIO_TogglePin(GPIOD ,GPIO_Pin_4);// PD4 翻转 
		
//		capture = TIM_GetCapture1(TIM1);
//		
//    TIM_SetCompare1(TIM1, capture);
  }
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
}
#endif /* USE_FULL_ASSERT */



公式:Tout= ((arr+1)*(psc+1))/Tclk

1s = ((10000+1)*(3200+1))/32000000

0.5s = 500ms = ((5000+1)*(3200+1))/32000000


实现效果

HK32F030MF4P6定时器计数,分别是从0到10000和0到5000计数

PWM输出

/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "hk32f030m.h"
#include "hk32f030m_gpio.h"
#include <stdio.h>
#include "stdarg.h"

uint16_t TimerPeriod = 0;
uint16_t Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0, Channel4Pulse = 0;

void RCC_Configuration(void);
void GPIO_Configuration(void);
void TIM_Config(void);

int main(void)
  /* Infinite loop */
{
	RCC_Configuration();
	GPIO_Configuration();
	TIM_Config();
	
  while (1)
  {
  }
}
/*配置时钟*/
void RCC_Configuration(void)
{
	RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE );
	RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE );
	RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOC, ENABLE );
	RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOD, ENABLE );
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
}
/*配置GPIO*/
void GPIO_Configuration(void)
{
	//GPIOA Configuration: Channel 1N, 2N, 3N
	GPIO_InitTypeDef GPIO_InitStructure;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_3);	
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_3);	
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_3);	
	//GPIOC Configuration: Channel 1, 2
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
	GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_3);	
	GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_3);	
  /*PD3 = TIMER Channel 3*/
	//GPIOD Configuration: Channel 3
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource3,GPIO_AF_3);	
	  /*PD4 = TIMER Channel 4*/
	//GPIOD Configuration: Channel 4
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource4,GPIO_AF_3);
	
//	//GPIOB Configuration: Break
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_3);	
	
//	/*PC3 = TIMER Channel 3*/
//	GPIO_IOMUX_PinAFConfig(GPIOC,GPIO_PinSource3,IOMUX_PC3_TIM1CH3);
//	//GPIOC Configuration: Channel 3 PC3
//  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
//  GPIO_Init(GPIOC, &GPIO_InitStructure);
//	GPIO_PinAFConfig(GPIOC,GPIO_PinSource3,GPIO_AF_3);	
}
/*配置TIMER*/
void TIM_Config(void)
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_OCInitTypeDef  TIM_OCInitStructure;
	//TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
  TimerPeriod = (SystemCoreClock / 3200 ) - 1;//SystemCoreClock=32MHZ  // 自动重载值是10000 
  /* Compute CCR1 value to generate a duty cycle at 50% for channel 1 and 1N */
  Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);
  /* Compute CCR2 value to generate a duty cycle at 37.5%  for channel 2 and 2N */
  Channel2Pulse = (uint16_t) (((uint32_t) 375 * (TimerPeriod - 1)) / 1000);
  /* Compute CCR3 value to generate a duty cycle at 25%  for channel 3 and 3N */
  Channel3Pulse = (uint16_t) (((uint32_t) 25 * (TimerPeriod - 1)) / 100);
  /* Compute CCR4 value to generate a duty cycle at 12.5%  for channel 4 */
  Channel4Pulse = (uint16_t) (((uint32_t) 125 * (TimerPeriod- 1)) / 1000);
 
  /* Time Base configuration */
  TIM_TimeBaseStructure.TIM_Prescaler = 0;// 预分频系数
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
  TIM_TimeBaseStructure.TIM_Period = TimerPeriod;// 自动重载值
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;//一般不使用,默认TIM_CKD_DIV1
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

	
  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

  /* Channel 1, 2,3 and 4 Configuration in PWM mode */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;//设置待装入捕获比较寄存器的脉冲值
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

  TIM_OC1Init(TIM1, &TIM_OCInitStructure);

  TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;
  TIM_OC2Init(TIM1, &TIM_OCInitStructure);

  TIM_OCInitStructure.TIM_Pulse = Channel3Pulse;
  TIM_OC3Init(TIM1, &TIM_OCInitStructure);

  TIM_OCInitStructure.TIM_Pulse = Channel4Pulse;
  TIM_OC4Init(TIM1, &TIM_OCInitStructure);
//  /* Automatic Output enable, Break, dead time and lock configuration*/
//  TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
//  TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
//  TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
//  TIM_BDTRInitStructure.TIM_DeadTime = 50;
//  TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
//  TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
//  TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;

//  TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);

  /* TIM1 counter enable */
  TIM_Cmd(TIM1, ENABLE);

  /* TIM1 Main Output Enable */
  TIM_CtrlPWMOutputs(TIM1, ENABLE);

}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
}
#endif /* USE_FULL_ASSERT */



实验结果

50%占空比

在这里插入图片描述

周期316us怎么来的?

自动重载值、预分频系数和周期计算公式:

公式:Tout= ((arr+1)*(psc+1))/Tclk

  • Tout 周期/溢出时间 单位us
  • Tclk 系统时钟 单位Mhz (这里是32MHZ)

(10000 + 1)*( 0 + 1 )/ 32000 000 = 0.00031253s = 31254ms = 312.54us

37.5%占空比

在这里插入图片描述

25%占空比

在这里插入图片描述

12.5%占空比

在这里插入图片描述


翻转输出

HK32F030MF4P6 定时器TIMER输出翻转

/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "hk32f030m.h"
#include "hk32f030m_gpio.h"
#include <stdio.h>
#include "stdarg.h"

uint16_t TimerPeriod = 0;  // 自动重载值
uint16_t Channel1Pulse = 0;// 脉冲宽度

void RCC_Configuration(void);
void GPIO_Configuration(void);
void TIM_Config(void);

int main(void)
  /* Infinite loop */
{
	RCC_Configuration();
	GPIO_Configuration();
	TIM_Config();
	
  while (1)
  {
  }
}
/*配置时钟*/
void RCC_Configuration(void)
{
	RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE );
	RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE );
	RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOC, ENABLE );
	RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOD, ENABLE );
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
}
/*配置GPIO*/
void GPIO_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
	  /*PD4 = TIM2 Channel 1*/
	//GPIOD Configuration: Channel 1
    GPIO_Init(GPIOD, &GPIO_InitStructure);
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource4,GPIO_AF_4);
}
/*配置TIMER*/
void TIM_Config(void)
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_OCInitTypeDef  TIM_OCInitStructure;
	// 频率配置为100KHZ  SystemCoreClock = 32Mhz(内部高速时钟)    自动重载值 = 32Mhz/100 000 -  1 = 3199
  TimerPeriod = (SystemCoreClock / 100000 ) - 1;
  /* Compute CCR1 value to generate a duty cycle at 50% for channel 1 */
	// 脉宽值 // 5*(3199-1)/10 = 1599
	// duty cycle 占空比为 1/2 = 50%
  Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10); 

	
  /* Time Base configuration */
  TIM_TimeBaseStructure.TIM_Prescaler = 0;//预分频系数
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
  TIM_TimeBaseStructure.TIM_Period = TimerPeriod;//自动重载值3200
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  /* Channel 1 Configuration in PWM mode */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;//翻转
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;

  TIM_OC1Init(TIM2, &TIM_OCInitStructure);
	//TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single);
  /* TIM2 counter enable */
  TIM_Cmd(TIM2, ENABLE);
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
}
#endif /* USE_FULL_ASSERT */




输入捕获

在这里插入图片描述

在这里插入图片描述

/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "hk32f030m.h"
#include "hk32f030m_gpio.h"
#include <stdio.h>
#include "stdarg.h"

USART_InitTypeDef USART_InitStructure;
void RCC_Configuration(void);
void GPIO_Configuration(void);
void TIM_Config(void);
void USART_Configuration(void);
void softWareDelay(void);
void TIM1_CC_IRQHandler(void);
int main(void)
  /* Infinite loop */
{
	RCC_Configuration();
	GPIO_Configuration();
	TIM_Config();
	
  while (1)
  {
  }
}
/*配置时钟*/
void RCC_Configuration(void)
{
	RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE );
	RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE );
	RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOC, ENABLE );
	RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOD, ENABLE );
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
}
/*配置GPIO*/
void GPIO_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
	  /*PC6 = TIM1 Channel 1*/
	//GPIOC Configuration: Channel 1
  GPIO_Init(GPIOC, &GPIO_InitStructure);
	GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_3);	
}
/*配置TIMER*/
void TIM_Config(void)
{
  TIM_ICInitTypeDef  TIM_ICInitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
  /* TIM1 configuration: Input Capture mode ---------------------
     The external signal is connected to TIM1 CH1 pin (PC.06)  
     The Rising edge is used as active edge,
     The TIM1 CCR1 is used to compute the frequency value 
  ------------------------------------------------------------ */
  TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿触发
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//方向选择定义到TI1上
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;//捕获预分频,多少个上升沿触发一次
  TIM_ICInitStructure.TIM_ICFilter = 0x0;//数字滤波配置

  TIM_ICInit(TIM1, &TIM_ICInitStructure);
  
  /* TIM enable counter */
  TIM_Cmd(TIM1, ENABLE);

  /* Enable the CC1 Interrupt Request */
  TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);
  
  /* Enable the TIM1 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}


uint16_t IC3ReadValue1 = 0, IC3ReadValue2 = 0;
uint16_t CaptureNumber = 0; // 捕获成功/失败 标识位
uint32_t Capture = 0; // 捕获
uint32_t TIM1Freq = 0;//频率
// 定时器1 中断你服务函数
void TIM1_CC_IRQHandler(void)
{ 
  if(TIM_GetITStatus(TIM1, TIM_IT_CC1) == SET) 
  {
    /* Clear TIM1 Capture compare interrupt pending bit */
    TIM_ClearITPendingBit(TIM1, TIM_IT_CC1);
    if(CaptureNumber == 0)
    {
      /* Get the Input Capture value */
      IC3ReadValue1 = TIM_GetCapture1(TIM1);
      CaptureNumber = 1;
    }
    else if(CaptureNumber == 1)
    {
      /* Get the Input Capture value */
      IC3ReadValue2 = TIM_GetCapture1(TIM1); 
      
      /* Capture computation */
      if (IC3ReadValue2 > IC3ReadValue1)
      {
        Capture = (IC3ReadValue2 - IC3ReadValue1); 
      }
      else if (IC3ReadValue2 < IC3ReadValue1)
      {
        Capture = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2); 
      }
      else
      {
        Capture = 0;
      }
      /* Frequency computation */ 
      TIM1Freq = (uint32_t) SystemCoreClock / Capture;//频率计算
      CaptureNumber = 0;
    }
  }
}
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
}
#endif /* USE_FULL_ASSERT */



在这里插入图片描述


参考资料

  • [1] 国产航顺HK32F030M开发资料(by JL)\软件:航顺HK32F030M\航顺HK32F030M库文件\HK32F030Mxx_ExampleV1.0.15\HK32F030Mxx_Example\project\15 TIM\TIMER例程\TIMER输出翻转

http://www.kler.cn/a/1031.html

相关文章:

  • 使用uniapp 微信小程序一些好用的插件分享
  • git: hint:use --reapply-cherry-picks to include skipped commits
  • 开源模型应用落地-qwen2-7b-instruct-LoRA微调合并-ms-swift-单机单卡-V100(十三)
  • 高性能网络模式:Reactor 和 Proactor
  • jenkins的作用以及操作
  • Scala 异常处理
  • 【操作系统复习】ch3 内存基础
  • 为什么VMware会给我多创建了两个网络呢?Windows和Linux为什么可以彼此ping的通呢
  • 【文心一言】什么是文心一言,如何获得内测和使用方法。
  • 基于python的奥运会历史数据分析【120年】
  • 「Python 基础」异步 I/O 编程
  • 数据分析之Matplotilb数据可视化
  • Integer和int的比较大小
  • HelloWorld
  • python并发编程多线程
  • QT VTK开发 (一、下载编译)
  • wait 和 notify
  • Cadence Allegro 导出Netin(back anno.)报告详解
  • Ununtu环境下的判断字符串相等出现sh: xxx: [: xxx: unexpected operator的问题
  • QT Plugin 插件开发
  • 跨境老兵多年经验整理出的WhatsApp养号攻略分享
  • 漫画:什么是归并排序算法?
  • Adam优化器算法详解及代码实现
  • ubuntu不同版本的源(换源)(镜像源)(lsb_release -c命令,显示当前系统的发行版代号(Codename))
  • 【Android笔记85】Android之使用Camera和MediaRecorder录制视频
  • Java的jar包打包成exe应用