Advertisement
Guest User

PWM STM32F2XX problem

a guest
Feb 2nd, 2012
1,259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.25 KB | None | 0 0
  1. #include "stm32f2xx.h"
  2. #include "stm32f2xx_gpio.h"
  3. #include "stm32f2xx_rcc.h"
  4. #include "stm32f2xx_tim.h"
  5. #include "system_stm32f2xx.h"
  6.  
  7. /****************************************************************************/
  8. void main(void) {
  9.     TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  10.     TIM_OCInitTypeDef       TIM_OCInitStructure;
  11.     GPIO_InitTypeDef        GPIO_InitStructure;
  12.     uint16_t                period;
  13.     uint16_t                pulse;
  14.  
  15.     /* Initialize clock - defined in system_stm32f2xx.h */
  16.     SystemInit();
  17.  
  18.     /* Compute the value for the ARR register to have a period of 20 KHz */
  19.     period = (SystemCoreClock / 20000 ) - 1;   
  20.  
  21.     /* Compute the CCR1 value to generate a PWN signal with 50% duty cycle */
  22.     pulse = (uint16_t) (((uint32_t) 5 * (period - 1)) / 10);
  23.  
  24.     /* GPIOA clock enable */
  25.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  26.  
  27.     /* Initialize PA8, Alternative Function, 100Mhz, Output, Push-pull */
  28.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  29.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  30.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  31.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  32.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  33.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  34.     GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);
  35.  
  36.     /* TIM1 clock enable */
  37.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
  38.  
  39.     /* Timer Base configuration */
  40.     TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  41.     TIM_TimeBaseStructure.TIM_Prescaler = 0;
  42.     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  43.     TIM_TimeBaseStructure.TIM_Period = period;
  44.     TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  45.     TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  46.     TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
  47.  
  48.     /* Channel 1 output configuration */
  49.     TIM_OCStructInit(&TIM_OCInitStructure);
  50.     TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  51.     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  52.     TIM_OCInitStructure.TIM_Pulse = pulse;
  53.     TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  54.     TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  55.     TIM_OC1Init(TIM1, &TIM_OCInitStructure);
  56.  
  57.     /* TIM1 counter enable */
  58.     TIM_Cmd(TIM1, ENABLE);
  59.  
  60.     /* TIM1 Main Output Enable */
  61.     TIM_CtrlPWMOutputs(TIM1, ENABLE);
  62.  
  63.     /* forever... */
  64.     while (1) {
  65.         __asm("nop");
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement