用STM32F101VC做PWM:
程序如下:
////////////////////////////////////////////
void Timer3Init(void)
{
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Time base configuration */
//--------------------For pwm--------------
//----------第一步:配置时钟 ---------------
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
//--------------------For pwm--------------
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
//TIM_InternalClockConfig(TIM3);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = 35;//8+1=9
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* Prescaler configuration */
// TIM_PrescalerConfig(TIM3, 1, TIM_PSCReloadMode_Immediate);
/* Output Compare Timing Mode configuration: Channel1 */
// TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
// TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
// TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
// TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
// TIM_OC1Init(TIM3, &TIM_OCInitStructure);
// TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
/* TIM IT enable */
// TIM_ITConfig(TIM3, TIM_IT_Update , ENABLE);
/* TIM2 enable counter */
TIM_Cmd(TIM3, DISABLE);
// TIM_SelectOnePulseMode(TIM3, TIM_OPMode_Single);
// NVIC_InitStructure.NVIC_IRQChannel = TIM3_UP_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//------------For PWM----------------------
//-----------第二步,配置goio口-------------
GPIO_InitStructure2.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure2.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure2.GPIO_Mode=GPIO_Mode_AF_PP; //设置为复用浮空输出
GPIO_Init(GPIOC,&GPIO_InitStructure2);
/*********TIM1_CH1N 引脚配置********/
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
//第三步,定时器基本配置
TIM_TimeBaseStructure.TIM_Period=1000-1; // 自动重装载寄存器的值
TIM_TimeBaseStructure.TIM_Prescaler=72-1; // 时钟预分频数
TIM_TimeBaseStructure.TIM_ClockDivision=0; // 采样分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上计数
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;//重复寄存器,用于自动更新pwm占空比
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//第四步pwm输出配置
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1; //设置为pwm1输出模式
TIM_OCInitStructure.TIM_Pulse=500; //设置占空比时间
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low; //设置输出极性
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High; //设置互补端输出极性
//下面几个参数是高级定时器才会用到,通用定时器不用配置
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; //使能该通道输出
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;//使能互补端输出
TIM_OC1Init(TIM3,&TIM_OCInitStructure); //按照指定参数初始化
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);
//第四步pwm输出配置
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1; //设置为pwm1输出模式
TIM_OCInitStructure.TIM_Pulse=500; //设置占空比时间
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low; //设置输出极性
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High; //设置互补端输出极性
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; //使能该通道输出
//下面几个参数是高级定时器才会用到,通用定时器不用配置
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;//使能互补端输出
TIM_OC2Init(TIM3,&TIM_OCInitStructure); //按照指定参数初始化
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);
//第四步pwm输出配置
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1; //设置为pwm1输出模式
TIM_OCInitStructure.TIM_Pulse=500; //设置占空比时间
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low; //设置输出极性
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High; //设置互补端输出极性
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; //使能该通道输出
//下面几个参数是高级定时器才会用到,通用定时器不用配置
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;//使能互补端输出
TIM_OC3Init(TIM3,&TIM_OCInitStructure); //按照指定参数初始化
TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable);
//第四步pwm输出配置
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1; //设置为pwm1输出模式
TIM_OCInitStructure.TIM_Pulse=500; //设置占空比时间
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low; //设置输出极性
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High; //设置互补端输出极性
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; //使能该通道输出
//下面几个参数是高级定时器才会用到,通用定时器不用配置
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;//使能互补端输出
TIM_OC4Init(TIM3,&TIM_OCInitStructure); //按照指定参数初始化
TIM_OC4PreloadConfig(TIM3,TIM_OCPreload_Enable);
//第五步,死区和刹车功能配置,高级定时器才有的,通用定时器不用配置
//第六步,使能端的打开
//使能TIMx在ARR上的预装载寄存器
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
TIM_Cmd(TIM3,ENABLE);
//打开TIM2
TIM_ARRPreloadConfig(TIM3, ENABLE);//使能ARR预装载缓冲器
//下面这句是高级定时器才有的,输出pwm必须打开
TIM_CtrlPWMOutputs(TIM3, ENABLE); //pwm输出使能,一定要记得打开
//------------For PWM----------------------
}
我的PWM为什么没输出?用的是TIM3四个通道。 |