STM32 TIM1 PWM 输出

     STM32F103x包含最多两个高级定时器(TIM1和TIM8),每个高级定时器可同时产生多达7路PWM输出,以TIM1为例,有TIM1_CH1和TIM1_CH1N、TIM1_CH2和TIM1_CH2N、TIM1_CH3和TIM1_CH3N、TIM1_CH4。通道1其实有两个输出通道对应:TIM1_CH1与TIM1_CH1N,TIM1_CH1N称为TIM1_CH1的互补通道,输出与TIM1_CH1相反的波形

STM32高级定时器TIM1的PWM输出配置(高级定时器与普通定时器的配置有所不同):

GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; 
TIM_OCInitTypeDef TIM_OCInitStructure; 

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);   

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_6|GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA,&GPIO_InitStructure);

TIM_TimeBaseInitStructure.TIM_Period = 100; //设置自动重装载寄存器周期的值

TIM_TimeBaseInitStructure.TIM_Prescaler = 3000;//设置用来作为TIMx时钟频率预分频值
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;//设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数模式
TIM_TimeBaseInit(TIM1, & TIM_TimeBaseInitStructure);

TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//PWM输出使能
         //TIM_OCInitStructure.TIM_OutputState=TIM_OutputNState_Disable;  //PWM输出使能
TIM_OCInitStructure.TIM_Pulse=10;
         //TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
        // TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;
//TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;
TIM_OC3Init(TIM1,&TIM_OCInitStructure);


TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse=10;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;
TIM_OC4Init(TIM1,&TIM_OCInitStructure);

TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);

TIM_Cmd(TIM1,ENABLE);//ʹÄÜ»òÕßʧÄÜTIMxÍâÉè

//下面这句是高级定时器才有,输出PWM必须打开

TIM_CtrlPWMOutputs(TIM1, ENABLE);                             //pwm输出


已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页