今天用到N32G435RB的PB15引脚作为pwm脉冲输出,找了一圈资料,仅记录
void Tim9PwnInit(u32 arr,u32 psc)
{
GPIO_InitType GPIO_InitStructure;
TIM_TimeBaseInitType TIM_TimeBaseStructure;
OCInitType TIM_OCInitStructure;
/* TIM9 clock enable */
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM9, ENABLE);
/* GPIOA and GPIOB clock enable */
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO,ENABLE);
GPIO_InitStruct(&GPIO_InitStructure);
// 引脚配置
GPIO_InitStructure.Pin = SEND_IR_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Current = GPIO_DC_4mA;
GPIO_InitStructure.GPIO_Pull = GPIO_Pull_Up;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF1_TIM9;
GPIO_InitPeripheral(SEND_IR_PORT, &GPIO_InitStructure);
// 时基配置
TIM_TimeBaseStructure.Period = arr;
TIM_TimeBaseStructure.Prescaler = psc;
TIM_TimeBaseStructure.ClkDiv = 0;
TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
TIM_InitTimeBase(TIM9, &TIM_TimeBaseStructure);
//pwm通道配置
TIM_OCInitStructure.OcMode = TIM_OCMODE_PWM1;
TIM_OCInitStructure.OcPolarity = TIM_OC_POLARITY_HIGH;
TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
TIM_OCInitStructure.Pulse = 0;
TIM_InitOc4(TIM9, &TIM_OCInitStructure);
TIM_ConfigOc4Preload(TIM9, TIM_OC_PRE_LOAD_ENABLE);
TIM_ConfigArPreload(TIM9, ENABLE);
/* TIM9 enable counter */
TIM_Enable(TIM9, ENABLE);
TIM_SetCmp4(TIM9,0);// 0%占空比
}
初始化:
Tim9PwnInit(210-1, 0);
由于板子没有贴外部晶振,所以使用了内部高速时钟
RCC_DeInit();
RCC_EnableHsi(ENABLE);
RCC_ConfigSysclk(RCC_SYSCLK_SRC_HSI);