void TIM1_INT_Init(u16 PeriodVal,u16 PrescalerVal)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化GPIOD PC0
TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
TIM_TimeBaseStruct.TIM_Period = PeriodVal;//周期值
TIM_TimeBaseStruct.TIM_Prescaler = PrescalerVal;//预分频
TIM_TimeBaseStruct.TIM_ClockDivision = TIM_CKD_DIV1;//时钟分割
TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up; //递增计数(向上计数)
TIM_TimeBaseStruct.TIM_RepetitionCounter = 0;//只对TIM1有效
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStruct);//使能TIM2中断 初始化相关参数
//设置中断占优先级组
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);//允许更新中断
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; //TIM1 INT
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);//根据结构体初始化相关参数
TIM_Cmd(TIM1, ENABLE);
}
//初始化
TIM1_INT_Init(125,48);
//中断
void TIM1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM1_IRQHandler(void)
{ static u8 i=0;
if(TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
GPIO_WriteBit(GPIOD, GPIO_Pin_3, (i == 0) ? (i = Bit_SET) : (i = Bit_RESET));
}
}
//以上代码进不了中断