怎么用STM32配置产生PWM(一个定时器用输出比较方式产生不同频率的PWM)

时间:2021-06-20 19:32:49
STM32产生PWM是非常的方便的,要需要简单的设置定时器,即刻产生!当然,简单的设置对于新手来讲,也是麻烦的,主要包括: (1)使能定时器时钟
  1. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
复制代码

(2)定义相应的GPIO:

  1. /* PA2,3,4,5,6输出->Key_Up,Key_Down,Key_Left,Key_Right,Key_Ctrl */
  2. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6;
  3. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //下拉接地,检测输入的高电平
  4. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度
  5. GPIO_Init(GPIOA, &GPIO_InitStructure);
  6. /* PA7用于发出PWM波,即无线数据传送 */
  7. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  8. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 
  9. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度
  10. GPIO_Init(GPIOA, &GPIO_InitStructure);
复制代码


(3)如果是产生PWM(频率不变,占空比可变),记得打开PWM控制,在TIM_Configuration()中。
  1. TIM_Cmd(TIM3,ENABLE);

  2. /* TIM1 Main Output Enable */
  3. TIM_CtrlPWMOutputs(TIM1,ENABLE);
复制代码


利用定时器产生不同频率的PWM 有时候,需要产生不同频率的PWM,这个时候,设置与产生相同PWM的程序,有关键的不一样。 (一) 设置的原理 利用改变定时器输出比较通道的捕获值,当输出通道捕获值产生中断时,在中断中将捕获值改变,这时, 输出的I/O会产生一个电平翻转,利用这种办法,实现不同频率的PWM输出。 (二)关键设置 在定时器设置中:
  1. TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);
复制代码

在中断函数中:

  1. if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
  2. {
  3. TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
  4. capture = TIM_GetCapture2(TIM3);
  5. TIM_SetCompare2(TIM3, capture + Key_Value);
  6. }
复制代码


一个定时器四个通道,分别产生不同频率(这个例子网上也有)

  1. vu16 CCR1_Val = 32768;
  2. vu16 CCR2_Val = 16384;
  3. vu16 CCR3_Val = 8192;
  4. vu16 CCR4_Val = 4096;
  5. void TIM_Configuration(void)
  6. {
  7.   TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  8.   TIM_OCInitTypeDef TIM_OCInitStructure;
  9.   /* TIM2 clock enable */
  10.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  11.   /* ---------------------------------------------------------------
  12.   TIM2 Configuration: Output Compare Toggle Mode:
  13.   TIM2CLK = 36 MHz, Prescaler = 0x2, TIM2 counter clock = 12 MHz 
  14.   CC1 update rate = TIM2 counter clock / CCR1_Val = 366.2 Hz
  15.   CC2 update rate = TIM2 counter clock / CCR2_Val = 732.4 Hz 
  16.   CC3 update rate = TIM2 counter clock / CCR3_Val = 1464.8 Hz 
  17.   CC4 update rate = TIM2 counter clock / CCR4_Val = 2929.6 Hz 
  18.   --------------------------------------------------------------- *//* Time base configuration */
  19.   TIM_TimeBaseStructure.TIM_Period = 65535; 
  20.   TIM_TimeBaseStructure.TIM_Prescaler = 2; 
  21.   TIM_TimeBaseStructure.TIM_ClockDivision = 0; 
  22.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 

  23.   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);/* Channel 1 Configuration in PWM mode */
  24.   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; //PWM模式2
  25.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //正向通道有效
  26.   TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;//反向通道无效
  27.   TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //占空时间
  28.   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性
  29.   TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补端的极性 
  30.   TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  31.   TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;TIM_OC1Init(TIM2,&TIM_OCInitStructure); //通道1
  32.   TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);TIM_OCInitStructure.TIM_Pulse = CCR2_Val; //占空时间
  33.   TIM_OC2Init(TIM2,&TIM_OCInitStructure); //通道2
  34.   TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);TIM_OCInitStructure.TIM_Pulse = CCR3_Val; //占空时间
  35.   TIM_OC3Init(TIM2,&TIM_OCInitStructure); //通道3
  36.   TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Disable);TIM_OCInitStructure.TIM_Pulse = CCR4_Val; //占空时间
  37.   TIM_OC4Init(TIM2,&TIM_OCInitStructure); //通道4
  38.   TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Disable);
  39.   /* TIM2 counter enable */
  40.   TIM_Cmd(TIM2,ENABLE);

  41.   /* TIM2 Main Output Enable */
  42.   //TIM_CtrlPWMOutputs(TIM2,ENABLE);/* TIM IT enable */
  43.   TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
  44. }
  45. void GPIO_Configuration(void)
  46. {
  47.   GPIO_InitTypeDef GPIO_InitStructure;/*允许总线CLOCK,在使用GPIO之前必须允许相应端的时钟.从STM32的设计角度上说,没被允许的端将不接入时钟,也就不会耗能,这是STM32节能的一种技巧,*/

  48. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  49. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  50. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  51. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);

  52. /* PA2,3,4,5,6,7输出->LED1,LED2,LED3,LED4,LED5,LED6 */
  53. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  54. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; //开漏输出
  55. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度
  56. GPIO_Init(GPIOA, &GPIO_InitStructure);

  57. /* PB0,1输出->LED7,LED8*/
  58. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
  59. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; //开漏输出
  60. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度
  61. GPIO_Init(GPIOB, &GPIO_InitStructure);

  62. /* PA0,1->KEY_LEFT,KEY_RIGHT*/
  63. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
  64. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
  65. GPIO_Init(GPIOA, &GPIO_InitStructure);/* PC13->KEY_UP*/
  66. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  67. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
  68. GPIO_Init(GPIOC, &GPIO_InitStructure);/* PB5->KEY_DOWN*/
  69. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  70. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
  71. GPIO_Init(GPIOB, &GPIO_InitStructure);
  72. /* GPIOA Configuration:TIM2 Channel1, 2, 3 and 4 in Output */
  73. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  74. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  75. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  76. GPIO_Init(GPIOA, &GPIO_InitStructure);
  77. }
  78. void NVIC_Configuration(void)
  79. {
  80. NVIC_InitTypeDef NVIC_InitStructure;/* Configure one bit for preemption priority */
  81. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;
  82. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
  83. NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
  84. NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
  85. NVIC_Init(&NVIC_InitStructure);
  86. }

  87. u16 capture = 0;
  88. extern vu16 CCR1_Val;
  89. extern vu16 CCR2_Val;
  90. extern vu16 CCR3_Val;
  91. extern vu16 CCR4_Val;

  92. void TIM2_IRQHandler(void)
  93. {
  94. /* TIM2_CH1 toggling with frequency = 183.1 Hz */
  95. if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
  96. {
  97. TIM_ClearITPendingBit(TIM2, TIM_IT_CC1 );
  98. capture = TIM_GetCapture1(TIM2);
  99. TIM_SetCompare1(TIM2, capture + CCR1_Val );
  100. }

  101. /* TIM2_CH2 toggling with frequency = 366.2 Hz */
  102. if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
  103. {
  104. TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
  105. capture = TIM_GetCapture2(TIM2);
  106. TIM_SetCompare2(TIM2, capture + CCR2_Val);
  107. }/* TIM2_CH3 toggling with frequency = 732.4 Hz */
  108. if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)
  109. {
  110. TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
  111. capture = TIM_GetCapture3(TIM2);
  112. TIM_SetCompare3(TIM2, capture + CCR3_Val);
  113. }/* TIM2_CH4 toggling with frequency = 1464.8 Hz */
  114. if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET) 
  115. {
  116. TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);
  117. capture = TIM_GetCapture4(TIM2);
  118. TIM_SetCompare4(TIM2, capture + CCR4_Val);
  119. }}
复制代码


一个定时器一个通道,产生不同频率 其它的设置都一样,就是在主函数中修改一个参数,然后在定时器中断中,根据这个参数,改变频率。
  1. #include "stm32lib\\stm32f10x.h"
  2. #include "hal.h"volatile u16 Key_Value=1000; //用于保存按键相应的PWM波占空比值
  3. int main(void)
  4. {
  5. ChipHalInit();
  6. ChipOutHalInit();while(1)

  7. if( (!Get_Key_Up)&(!Get_Key_Down)&(!Get_Key_Left)&(!Get_Key_Right)&(!Get_Key_Ctrl) )
  8. {
  9. Key_Value=12000;
  10. }
  11. else 
  12. {
  13. if(Get_Key_Up) //按键前进按下 ,对应1kHz
  14. {
  15. Key_Value=6000;
  16. }
  17. else if(Get_Key_Down) //按键后退按下 ,对应2kHz
  18. {
  19. Key_Value=3000;
  20. }
  21. Delay_Ms(20); //10ms延时if(Get_Key_Left) //按键左转按下,对应3kHz
  22. {
  23. Key_Value=2000;
  24. }
  25. else if(Get_Key_Right) //按键右转按下,对应4kHz
  26. {
  27. Key_Value=1500;

  28. Delay_Ms(20); //10ms延时if(Get_Key_Ctrl) //按键控制按下,对应5kHz
  29. {
  30. Key_Value=1200;
  31. }
  32. Delay_Ms(20); //10ms延时 
  33. }
  34. }
  35. }extern volatile u16 Key_Value;
  36. u16 capture=0;
  37. void TIM3_IRQHandler(void)
  38. {
  39. /* TIM2_CH2 toggling with frequency = 366.2 Hz */
  40. if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
  41. {
  42. TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
  43. capture = TIM_GetCapture2(TIM3);
  44. TIM_SetCompare2(TIM3, capture + Key_Value);

  45. }void TIM3_Configuration(void)
  46. {
  47. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  48. TIM_OCInitTypeDef TIM_OCInitStructure;/* TIM2 clock enable */
  49. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);/*TIM1时钟配置*/
  50. TIM_TimeBaseStructure.TIM_Prescaler = 5; //预分频(时钟分频)72M/6=12M
  51. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
  52. TIM_TimeBaseStructure.TIM_Period = 65535; //装载值选择最大
  53. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  54. TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
  55. TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);/* Channel 1 Configuration in PWM mode */
  56. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; //PWM模式2
  57. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //正向通道有效
  58. TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;//反向通道无效
  59. TIM_OCInitStructure.TIM_Pulse = Key_Value; //占空时间
  60. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性
  61. TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补端的极性 
  62. TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  63. TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;TIM_OC2Init(TIM3,&TIM_OCInitStructure); //通道2
  64. TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);

  65. /* TIM1 counter enable */
  66. TIM_Cmd(TIM3,ENABLE);

  67. /* TIM1 Main Output Enable */
  68. //TIM_CtrlPWMOutputs(TIM1,ENABLE); 
  69. TIM_ITConfig(TIM3, TIM_IT_CC2 , ENABLE); 
  70. }
复制代码


注意:在计算PWM频率的时候,TIMx的时钟都是72Mhz,分频后,因为翻转两次才能形成一个PWM波,因为,PWM的频率是捕获改变频率的1/2。