单片机
返回首页

STM32 TIM1 F1 四通道完全重映射PWM 配置

2019-04-09 来源:eefocus

void TIM1_PWM_Init(u16 arr,u16 psc)

{  

GPIO_InitTypeDef GPIO_InitStructure;

TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

TIM_OCInitTypeDef  TIM_OCInitStructure;

 

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);// 

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE , ENABLE);  

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);                                                                   

    GPIO_PinRemapConfig(GPIO_FullRemap_TIM1,ENABLE);                                                                    

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; //TIM1_CH3

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOE, &GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //TIM1_CH4

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOE, &GPIO_InitStructure);

 


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //TIM1_CH2

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOE, &GPIO_InitStructure);

 

 

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; 

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOE, &GPIO_InitStructure);

    GPIO_ResetBits(GPIOE,GPIO_Pin_9);

 


TIM_TimeBaseStructure.TIM_Period = arr; 

TIM_TimeBaseStructure.TIM_Prescaler =psc;  

TIM_TimeBaseStructure.TIM_ClockDivision = 0; 

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  

TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); 

 

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 

TIM_OCInitStructure.TIM_Pulse = 0; 

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

TIM_OC1Init(TIM1, &TIM_OCInitStructure);  


TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 

TIM_OCInitStructure.TIM_Pulse = 0; 

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; 

TIM_OC2Init(TIM1, &TIM_OCInitStructure);  



TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 

TIM_OCInitStructure.TIM_Pulse = 0; 

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; 

TIM_OC3Init(TIM1, &TIM_OCInitStructure);  


TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 

TIM_OCInitStructure.TIM_Pulse = 0; 

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; 

TIM_OC4Init(TIM1, &TIM_OCInitStructure);  

    TIM_CtrlPWMOutputs(TIM1,ENABLE);

 

TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);   

TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);  

TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);  // 

TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);  //  


TIM_ARRPreloadConfig(TIM1, ENABLE); //


TIM_Cmd(TIM1, ENABLE);  //

 

   

}

进入单片机查看更多内容>>
相关视频
  • RISC-V嵌入式系统开发

  • SOC系统级芯片设计实验

  • 云龙51单片机实训视频教程(王云,字幕版)

  • 2022 Digi-Key KOL 系列: 你见过1GHz主频的单片机吗?Teensy 4.1开发板介绍

  • TI 新一代 C2000™ 微控制器:全方位助力伺服及马达驱动应用

  • MSP430电容触摸技术 - 防水Demo演示

精选电路图
  • 简洁的过零调功器电路设计与分析

  • 光控电路设计与分析

  • 永不缺相启动运行的电动机控制电路

  • 比较常见的功率整流器和滤波电路

  • 基于M66T旋律发​​生器的电路图解析

  • 基于CA3193的热电偶放大器电路

    相关电子头条文章