当前位置:   article > 正文

【STM32】STM32F4系列PWM与死区互补PWM 库函数配置_stm32怎么配置pwm ch1 ch1n

stm32怎么配置pwm ch1 ch1n

如何查看引脚

以TIM14为例,查看IO口资源分布图

标红TIM14_CH1为TIM14的通道1。由于TIM14只有一个通道,所有只有CH1。例如TIM2就拥有4个通道(CH1,CH2,CH3,CH4)

使用到的寄存器

TIMx_CCMR1

TIMx_CCER

TIMx_CCR1

其他寄存器查看STM32F4xx中文参考手册

使用到的结构体

TIM_OCInitTypeDef

  1. typedef struct
  2. {
  3. uint16_t TIM_OCMode; //输出模式
  4. uint16_t TIM_OutputState; //输出状态
  5. uint16_t TIM_OutputNState; //互补通道的输出状态
  6. uint16_t TIM_Pulse; //占空比
  7. uint16_t TIM_OCPolarity; //输出极性
  8. uint16_t TIM_OCNPolarity; //互补通道的输出极性
  9. uint16_t TIM_OCIdleState; //空闲状态
  10. uint16_t TIM_OCNIdleState; //互补通道的空闲状态
  11. } TIM_OCInitTypeDef;

TIM_BDTRInitTypeDef

  1. typedef struct
  2. {
  3. uint16_t TIM_OSSRState; /*指定在运行模式下使用的“关闭状态”选项。*/
  4. uint16_t TIM_OSSIState; /*指定在空闲状态中使用的关闭状态*/
  5. uint16_t TIM_LOCKLevel; /*指定锁级别参数.*/
  6. uint16_t TIM_DeadTime; /*指定输出关断和接通之间的延迟时间*/
  7. uint16_t TIM_Break; /*指定是否启用 TIM 中断输入 */
  8. uint16_t TIM_BreakPolarity; /*指定 TIM 断开输入引脚极性*/
  9. uint16_t TIM_AutomaticOutput; /*指定是否启用 TIM 自动输出功能 */
  10. } TIM_BDTRInitTypeDef;

TIM_OSSRState:

TIM_OSSRState_Enable

使能TIM OSSR状态

TIM_OSSRState_Disable

失能TIM OSSR状态

TIM_OSSIState

TIM_OSSIState_Enable

使能TIM OSSI状态

TIM_OSSIState_Disable

失能TIM OSSI状态

TIM_LOCKLevel

TIM_LOCKLevel_OFF

不锁任何位

TIM_LOCKLevel_1

使用锁电平1

TIM_LOCKLevel_2

使用锁电平2

TIM_LOCKLevel_3

使用锁电平3

TIM_DeadTime

计算参考这篇文章https://blog.csdn.net/hmc_123/article/details/109549573

TIM_Break

TIM_Break_Enable

使能TIM刹车输入

TIM_Break_Diable

失能TIM刹车输入

TIM_BreakPolarity

TIM_BreakPolarity_Low

TIM刹车输入管脚极性低

TIM_BreakPolarity_High

TIM刹车输入管脚极性高

TIM_AutomaticOutput

TIM_AutomaticOutput_Enable

自动输出功能使能

TIM_AutomaticOutput_Diable

自动输出功能失能

TIM14做PWM输出

配置步骤:

  1. 调用对应RCC时钟函数

  1.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE);
  2.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
  1. 将IO配置为复用模式及初始化

  1.     GPIO_InitTypeDef GPIO_PWMTYPE;
  2.     GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14);
  3.     GPIO_PWMTYPE.GPIO_Pin = GPIO_Pin_9;
  4. GPIO_PWMTYPE.GPIO_OType = GPIO_OType_PP;
  5. GPIO_PWMTYPE.GPIO_Mode = GPIO_Mode_AF;
  6. GPIO_PWMTYPE.GPIO_Speed = GPIO_Speed_100MHz;
  7. GPIO_PWMTYPE.GPIO_PuPd = GPIO_PuPd_UP;
  8. GPIO_Init(GPIOF,&GPIO_PWMTYPE);
  1. 将定时器进行初始化

  1.     TIM_TimeBaseInitTypeDef TIM_PWMTYPE;
  2.     TIM_PWMTYPE.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
  3. TIM_PWMTYPE.TIM_Period = arr; //自动重装载值
  4. TIM_PWMTYPE.TIM_Prescaler = psc; //预分频
  5. TIM_PWMTYPE.TIM_ClockDivision = TIM_CKD_DIV1;
  6. TIM_TimeBaseInit(TIM14,&TIM_PWMTYPE);
  1. 配置OC初始化外设函数

  1.     TIM_OCInitTypeDef TIM_OCInitType;
  2.     TIM_OCInitType.TIM_OCMode = TIM_OCMode_PWM2; //模式2
  3. TIM_OCInitType.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
  4. TIM_OCInitType.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低
  5. TIM_OC1Init(TIM14,&TIM_OCInitType); //初始化配置TIM14在CCR1的各个参数
  1. 使能通道1预装载寄存器

