GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM1_BDTRInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);//
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA , ENABLE); //使能GPIO外设时钟使能
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO , ENABLE); //复用引脚
//设置该引脚为复用输出功能,输出TIM1 CH1的PWM脉冲波形
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //TIM_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //TIM_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//GPIO_PinRemapConfig(GPIO_PartialRemap_TIM1,ENABLE);
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 80K
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 不分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
///****** 配置BDTR寄存器,配置死区时间****************/
///*
//20 定时器时钟 72M TIM_ClockDivision = TIM_CKD_DIV1时, Tdts = 13.89ns
//21 0 - 1.764us 用算法一
//22 1.778us - 3.505us 用算法二
//23 3.556us - 7.000us 用算法三
//24 7.1117us - 14us 用算法四
//25 需要更长时间,使用TIM_ClockDivision分频
//26 */
// TIM1_BDTRInitStruct.TIM_OSSRState = TIM_OSSRState_Disable;
// TIM1_BDTRInitStruct.TIM_OSSIState = TIM_OSSIState_Disable;
// TIM1_BDTRInitStruct.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
// TIM1_BDTRInitStruct.TIM_DeadTime = 205; //死区时间 72:1us 172:3us 205:5us
// TIM_BDTRConfig(TIM1,&TIM1_BDTRInitStruct);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //比较输出使能 N
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 400; //设置待装入捕获比较寄存器的脉冲值
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; //输出极性:TIM输出比较极性高
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset ;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset ;
TIM_OC3Init(TIM1, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_CtrlPWMOutputs(TIM1,ENABLE); //MOE 主输出使能
//TIM_SetCompare4(TIM8,700);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH1预装载使能
TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIMx在ARR上的预装载寄存器
TIM_Cmd(TIM1, ENABLE); //使能TIM1
标红对的要注意