蓝桥杯嵌入式组开发——TIM3实现定时器功能

蓝桥杯嵌入式组开发——TIM3实现定时器功能

1.定时器初始化

时钟初始化
中断配置
TIM初始化50000 // 50ms一次中断
TIM_PrescalerConfig 72分频 //和TIM_PWM_OUT一样
中断使能
打开总开关



void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable the TIM2 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);
}
void TIM_Init(void)
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

	NVIC_Configuration();
	
	/* Time base configuration */
	TIM_TimeBaseStructure.TIM_Period = 50000;
	TIM_TimeBaseStructure.TIM_Prescaler = 0;
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
	
	/* Prescaler configuration */
	TIM_PrescalerConfig(TIM3, 71, TIM_PSCReloadMode_Immediate);

	TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);

	TIM_Cmd(TIM3, ENABLE);
	
}

2.定时器中断处理函数

void TIM3_IRQHandler(void)
{
	if (TIM_GetFlagStatus(TIM3, TIM_FLAG_Update) == 1)
	{
		TIM_ClearFlag(TIM3, TIM_FLAG_Update);
		t_50ms++;
		if( t_50ms >=20 )
		{
			t_50ms = 0;
			num++;	  	
		}

	}	
}

3.参考官方库函数

STM32_MCU\stm32f10x_stdperiph_lib\STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Examples\TIM\TimeBase
蓝桥杯嵌入式组开发——TIM3实现定时器功能_第1张图片

你可能感兴趣的:(蓝桥杯)