硬汉嵌入式论坛

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

[TIMER] STM32H7的PWM+DMA效果测试,发现测试RCR重复计数器没有效果,已解决是官方例子的bug

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106738
QQ
发表于 2018-8-14 13:29:44 | 显示全部楼层 |阅读模式
说明:
实现方法就是通过DMA更新捕获比较寄存器CCR,从而实现不同的占空比效果。

此时再配合定时器的重复计数器RCR,可以设置多少个更新周期后切换一次CCR捕获比较寄存器的数值。

实际调试发现也使能预装载了,即产生更新事件后才实际更新CCR的数值,但是设置的RCR值没有起到任何作用。

原因分析:
已经解决,是官方例子bug,看此贴就明白了:http://www.armbbs.cn/forum.php?mod=viewthread&tid=88977

效果:
QQ截图20180814132611.png
正常效果应该是下面这个:



程序:
  1. /**
  2.   * @brief  Main program.
  3.   * @param  None
  4.   * @retval None
  5.   */
  6. int main(void)
  7. {
  8.   
  9.   /* Enable the CPU Cache */
  10.   CPU_CACHE_Enable();

  11. /* This sample code shows how to use DMA with TIM3 Update request to transfer
  12.   Data from memory to TIM3 Capture Compare Register 3 (CCR3), through the
  13.   STM32H7xx HAL API. To proceed, 3 steps are required */

  14.   /* STM32H7xx HAL library initialization:
  15.        - Systick timer is configured by default as source of time base, but user
  16.          can eventually implement his proper time base source (a general purpose
  17.          timer for example or other time source), keeping in mind that Time base
  18.          duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  19.          handled in milliseconds basis.
  20.        - Set NVIC Group Priority to 4
  21.        - Low Level Initialization
  22.      */
  23.   HAL_Init();

  24.   /* Configure the system clock to 400 MHz */
  25.   SystemClock_Config();

  26.   /* Configure LED2 */
  27.   BSP_LED_Init(LED2);

  28.   /* Compute the value of ARR regiter to generate signal frequency at 17.57 Khz */
  29.   uwTimerPeriod = (uint32_t)(((SystemCoreClock/2) / 17570) - 1);
  30.   /* Compute CCR1 value to generate a duty cycle at 75% */
  31.   aCCValue_Buffer[0] = (uint32_t)(((uint32_t) 75 * (uwTimerPeriod - 1)) / 100);
  32.   /* Compute CCR2 value to generate a duty cycle at 50% */
  33.   aCCValue_Buffer[1] = (uint32_t)(((uint32_t) 50 * (uwTimerPeriod - 1)) / 100);
  34.   /* Compute CCR3 value to generate a duty cycle at 25% */
  35.   aCCValue_Buffer[2] = (uint32_t)(((uint32_t) 25 * (uwTimerPeriod - 1)) / 100);

  36.   /* Clean Data Cache to update the content of the SRAM to be used by the DMA */
  37.   SCB_CleanDCache_by_Addr((uint32_t *) aCCValue_Buffer, sizeof(aCCValue_Buffer) );
  38.   
  39.   /*##-1- Configure the TIM peripheral #######################################*/
  40.   /* ---------------------------------------------------------------------------
  41.   TIM3 input clock (TIM3CLK) is set to APB1 clock (PCLK1)x2, since APB1
  42.   prescaler is 4.
  43.     TIM3CLK = PCLK1*2
  44.     PCLK1 = HCLK/2
  45.     => TIM3CLK = HCLK/2 = SystemCoreClock/2

  46.   TIM3CLK = (SystemCoreClock/2), Prescaler = 0, TIM3 counter clock = (SystemCoreClock/2)
  47.   SystemCoreClock is set to 400 MHz for STM32H7xx devices.

  48.   The objective is to configure TIM3 channel 3 to generate a PWM
  49.   signal with a frequency equal to 17.57 KHz:
  50.      - TIM3_Period = ((SystemCoreClock/2) / 17570) - 1
  51.   and a variable duty cycle that is changed by the DMA after a specific number of
  52.   Update DMA request.

  53.   The number of this repetitive requests is defined by the TIM3 Repetition counter,
  54.   each 4 Update Requests, the TIM3 Channel 3 Duty Cycle changes to the next new
  55.   value defined by the aCCValue_Buffer.

  56.     Note:
  57.      SystemCoreClock variable holds HCLK frequency and is defined in system_stm32h7xx.c file.
  58.      Each time the core clock (HCLK) changes, user had to update SystemCoreClock
  59.      variable value. Otherwise, any configuration based on this variable will be incorrect.
  60.      This variable is updated in three ways:
  61.       1) by calling CMSIS function SystemCoreClockUpdate()
  62.       2) by calling HAL API function HAL_RCC_GetSysClockFreq()
  63.       3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  64.   -----------------------------------------------------------------------------*/
  65.   /* Initialize TIM3 peripheral as follows:
  66.       + Period = TimerPeriod (To have an output frequency equal to 17.570 KHz)
  67.       + Repetition Counter = 3
  68.       + Prescaler = 0
  69.       + ClockDivision = 0
  70.       + Counter direction = Up
  71.   */
  72.   TimHandle.Instance = TIMx;

  73.   TimHandle.Init.Period            = uwTimerPeriod;
  74.   TimHandle.Init.RepetitionCounter = 1;
  75.   TimHandle.Init.Prescaler         = 0;
  76.   TimHandle.Init.ClockDivision     = 0;
  77.   TimHandle.Init.CounterMode       = TIM_COUNTERMODE_UP;
  78.   if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
  79.   {
  80.     /* Initialization Error */
  81.     Error_Handler();
  82.   }

  83.   /*##-2- Configure the PWM channel 3 ########################################*/
  84.   sConfig.OCMode       = TIM_OCMODE_PWM1;
  85.   sConfig.OCPolarity   = TIM_OCPOLARITY_HIGH;
  86.   sConfig.Pulse        = aCCValue_Buffer[0];
  87.   sConfig.OCNPolarity  = TIM_OCNPOLARITY_HIGH;
  88.   sConfig.OCFastMode   = TIM_OCFAST_DISABLE;
  89.   sConfig.OCIdleState  = TIM_OCIDLESTATE_RESET;
  90.   sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  91.   if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3) != HAL_OK)
  92.   {
  93.     /* Configuration Error */
  94.     Error_Handler();
  95.   }

  96.   /*##-3- Start PWM signal generation in DMA mode ############################*/
  97.   if (HAL_TIM_PWM_Start_DMA(&TimHandle, TIM_CHANNEL_3, aCCValue_Buffer, 3) != HAL_OK)
  98.   {
  99.     /* Starting Error */
  100.     Error_Handler();
  101.   }

  102.   while (1)
  103.   {
  104.   }

  105. }