TIM_OC1PreloadConfig(TIM14,TIM_OCPreload_Enable);      //使能TIM14在CCR1上的预装载寄存器
  1. ARPE进行使能

TIM_ARRPreloadConfig(TIM14,ENABLE);                  //ARPE使能
  1. 使能定时器

TIM_Cmd(TIM14,ENABLE);                               //使能定时器
  1. 主函数对中断进行分组

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  1. 对频率进行计算

  1. TIM14的频率为84Mhz,我们将频率取为2k
  2. 公式为 84000000/2000=42000;及ARR为42000,psc为0
  3. 也可以使用Tout=((arr+1)*(psc+1))/Ft us.
  4. Tout为时间 单位:us
  5. Ft=定时器工作频率,单位:Mhz
  6. 将频率转换为时钟T=1/F:T=1/2000=500 us
  7. psc我们取值为0;计算arr
  8. 500=(arr+1*0+1)/84 = 500*84 =42,000;
  1. 使用TIM_SetCompare1();函数进行赋值

TIM_SetCompare1(TIM14,PWM_DATA);

TIM14 PWM例程

  1. void Tim14_PWM_Init(u16 arr,u16 psc)
  2. {
  3. GPIO_InitTypeDef GPIO_PWMTYPE;
  4. TIM_TimeBaseInitTypeDef TIM_PWMTYPE;
  5. TIM_OCInitTypeDef TIM_OCInitType;
  6. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
  7. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE);
  8. GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14);
  9. GPIO_PWMTYPE.GPIO_Pin = GPIO_Pin_9;
  10. GPIO_PWMTYPE.GPIO_OType = GPIO_OType_PP;
  11. GPIO_PWMTYPE.GPIO_Mode = GPIO_Mode_AF;
  12. GPIO_PWMTYPE.GPIO_Speed = GPIO_Speed_100MHz;
  13. GPIO_PWMTYPE.GPIO_PuPd = GPIO_PuPd_UP;
  14. GPIO_Init(GPIOF,&GPIO_PWMTYPE);
  15. TIM_PWMTYPE.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
  16. TIM_PWMTYPE.TIM_Period = arr; //自动重装载值
  17. TIM_PWMTYPE.TIM_Prescaler = psc; //预分频
  18. TIM_PWMTYPE.TIM_ClockDivision = TIM_CKD_DIV1;
  19. TIM_TimeBaseInit(TIM14,&TIM_PWMTYPE);
  20. TIM_OCInitType.TIM_OCMode = TIM_OCMode_PWM2; //模式2
  21. TIM_OCInitType.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
  22. TIM_OCInitType.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低
  23. TIM_OC1Init(TIM14,&TIM_OCInitType); //初始化配置TIM14在CCR1的各个参数
  24. TIM_OC1PreloadConfig(TIM14,TIM_OCPreload_Enable); //使能TIM14在CCR1上的预装载寄存器
  25. TIM_ARRPreloadConfig(TIM14,ENABLE); //ARPE使能
  26. TIM_Cmd(TIM14,ENABLE); //使能定时器
  27. }
  28. int main(void)
  29. {
  30. u16 PWM_DATA=0;
  31. // u8 t=1;
  32. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  33. uart_init(115200);
  34. delay_init(168);
  35. Tim14_PWM_Init(2100,0);
  36. while(1)
  37. {
  38. delay_ms(5);
  39. // if(t){PWM_DATA++;}
  40. // else {PWM_DATA--;}
  41. // if(PWM_DATA>2797) t=0;
  42. // if(PWM_DATA==5) t=1;
  43. PWM_DATA=1600;
  44. TIM_SetCompare1(TIM14,PWM_DATA);
  45. }
  46. }

