TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// TIM_OCInitTypeDef TIM_OCInitStructure ;
/*下面是配置定时器1的时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE ); //最先要配置时钟,有了时钟才可以配置外设
/*下面是配置定时器2的工作模式即使能*/
TIM_DeInit(TIM1); //复位TIM1定时器
/* TIM4 configuration */
TIM_TimeBaseStructure.TIM_Period = 100; //TIM4最大计数值100
TIM_TimeBaseStructure.TIM_Prescaler = 359; // 分频359+1
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // 时钟分割,这里为0的话那么TIM_Period 就为2500;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数方向向上计数
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* Clear TIM1 update pending flag[清除TIM4溢出中断标志] */
TIM_ClearFlag(TIM1, TIM_FLAG_Update);
/* Enable TIM1 Update interrupt [TIM4溢出中断允许]*/
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
/* TIM1enable counter [允许tim4计数]*/
TIM_Cmd(TIM1, ENABLE);
/*下面是配置定时器2的中断向量*/
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
这样配置的目的是想得到一个500us 中断溢出的定时器,但是结果却不尽人意
中断里 将某个IO高低高低输出,示波器测试频率是1HZ,也就是500MS 定时器溢出
同样的参数,设置到TIM2,3,4都是正确的 |