MightyPork

Untitled

Apr 5th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. void xx() {
  2.     RCC_APB2PeriphClockCmd(RCC_APB2ENR_TIM1EN, ENABLE);
  3.     RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOAEN, ENABLE);
  4.  
  5.     GPIO_InitTypeDef gpio_cfg;
  6.     gpio_cfg.GPIO_Pin = GPIO8;
  7.     gpio_cfg.GPIO_Mode = GPIO_Mode_AF;
  8.     gpio_cfg.GPIO_Speed = GPIO_Speed_50MHz;
  9.     gpio_cfg.GPIO_OType = GPIO_OType_PP;
  10.     gpio_cfg.GPIO_PuPd = GPIO_PuPd_NOPULL;
  11.     GPIO_Init(GPIOA, &gpio_cfg);
  12.  
  13.     GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_6);
  14.  
  15.     // timer core
  16.     TIM_TimeBaseInitTypeDef tbinit = {0,};
  17.     tbinit.TIM_Period = 400;
  18.     tbinit.TIM_Prescaler = 0;
  19.     tbinit.TIM_ClockDivision = TIM_CKD_DIV1;
  20.     tbinit.TIM_CounterMode = TIM_CounterMode_Up;
  21.     tbinit.TIM_RepetitionCounter = 0x0000;
  22.     TIM_TimeBaseInit(TIM1, &tbinit);
  23.     TIM_Cmd(TIM1, ENABLE); // start the timer
  24.  
  25.     // pwm generation
  26.     TIM_OCInitTypeDef ocinit = {0,};
  27.     ocinit.TIM_OCMode = TIM_OCMode_PWM1;
  28.     ocinit.TIM_Pulse = 200;
  29.     ocinit.TIM_OutputState = TIM_OutputState_Enable;
  30.     ocinit.TIM_OCPolarity = TIM_OCPolarity_High;
  31.     TIM_OC1Init(TIM1, &ocinit);
  32.     TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
  33. }
Advertisement
Add Comment
Please, Sign In to add comment