硬汉嵌入式论坛

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

[TIMER] STM32H7的非对称PWM模式(Asymmetric PWM mode),波形的相位差可编程

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107122
QQ
发表于 2018-8-23 00:56:44 | 显示全部楼层 |阅读模式
说明:
官方配套的例子实现了一个TIM1的CH1作为触发,主控模式;TIM8作为从控模式。

他这模式有一点不太理解,相移后,占空比的计算,发现怎么算都不太对。

输出效果如下:
QQ截图20180823005449.png


  1. /* Private typedef -----------------------------------------------------------*/
  2. /* Private define ------------------------------------------------------------*/
  3. #define INITIAL_PHASE         (uint32_t) 15000
  4. #define INITIAL_LENGTH        (uint32_t) 5000
  5. #define PWM_FREQUENCY         (uint32_t) 22000
  6. /* Private macro -------------------------------------------------------------*/
  7. /* Private variables ---------------------------------------------------------*/
  8. /* Timer handler declaration */
  9. TIM_HandleTypeDef    TimHandle8;
  10. TIM_HandleTypeDef    TimHandle1;

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

  13. /* Master and slave configurations */
  14. TIM_SlaveConfigTypeDef  sSlaveConfig;
  15. TIM_MasterConfigTypeDef sMasterConfig;

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

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

  43.   /*##-1- Configure the TIM peripheral #######################################*/
  44.   /* ---------------------------------------------------------------------------
  45.      TIM8 is configured to generate an Asymetric signal with a programmable
  46.    Phase-Shifted signal on TIM8_CH2:
  47.    - TIM8 Channel 1 is configured in PWM2 mode
  48.    - TIM8 Channel 2 is configured in Asymetric PWM2 mode
  49.    - The counter mode is center aligned mode
  50.    - The pulse length and the phase shift are programmed consecutively in TIM8_CCR2 and TIM8_CCR1.
  51.    
  52.    TIM1 is configured to generating the reference signal on Channel1 used by TIM8:
  53.     - TIM1 is generating a PWM signal with frequency equal to 4.54 KHz
  54.     - TIM1 is used as master for TIM8, the update event of TIM1 genarates the Reset counter
  55.       of TIM8 to synchronize the two signals: the reference signal (TIM1_CH1) and
  56.             the shifted signal (TIM8_CH2).
  57.          
  58.     In this example TIM1 and TIM8 input clock (TIM18CLK) is set to APB2 clock (2*PCLK2)   
  59.     TIM1 and TIM8 signals are at frequency of  ((SystemCoreClock/2) / (PWM_FREQUENCY + 1))  
  60.       
  61.     TIM8 is gerating a signal with the following caracteristics:
  62.     - Pulse lenght = (TIM8_CCR1 + TIM8_CCR2) / TIM8_CLK
  63.     - Phase shift = TIM8_CCR1/TIM8_CLK
  64.       with TIM8_CLK = ((SystemCoreClock/2) / (Period + 1)), as the prescaler is equal to zero.
  65.   
  66.     Note:
  67.      SystemCoreClock variable holds HCLK frequency and is defined in system_stm32h7xx.c file.
  68.      Each time the core clock (HCLK) changes, user had to update SystemCoreClock
  69.      variable value. Otherwise, any configuration based on this variable will be incorrect.
  70.      This variable is updated in three ways:
  71.       1) by calling CMSIS function SystemCoreClockUpdate()
  72.       2) by calling HAL API function HAL_RCC_GetSysClockFreq()
  73.       3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency     
  74.   --------------------------------------------------------------------------- */
  75.   /* Initialize Timers: TIM1 & TIM8 */  
  76.   TimHandle1.Instance = TIM1;
  77.   TimHandle1.Init.Prescaler         = 0;
  78.   TimHandle1.Init.Period            = 2 * PWM_FREQUENCY;
  79.   TimHandle1.Init.ClockDivision     = 0;
  80.   TimHandle1.Init.CounterMode       = TIM_COUNTERMODE_UP;
  81.   TimHandle1.Init.RepetitionCounter = 0;
  82.   if(HAL_TIM_PWM_Init(&TimHandle1) != HAL_OK)
  83.   {
  84.     /* Initialization Error */
  85.     Error_Handler();
  86.   }
  87.   TimHandle8.Instance = TIM8;
  88.   TimHandle8.Init.Prescaler         = 0;
  89.   TimHandle8.Init.Period            = PWM_FREQUENCY;
  90.   TimHandle8.Init.ClockDivision     = 0;
  91.   TimHandle8.Init.CounterMode       = TIM_COUNTERMODE_CENTERALIGNED1;
  92.   TimHandle8.Init.RepetitionCounter = 0;
  93.   if(HAL_TIM_PWM_Init(&TimHandle8) != HAL_OK)
  94.   {
  95.     /* Initialization Error */
  96.     Error_Handler();
  97.   }

  98.   /*##-2- Configure the PWM channels #########################################*/
  99.   /* Channels 1&2 configuration on TIM8 */
  100.   sConfig.OCMode = TIM_OCMODE_PWM2;
  101.   sConfig.Pulse = INITIAL_PHASE;
  102.   sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
  103.   sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  104.   sConfig.OCFastMode = TIM_OCFAST_DISABLE;
  105.   sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
  106.   sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  107.   if(HAL_TIM_PWM_ConfigChannel(&TimHandle8, &sConfig, TIM_CHANNEL_1) != HAL_OK)
  108.   {
  109.     /* Configuration Error */
  110.     Error_Handler();
  111.   }
  112.   sConfig.OCMode = TIM_OCMODE_ASSYMETRIC_PWM2;
  113.   sConfig.Pulse = INITIAL_LENGTH;
  114.   if(HAL_TIM_PWM_ConfigChannel(&TimHandle8, &sConfig, TIM_CHANNEL_2) != HAL_OK)
  115.   {
  116.     /* Configuration Error */
  117.     Error_Handler();
  118.   }
  119.   
  120.   /* Channel1 configuration on TIM1 */
  121.   sConfig.OCMode = TIM_OCMODE_PWM1;
  122.   sConfig.Pulse = PWM_FREQUENCY;
  123.   if(HAL_TIM_PWM_ConfigChannel(&TimHandle1, &sConfig, TIM_CHANNEL_1) != HAL_OK)
  124.   {
  125.     /* Configuration Error */
  126.     Error_Handler();
  127.   }

  128.   /*##-3- Configure master/slave mode and trigger ############################*/
  129.   /* Synchronization between TIM1 and TIM8
  130.   The aim is to generate a reference signal on TIM1_CH1
  131.   The Phase-Shifted signal generated on TIM8_CH2 is compared to the reference
  132.   signal */
  133.   /* Configure TIM8 in slave mode: an active edge on  trigger input generates a
  134.   reset on TIM8 */
  135.   sSlaveConfig.SlaveMode        = TIM_SLAVEMODE_RESET;
  136.   sSlaveConfig.InputTrigger     = TIM_TS_ITR0;
  137.   sSlaveConfig.TriggerPolarity  = TIM_TRIGGERPOLARITY_NONINVERTED;
  138.   sSlaveConfig.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1;
  139.   sSlaveConfig.TriggerFilter    = 0;
  140.   if(HAL_TIM_SlaveConfigSynchronization(&TimHandle8, &sSlaveConfig) != HAL_OK)
  141.   {
  142.     /* Configuration Error */
  143.     Error_Handler();
  144.   }
  145.   /* Configure TIM1 in master mode */
  146.   sMasterConfig.MasterOutputTrigger  = TIM_TRGO_UPDATE;
  147.   sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
  148.   sMasterConfig.MasterSlaveMode      = TIM_MASTERSLAVEMODE_DISABLE;
  149.   if(HAL_TIMEx_MasterConfigSynchronization(&TimHandle1, &sMasterConfig) != HAL_OK)
  150.   {
  151.     /* Configuration Error */
  152.     Error_Handler();
  153.   }
  154.   
  155.   /*##-4- Start PWM signals generation #######################################*/
  156.   /* Start TIM1 channel 1 */
  157.   if(HAL_TIM_PWM_Start(&TimHandle1, TIM_CHANNEL_1) != HAL_OK)
  158.   {
  159.     /* PWM Generation Error */
  160.     Error_Handler();
  161.   }
  162.   /* Start TIM8 channel 1 */
  163.   if(HAL_TIM_PWM_Start(&TimHandle8, TIM_CHANNEL_1) != HAL_OK)
  164.   {
  165.     /* PWM Generation Error */
  166.     Error_Handler();
  167.   }
  168.   /* Start TIM8 channel 2 */
  169.   if(HAL_TIM_PWM_Start(&TimHandle8, TIM_CHANNEL_2) != HAL_OK)
  170.   {
  171.     /* PWM Generation Error */
  172.     Error_Handler();
  173.   }

  174.   while (1)
  175.   {
  176.   }
  177. }
复制代码
回复

使用道具 举报

56

主题

131

回帖

299

积分

高级会员

积分
299
发表于 2021-7-8 10:50:38 | 显示全部楼层
参照这个例子搬到G0上实现,不知道怎么算这个高低电平时间了
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107122
QQ
 楼主| 发表于 2021-7-8 11:11:41 | 显示全部楼层
lindahnu 发表于 2021-7-8 10:50
参照这个例子搬到G0上实现,不知道怎么算这个高低电平时间了

有点乱,没有再研究二楼。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-19 09:21 , Processed in 0.210586 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

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