复制代码


















回复

使用道具 举报

1

主题

34

回帖

37

积分

新手上路

积分
37
发表于 2018-8-14 13:35:01 | 显示全部楼层
帮顶!!!!
咱们的H7开发板今年能出吗
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106738
QQ
 楼主| 发表于 2018-8-14 17:00:31 | 显示全部楼层
张家村村长 发表于 2018-8-14 13:35
帮顶!!!!
咱们的H7开发板今年能出吗

还没准
回复

使用道具 举报

0

主题

2

回帖

2

积分

新手上路

积分
2
发表于 2018-9-18 21:12:52 | 显示全部楼层
大神 这个DMA不用配置吗?
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106738
QQ
 楼主| 发表于 2018-9-19 01:42:50 | 显示全部楼层
易水萧风 发表于 2018-9-18 21:12
大神 这个DMA不用配置吗?

要的,ST的做法一般都是放在了stm32h7xx_hal_msp.c文件里面
回复

使用道具 举报

1

主题

8

回帖

11

积分

新手上路

积分
11
发表于 2024-4-7 14:07:55 | 显示全部楼层
本帖最后由 fairyArm 于 2024-4-7 14:32 编辑

大神,请教你一个问题。把某个GPIO脚配置到 TIM1 的复用脚时, TIM 里这个RCR寄存器是不是这个意思。 因为溢出后RCR 要减1,到0后才更新一次,所以。 那个脉冲引脚最终频率的时候是不是 (ARR+1)*(RCR+1) ?
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106738
QQ
 楼主| 发表于 2024-4-8 07:46:26 | 显示全部楼层
fairyArm 发表于 2024-4-7 14:07
大神,请教你一个问题。把某个GPIO脚配置到 TIM1 的复用脚时, TIM 里这个RCR寄存器是不是这个意思。 因为 ...

PSC一定的情况下,是的。
回复

使用道具 举报

1

主题

8

回帖

11

积分

新手上路

积分
11
发表于 2024-4-8 11:33:56 | 显示全部楼层
eric2013 发表于 2024-4-8 07:46
PSC一定的情况下,是的。

谢谢你的解答!
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-3 02:05 , Processed in 0.221608 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

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