专注电子技术学习与研究
当前位置:单片机教程网 >> STM32 >> 浏览文章

ARM107的TIM1输出PWM波形

作者:星星and宇   来源:不详   点击数:  更新时间:2014年08月03日   【字体:

  void PWM_Out_Init(void)

{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */     
       
  /* System Clocks Configuration */
  RCC_PWM_Configuration();
 
  /* GPIO Configuration */
  GPIOPWM_Configuration();
 
  /* Compute the prescaler value */
  TIM_DeInit(TIM1);//初始化TIM1寄存器
  PrescalerValue = (uint16_t) (SystemCoreClock / 2000) - 1;
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 499;
  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
 
  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
 
  /* PWM1 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//PWM1为正常占空比模式,PWM2为反极性模式
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;//互补输出允许
 
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//High为占空比高极性
  TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High;//互补输出,与以上相反
  TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;
  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
  TIM_OC1Init(TIM1, &TIM_OCInitStructure);
 
  TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
  TIM_ARRPreloadConfig(TIM1, ENABLE);
 
  TIM_Cmd(TIM1, ENABLE);
  TIM_CtrlPWMOutputs(TIM1,ENABLE);//使能TIM1的PWM输出,TIM1与TIM8有效
 
}
 
/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */
void RCC_PWM_Configuration(void)
{
  /* TIM3 clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
 
  /* GPIOA and GPIOB clock enable */
  RCC_APB2PeriphClockCmd(  RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);
}
 
/**
  * @brief  Configure the TIM3 Ouput Channels.
  * @param  None
  * @retval None
  */
void GPIOPWM_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
 
//#ifdef STM32F10X_CL
  /*GPIOB Configuration: TIM3 channel1, 2, 3 and 4 */
  //GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
 // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 
 // GPIO_Init(GPIOC, &GPIO_InitStructure);
 
 // GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
 
//#else
  /* GPIOA Configuration:TIM3 Channel1, 2, 3 and 4 as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  
  GPIO_Init(GPIOE, &GPIO_InitStructure);
  GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);
//#endif
}
关闭窗口

相关文章