[应用相关] STM32 Timer PWM_Output

[复制链接]
 楼主| gaonaiweng 发表于 2022-4-30 12:10 | 显示全部楼层 |阅读模式
脉冲宽度调制模式可以产生一个由TIMx_ARR寄存器确定频率、由TIMx_CCRx寄存器确定占空比的信号。
下面是一个PWM模式1的例子。当TIMx_CNT<TIMx_CCRx时PWM信号参考OCxREF为高,否则为低。如果TIMx_CCRx中的比较值大于自动重装载值(TIMx_ARR),则OCxREF保持为’1’。如果比较值为0,则OCxREF保持为’0’。 下图为TIMx_ARR=8时边沿对齐的PWM波形实例。 图128 边沿对齐的PWM波形(ARR=8)


94307626cb631e360a.png
 楼主| gaonaiweng 发表于 2022-4-30 12:11 | 显示全部楼层
库函数
  1. STM32F10x_StdPeriph_Lib_V3.3.0\Project\STM32F10x_StdPeriph_Examples\TIM\PWM_Output
 楼主| gaonaiweng 发表于 2022-4-30 12:11 | 显示全部楼层
主要程序如下:
  1. uint16_t CCR1_Val = 333; //   333/666=0.5   占空比
  2. uint16_t CCR2_Val = 249; // 249/666=0.374
  3. uint16_t CCR3_Val = 166; // 166/666=0.25
  4. uint16_t CCR4_Val = 83;   // 83/666 =0.125
  5. uint16_t PrescalerValue = 0;

  6. /* Private function prototypes -----------------------------------------------*/
  7. void RCC_Configuration(void);
  8. void GPIO_Configuration(void);

  9. /* Private functions ---------------------------------------------------------*/

  10. /**
  11. * [url=home.php?mod=space&uid=247401]@brief[/url]   Main program
  12. * @param None
  13. * @retval None
  14. */
  15. int main(void)
  16. {
  17. /*!< At this stage the microcontroller clock setting is already configured,
  18.        this is done through SystemInit() function which is called from startup
  19.        file (startup_stm32f10x_xx.s) before to branch to application main.
  20.        To reconfigure the default setting of SystemInit() function, refer to
  21.        system_stm32f10x.c file
  22.      */   
  23.       
  24. /* System Clocks Configuration */
  25. RCC_Configuration();

  26. /* GPIO Configuration */
  27. GPIO_Configuration();

  28. /* -----------------------------------------------------------------------
  29.     TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
  30.     The TIM3CLK frequency is set to SystemCoreClock (Hz), to get TIM3 counter
  31.     clock at 24 MHz the Prescaler is computed as following:
  32.      - Prescaler = (TIM3CLK / TIM3 counter clock) - 1
  33.     SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density
  34.     and Connectivity line devices and to 24 MHz for Low-Density Value line and
  35.     Medium-Density Value line devices

  36.     The TIM3 is running at 36 KHz: TIM3 Frequency = TIM3 counter clock/(ARR + 1)
  37.                                                   = 24 MHz / 666 = 36 KHz   36036HZ
  38.     TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
  39.     TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
  40.     TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
  41.     TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
  42. ----------------------------------------------------------------------- */
  43. /* Compute the prescaler value */
  44. PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
  45. /* Time base configuration */
  46. TIM_TimeBaseStructure.TIM_Period = 665; //Autoreload value ARR
  47. TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  48. TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  49. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  50. TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

  51. /* PWM1 Mode configuration: Channel1 */
  52. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  53. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  54. TIM_OCInitStructure.TIM_Pulse = CCR1_Val; /* Set the Capture Compare Register value CCR1 */
  55. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  56. TIM_OC1Init(TIM3, &TIM_OCInitStructure);
  57. // Enables or disables the TIMx peripheral Preload register on CCR1.
  58. TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);

  59. /* PWM1 Mode configuration: Channel2 */
  60. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  61. TIM_OCInitStructure.TIM_Pulse = CCR2_Val;

  62. TIM_OC2Init(TIM3, &TIM_OCInitStructure);

  63. TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);

  64. /* PWM1 Mode configuration: Channel3 */
  65. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  66. TIM_OCInitStructure.TIM_Pulse = CCR3_Val;

  67. TIM_OC3Init(TIM3, &TIM_OCInitStructure);

  68. TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);

  69. /* PWM1 Mode configuration: Channel4 */
  70. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  71. TIM_OCInitStructure.TIM_Pulse = CCR4_Val;

  72. TIM_OC4Init(TIM3, &TIM_OCInitStructure);

  73. TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);

  74. TIM_ARRPreloadConfig(TIM3, ENABLE);

  75. /* TIM3 enable counter */
  76. TIM_Cmd(TIM3, ENABLE);

  77. while (1)
  78. {}
  79. }
 楼主| gaonaiweng 发表于 2022-4-30 12:12 | 显示全部楼层
通用TIMx (TIM2、TIM3、TIM4和TIM5)定时器功能包括:

● 16位向上、向下、向上/向下自动装载计数器

● 16位可编程(可以实时修改)预分频器,计数器时钟频率的分频系数为1~65536之间的任意数值

● 4个独立通道: ─ 输入捕获 ─ 输出比较 ─ PWM生成(边缘或中间对齐模式) ─ 单脉冲模式输出

● 使用外部信号控制定时器和定时器互连的同步电路

● 如下事件发生时产生中断/DMA: ─ 更新:计数器向上溢出/向下溢出,计数器初始化(通过软件或者内部/外部触发) ─ 触发事件(计数器启动、停止、初始化或者由内部/外部触发计数) ─ 输入捕获 ─ 输出比较

● 支持针对定位的增量(正交)编码器和霍尔传感器电路

● 触发输入作为外部时钟或者按周期的电流管理
您需要登录后才可以回帖 登录 | 注册

本版积分规则

79

主题

811

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部

79

主题

811

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部