金牌会员
- 积分
- 1247
- 威望
- 698
- 贡献
- 385
- 兑换币
- 359
- 注册时间
- 2012-7-18
- 在线时间
- 82 小时
|
void PWM_INIT(void)
{
SIM_SCGC6 |= SIM_SCGC6_FTM0_MASK; //使能FTM0时钟
FTM0_C0SC |= FTM_CnSC_ELSB_MASK;
FTM0_C0SC &= ~FTM_CnSC_ELSA_MASK;//clear output on match
FTM0_C0SC |= FTM_CnSC_MSB_MASK;//·边沿对齐模式
SIM_SCGC5 |= SIM_SCGC5_PORTC_MASK;
PORT_PCR_REG(PORTC_BASE_PTR, 1) = PORT_PCR_MUX(4); // PTC1,输出到电机PWM
FTM0_C1SC |= FTM_CnSC_ELSB_MASK;
FTM0_C1SC &= ~FTM_CnSC_ELSA_MASK;//clear output on match
FTM0_C1SC |= FTM_CnSC_MSB_MASK;//边沿对齐模式
SIM_SCGC5 |= SIM_SCGC5_PORTC_MASK;
PORT_PCR_REG(PORTC_BASE_PTR, 2) = PORT_PCR_MUX(4); // PTC2,输出到电机PWM
FTM0_SC = FTM_SC_PS(0) | FTM_SC_CLKS(1);//not enable the interrupt mask,select system clock,上升模式
//divide by 1 50MHz
FTM0_MODE &= ~3; //BIT0 FTM Enable
FTM0_MODE |= 0x04;
FTM0_OUTMASK &= ~3; //0 Channel output is not masked. It continues to operate normally.
FTM0_COMBINE=0; //Function for Linked Channels (FTMx_COMBINE)
FTM0_OUTINIT=0;
FTM0_EXTTRIG=0; //FTM External Trigger (FTMx_EXTTRIG)
FTM0_POL=0; //Channels Polarity (FTMx_POL)
//0 The channel polarity is active high.
//1 The channel polarity is active low.
FTM0_INVCTRL=0; //反转控制
FTM0_SWOCTRL=0; //软件输出控制F TM Software Output Control (FTMx_SWOCTRL)
FTM0_PWMLOAD=0; //FTM PWM Load
//BIT9: 0 Loading updated values is disabled.
//1 Loading updated values is enabled.
FTM0_CNTIN=0; //Counter Initial Value
FTM0_MOD=5000; //Modulo value,The EPWM period is determined by (MOD - CNTIN + 0x0001)
//PMW频率=50000000/5000=5000HZ
FTM0_C0V=0; //设置 the pulse width(duty cycle) is determined by (CnV - CNTIN).
FTM0_C1V=0; //设置 the pulse width(duty cycle) is determined by (CnV - CNTIN).
FTM1_CNT=0; //只有低16位可用
//PMW频率=50000000/5000=5000HZ
SIM_SCGC3 |= SIM_SCGC3_FTM2_MASK;//使能FTM2时钟
FTM2_C0SC |= FTM_CnSC_ELSB_MASK;
FTM2_C0SC &= ~FTM_CnSC_ELSA_MASK;//clear output on match
FTM2_C0SC |= FTM_CnSC_MSB_MASK;//边沿对齐模式
SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK;
PORT_PCR_REG(PORTB_BASE_PTR, 18) = PORT_PCR_MUX(3); // PTB18,舵机PWM
FTM2_SC = FTM_SC_PS(7) | FTM_SC_CLKS(1);//not enable the interrupt mask,select system clock,上升模式
//divide by 128 195312.5Hz
FTM2_MODE &= ~3; //BIT0 FTM Enable
FTM2_OUTMASK &= ~3; //0 Channel output is not masked. It continues to operate normally.
FTM2_COMBINE=0; //Function for Linked Channels (FTMx_COMBINE)
FTM2_OUTINIT=0;
FTM2_EXTTRIG=0; //FTM External Trigger (FTMx_EXTTRIG)
FTM2_POL=0; //Channels Polarity (FTMx_POL)
//0 The channel polarity is active high.
//1 The channel polarity is active low.
FTM2_INVCTRL=0; //反转控制
FTM2_SWOCTRL=0; //软件输出控制F TM Software Output Control (FTMx_SWOCTRL)
FTM2_PWMLOAD=0; //FTM PWM Load
//BIT9: 0 Loading updated values is disabled.
//1 Loading updated values is enabled.
FTM2_CNTIN=0; //Counter Initial Value
FTM2_MOD=8000; //Modulo value,The EPWM period is determined by (MOD - CNTIN + 0x0001)
//PMW频率=195312.5/4000=50HZ
FTM2_C0V=610;//545~675
} |
|