硬件:FRDM-KE02Z
IDE:KEIL4.72a
修改原因:在使用void FTM_PWMInit(FTM_Type *pFTM, uint8_t u8PWMModeSelect, uint8_t u8PWMEdgeSelect)函数过程中,发现他会把所有的PWM输出通道口都初始化成PWM,但我用的时候可能仅需要几个口而已
改动方案:在FTM_PWMInit函数中增加一个uint8_t Channels参数,在FTM.h文件中增加下面的宏定义:
#define FTM_FLAG_CHANNEL0 0x01
#define FTM_FLAG_CHANNEL1 0x02
#define FTM_FLAG_CHANNEL2 0x04
#define FTM_FLAG_CHANNEL3 0x08
#define FTM_FLAG_CHANNEL4 0x10
#define FTM_FLAG_CHANNEL5 0x20
将FTM_PWMInit函数修改内容出下:
void FTM_PWMInit(FTM_Type *pFTM, uint8_t u8PWMModeSelect, uint8_t u8PWMEdgeSelect, uint8_t ChannelFlag)
{
int i, ChannelTotal;
ASSERT((FTM0== pFTM) || (FTM1== pFTM) || (FTM2== pFTM));
/* open the clock gate */
if (FTM0 == pFTM)
{
ChannelTotal = 2;
SIM->SCGC |= SIM_SCGC_FTM0_MASK;
}
else if(FTM1 == pFTM)
{
ChannelTotal = 2;
SIM->SCGC |= SIM_SCGC_FTM1_MASK;
}
else
{
ChannelTotal = 6;
SIM->SCGC |= SIM_SCGC_FTM2_MASK;
}
pFTM->SC = 0x0; /* disable counter */
pFTM->MOD = FTM_MOD_INIT;
if(FTM_PWMMODE_CENTERALLIGNED == u8PWMModeSelect) /* enable CPWM */
{
pFTM->SC |= FTM_SC_CPWMS_MASK;
}
else if(FTM_PWMMODE_COMBINE == u8PWMModeSelect) /* enable combine pwm mode */
{
ASSERT(FTM2 == pFTM);
pFTM->MODE |= FTM_MODE_WPDIS_MASK | FTM_MODE_FTMEN_MASK;
pFTM->COMBINE = FTM_COMBINE_COMBINE0_MASK | FTM_COMBINE_COMP0_MASK | FTM_COMBINE_SYNCEN0_MASK | FTM_COMBINE_DTEN0_MASK |
FTM_COMBINE_COMBINE1_MASK | FTM_COMBINE_COMP1_MASK | FTM_COMBINE_SYNCEN1_MASK | FTM_COMBINE_DTEN1_MASK |
FTM_COMBINE_COMBINE2_MASK | FTM_COMBINE_COMP2_MASK | FTM_COMBINE_SYNCEN2_MASK | FTM_COMBINE_DTEN2_MASK
;
pFTM->SC &= ~FTM_SC_CPWMS_MASK;
}
if(FTM_PWM_HIGHTRUEPULSE == u8PWMEdgeSelect)
{
/* Configure timers PWM High True Pulses */
for(i=0; i < ChannelTotal; i++)
{
if(ChannelFlag & (1 << i))
{
pFTM->CONTROLS.CnSC = FTM_CnSC_MSB_MASK | FTM_CnSC_ELSB_MASK;
pFTM->CONTROLS.CnV = FTM_C0V_INIT + i*100;
}
}
}
else if(FTM_PWM_LOWTRUEPULSE == u8PWMEdgeSelect)
{
/* Configure timers for PWM Low True Pulses */
for(i=0; i < ChannelTotal; i++)
{
if(ChannelFlag & (1 << i))
{
pFTM->CONTROLS.CnSC = FTM_CnSC_MSB_MASK | FTM_CnSC_ELSA_MASK;
pFTM->CONTROLS.CnV = FTM_C0V_INIT + i*100 ;
}
}
}
}
其中红 {MOD}部分是修改过的内容!
ftm.zip
(10.98 KB, 下载次数: 9)
2014-3-1 14:35 上传
点击文件名下载附件
一周热门 更多>