1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- #include "timer.h"
- void timer_nvic_config(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- // Usart1 NVIC 配置
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- void timer_config(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); ///使能TIM2时钟
- /* TIMER1 configuration */
- TIM_TimeBaseInitStructure.TIM_Period = 16800; // 239;
- TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
- TIM_TimeBaseInitStructure.TIM_Prescaler = 10000; // 7999;
- TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure); //初始化TIM2
- TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //允许定时器2更新中断
- TIM_Cmd(TIM2, ENABLE); //使能定时器3
- }
- void user_timer_init(void)
- {
- timer_nvic_config();
- timer_config();
- }
- void TIM2_IRQHandler(void)
- {
- static uint16_t cnt = 0;
- if (cnt >= 100)
- {
- IWDG_ReloadCounter();
- cnt = 0;
- }
- else
- {
- cnt++;
- }
- }
|