AnsweredAssumed Answered

[MKL26]怎么设置TMP的输入捕获模式

Question asked by bi shihao on Aug 2, 2015
Latest reply on Aug 3, 2015 by jeremyzhou

大家好,又有一个问题,我想实现 TPM0 CH1 CH2 输出电平互补的边沿对齐 PWM,且 CH1 TPM1 CH0 CH1 相连,经 TPM1 输入捕捉后,获得 PWM 的脉宽。外部PTA12和PTA13已经与PTC2相连接。

 

程序大约是例程,但是TMP0和1都不进中断,好像tmp0和1都没有启动啊,为什么,

 

////////////////////////////////////////////////////////////////////////////////

// Definitions

////////////////////////////////////////////////////////////////////////////////

#define TPM_MOD_1TO10    0x1E0

uint16_t u16PWMDuty = TPM_MOD_1TO10*2;

////////////////////////////////////////////////////////////////////////////////

// Code

////////////////////////////////////////////////////////////////////////////////

 

void SIM_Init(void)

{

    SIM_SOPT2 |= SIM_SOPT2_TPMSRC(1);

    SIM_SOPT2 &= ~SIM_SOPT2_PLLFLLSEL_MASK;

    SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK| SIM_SCGC5_PORTC_MASK;

    SIM_SCGC6 |= SIM_SCGC6_TPM0_MASK| SIM_SCGC6_TPM1_MASK;

}

 

 

void TPM0_Init(void)

{

    NVIC_EnableIRQ(TPM0_IRQn);

    TPM0_CNT = 0;

    TPM0_MOD = 0x12C0;

    TPM0_SC = TPM_SC_TOIE_MASK|TPM_SC_CMOD(1);

    TPM0_C1SC = TPM_CnSC_MSB_MASK| TPM_CnSC_ELSB_MASK;

    TPM0_C1V = 0x00;

    TPM0_C2SC = TPM_CnSC_MSB_MASK| TPM_CnSC_ELSA_MASK;

    TPM0_C2V = 0x00;

}

 

 

void TPM1_Init(void)

{

    NVIC_EnableIRQ(TPM1_IRQn);

    TPM1_CONF = TPM_CONF_TRGSEL(8)|TPM_CONF_CSOO_MASK|TPM_CONF_CSOT_MASK;

    TPM1_CNT = 0;

    TPM1_MOD = 0x2BC0;

    TPM1_SC = TPM_SC_TOIE_MASK|TPM_SC_CMOD(1);

    TPM1_C0SC = TPM_CnSC_ELSA_MASK;

    TPM1_C0V = 0x00;

    TPM1_C1SC = TPM_CnSC_ELSB_MASK;

    TPM1_C1V = 0x00;

}

 

int main (void)

{

    hardware_init();    //config pee. enable all port clock;

  

    PORTA_PCR12 = PORT_PCR_ISF_MASK |PORT_PCR_MUX(0x3);                 //config PTA12,13 as TPM1 Ch0,1

    PORTA_PCR13 = PORT_PCR_ISF_MASK |PORT_PCR_MUX(0x3);

    PORTC_PCR2 = PORT_PCR_ISF_MASK |PORT_PCR_MUX(0x4)| PORT_PCR_DSE_MASK;     

  

    SIM_Init();

    TPM0_Init();

    TPM1_Init(); 

    while(1);

}

 

void TPM0_IRQHandler(void)

{

    TPM0_SC |= TPM_SC_TOF_MASK;

    TPM0_C1V = (uint16_t)u16PWMDuty;

    TPM0_C2V = (uint16_t)u16PWMDuty;

}

  

void TPM1_IRQHandler(void)

{

    TPM1_SC |= TPM_SC_TOF_MASK;

    TPM1_C0SC |= TPM_CnSC_CHF_MASK;

    TPM1_C1SC |= TPM_CnSC_CHF_MASK;

    //断点查看pwmm脉宽值

}

Outcomes