本帖最后由 defalut 于 2014-6-19 10:33 编辑
这段时间在做一个心电采集的功能,现在已经把freescale官方的这个实例移植到我自己的工程了FRDM-KL26Z_SC\FRDM-KL26Z_SC_Rev_1.0\klxx-sc-baremetal\build\iar\LQRUG_tpm_ex1。
我只需要采集单路PWM脉宽,这个实例是互补对称模式的。我屏蔽了一路,发现不能进中断,就4个寄存器。
昨天折腾了一天。
下面是我屏蔽的内容,只留下TPM0的ch1通道(PTC2)发出PWM脉冲,然后用TPM1的CH1通道(PTA13)检测PTC2的脉冲宽度. PTC2产生PWM是正常的,但是PTA13不产生中断了。
int main (void)
{
char ch;
printf("\nRunning the LQRUG_tpm_ex1 project.\n");
SIM_Init();
PORT_Init();
TPM0_Init();
TPM1_Init();
while(1)
{
printf("\nChange PWM duty: 0-9 (0-90%%)\n");
ch = in_char();
u16PWMDuty = TPM_MOD_1TO10*(ch - 0x30);
out_char(ch);
printf("\nPress any key to detect pulse width");
ch = in_char();
printf("\nMeasured Pulse width: %d TPM1 cycles\n\n", i16PulseWidth);
}
}
/********************************************************************/
/***************************************************************************//*!
* [url=home.php?mod=space&uid=247401]@brief[/url] SIM module initialization.
******************************************************************************/
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;
}
/***************************************************************************//*!
* [url=home.php?mod=space&uid=247401]@brief[/url] PORT module initialization.
******************************************************************************/
void PORT_Init(void)
{
//PORTA_PCR12 = PORT_PCR_ISF_MASK |PORT_PCR_MUX(0x3);
PORTA_PCR13 = PORT_PCR_ISF_MASK |PORT_PCR_MUX(0x3);
PORTC_PCR2 = PORT_PCR_ISF_MASK |PORT_PCR_MUX(0x4)| PORT_PCR_DSE_MASK;
//PORTC_PCR1 = PORT_PCR_ISF_MASK |PORT_PCR_MUX(0x4)| PORT_PCR_DSE_MASK; /* TMP0_CH0 located on PTC1 on FRDM KL26Z. TMP0_CH2 not broken out on FRMD KL26Z. */
}
/***************************************************************************//*!
* @brief TPM0 module initialization.
******************************************************************************/
void TPM0_Init(void)
{
enable_irq(17); set_irq_priority(17, 3);
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; /* TMP0_CH1 */
TPM0_C1V = 0x00;
//TPM0_C0SC = TPM_CnSC_MSB_MASK| TPM_CnSC_ELSA_MASK; /* TMP0_CH0 */
//TPM0_C0V = 0x00;
}
/***************************************************************************//*!
* @brief TPM1 module initialization.
******************************************************************************/
void TPM1_Init(void)
{
enable_irq(18); set_irq_priority(18, 1);
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_SC |= (1<<5);
//TPM1_C0SC = TPM_CnSC_ELSA_MASK;
//TPM1_C0V = 0x00;
TPM1_C1SC = (TPM_CnSC_ELSB_MASK);
TPM1_C1V = 0x00;
}
/***************************************************************************//*!
* @brief TPM0 overflow interrupt service routine.
******************************************************************************/
void tpm0_isr(void)
{
TPM0_SC |= TPM_SC_TOF_MASK;
TPM0_C1V = (uint16)u16PWMDuty; /* TMP0_CH1 */
//TPM0_C0V = (uint16)u16PWMDuty; /* TMP0_CH0 */
}
/***************************************************************************//*!
* @brief TPM1 overflow interrupt service routine.
******************************************************************************/
void tpm1_isr(void)
{
int16 TPM_c0v = 0, TPM_c1v = 0;
TPM1_SC |= TPM_SC_TOF_MASK;
TPM_c1v = (int16)TPM1_C1V;
//TPM_c0v = (int16)TPM1_C0V;
//i16PulseWidth = TPM_c1v - TPM_c0v;
i16PulseWidth = TPM_c1v;
//TPM1_C0SC |= TPM_CnSC_CHF_MASK;
TPM1_C1SC |= TPM_CnSC_CHF_MASK;
}
|