Guest User

stm32f4 pwm and timer interrupt

a guest
Apr 19th, 2014
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.88 KB | None | 0 0
  1. void TIM2_IRQHandler(void)
  2. {
  3.     if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
  4.         {
  5.             TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  6.             ++global_interruptcount;
  7.         }
  8. }
  9.  
  10. TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  11. TIM_OCInitTypeDef  TIM_OCInitStructure;
  12.  
  13. uint32_t CCR1_Val = 12000000;
  14. uint32_t PrescalerValue = 0;
  15.  
  16. /**
  17.   * @brief  Configure the TIM4 Ouput Channels.
  18.   * @param  None
  19.   * @retval None
  20.   */
  21. void TIM_Config1(void)
  22. {
  23.     GPIO_InitTypeDef GPIO_InitStructure;
  24.  
  25.     /* TIM4 clock enable */
  26.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  27.  
  28.     /* GPIOA clock enable */
  29.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  30.  
  31.     /* GPIOD Configuration: TIM2 CH1 (PA5) */
  32.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;// | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  33.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  34.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  35.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  36.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  37.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  38.  
  39.     /* Connect TIM2 pins to AF2 */
  40.     GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_TIM2);
  41. }
  42.  
  43. void TIM_Config2(){
  44.  
  45.     NVIC_InitTypeDef NVIC_InitStructure;
  46.  
  47.     NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  48.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  49.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  50.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  51.     NVIC_Init(&NVIC_InitStructure);
  52.  
  53.     /* -----------------------------------------------------------------------
  54.         TIM4 Configuration: generate 4 PWM signals with 4 different duty cycles.
  55.  
  56.         In this example TIM4 input clock (TIM4CLK) is set to 4 * APB1 clock (PCLK1),
  57.         since TIMPRE bit from RCC_DCKCFGR register is set.
  58.           TIM4CLK = 4 * PCLK1
  59.           PCLK1 = HCLK / 4
  60.           => TIM4CLK = HCLK = SystemCoreClock
  61.  
  62.         To get TIM4 counter clock at 21 MHz, the prescaler is computed as follows:
  63.            Prescaler = (TIM4CLK / TIM4 counter clock) - 1
  64.            Prescaler = (SystemCoreClock /21 MHz) - 1
  65.  
  66.         To get TIM4 output clock at 30 KHz, the period (ARR)) is computed as follows:
  67.            ARR = (TIM4 counter clock / TIM4 output clock) - 1
  68.                = 699
  69.  
  70.         TIM4 Channel1 duty cycle = (TIM4_CCR1/ TIM4_ARR)* 100 = 50%
  71.         TIM4 Channel2 duty cycle = (TIM4_CCR2/ TIM4_ARR)* 100 = 37.5%
  72.         TIM4 Channel3 duty cycle = (TIM4_CCR3/ TIM4_ARR)* 100 = 25%
  73.         TIM4 Channel4 duty cycle = (TIM4_CCR4/ TIM4_ARR)* 100 = 12.5%
  74.  
  75.         Note:
  76.          SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file.
  77.          Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
  78.          function to update SystemCoreClock variable value. Otherwise, any configuration
  79.          based on this variable will be incorrect.
  80.       ----------------------------------------------------------------------- */
  81.       RCC_TIMCLKPresConfig(RCC_TIMPrescActivated);
  82.  
  83.       /* Compute the prescaler value */
  84.       PrescalerValue = (uint32_t) (SystemCoreClock / 21000000) - 1;
  85.       //PrescalerValue = (uint32_t) (SystemCoreClock / 10) - 1;
  86.  
  87.  
  88.       /* Time base configuration */
  89.       TIM_TimeBaseStructure.TIM_Period = 21000000/100-1;
  90.       TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  91.       TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  92.       TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  93.  
  94.       TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  95.  
  96.       /* PWM1 Mode configuration: Channel1 */
  97.       TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  98.       TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  99.       TIM_OCInitStructure.TIM_Pulse = CCR1_Val/100;
  100.       TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  101.  
  102.       TIM_OC1Init(TIM2, &TIM_OCInitStructure);
  103.  
  104.       TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
  105.  
  106.       //---
  107.  
  108.       TIM_ARRPreloadConfig(TIM2, ENABLE);
  109.  
  110.       TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  111.  
  112.       /* TIM2 enable counter */
  113.       TIM_Cmd(TIM2, ENABLE);
  114. }
Advertisement
Add Comment
Please, Sign In to add comment