초보의 아웅다웅 설계하기/STM32

STM32F103 - PB10, PB11 PWM REMAP

로망와니 2021. 12. 14. 13:30

 

 

 

 

void PWM2_Init(u32 a_nPrescaler, u32 a_nPeriod, u8 a_nDuty)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

/* TIM2 Remap */
GPIO_PinRemapConfig (AFIO_MAPR_TIM2_REMAP, ENABLE);

//GPIO_PinRemapConfig (AFIO_MAPR_TIM2_REMAP_0, ENABLE);
//GPIO_PinRemapConfig (AFIO_MAPR_TIM2_REMAP_1, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* GPIOB Configuration:TIM1 Channel 4 alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = a_nPeriod - 1;
  TIM_TimeBaseStructure.TIM_Prescaler = a_nPrescaler - 1;
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter=0; 

  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  /* PWM1 Mode configuration: Channel 1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_Pulse = a_nDuty;//a_nDuty;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; 

  TIM_OC4Init(TIM2, &TIM_OCInitStructure);

  TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);

  TIM_ARRPreloadConfig(TIM2, ENABLE);

  /* TIM1 enable counter */
  TIM_Cmd(TIM2, ENABLE);
TIM_CtrlPWMOutputs(TIM2, ENABLE);
}

'초보의 아웅다웅 설계하기 > STM32' 카테고리의 다른 글

STM32F DAC, Digital POT  (0) 2022.02.03
HMAC-SHA256(KISA)  (0) 2021.12.14
SOFT SPI - nRF24L01/STM32F103  (0) 2021.12.12
nRF24L01 + STM32L073  (0) 2021.12.05
STM32F103 Unique ID Read  (0) 2021.06.27