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.

TM4C123GH6PM怎么用TIVA像dsp程序写入pwm发生器模块比较器的比较值



#define PWM_MACRO(ch1,ch2,ch3,v) \
\
/* Mfuncx range is (-1,1) */ \
/* The code below changes PeriodMax*Mfuncx range .... */ \
/* from (-PeriodMax,PeriodMax) to (0,PeriodMax) where HalfPerMax=PeriodMax/2 */ \
\
(*ePWM[ch1]).CMPA.half.CMPA = _IQmpy(v.HalfPerMax,v.MfuncC1)+ v.HalfPerMax; \
(*ePWM[ch2]).CMPA.half.CMPA = _IQmpy(v.HalfPerMax,v.MfuncC2)+ v.HalfPerMax; \
(*ePWM[ch3]).CMPA.half.CMPA = _IQmpy(v.HalfPerMax,v.MfuncC3)+ v.HalfPerMax; \

#endif // __F2833X_PWM_H__

  • PWM模块可以直接配置周期和占空比,函数如下(pwm.c文件中)

    //*****************************************************************************
    //
    //! Sets the period of a PWM generator.
    //!
    //! \param ui32Base is the base address of the PWM module.
    //! \param ui32Gen is the PWM generator to be modified. This parameter must be
    //! one of \b PWM_GEN_0, \b PWM_GEN_1, \b PWM_GEN_2, or \b PWM_GEN_3.
    //! \param ui32Period specifies the period of PWM generator output, measured
    //! in clock ticks.
    //!
    //! This function sets the period of the specified PWM generator block, where
    //! the period of the generator block is defined as the number of PWM clock
    //! ticks between pulses on the generator block zero signal.
    //!
    //! \note Any subsequent calls made to this function before an update occurs
    //! cause the previous values to be overwritten.
    //!
    //! \return None.
    //
    //*****************************************************************************
    void
    PWMGenPeriodSet(uint32_t ui32Base, uint32_t ui32Gen,
    uint32_t ui32Period)
    {
    //
    // Check the arguments.
    //
    ASSERT((ui32Base == PWM0_BASE) || (ui32Base == PWM1_BASE));
    ASSERT(_PWMGenValid(ui32Gen));

    //
    // Compute the generator's base address.
    //
    ui32Gen = PWM_GEN_BADDR(ui32Base, ui32Gen);

    //
    // Set the reload register based on the mode.
    //
    if(HWREG(ui32Gen + PWM_O_X_CTL) & PWM_X_CTL_MODE)
    {
    //
    // In up/down count mode, set the reload register to half the requested
    // period.
    //
    ASSERT((ui32Period / 2) < 65536);
    HWREG(ui32Gen + PWM_O_X_LOAD) = ui32Period / 2;
    }
    else
    {
    //
    // In down count mode, set the reload register to the requested period
    // minus one.
    //
    ASSERT((ui32Period <= 65536) && (ui32Period != 0));
    HWREG(ui32Gen + PWM_O_X_LOAD) = ui32Period - 1;
    }
    }

    //*****************************************************************************
    //
    //! Sets the pulse width for the specified PWM output.
    //!
    //! \param ui32Base is the base address of the PWM module.
    //! \param ui32PWMOut is the PWM output to modify. This parameter must be one
    //! of \b PWM_OUT_0, \b PWM_OUT_1, \b PWM_OUT_2, \b PWM_OUT_3, \b PWM_OUT_4,
    //! \b PWM_OUT_5, \b PWM_OUT_6, or \b PWM_OUT_7.
    //! \param ui32Width specifies the width of the positive portion of the pulse.
    //!
    //! This function sets the pulse width for the specified PWM output, where the
    //! pulse width is defined as the number of PWM clock ticks.
    //!
    //! \note Any subsequent calls made to this function before an update occurs
    //! cause the previous values to be overwritten.
    //!
    //! \return None.
    //
    //*****************************************************************************
    void
    PWMPulseWidthSet(uint32_t ui32Base, uint32_t ui32PWMOut,
    uint32_t ui32Width)
    {
    uint32_t ui32GenBase, ui32Reg;

    //
    // Check the arguments.
    //
    ASSERT((ui32Base == PWM0_BASE) || (ui32Base == PWM1_BASE));
    ASSERT(_PWMOutValid(ui32PWMOut));

    //
    // Compute the generator's base address.
    //
    ui32GenBase = PWM_OUT_BADDR(ui32Base, ui32PWMOut);

    //
    // If the counter is in up/down count mode, divide the width by two.
    //
    if(HWREG(ui32GenBase + PWM_O_X_CTL) & PWM_X_CTL_MODE)
    {
    ui32Width /= 2;
    }

    //
    // Get the period.
    //
    ui32Reg = HWREG(ui32GenBase + PWM_O_X_LOAD);

    //
    // Make sure the width is not too large.
    //
    ASSERT(ui32Width < ui32Reg);

    //
    // Compute the compare value.
    //
    ui32Reg = ui32Reg - ui32Width;

    //
    // Write to the appropriate registers.
    //
    if(PWM_IS_OUTPUT_ODD(ui32PWMOut))
    {
    HWREG(ui32GenBase + PWM_O_X_CMPB) = ui32Reg;
    }
    else
    {
    HWREG(ui32GenBase + PWM_O_X_CMPA) = ui32Reg;
    }
    }

  • 有个PWM配置函数的,你可以看看驱动手册,详细了解下该函数每个参数的作用。每种MCU的配置函数都是不一样的,要想像DSP中那样实现,你只能操作寄存器自己写了。