原创 STM32----3----PWM

2011-5-9 10:13 2721 2 2 分类: MCU/ 嵌入式

STM32 4路PWM配置完全函数:

void PWM_Configuration(void)//PWM初始化函数
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;//定时器初始化结构
  TIM_OCInitTypeDef  TIM_OCInitStructure;//通道输出初始化结构
  GPIO_InitTypeDef GPIO_InitStructure;//输出通道的GPIO管脚结构
 //(1)TIM3初始化,时间基部分
  TIM_TimeBaseStructure.TIM_Period =999;          //周期0~999
  TIM_TimeBaseStructure.TIM_Prescaler = 71;          //时钟分频
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;       //时钟分割
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//模式
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);   //基本初始化
  //TIM_ITConfig(TIM3, TIM_IT_CC4, ENABLE);   //打开中断,中断需要这行代码
  //(2)TIM3通道初始化 ,CCP部分
  TIM_OCStructInit(& TIM_OCInitStructure);                             //默认参数
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;                    //工作状态
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;        //设定为输出,需要PWM输出才需要这行代码
  TIM_OCInitStructure.TIM_Pulse =250 ;                                 //占空长度
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;            //高电平
  TIM_OC4Init(TIM3, &TIM_OCInitStructure);                             //通道初始化

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;                    //工作状态
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;        //设定为输出,需要PWM输出才需要这行代码
  TIM_OCInitStructure.TIM_Pulse =500 ;                                 //占空长度
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;            //高电平
  TIM_OC3Init(TIM3, &TIM_OCInitStructure);                             //通道初始化
  TIM_OC3Init(TIM3, &TIM_OCInitStructure);  

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;                    //工作状态
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;        //设定为输出,需要PWM输出才需要这行代码
  TIM_OCInitStructure.TIM_Pulse =750 ;                                 //占空长度
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;            //高电平
  TIM_OC2Init(TIM3, &TIM_OCInitStructure);  

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;                    //工作状态
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;        //设定为输出,需要PWM输出才需要这行代码
  TIM_OCInitStructure.TIM_Pulse =900 ;                                 //占空长度
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;            //高电平
  TIM_OC1Init(TIM3, &TIM_OCInitStructure);          
  TIM_Cmd(TIM3, ENABLE);                   //使能TIM3,到此在TIM中已配置完成,下面开始配置GPIO            
 
   //(3)PWM输出通道初始化
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;//选择PC
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //管脚频率为50MHZ
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //模式为推挽输出
   GPIO_Init(GPIOC, &GPIO_InitStructure);   //初始化GPIOA寄存器
  GPIO_PinRemapConfig(GPIO_FullRemap_TIM3 , ENABLE);
                                                        
}

 

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
2
关闭 站长推荐上一条 /3 下一条