STM32
直播中

刘丽菲

7年用户 935经验值
私信 关注
[问答]

STM32F0 Timer3是怎样作为定时器输出的

STM32F0 timer3是怎样作为定时器输出的?
STM32F0 Timer3是怎样作为中断输出的?

回帖(1)

陈瑞微

2021-11-16 14:22:43
  设置Timer3 中断
  void TIM3_INT_Config(void)
  {
  NVIC_InitTypeDef NVIC_InitStructure;
  /* TIM3 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  /* TIM3 ÖжÏǶÌ×Éè¼Æ*/
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  }
  设置Timer3 模式
  void TIM3_OUT_Config(void)
  {
  TIM_TimeBaseInitTypeDef TIM3_TimeBaseStructure;
  TIM_OCInitTypeDef TIM3_OCInitStructure;
  /* ¼ÆËãÔ¤·ÖƵֵ */
  TM3_PrescalerValue = (uint16_t) (SystemCoreClock / TM3_PRESCALER) - 1;
  /* Time ¶¨Ê±Æ÷»ù´¡ÉèÖà */
  TIM3_TimeBaseStructure.TIM_Period = 65535;
  TIM3_TimeBaseStructure.TIM_Prescaler = 0;
  TIM3_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM3_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM3, &TIM3_TimeBaseStructure);
  /* Ô¤·ÖƵÆ÷ÅäÖà */
  TIM_PrescalerConfig(TIM3, TM3_PrescalerValue, TIM_PSCReloadMode_Immediate);
  /* Êä³ö±È½ÏʱÐòģʽÅäÖÃÉèÖà */
  TIM3_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
  TIM3_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  /* Êä³ö±È½ÏʱÐòģʽÅäÖÃ: ƵµÀ3*/
  TIM3_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM3_OCInitStructure.TIM_Pulse = TM3_CCR1_Val; //3000
  TIM_OC3Init(TIM3, &TIM3_OCInitStructure);
  TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);
  /* TIM ÖжÏʹÄÜ */
  TIM_ITConfig(TIM3, TIM_IT_CC3 , ENABLE);
  /* TIM3 ʹÄÜ */
  TIM_Cmd(TIM3, ENABLE);
  }
  3. 中断子程序
  void TIM3_IRQHandler(void)
  {
  if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)
  {
  TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);
  /* LED3 toggling with frequency = 219.7 Hz */
  // LED1_Toggle();
  // capture3 = TIM_GetCapture3(TIM3);
  // TIM_SetCompare3(TIM3, capture3 + TM3_CCR1_Val);
  OutToggle();
  if(TimeRxFlag)
  {
  TimeInterval++;
  if(TimeInterval》4)
  {
  RxFrameEndFlag =1;
  }
  }
  if(KeyCtrl.bDebunceTime != 0)
  {
  KeyCtrl.bDebunceTime--;
  }
  // SevenSegShow(DispBuffer);
  capture3 = TIM_GetCapture3(TIM3);
  TIM_SetCompare3(TIM3, capture3 + TM3_CCR1_Val);
  }
  }
举报

更多回帖

发帖
×
20
完善资料,
赚取积分