小学生
最后登录1970-1-1
在线时间 小时
注册时间2023-5-15
|
- void Timer_Init(void)
- {
- //配置RCC,开启时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
- // 配置GPIO
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- //配置TIM
- TIM_InternalClockConfig(TIM2);
-
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_Period = 1000 - 1;
- TIM_TimeBaseStructure.TIM_Prescaler = 7200 - 1;
- TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
-
- TIM_ClearFlag(TIM2, TIM_FLAG_Update);
-
- TIM_ITConfig(TIM2,TIM_IT_Update, ENABLE);
-
- //配置NVIC
- // 选择中断分组
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- NVIC_InitTypeDef NVIC_InitStructure;
-
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;//选择TIM2的中断通道
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_Init(&NVIC_InitStructure);
- //启动定时器
- TIM_Cmd(TIM2, ENABLE);
- }
- // 中断函数代码
- void TIM2_IRQHandler(void)
- {
- if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
- {
- LED_TOOGLE;
- }
- TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
-
- }
复制代码
|
|