This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C1294输出PWM,修改不了占空比

先附上代码:

//
// 使能M0PWM外设  
//  
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);    
 
//  
// PWM不分频  
//  
MAP_SysCtlPWMClockSet(SYSCTL_PWMDIV_1);    
 
//  
// Configure the PWM0 to count down without synchronization  
// 配置GEN3递减计数模式不同步模式  
//  
PWMGenConfigure(PWM0_BASE,PWM_GEN_3,PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
   
//  
// 设置PWM周期  
// N=(1/f)*SysClk,f is the desired frequency  
//  
PWMGenPeriodSet(PWM0_BASE,PWM_GEN_3,100);    
 
//  
// 设置PWM占空比  
// Default 50%  
//  
PWMPulseWidthSet(PWM0_BASE,PWM_OUT_7,50);    
 
//  
// 使能PWM输出状态  
//  
PWMOutputState(PWM0_BASE,PWM_OUT_7_BIT,true);    
 
//  
// 使能PWM发生器  
//  
PWMGenEnable(PWM0_BASE,PWM_GEN_3);
 
这种情况下(SYSCLK:120M):
 
1、其他都不变,更改占空比,示波器查看正常;
 
2、其他都不会,修改PWM分频为MAP_SysCtlPWMClockSet(SYSCTL_PWMDIV_64);,示波器查看的周期没变;
 
3、其他都不变,修改周期100为30000,示波器查看的波形周期正常4K,但是占空比却不是50%,继续更改占空比为80,也没效果,占空比很小;
 
综上,就是1、修改分频没效果;2、更改了周期100数字之后,占空没有输出预期值。 初次涉及TM4C的芯片,有很多不懂的,不知道我这个问题出在哪了,谢谢
 
大家指点指点。
  • //  
    // 设置PWM周期  
    // N=(1/f)*SysClk,f is the desired frequency  
    //  
    PWMGenPeriodSet(PWM0_BASE,PWM_GEN_3,3000);    
     
    //  
    // 设置PWM占空比  
    // Default 50%  
    //  
    PWMPulseWidthSet(PWM0_BASE,PWM_OUT_7,1500); //如果占空比是50%, 这个值应该是1500/3000 而不是50.
    也可以参照TI官方例程写法,如下:

    //
    // Set the PWM period to 250Hz. To calculate the appropriate parameter
    // use the following equation: N = (1 / f) * SysClk. Where N is the
    // function parameter, f is the desired frequency, and SysClk is the
    // system clock frequency.
    // In this case you get: (1 / 250Hz) * 16MHz = 64000 cycles. Note that
    // the maximum period you can set is 2^16 - 1.
    // TODO: modify this calculation to use the clock frequency that you are
    // using.
    //
    PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, 64000);

    //
    // Set PWM0 PD0 to a duty cycle of 25%. You set the duty cycle as a
    // function of the period. Since the period was set above, you can use the
    // PWMGenPeriodGet() function. For this example the PWM will be high for
    // 25% of the time or 16000 clock cycles (64000 / 4).
    //
    PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0,PWMGenPeriodGet(PWM0_BASE, PWM_OUT_0) / 4); //这个是配置25%占空比的方法

    //
    // Enable the dead-band generation on the PWM0 output signal. PWM bit 0
    // (PD0), will have a duty cycle of 25% (set above) and PWM bit 1 will have
    // a duty cycle of 75%. These signals will have a 10us gap between the
    // rising and falling edges. This means that before PWM bit 1 goes high,
    // PWM bit 0 has been low for at LEAST 160 cycles (or 10us) and the same
    // before PWM bit 0 goes high. The dead-band generator lets you specify
    // the width of the "dead-band" delay, in PWM clock cycles, before the PWM
    // signal goes high and after the PWM signal falls. For this example we
    // will use 160 cycles (or 10us) on both the rising and falling edges of
    // PD0. Reference the datasheet for more information on dead-band
    // generation.
    //

      
  • 如果周期改为30000, 50%占空比,其占空比配置为15000。 

  • 谢谢大神,完美解决了问题。

     

    就是有一点,PWMGenPeriodGet(PWM0_BASE, PWM_OUT_0) / 4,这个官方历程函数用的有问题,申明函数是这样的:PWMGenPeriodGet(uint32_t u32Base, uint32_t u32Gen)。