本帖最后由 zhangqi123zq 于 2016-1-26 15:04 编辑
您好,新人求助,FTM2 PWM输出设置,独立模式,输出相同。
void FTM2_Init(u16 val,u8 interrupt_EN)
{
FTM_OVERTIME_DISEN_typ interrupt_en;
interrupt_en = (FTM_OVERTIME_DISEN_typ) (interrupt_EN&0x01);
SIM->SCGC |=0x80; //¿ªÆôFTM2 ʱÖÓ
//SIM->SOPT|=4000; //FTM2 ×Ô¶¯Í¬²½
//FTM2->POL|=0x20; //ϵͳʱÖÓ
FTM2->SC =0X00; //Çå³ýÅäÖÃ
FTM2->MOD=val ; //×î´ó¼ÆÊýÖµ
FTM2->CNT = 0; //Çå³ý¼ÆÊýÆ÷
FTM2->SC&=~0x20; //ÏòÉϼÆÊýģʽ
FTM2->SC &=~ 0x07; //ʱÖÓ²»·ÖƵ 128
// FTM2->SC|=0x08; //ϵͳʱÖÓ
FTM2->SC&=~0x80; //Çå³ýÖжϱê¼Ç
if(interrupt_en==FTM_OVERTIME_EN)
{
FTM2->SC&=~0x80; //Çå³ýÖжϱê¼Ç
FTM2->SC |=0x60; //ʹÄÜÒç³öÖжÏ
NVIC_EnableIRQ(FTM2_IRQn); //ʹÄÜÖжÏÏß
}
}
void FTM2_PWM_Init(u8 CHannels,u8 CH_SW,u8 interrupt_EN)
{
u8 i=0;
FTM_CH_typ channels;
FTM2_PWM_SW_typ CH_sw;
FTM_CH_EN_typ interrupt_en;
channels = (FTM_CH_typ) (CHannels&0X3F) ;
CH_sw = (FTM2_PWM_SW_typ) (CH_SW&0X3F) ;
interrupt_en = (FTM_CH_EN_typ) (interrupt_EN&0X3F) ;
for(i=0;i<6;i++)
{
if((channels&(0x01<<i))>0)
{
if(( FTM2->MODE&0x04)==0)
{
if(FTM2->FMS & 0x40)
{
FTM2->MODE |= 0x04;
}
}
//ɨÃèÒý½ÅÇл»-------------------------------------
switch(i)
{
case 2:
if(CH_sw&FTM2_PWM_CH2_PTC4)
{
SIM->PINSEL |= 0x4000;
}
else
{
SIM->PINSEL &=~ 0x4000;
}
break;
case 3:
if(CH_sw&FTM2_PWM_CH3_PTC5)
{
SIM->PINSEL |= 0x1000; //FTM2 ͨµÀ 3 Ó³Éäµ½ PTC5
}
else
{
SIM->PINSEL &=~ 0x1000; //FTM2 ͨµÀ 3 Ó³Éäµ½ PTC3
}
break;
}
//ͨµÀÅäÖÃ---------------------------------------
FTM2->CONTROLS.CnSC=0;
FTM2->CONTROLS.CnSC |= 0x20; //±ßÔµ¶ÔÆë PWM ģʽ
FTM2->CONTROLS.CnSC |= 0x08; // ¸ßµçƽÓÐЧ
FTM2->CONTROLS.CnV =1000 ; //PWMÕ¼¿Õ±È
FTM2->MODE=0x04;
FTM2->POL|=0x20;
FTM2->FMS|=0x60;
//ÖжÏÅäÖÃ----------------------------------------
if(interrupt_en&(FTM_CH2_EN<<i))
{
FTM2->CONTROLS.CnSC &=~0X80; //Çå³ýÖжϱê¼Ç
FTM2->CONTROLS.CnSC |=0X40; //ÔÊÐíPWM ÖжÏ
}
}
}
}
void MY_PWM_VAl_SET(FTM_Type *FTMx,u8 CHannels,u16 VAL)
{
PWM_CH_typ channels;
if((FTMx!=FTM0)&&(FTMx!=FTM2))
{
return;
}
if(FTMx==FTM0)
{
channels = (PWM_CH_typ) (CHannels);
FTM0->CONTROLS[channels].CnV=VAL;
}
else
{
channels = (PWM_CH_typ) (CHannels);
//
FTM2->CONTROLS[channels].CnV=VAL;
FTM2->PWMLOAD |= 0x200; //¿ªÆôͬ²½
//
}
}
为什么输出FTM2 CH4 与CH5 始终为互补输出。
运行:COMBINE =0;
CSC4=0xA8;
CSC5=0xA8;
SC =0x88;
MOD=0x54c0;
占空比设置30%,CH5 输出30%,CH4 输出70%,互补,为什么?
|