GPT PWM

我想在EK-RA6M3板子上输出一个PWM波,频率是4Mhz的,但是我最多只能设置为4Mhz,然后频率需要微调,比如4.01M4.02M(步进10Khz左右的PWM),请问要怎么设置才能设置4.01Mhz呢?我这边只能设置为4Mhz。

  • 6m3上面的gpt时钟源PCLKD是120Mhz的,所以我periodset设置了30输出4M的PWM

  • 120M->变4M, 设置的数值是30,  其他频率要设置的数值x = 4.01,  就是count = x*30/4, 得到的浮点值30.074再转换成十六进制 还是30, 所以是分辨率不够造成的, 
    #define GPT_EXAMPLE_MSEC_PER_SEC (1000)
    #define GPT_EXAMPLE_DESIRED_PERIOD_MSEC (20)
    /* This example shows how to calculate a new period value at runtime. */
    void gpt_period_calculation_example (void)
    {
    fsp_err_t err = FSP_SUCCESS;
    /* Initializes the module. */
    err = R_GPT_Open(&g_timer0_ctrl, &g_timer0_cfg);
    /* Handle any errors. This function should be defined by the user. */
    assert(FSP_SUCCESS == err);
    /* Start the timer. */
    (void) R_GPT_Start(&g_timer0_ctrl);
    /* Get the source clock frequency (in Hz). There are 3 ways to do this in FSP:
    * - If the PCLKD frequency has not changed since reset, the source clock frequency is
    * BSP_STARTUP_PCLKD_HZ >> timer_cfg_t::source_div
    * - Use the R_GPT_InfoGet function (it accounts for the divider).
    * - Calculate the current PCLKD frequency using R_FSP_SystemClockHzGet(FSP_PRIV_CLOCK_PCLKD) and right shift
    * by timer_cfg_t::source_div.
    *
    * This example uses the 3rd option (R_FSP_SystemClockHzGet).
    */
    uint32_t pclkd_freq_hz = R_FSP_SystemClockHzGet(FSP_PRIV_CLOCK_PCLKD) >> g_timer0_cfg.source_div;
    /* Calculate the desired period based on the current clock. Note that this calculation could overflow if the
    * desired period is larger than UINT32_MAX / pclkd_freq_hz. A cast to uint64_t is used to prevent this. */
    uint32_t period_counts =
    (uint32_t) (((uint64_t) pclkd_freq_hz * GPT_EXAMPLE_DESIRED_PERIOD_MSEC) / GPT_EXAMPLE_MSEC_PER_SEC);
    /* Set the calculated period. */
    err = R_GPT_PeriodSet(&g_timer0_ctrl, period_counts);
    assert(FSP_SUCCESS == err);
  • 所以如果我要4.01MHZ的PWM波我要怎么修改呢?我还是不太懂,如果可以麻烦讲得详细一些谢谢

  • 因为根据这个我改GPT_EXAMPLE_MSEC_PER_SEC (4010000)   GPT_EXAMPLE_DESIRED_PERIOD_MSEC (1)也设置不出4.01Mhz出来

  • 120M时钟输出4MHz的PWM, 在周期寄存器里面设置的数值是29 (30 -1), 4M / 30 = 0.133M, 也就是在周期寄存器里面变化1个count, 实际频率变化为0.133M, 你的目标是要变化0.01M, 这个相差了13倍

    例程只是给的一个方法, 简单地说, 4M对应的period_counts = 30(忽略减1),  那么目标频率为4.01, 这对应的period_counts = 4.01*30/4 = 30.075. 那么对映16进制数还是30, 所以你测到的频率没有变化.

          

    如此, 分辨率不够, 无法实现变化0.01M的频率变化.

  • RA系列的MCU里面的GPT的时钟源PCLKD最大是不是只有120Mhz,如果这样的话那就是不能实现0.01M的频率变化了?

  • 每变化一个count, 变化0.1333Mhz