time.c 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. #include "time.h"
  2. /*
  3. 使用案例
  4. TIME_Config(TIM1, 400-1, 84-1, 1, 0, RCC_APB2PeriphClockCmd, RCC_APB2Periph_TIM1, TIM1_UP_TIM10_IRQn);
  5. */
  6. void TIM_Mod_Config(void(*ClockCmd)(uint32_t, FunctionalState), u32 RCC_APBnPeriphn,TIM_TypeDef *TIMx, u16 psc, u16 per, u8 pre, u8 sub)
  7. {
  8. (*ClockCmd)(RCC_APBnPeriphn, ENABLE);
  9. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  10. TIM_TimeBaseStructure.TIM_Prescaler = psc; //定时器分频
  11. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
  12. TIM_TimeBaseStructure.TIM_Period = per; //自动重装载值
  13. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  14. TIM_TimeBaseInit(TIMx,&TIM_TimeBaseStructure);//初始化定时器
  15. TIM_ARRPreloadConfig(TIMx, ENABLE); //允许定时重载
  16. TIM_SelectOutputTrigger(TIMx, TIM_TRGOSource_Update); //选择事件更新为触发源
  17. TIM_ITConfig(TIMx, TIM_IT_Update, ENABLE);
  18. TIM_Cmd(TIMx, ENABLE); //使能
  19. }
  20. void TIM_NVIC_Config(u8 NVIC_IRQChannel, u8 pre, u8 sub)
  21. {
  22. NVIC_InitTypeDef NVIC_InitStructure;
  23. NVIC_InitStructure.NVIC_IRQChannel = NVIC_IRQChannel;
  24. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pre;
  25. NVIC_InitStructure.NVIC_IRQChannelSubPriority = sub;
  26. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  27. NVIC_Init(&NVIC_InitStructure);
  28. }