这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 企业专区 » GD32 » [GD32130C-START]GD32F130C8真的可以兼容ST的吗?(六)

共1条 1/1 1 跳转至

[GD32130C-START]GD32F130C8真的可以兼容ST的吗?(六)TIM3频率发生器实验

院士
2015-03-31 23:12:52     打赏

定时器是我们在嵌入式单片机里应用最广泛的外设。使用定时器的水平高低也决定着整个项目实施的水平高低。本次版主给大家带了使用TIM3定时器产生不同频率的示例。例程主要使用TIM3来产生不同频率的方波,占空比为50%,按键按下时,频率增加。


实例还是比较简单的,版主就不再多多叙述,大家直接看源代码吧!

源代码:

/**
  * @brief  tim3 base 1KHz
  * @param  
  * @retval 
  * @date   2015-03-31
  * @note   
  */

void TIM3Freq(void)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
  TIM_OCInitTypeDef  TIM_OCInitStructure;


  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  
  /* GPIOA Configuration: TIM3 CH4 (PB1) */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  GPIO_Init(GPIOB, &GPIO_InitStructure); 
  
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_1);


  /* Enable the TIM3 gloabal Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
  TIM_TimeBaseStructure.TIM_Prescaler = 23;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = gCCR4_Val;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  
  TIM_OC4Init(TIM3, &TIM_OCInitStructure);
  TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable); 
  
  TIM_ITConfig(TIM3, TIM_IT_CC4, ENABLE);
  TIM_Cmd(TIM3, ENABLE);
}

/**
  * @brief  TIM3_IRQHandler的中断入口函数
  * @param  
  * @retval 
  * @date   2015-03-30
  * @note   
  */

void TIM3_IRQHandler(void)
{
  uint16_t capture;
  /* TIM3_CH4 toggling with frequency = 4687.5 Hz */
  if (TIM_GetITStatus(TIM3, TIM_IT_CC4) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);
    capture = TIM_GetCapture4(TIM3);
    TIM_SetCompare4(TIM3, capture + gCCR4_Val);
  }


}

 


工程文件:

exp07_TIM3Frequence.zip






关键词: GD32130C-START     兆易     GD32F13    

共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]