TIM1_CH1,CH1N死区互补PWM例程

  1. void TIMER1_Init_PWM(void)
  2. {
  3. GPIO_InitTypeDef GPIO_PWMInit;
  4. TIM_TimeBaseInitTypeDef TIM1_TIMERType;
  5. TIM_OCInitTypeDef TIM1_PWMOC; //选择第一通道
  6. TIM_BDTRInitTypeDef TIM1_BDTRType;
  7. //启动对应RCC时钟
  8. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
  9. RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
  10. /
  11. //复用引脚///
  12. GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_TIM1);
  13. GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_TIM1);
  14. //初始化定义
  15. GPIO_PWMInit.GPIO_Mode = GPIO_Mode_AF;
  16. GPIO_PWMInit.GPIO_OType = GPIO_OType_PP;
  17. GPIO_PWMInit.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8;
  18. GPIO_PWMInit.GPIO_Speed = GPIO_Speed_100MHz;
  19. GPIO_PWMInit.GPIO_PuPd = GPIO_PuPd_UP;
  20. GPIO_Init(GPIOA,&GPIO_PWMInit);
  21. 定时器初始化定义/
  22. TIM1_TIMERType.TIM_ClockDivision = TIM_CKD_DIV1;
  23. TIM1_TIMERType.TIM_Period = 2787; //自动重装载值
  24. TIM1_TIMERType.TIM_Prescaler = 0; //分频系数
  25. TIM1_TIMERType.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
  26. TIM_TimeBaseInit(TIM1,&TIM1_TIMERType);
  27. //PWM初始化配置/
  28. TIM1_PWMOC.TIM_OCMode = TIM_OCMode_PWM1; //使用脉冲调制宽度模式1
  29. TIM1_PWMOC.TIM_Pulse = 0; //占空比 取值必须在0x0000到0xFFFF
  30. TIM1_PWMOC.TIM_OCIdleState = TIM_OCIdleState_Reset; //输出空闲时为低电平
  31. TIM1_PWMOC.TIM_OutputState = TIM_OutputState_Enable; //输出使能
  32. TIM1_PWMOC.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性高
  33. TIM1_PWMOC.TIM_OutputNState = TIM_OutputNState_Enable; //互补输出打开
  34. TIM1_PWMOC.TIM_OCNIdleState = TIM_OCNIdleState_Reset; //互补输出空闲时为低电平
  35. TIM1_PWMOC.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补输出极性为高
  36. TIM_OC1Init(TIM1,&TIM1_PWMOC);
  37. TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);
  38. ///
  39. 刹车死区配置///
  40. TIM1_BDTRType.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; //自动输出功能使能
  41. TIM1_BDTRType.TIM_Break = TIM_Break_Disable; //失能刹车输入
  42. TIM1_BDTRType.TIM_BreakPolarity = TIM_BreakPolarity_High; //刹车输入管脚极性高
  43. TIM1_BDTRType.TIM_DeadTime = 0x14; //死区时间配置 参考CSDN计算方法,这里是3us
  44. TIM1_BDTRType.TIM_LOCKLevel = TIM_LOCKLevel_OFF; //锁电平参数:不锁任何位
  45. TIM1_BDTRType.TIM_OSSIState = TIM_OSSIState_Disable; //设置在运行模式下非工作状态
  46. TIM1_BDTRType.TIM_OSSRState = TIM_OSSRState_Disable; //设置在运行模式下非工作状态
  47. TIM_BDTRConfig(TIM1,&TIM1_BDTRType);
  48. ///
  49. TIM_ARRPreloadConfig(TIM1,ENABLE); //ARPE使能
  50. TIM_Cmd(TIM1,ENABLE); //使能TIM1
  51. TIM_CtrlPWMOutputs(TIM1,ENABLE); //开启OC和OCN输出
  52. }
  53. int main(void)
  54. {
  55. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  56. delay_init(168);
  57. TIMER1_Init_PWM();
  58. LED_init();
  59. while(1){
  60. TIM_SetCompare1(TIM1,1394);
  61. LED0 = !LED0;
  62. delay_ms(500);
  63. }
  64. }

输出波形图片

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/281087
推荐阅读
相关标签
  

闽ICP备14008679号