[技术问答] M263A的EPWM Period应该怎么计算

[复制链接]
742|4
 楼主| alphax20 发表于 2020-4-22 17:10 | 显示全部楼层 |阅读模式
PWM, rio, ck, TE
  1. <font size="2">#include <stdio.h>
  2. #include "NuMicro.h"

  3. /*---------------------------------------------------------------------------------------------------------*/
  4. /* Macro, type and constant definitions                                                                    */
  5. /*---------------------------------------------------------------------------------------------------------*/


  6. /*---------------------------------------------------------------------------------------------------------*/
  7. /* Global variables                                                                                        */
  8. /*---------------------------------------------------------------------------------------------------------*/


  9. void SYS_Init(void)
  10. {
  11.     /* Set PF multi-function pins for XT1_OUT(PF.2) and XT1_IN(PF.3) */
  12.     SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF2MFP_Msk)) | XT1_OUT_PF2;
  13.     SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF3MFP_Msk)) | XT1_IN_PF3;

  14.     /*---------------------------------------------------------------------------------------------------------*/
  15.     /* Init System Clock                                                                                       */
  16.     /*---------------------------------------------------------------------------------------------------------*/
  17.     /* Enable HIRC clock (Internal RC 22.1184 MHz) */
  18.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  19.     /* Waiting for HIRC clock ready */
  20.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  21.     /* Select HCLK clock source as HIRC and and HCLK clock divider as 1 */
  22.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  23.     /* Enable HXT clock (external XTAL 12MHz) */
  24.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  25.     /* Waiting for HXT clock ready */
  26.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  27.     /* Enable PLL */
  28.     CLK->PLLCTL = CLK_PLLCTL_128MHz_HIRC;

  29.     /* Waiting for PLL stable */
  30.     CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);

  31.     /* Select HCLK clock source as PLL and HCLK source divider as 2 */
  32.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(2));

  33.     /* Waiting for PLL clock ready */
  34.     CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);

  35.     /* Enable EPWM1 module clock */
  36.     CLK_EnableModuleClock(EPWM1_MODULE);

  37.     /*---------------------------------------------------------------------------------------------------------*/
  38.     /* EPWM clock frequency configuration                                                                      */
  39.     /*---------------------------------------------------------------------------------------------------------*/

  40.     CLK_SetModuleClock(EPWM1_MODULE, CLK_CLKSEL2_EPWM1SEL_PCLK1, 0);

  41.     /* Enable UART module clock */
  42.     CLK_EnableModuleClock(UART0_MODULE);

  43.     /* Select UART module clock source as HXT and UART module clock divider as 1 */
  44.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HXT, CLK_CLKDIV0_UART0(1));

  45.     /* Reset EPWM1 module */
  46.     SYS_ResetModule(EPWM1_RST);

  47.     /* Update System Core Clock */
  48.     SystemCoreClockUpdate();

  49.     /*---------------------------------------------------------------------------------------------------------*/
  50.     /* Init I/O Multi-function                                                                                 */
  51.     /*---------------------------------------------------------------------------------------------------------*/
  52.     /* Set multi-function pins for UART0 RXD and TXD */
  53.     SYS->GPB_MFPH = (SYS->GPB_MFPH & (~(UART0_RXD_PB12_Msk | UART0_TXD_PB13_Msk))) | UART0_RXD_PB12 | UART0_TXD_PB13;

  54.     /* Set PC multi-function pins for EPWM1 Channel 0~1 */
  55.     SYS->GPC_MFPL = (SYS->GPC_MFPL & (~SYS_GPC_MFPL_PC5MFP_Msk)) | EPWM1_CH0_PC5;
  56.     SYS->GPC_MFPL = (SYS->GPC_MFPL & (~SYS_GPC_MFPL_PC4MFP_Msk)) | EPWM1_CH1_PC4;
  57. }

  58. void UART0_Init()
  59. {
  60.     /*---------------------------------------------------------------------------------------------------------*/
  61.     /* Init UART                                                                                               */
  62.     /*---------------------------------------------------------------------------------------------------------*/
  63.     /* Configure UART0 and set UART0 baud rate */
  64.     UART_Open(UART0, 115200);
  65. }


  66. /*---------------------------------------------------------------------------------------------------------*/
  67. /*  Main Function                                                                                          */
  68. /*---------------------------------------------------------------------------------------------------------*/
  69. int32_t main(void)
  70. {
  71.     /* Init System, IP clock and multi-function I/O
  72.        In the end of SYS_Init() will issue SYS_LockReg()
  73.        to lock protected register. If user want to write
  74.        protected register, please issue SYS_UnlockReg()
  75.        to unlock protected register if necessary */

  76.     /* Unlock protected registers */
  77.     SYS_UnlockReg();

  78.     /* Init System, IP clock and multi-function I/O */
  79.     SYS_Init();

  80.     /* Lock protected registers */
  81.     SYS_LockReg();

  82.     /* Init UART to 115200-8n1 for print message */
  83.     UART0_Init();

  84.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz(PLL@ %dHz)\n", SystemCoreClock, PllClock);
  85.     printf("+-----------------------------------------------------------+\n");
  86.     printf("|                      EPWM Driver                          |\n");
  87.     printf("|                                                           |\n");
  88.     printf("+-----------------------------------------------------------+\n");
  89.     printf("  This code will output waveform with EPWM1 channel 0~1.\n");
  90.     printf("  I/O configuration:\n");
  91.     printf("  EPWM1 channel 0: 100 kHz, duty 40%%.\n");
  92.     printf("  EPWM1 channel 1: 100 kHz, duty 60%%.\n");
  93.     printf("  Waveform output pin: EPWM1_CH0(D3), EPWM1_CH1(D2),\n");

  94.                 /* Set EPWM mode as complementary mode */
  95.                 EPWM_ENABLE_COMPLEMENTARY_MODE(EPWM1);
  96.                
  97.     /* EPWM1 channel 0~1 frequency and duty configuration are as follows */
  98.     EPWM_ConfigOutputChannel(EPWM1, 0, 100000, 40);

  99.     /* Enable output of EPWM1 channel 0~1 */
  100.     EPWM_EnableOutput(EPWM1, 0x3F);

  101.     /* Start EPWM1 counter */
  102.     EPWM_Start(EPWM1, 0x3F);

  103.     printf("Press any key to stop.\n");
  104.     getchar();

  105.     /* Start EPWM1 counter */
  106.     EPWM_ForceStop(EPWM1, 0x3F);

  107.     printf("Done.");
  108.     while(1);

  109. }</font>
用sample code修改得到了一对互补的EPWM,频率100Hz,占空比是40%和60%,怎么计算技术手册里边的EPWM Period?公式里的PERIOD,CLKPSC,EPWMx_CLK在这里都是多少?uvision里能不能看到对应计数器的动作(用来验证EPWM Period)? period.PNG
huangcunxiake 发表于 2020-4-22 23:05 | 显示全部楼层
看那个配置函数原型
huangcunxiake 发表于 2020-4-22 23:06 | 显示全部楼层
722495ea05d663844f.png
频率的倒数不就是周期吗
huangcunxiake 发表于 2020-4-22 23:07 | 显示全部楼层
我发的这个是029系列的库函数介绍,你用的这 系列应该也是这几个参数。
 楼主| alphax20 发表于 2020-4-23 16:50 | 显示全部楼层
huangcunxiake 发表于 2020-4-22 23:06
频率的倒数不就是周期吗

抱歉可能问题没说明白,我想知道的是这个counter的PERIOD是多少,记到几之后归零这样?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

3

主题

5

帖子

0

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