Problem With Input Capture of K20D50M

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problem With Input Capture of K20D50M

633 Views
seahorse333
Contributor I

Hi.

I tried to make K20D50M to measure distance using HC-SR04 sensor with FTM, but the result show 0 and it doesn't change when I put the object near and further away from the sensor. I have put the code for reference

.

// Global variable for ISR to save period in
static volatile uint16_t period;

//Initialise Output Compare for TRIG pin
void initialiseFTM0(void) {
// Initialise C.1(Alt4) = FTM0.Ch0, used to drive pin
SIM_SCGC5 |= SIM_SCGC5_PORTC_MASK;
PORTC_PCR1 = PORT_PCR_MUX(4)|PORT_PCR_DSE_MASK;
// Enable clock to FTM
SIM_SCGC6 |= SIM_SCGC6_FTM0_MASK;
// Common registers
FTM0_SC = FTM_SC_CLKS(0);
FTM0_CNTIN = 0;
FTM0_CNT = 0;
FTM0_MOD = 0xFFFF;
FTM0_SC = FTM_SC_CLKS(1)|FTM_SC_PS(6);
// Enable FTM0 interrupts in NVIC
NVIC_EnableIrq(INT_FTM0);
// Channel register
FTM0_C0SC = FTM_CnSC_CHIE_MASK|FTM_CnSC_ELSA_MASK|FTM_CnSC_ELSB_MASK|FTM_CnSC_MSA_MASK;
FTM0_C0V = 0;
}

//Initialise Input Capture for ECHO pin
void initialiseFTM1(void) {
// Initialise B.1(Alt3) = FTM1.Ch1, used to drive pin
SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK;
PORTB_PCR1 = PORT_PCR_MUX(3)|PORT_PCR_DSE_MASK;
// Enable clock to FTM
SIM_SCGC6 |= SIM_SCGC6_FTM1_MASK;
// Common registers
FTM1_SC = FTM_SC_CLKS(0);
FTM1_CNTIN = 0;
FTM1_CNT = 0;
FTM1_MOD = 0x258;
FTM1_SC = FTM_SC_CLKS(1)|FTM_SC_PS(0);
// Enable FTM0 interrupts in NVIC
NVIC_EnableIrq(INT_FTM1);
// Channel register
FTM1_C1SC = FTM_CnSC_CHIE_MASK|FTM_CnSC_ELSA_MASK;
}

//Output Compare
void FTM0_IRQHandler(void)
{
// Rising edges on the pin are detected
if ((FTM0_C0SC & FTM_CnSC_CHF_MASK) != 0)
{

if ((FTM0_C0SC & FTM_CnSC_ELSA_MASK) != 0)
{
FTM0_C0SC = FTM_CnSC_CHIE_MASK|FTM_CnSC_MSA_MASK|FTM_CnSC_ELSB_MASK; //high
FTM0_C0V = 60;
}
else
{
FTM0_C0SC = FTM_CnSC_CHIE_MASK|FTM_CnSC_MSA_MASK|FTM_CnSC_ELSA_MASK|FTM_CnSC_ELSB_MASK;// low
FTM0_C0V = 0;
}

}
if (FTM0_STATUS != 0)
{
// Unexpected interrupt - die here for debug
__asm("bkpt");
}
}

//Input Capture
void FTM1_IRQHandler(void)
{
static uint16_t riseIC1Edge;
static uint16_t fallIC1Edge;


// Rising edges on the pin are detected
if ((FTM1_C1SC & FTM_CnSC_CHF_MASK) != 0)
{
if ((FTM1_C1SC & FTM_CnSC_ELSA_MASK) != 0)
{
uint16_t valueMin = FTM1_C1V;
riseIC1Edge = valueMin;
FTM1_C1SC = FTM_CnSC_CHIE_MASK|FTM_CnSC_ELSB_MASK;

}
else
{
uint16_t valueMax = FTM1_C1V;
fallIC1Edge = valueMax;
FTM1_C1SC = FTM_CnSC_CHIE_MASK|FTM_CnSC_ELSA_MASK;
period = fallIC1Edge - riseIC1Edge;
}


}
if (FTM1_STATUS != 0)
{
__asm("bkpt");
}
}

void getDistanceCentimeters()
{
int distanceCentimeters;
distanceCentimeters = (period*10^-6)/56;
printf("Distance = %i cm \n", distanceCentimeters);
}


void delay()
{
int delayCount;
for (delayCount = 0;delayCount <1000000;delayCount++)
{
__asm("bkpt");
}
}


int main() {
clock_initialise();
initialiseFTM0();
initialiseFTM1();


for(;;)
{
getDistanceCentimeters();
//delay();
}
}

so, is there somthing wrng with my code?

Labels (1)
Tags (2)
0 Kudos
1 Reply

541 Views
Robin_Shen
NXP TechSupport
NXP TechSupport

Hi Uma Horse,

I can see you configure FTM1 work as Input Capture mode and Capture on Rising Edge Only in initialiseFTM1.
But seems that you need: period = fallIC1Edge - riseIC1Edge;
Why not direct configure FTM1 work as Dual Edge Capture mode?

Dual Edge Capture mode.png

Here you can refer:
FRDM-K64&#160; FTM measure pulse width

k64F precision pulse width measurement

Best Regards,

Robin

 

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
----------------------------------------------------------------------------------------------------------------------

0 Kudos