📄 lpc177x_8x_mcpwm.c
字号:
/**********************************************************************
* $Id$ lpc177x_8x_mcpwm.c 2011-06-02
*//**
* @file lpc177x_8x_mcpwm.c
* @brief Contains all functions support for Motor Control PWM
* firmware library on LPC177x_8x
* @version 1.0
* @date 02. June. 2011
* @author NXP MCU SW Application Team
*
* Copyright(C) 2011, NXP Semiconductor
* All rights reserved.
*
***********************************************************************
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* NXP Semiconductors assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. NXP Semiconductors
* reserves the right to make changes in the software without
* notification. NXP Semiconductors also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
**********************************************************************/
/* Peripheral group ----------------------------------------------------------- */
/** @addtogroup MCPWM
* @{
*/
/* Includes ------------------------------------------------------------------- */
#include "lpc177x_8x_mcpwm.h"
#include "lpc177x_8x_clkpwr.h"
/* Public Functions ----------------------------------------------------------- */
/** @addtogroup MCPWM_Public_Functions
* @{
*/
/*********************************************************************//**
* @brief Initializes the MCPWM peripheral
* @param[in] MCPWMx Motor Control PWM peripheral selected,
* Should be: LPC_MCPWM
* @return None
**********************************************************************/
void MCPWM_Init(LPC_MCPWM_TypeDef *MCPWMx)
{
/* Turn On MCPWM PCLK */
CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCMCPWM, ENABLE);
MCPWMx->CAP_CLR = MCPWM_CAPCLR_CAP(0) | MCPWM_CAPCLR_CAP(1) | MCPWM_CAPCLR_CAP(2);
MCPWMx->INTF_CLR = MCPWM_INT_ILIM(0) | MCPWM_INT_ILIM(1) | MCPWM_INT_ILIM(2) \
| MCPWM_INT_IMAT(0) | MCPWM_INT_IMAT(1) | MCPWM_INT_IMAT(2) \
| MCPWM_INT_ICAP(0) | MCPWM_INT_ICAP(1) | MCPWM_INT_ICAP(2);
MCPWMx->INTEN_CLR = MCPWM_INT_ILIM(0) | MCPWM_INT_ILIM(1) | MCPWM_INT_ILIM(2) \
| MCPWM_INT_IMAT(0) | MCPWM_INT_IMAT(1) | MCPWM_INT_IMAT(2) \
| MCPWM_INT_ICAP(0) | MCPWM_INT_ICAP(1) | MCPWM_INT_ICAP(2);
}
/*********************************************************************//**
* @brief Configures each channel in MCPWM peripheral according to the
* specified parameters in the MCPWM_CHANNEL_CFG_Type.
* @param[in] MCPWMx Motor Control PWM peripheral selected
* should be: LPC_MCPWM
* @param[in] channelNum Channel number, should be: 0..2.
* @param[in] channelSetup Pointer to a MCPWM_CHANNEL_CFG_Type structure
* that contains the configuration information for the
* specified MCPWM channel.
* @return None
**********************************************************************/
void MCPWM_ConfigChannel(LPC_MCPWM_TypeDef *MCPWMx, uint32_t channelNum,
MCPWM_CHANNEL_CFG_Type * channelSetup)
{
if ((channelNum >= 0) && (channelNum <= 2))
{
if (channelNum == MCPWM_CHANNEL_0)
{
MCPWMx->TC0 = channelSetup->channelTimercounterValue;
MCPWMx->LIM0 = channelSetup->channelPeriodValue;
MCPWMx->MAT0 = channelSetup->channelPulsewidthValue;
}
else if (channelNum == MCPWM_CHANNEL_1)
{
MCPWMx->TC1 = channelSetup->channelTimercounterValue;
MCPWMx->LIM1 = channelSetup->channelPeriodValue;
MCPWMx->MAT1 = channelSetup->channelPulsewidthValue;
}
else if (channelNum == MCPWM_CHANNEL_2)
{
MCPWMx->TC2 = channelSetup->channelTimercounterValue;
MCPWMx->LIM2 = channelSetup->channelPeriodValue;
MCPWMx->MAT2 = channelSetup->channelPulsewidthValue;
}
else
{
return;
}
if (channelSetup->channelType == MCPWM_CHANNEL_CENTER_MODE)
{
MCPWMx->CON_SET = MCPWM_CON_CENTER(channelNum);
}
else
{
MCPWMx->CON_CLR = MCPWM_CON_CENTER(channelNum);
}
if (channelSetup->channelPolarity == MCPWM_CHANNEL_PASSIVE_HI)
{
MCPWMx->CON_SET = MCPWM_CON_POLAR(channelNum);
}
else
{
MCPWMx->CON_CLR = MCPWM_CON_POLAR(channelNum);
}
if (channelSetup->channelDeadtimeEnable == ENABLE)
{
MCPWMx->CON_SET = MCPWM_CON_DTE(channelNum);
MCPWMx->DT &= ~(MCPWM_DT(channelNum, 0x3FF));
MCPWMx->DT |= MCPWM_DT(channelNum, channelSetup->channelDeadtimeValue);
}
else
{
MCPWMx->CON_CLR = MCPWM_CON_DTE(channelNum);
}
if (channelSetup->channelUpdateEnable == ENABLE)
{
MCPWMx->CON_CLR = MCPWM_CON_DISUP(channelNum);
}
else
{
MCPWMx->CON_SET = MCPWM_CON_DISUP(channelNum);
}
}
}
/*********************************************************************//**
* @brief Write to MCPWM shadow registers - Update the value for period
* and pulse width in MCPWM peripheral.
* @param[in] MCPWMx Motor Control PWM peripheral selected
* Should be: LPC_MCPWM
* @param[in] channelNum Channel Number, should be: 0..2.
* @param[in] channelSetup Pointer to a MCPWM_CHANNEL_CFG_Type structure
* that contains the configuration information for the
* specified MCPWM channel.
* @return None
**********************************************************************/
void MCPWM_WriteToShadow(LPC_MCPWM_TypeDef *MCPWMx, uint32_t channelNum,
MCPWM_CHANNEL_CFG_Type *channelSetup)
{
if (channelNum == MCPWM_CHANNEL_0)
{
MCPWMx->LIM0 = channelSetup->channelPeriodValue;
MCPWMx->MAT0 = channelSetup->channelPulsewidthValue;
}
else if (channelNum == MCPWM_CHANNEL_1)
{
MCPWMx->LIM1 = channelSetup->channelPeriodValue;
MCPWMx->MAT1 = channelSetup->channelPulsewidthValue;
}
else if (channelNum == MCPWM_CHANNEL_2)
{
MCPWMx->LIM2 = channelSetup->channelPeriodValue;
MCPWMx->MAT2 = channelSetup->channelPulsewidthValue;
}
}
/*********************************************************************//**
* @brief Configures capture function in MCPWM peripheral
* @param[in] MCPWMx Motor Control PWM peripheral selected
* Should be: LPC_MCPWM
* @param[in] channelNum MCI (Motor Control Input pin) number
* Should be: 0..2
* @param[in] captureConfig Pointer to a MCPWM_CAPTURE_CFG_Type structure
* that contains the configuration information for the
* specified MCPWM capture.
* @return
**********************************************************************/
void MCPWM_ConfigCapture(LPC_MCPWM_TypeDef *MCPWMx, uint32_t channelNum,
MCPWM_CAPTURE_CFG_Type *captureConfig)
{
if ((channelNum >= MCPWM_CHANNEL_0) && (channelNum <= MCPWM_CHANNEL_2))
{
if (captureConfig->captureFalling == ENABLE)
{
MCPWMx->CAPCON_SET = MCPWM_CAPCON_CAPMCI_FE(captureConfig->captureChannel, channelNum);
}
else
{
MCPWMx->CAPCON_CLR = MCPWM_CAPCON_CAPMCI_FE(captureConfig->captureChannel, channelNum);
}
if (captureConfig->captureRising == ENABLE)
{
MCPWMx->CAPCON_SET = MCPWM_CAPCON_CAPMCI_RE(captureConfig->captureChannel, channelNum);
}
else
{
MCPWMx->CAPCON_CLR = MCPWM_CAPCON_CAPMCI_RE(captureConfig->captureChannel, channelNum);
}
if (captureConfig->timerReset == ENABLE)
{
MCPWMx->CAPCON_SET = MCPWM_CAPCON_RT(captureConfig->captureChannel);
}
else
{
MCPWMx->CAPCON_CLR = MCPWM_CAPCON_RT(captureConfig->captureChannel);
}
if (captureConfig->hnfEnable == ENABLE)
{
MCPWMx->CAPCON_SET = MCPWM_CAPCON_HNFCAP(channelNum);
}
else
{
MCPWMx->CAPCON_CLR = MCPWM_CAPCON_HNFCAP(channelNum);
}
}
}
/*********************************************************************//**
* @brief Clears current captured value in specified capture channel
* @param[in] MCPWMx Motor Control PWM peripheral selected
* Should be: LPC_MCPWM
* @param[in] captureChannel Capture channel number, should be: 0..2
* @return None
**********************************************************************/
void MCPWM_ClearCapture(LPC_MCPWM_TypeDef *MCPWMx, uint32_t captureChannel)
{
MCPWMx->CAP_CLR = MCPWM_CAPCLR_CAP(captureChannel);
}
/*********************************************************************//**
* @brief Get current captured value in specified capture channel
* @param[in] MCPWMx Motor Control PWM peripheral selected,
* Should be: LPC_MCPWM
* @param[in] captureChannel Capture channel number, should be: 0..2
* @return None
**********************************************************************/
uint32_t MCPWM_GetCapture(LPC_MCPWM_TypeDef *MCPWMx, uint32_t captureChannel)
{
if (captureChannel == MCPWM_CHANNEL_0)
{
return (MCPWMx->CAP0);
}
else if (captureChannel == MCPWM_CHANNEL_1)
{
return (MCPWMx->CAP1);
}
else if (captureChannel == MCPWM_CHANNEL_2)
{
return (MCPWMx->CAP2);
}
return (0);
}
/*********************************************************************//**
* @brief Configures Count control in MCPWM peripheral
* @param[in] MCPWMx Motor Control PWM peripheral selected
* Should be: LPC_MCPWM
* @param[in] channelNum Channel number, should be: 0..2
* @param[in] countMode Count mode, should be:
* - ENABLE: Enables count mode.
* - DISABLE: Disable count mode, the channel is in timer mode.
* @param[in] countConfig Pointer to a MCPWM_COUNT_CFG_Type structure
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -