(Kinetis K60) FTM输出PWM
程序 LED渐亮渐灭
#include "project.h" void delay(U32); void FTM_PWM_Init(void); ; void main(void) { U16 FTM2_CH0_Duty,FTM2_CH1_Duty DisableInterrupts; UART4_Init(115200); FTM_PWM_Init(); EnableInterrupts; while(1) { for(FTM2_CH0_Duty = 0,FTM2_CH1_Duty = 0;FTM2_CH1_Duty < 300;FTM2_CH1_Duty++,FTM2_CH0_Duty++) { FTM2_C0V = FTM2_CH0_Duty; FTM2_C1V = FTM2_CH1_Duty; delay(100000); } for(FTM2_CH0_Duty = 300,FTM2_CH1_Duty = 300;FTM2_CH1_Duty > 0;FTM2_CH1_Duty--,FTM2_CH0_Duty--) { FTM2_C0V = FTM2_CH0_Duty; FTM2_C1V = FTM2_CH1_Duty; delay(100000); } } } void delay(U32 count) { while(count--); } void FTM_PWM_Init(void) { PORTA_PCR10 = PORT_PCR_MUX(0x03) | PORT_PCR_DSE_MASK; PORTA_PCR11 = PORT_PCR_MUX(0x03) | PORT_PCR_DSE_MASK; SIM_SCGC3 |= SIM_SCGC3_FTM2_MASK; FTM2_SC |= (FTM_SC_CLKS(1) // 系统时钟 |FTM_SC_PS(7)); // 128 分频 FTM2_C0SC |= (FTM_CnSC_MSB_MASK // MSB 1 |FTM_CnSC_ELSA_MASK); // ELSA 1 FTM2_C1SC |= (FTM_CnSC_MSB_MASK // MSB 1 |FTM_CnSC_ELSA_MASK); // ELSA 1 FTM2_OUTMASK = 0XFC; // 屏蔽CH2~CH7通道 打开CH0 CH1 FTM2_CNTIN = 0; //FTM 计数初始值 FTM2_MOD = 299; // 计数的值 频率=分频/(MOD - CNTIN + 1) FTM2_C0V = 0; // 占空比 (CnV-CNTIN)/(MOD-CNTIN+1) FTM2_C1V = 0; // 占空比 }