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; //遞增計數(shù)(向上計數(shù))
? ?TIM_TimeBaseStruct.TIM_RepetitionCounter = 0;//只對TIM1有效
? ?TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStruct);//使能TIM2中斷 初始化相關參數(shù)
? ?//設置中斷占優(yōu)先級組
? ?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);//根據(jù)結構體初始化相關參數(shù)
? ?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));
? ?}
}
//以上代碼進不了中斷