硬汉嵌入式论坛

 找回密码
 立即注册
查看: 1609|回复: 0
收起左侧

[TIMER] STM32H7单定时器实现简单易用的4通道PWM不同占空比方法

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107101
QQ
发表于 2018-8-16 01:34:06 | 显示全部楼层 |阅读模式
说明:

这个之前已经有发过例子,详见:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86445


现在H7的部分定时器是带6个通道的,官方的这个例子是测试了4个通道。

PE.09 (TIM1_Channel1)
PE.11(TIM1_Channel2)
PE.13 (TIM1_Channel3)
PA.11(TIM1_Channel4)

测量下前两个通道效果:
QQ截图20180816163954.png



代码:
  1. /* Private typedef -----------------------------------------------------------*/
  2. #define  PERIOD_VALUE       (uint32_t)(1000 - 1)  /* Period Value  */
  3. #define  PULSE1_VALUE       (uint32_t)(PERIOD_VALUE/2)        /* Capture Compare 1 Value  */
  4. #define  PULSE2_VALUE       (uint32_t)(PERIOD_VALUE*37.5/100) /* Capture Compare 2 Value  */
  5. #define  PULSE3_VALUE       (uint32_t)(PERIOD_VALUE/4)        /* Capture Compare 3 Value  */
  6. #define  PULSE4_VALUE       (uint32_t)(PERIOD_VALUE*12.5/100) /* Capture Compare 4 Value  */

  7. /* Private define ------------------------------------------------------------*/
  8. /* Private macro -------------------------------------------------------------*/
  9. /* Private variables ---------------------------------------------------------*/
  10. /* Timer handler declaration */
  11. TIM_HandleTypeDef    TimHandle;

  12. /* Timer Output Compare Configuration Structure declaration */
  13. TIM_OC_InitTypeDef sConfig;

  14. /* Counter Prescaler value */
  15. uint32_t uhPrescalerValue = 0;

  16. /* Private function prototypes -----------------------------------------------*/
  17. static void SystemClock_Config(void);
  18. static void Error_Handler(void);
  19. static void CPU_CACHE_Enable(void);

  20. /* Private functions ---------------------------------------------------------*/

  21. /**
  22.   * @brief  Main program.
  23.   * @param  None
  24.   * @retval None
  25.   */
  26. int main(void)
  27. {
  28.   /* Enable the CPU Cache */
  29.   CPU_CACHE_Enable();

  30.   /* STM32H7xx HAL library initialization:
  31.        - Systick timer is configured by default as source of time base, but user
  32.          can eventually implement his proper time base source (a general purpose
  33.          timer for example or other time source), keeping in mind that Time base
  34.          duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  35.          handled in milliseconds basis.
  36.        - Set NVIC Group Priority to 4
  37.        - Low Level Initialization
  38.      */
  39.   HAL_Init();

  40.   /* Configure the system clock to 400 MHz */
  41.   SystemClock_Config();

  42.   /* Configure LED3 */
  43.   BSP_LED_Init(LED3);

  44.   /* Compute the prescaler value to have TIM1 counter clock equal to 20000000 Hz */
  45.   uhPrescalerValue = (uint32_t)(SystemCoreClock / (2*20000000)) - 1;


  46.   /*##-1- Configure the TIM peripheral #######################################*/
  47.   /* -----------------------------------------------------------------------
  48.   TIM1 Configuration: generate 4 PWM signals with 4 different duty cycles.

  49.     In this example TIM1 input clock (TIM1CLK) is set to APB2 clock (PCLK2),
  50.     since APB2 prescaler is equal to 2.
  51.       TIM1CLK = 2*PCLK2
  52.       PCLK2 = HCLK/2 as AHB Clock divider is set to RCC_HCLK_DIV2
  53.       => TIM1CLK = HCLK = SystemCoreClock/2

  54.     To get TIM1 counter clock at 20 MHz, the prescaler is computed as follows:
  55.        Prescaler = (TIM1CLK / TIM1 counter clock) - 1
  56.        Prescaler = ((SystemCoreClock) /(2*20 MHz)) - 1

  57.     To get TIM1 output clock at 20 KHz, the period (ARR)) is computed as follows:
  58.        ARR = (TIM1 counter clock / TIM1 output clock) - 1
  59.            = 999

  60.     TIM1 Channel1 duty cycle = (TIM1_CCR1/ TIM1_ARR + 1)* 100 = 50%
  61.     TIM1 Channel2 duty cycle = (TIM1_CCR2/ TIM1_ARR + 1)* 100 = 37.5%
  62.     TIM1 Channel3 duty cycle = (TIM1_CCR3/ TIM1_ARR + 1)* 100 = 25%
  63.     TIM1 Channel4 duty cycle = (TIM1_CCR4/ TIM1_ARR + 1)* 100 = 12.5%

  64.     Note:
  65.      SystemCoreClock variable holds HCLK frequency and is defined in system_stm32h7xx.c file.
  66.      Each time the core clock (HCLK) changes, user had to update SystemCoreClock
  67.      variable value. Otherwise, any configuration based on this variable will be incorrect.
  68.      This variable is updated in three ways:
  69.       1) by calling CMSIS function SystemCoreClockUpdate()
  70.       2) by calling HAL API function HAL_RCC_GetSysClockFreq()
  71.       3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  72.   ----------------------------------------------------------------------- */

  73.   /* Initialize TIMx peripheral as follows:
  74.        + Prescaler = (SystemCoreClock / (2*20000000)) - 1
  75.        + Period = (1000 - 1)
  76.        + ClockDivision = 0
  77.        + Counter direction = Up
  78.   */
  79.   TimHandle.Instance = TIMx;

  80.   TimHandle.Init.Prescaler         = uhPrescalerValue;
  81.   TimHandle.Init.Period            = PERIOD_VALUE;
  82.   TimHandle.Init.ClockDivision     = 0;
  83.   TimHandle.Init.CounterMode       = TIM_COUNTERMODE_UP;
  84.   TimHandle.Init.RepetitionCounter = 0;
  85.   if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
  86.   {
  87.     /* Initialization Error */
  88.     Error_Handler();
  89.   }

  90.   /*##-2- Configure the PWM channels #########################################*/
  91.   /* Common configuration for all channels */
  92.   sConfig.OCMode       = TIM_OCMODE_PWM1;
  93.   sConfig.OCPolarity   = TIM_OCPOLARITY_HIGH;
  94.   sConfig.OCFastMode   = TIM_OCFAST_DISABLE;
  95.   sConfig.OCNPolarity  = TIM_OCNPOLARITY_HIGH;
  96.   sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;

  97.   sConfig.OCIdleState  = TIM_OCIDLESTATE_RESET;

  98.   /* Set the pulse value for channel 1 */
  99.   sConfig.Pulse = PULSE1_VALUE;
  100.   if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK)
  101.   {
  102.     /* Configuration Error */
  103.     Error_Handler();
  104.   }

  105.   /* Set the pulse value for channel 2 */
  106.   sConfig.Pulse = PULSE2_VALUE;
  107.   if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK)
  108.   {
  109.     /* Configuration Error */
  110.     Error_Handler();
  111.   }

  112.   /* Set the pulse value for channel 3 */
  113.   sConfig.Pulse = PULSE3_VALUE;
  114.   if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3) != HAL_OK)
  115.   {
  116.     /* Configuration Error */
  117.     Error_Handler();
  118.   }

  119.   /* Set the pulse value for channel 4 */
  120.   sConfig.Pulse = PULSE4_VALUE;
  121.   if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_4) != HAL_OK)
  122.   {
  123.     /* Configuration Error */
  124.     Error_Handler();
  125.   }

  126.   /*##-3- Start PWM signals generation #######################################*/
  127.   /* Start channel 1 */
  128.   if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
  129.   {
  130.     /* PWM Generation Error */
  131.     Error_Handler();
  132.   }
  133.   /* Start channel 2 */
  134.   if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK)
  135.   {
  136.     /* PWM Generation Error */
  137.     Error_Handler();
  138.   }
  139.   /* Start channel 3 */
  140.   if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK)
  141.   {
  142.     /* PWM generation Error */
  143.     Error_Handler();
  144.   }
  145.   /* Start channel 4 */
  146.   if (HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_4) != HAL_OK)
  147.   {
  148.     /* PWM generation Error */
  149.     Error_Handler();
  150.   }

  151.   while (1)
  152.   {
  153.   }

  154. }
复制代码








回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-17 20:20 , Processed in 0.161903 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表