📄 pwm.c
字号:
// The period is twice the reload register value.
//
return(HWREG(ulGen + PWM_O_X_LOAD) * 2);
}
else
{
//
// The period is the reload register value plus one.
//
return(HWREG(ulGen + PWM_O_X_LOAD) + 1);
}
}
//*****************************************************************************
//
//! Enables the timer/counter for a PWM generator block.
//!
//! \param ulBase is the base address of the PWM module.
//! \param ulGen is the PWM generator to be enabled. Must be one of
//! \b PWM_GEN_0, \b PWM_GEN_1, \b PWM_GEN_2, or \b PWM_GEN_3.
//!
//! This function allows the PWM clock to drive the timer/counter for the
//! specified generator block.
//!
//! \return None.
//
//*****************************************************************************
void
PWMGenEnable(unsigned long ulBase, unsigned long ulGen)
{
//
// Check the arguments.
//
ASSERT(ulBase == PWM_BASE);
ASSERT(PWMGenValid(ulGen));
//
// Enable the PWM generator.
//
HWREG(PWM_GEN_BADDR(ulBase, ulGen) + PWM_O_X_CTL) |= PWM_X_CTL_ENABLE;
}
//*****************************************************************************
//
//! Disables the timer/counter for a PWM generator block.
//!
//! \param ulBase is the base address of the PWM module.
//! \param ulGen is the PWM generator to be disabled. Must be one of
//! \b PWM_GEN_0, \b PWM_GEN_1, \b PWM_GEN_2, or \b PWM_GEN_3.
//!
//! This function blocks the PWM clock from driving the timer/counter for the
//! specified generator block.
//!
//! \return None.
//
//*****************************************************************************
void
PWMGenDisable(unsigned long ulBase, unsigned long ulGen)
{
//
// Check the arguments.
//
ASSERT(ulBase == PWM_BASE);
ASSERT(PWMGenValid(ulGen));
//
// Disable the PWM generator.
//
HWREG(PWM_GEN_BADDR(ulBase, + ulGen) + PWM_O_X_CTL) &= ~(PWM_X_CTL_ENABLE);
}
//*****************************************************************************
//
//! Sets the pulse width for the specified PWM output.
//!
//! \param ulBase is the base address of the PWM module.
//! \param ulPWMOut is the PWM output to modify. 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 ulWidth 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
//! will cause the previous values to be overwritten.
//!
//! \return None.
//
//*****************************************************************************
void
PWMPulseWidthSet(unsigned long ulBase, unsigned long ulPWMOut,
unsigned long ulWidth)
{
unsigned long ulGenBase, ulReg;
//
// Check the arguments.
//
ASSERT(ulBase == PWM_BASE);
ASSERT(PWMOutValid(ulPWMOut));
//
// Compute the generator's base address.
//
ulGenBase = PWM_OUT_BADDR(ulBase, ulPWMOut);
//
// If the counter is in up/down count mode, divide the width by two.
//
if(HWREG(ulGenBase + PWM_O_X_CTL) & PWM_X_CTL_MODE)
{
ulWidth /= 2;
}
//
// Get the period.
//
ulReg = HWREG(ulGenBase + PWM_O_X_LOAD);
//
// Make sure the width is not too large.
//
ASSERT(ulWidth < ulReg);
//
// Compute the compare value.
//
ulReg = ulReg - ulWidth;
//
// Write to the appropriate registers.
//
if(PWM_IS_OUTPUT_ODD(ulPWMOut))
{
HWREG(ulGenBase + PWM_O_X_CMPB) = ulReg;
}
else
{
HWREG(ulGenBase + PWM_O_X_CMPA) = ulReg;
}
}
//*****************************************************************************
//
//! Gets the pulse width of a PWM output.
//!
//! \param ulBase is the base address of the PWM module.
//! \param ulPWMOut is the PWM output to query. 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.
//!
//! This function gets the currently programmed pulse width for the specified
//! PWM output. If the update of the comparator for the specified output has
//! yet to be completed, the value returned may not be the active pulse width.
//! The value returned is the programmed pulse width, measured in PWM clock
//! ticks.
//!
//! \return Returns the width of the pulse in PWM clock ticks.
//
//*****************************************************************************
unsigned long
PWMPulseWidthGet(unsigned long ulBase, unsigned long ulPWMOut)
{
unsigned long ulGenBase, ulReg, ulLoad;
//
// Check the arguments.
//
ASSERT(ulBase == PWM_BASE);
ASSERT(PWMOutValid(ulPWMOut));
//
// Compute the generator's base address.
//
ulGenBase = PWM_OUT_BADDR(ulBase, ulPWMOut);
//
// Then compute the pulse width. If mode is UpDown, set
// width = (load - compare) * 2. Otherwise, set width = load - compare.
//
ulLoad = HWREG(ulGenBase + PWM_O_X_LOAD);
if(PWM_IS_OUTPUT_ODD(ulPWMOut))
{
ulReg = HWREG(ulGenBase + PWM_O_X_CMPB);
}
else
{
ulReg = HWREG(ulGenBase + PWM_O_X_CMPA);
}
ulReg = ulLoad - ulReg;
//
// If in up/down count mode, double the pulse width.
//
if(HWREG(ulGenBase + PWM_O_X_CTL) & PWM_X_CTL_MODE)
{
ulReg = ulReg * 2;
}
//
// Return the pulse width.
//
return(ulReg);
}
//*****************************************************************************
//
//! Enables the PWM dead band output, and sets the dead band delays.
//!
//! \param ulBase is the base address of the PWM module.
//! \param ulGen is the PWM generator to modify. Must be one of
//! \b PWM_GEN_0, \b PWM_GEN_1, \b PWM_GEN_2, or \b PWM_GEN_3.
//! \param usRise specifies the width of delay from the rising edge.
//! \param usFall specifies the width of delay from the falling edge.
//!
//! This function sets the dead bands for the specified PWM generator,
//! where the dead bands are defined as the number of \b PWM clock ticks
//! from the rising or falling edge of the generator's \b OutA signal.
//! Note that this function causes the coupling of \b OutB to \b OutA.
//!
//! \return None.
//
//*****************************************************************************
void
PWMDeadBandEnable(unsigned long ulBase, unsigned long ulGen,
unsigned short usRise, unsigned short usFall)
{
//
// Check the arguments.
//
ASSERT(ulBase == PWM_BASE);
ASSERT(PWMGenValid(ulGen));
ASSERT(usRise < 4096);
ASSERT(usFall < 4096);
//
// Compute the generator's base address.
//
ulGen = PWM_GEN_BADDR(ulBase, ulGen);
//
// Write the dead band delay values.
//
HWREG(ulGen + PWM_O_X_DBRISE) = usRise;
HWREG(ulGen + PWM_O_X_DBFALL) = usFall;
//
// Enable the deadband functionality.
//
HWREG(ulGen + PWM_O_X_DBCTL) |= PWM_X_DBCTL_ENABLE;
}
//*****************************************************************************
//
//! Disables the PWM dead band output.
//!
//! \param ulBase is the base address of the PWM module.
//! \param ulGen is the PWM generator to modify. Must be one of
//! \b PWM_GEN_0, \b PWM_GEN_1, \b PWM_GEN_2, or \b PWM_GEN_3.
//!
//! This function disables the dead band mode for the specified PWM generator.
//! Doing so decouples the \b OutA and \b OutB signals.
//!
//! \return None.
//
//*****************************************************************************
void
PWMDeadBandDisable(unsigned long ulBase, unsigned long ulGen)
{
//
// Check the arguments.
//
ASSERT(ulBase == PWM_BASE);
ASSERT(PWMGenValid(ulGen));
//
// Disable the deadband functionality.
//
HWREG(PWM_GEN_BADDR(ulBase, ulGen) + PWM_O_X_DBCTL) &=
~(PWM_X_DBCTL_ENABLE);
}
//*****************************************************************************
//
//! Synchronizes all pending updates.
//!
//! \param ulBase is the base address of the PWM module.
//! \param ulGenBits are the PWM generator blocks to be updated. Must be the
//! logical OR of any of \b PWM_GEN_0_BIT, \b PWM_GEN_1_BIT,
//! \b PWM_GEN_2_BIT, or \b PWM_GEN_3_BIT.
//!
//! For the selected PWM generators, this function causes all queued updates to
//! the period or pulse width to be applied the next time the corresponding
//! counter becomes zero.
//!
//! \return None.
//
//*****************************************************************************
void
PWMSyncUpdate(unsigned long ulBase, unsigned long ulGenBits)
{
//
// Check the arguments.
//
ASSERT(ulBase == PWM_BASE);
ASSERT(!(ulGenBits & ~(PWM_GEN_0_BIT | PWM_GEN_1_BIT | PWM_GEN_2_BIT |
PWM_GEN_3_BIT)));
//
// Synchronize pending PWM register changes.
//
HWREG(ulBase + PWM_O_CTL) = ulGenBits;
}
//*****************************************************************************
//
//! Synchronizes the counters in one or multiple PWM generator blocks.
//!
//! \param ulBase is the base address of the PWM module.
//! \param ulGenBits are the PWM generator blocks to be synchronized. Must be
//! the logical OR of any of \b PWM_GEN_0_BIT, \b PWM_GEN_1_BIT,
//! \b PWM_GEN_2_BIT, or \b PWM_GEN_3_BIT.
//!
//! For the selected PWM module, this function synchronizes the time base
//! of the generator blocks by causing the specified generator counters to be
//! reset to zero.
//!
//! \return None.
//
//*****************************************************************************
void
PWMSyncTimeBase(unsigned long ulBase, unsigned long ulGenBits)
{
//
// Check the arguments.
//
ASSERT(ulBase == PWM_BASE);
ASSERT(!(ulGenBits & ~(PWM_GEN_0_BIT | PWM_GEN_1_BIT | PWM_GEN_2_BIT |
PWM_GEN_3_BIT)));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -