新手入门
- 积分
- 19
- 金钱
- 19
- 注册时间
- 2021-11-23
- 在线时间
- 2 小时
|
1金钱
请教一下,在定时器中断中,通过定时器控制led的亮灭,实验现象出现led灯在程序下载完后就立马被点亮了,而不是等到定时器更新,而后面的现象便可以以一秒的间隔进行led亮灭交替,开始以为是led灯初始配置问题,但是通过串口发现在程序被下载进去的一瞬间这个定时器中断就被调用了,这是我程序写错了还是就是这样的呢
代码如下
------------------------------------------------------------------------------------------
#include "time.h"
#include "lib.h"
#include "stm32f10x.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_exti.h"
void Tim3_Init(u16 arr, u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//enable the clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
//Init the time
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = arr;
TIM_TimeBaseInitStructure.TIM_Prescaler = psc;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
//TIM_ITConfig()
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
//nvic init
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
//enable the time
TIM_Cmd(TIM2, ENABLE);
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
void TIM2_IRQHandler()
{
static int i = 1;
if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
{
printf("进入中断\r\n");
LED1(i);
i = !i;
}
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
|
|