input capture

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

input capture

2,592 Views
pandi
Contributor III

hi

@ xiangjun_rong

how to configure input capture mode KV31F512VLL12 guide me and if  you have sample program give to me  help for us.

                                            Thank you

BR

PANDI

0 Kudos
Reply
10 Replies

2,578 Views
pandi
Contributor III

HI

@ xiangjun_rong 

I `m trying this code not working if any changes let me know 

void TIM1_init(void) {

 

SIM->SCGC5 |= SIM_SCGC5_PORTB(1);

PORTB->PCR[0] = PORT_PCR_MUX(0x3); // Set pin as ALT3 (TIM1_CH1)

SIM->SCGC6 |= SIM_SCGC6_FTM1(1);

FTM1->SC = 0;

FTM1->MOD = 1999;

FTM1->CONTROLS[1].CnSC = FTM_CnSC_MSB_MASK | FTM_CnSC_ELSB_MASK;

FTM1->CONTROLS[1].CnV = 1000;

FTM1->SC = FTM_SC_PS(7) | FTM_SC_TOF(1);

}

 

void TIM2_init(void) {

// Enable clock for PORTA

SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK;

PORTA->PCR[1] = PORT_PCR_MUX(0x3); // Set pin as ALT3 (TIM2_CH1)

SIM->SCGC6 |= SIM_SCGC6_FTM2(1);

FTM2->SC = 0;

FTM2->CONTROLS[1].CnSC = FTM_CnSC_ELSA_MASK | FTM_CnSC_ELSB_MASK |FTM_CnSC_CHIE_MASK;

FTM2->SC = FTM_SC_PS(7) | FTM_SC_TOF(1);

NVIC_EnableIRQ(FTM2_IRQn);

}

void FTM2_IRQHandler() {

// Clear channel flag

FTM2->CONTROLS[1].CnSC &= ~FTM_CnSC_CHF_MASK;

 

if (gap == 0) {

counter0 = FTM2->CONTROLS[1].CnV;

gap = 1;

} else {

counter1 = FTM2->CONTROLS[1].CnV;

if (counter1 > counter0) {

Counter = counter1 - counter0;

Frequency = 10000 / Counter;

} else {

Counter = counter1 + 0xFFFF - counter0 + 1;

Frequency = 10000 / Counter;

}

counter0 = counter1;

}

}

BR 

PANDI

 

0 Kudos
Reply

2,514 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Pandi,

This is the capture mode configuration for FTM. You have to configure the FTM1_CHx as FTM1 function in the PORTx_PCRy register so that you can connect the captured signal to the FTM0_CHx pin.

Hope it can help you

BR

XiangJun Rong

xiangjun_rong_0-1705910259069.png

 

0 Kudos
Reply

2,507 Views
pandi
Contributor III

hi

@xiangjun_rong 

i`m trying but did not get frequency value .please you have to give input frequency calculation code guide me 

This code  TIM1 PWM generation and TIM2 to get frequency  if any changes you have to guide me and you have change this code

void TIM1_init(void) {

 

SIM->SCGC5 |= SIM_SCGC5_PORTB(1);

PORTB->PCR[0] = PORT_PCR_MUX(0x3); // Set pin as ALT3 (TIM1_CH1)

SIM->SCGC6 |= SIM_SCGC6_FTM1(1);

FTM1->SC = 0;

FTM1->MOD = 1999;

FTM1->CONTROLS[1].CnSC = FTM_CnSC_MSB_MASK | FTM_CnSC_ELSB_MASK;

FTM1->CONTROLS[1].CnV = 1000;

FTM1->SC = FTM_SC_PS(7) | FTM_SC_TOF(1);

}

 

void TIM2_init(void) {

// Enable clock for PORTA

SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK;

PORTA->PCR[1] = PORT_PCR_MUX(0x3); // Set pin as ALT3 (TIM2_CH1)

SIM->SCGC6 |= SIM_SCGC6_FTM2(1);

FTM2->SC = 0;

FTM2->CONTROLS[1].CnSC = FTM_CnSC_ELSA_MASK | FTM_CnSC_ELSB_MASK |FTM_CnSC_CHIE_MASK;

FTM2->SC = FTM_SC_PS(7) | FTM_SC_TOF(1);

NVIC_EnableIRQ(FTM2_IRQn);

}

void FTM2_IRQHandler() {

// Clear channel flag

FTM2->CONTROLS[1].CnSC &= ~FTM_CnSC_CHF_MASK;

 

if (gap == 0) {

counter0 = FTM2->CONTROLS[1].CnV;

gap = 1;

else {

counter1 = FTM2->CONTROLS[1].CnV;

if (counter1 > counter0) {

Counter = counter1 - counter0;

Frequency = 10000 / Counter;

else {

Counter = counter1 + 0xFFFF - counter0 + 1;

Frequency = 10000 / Counter;

}

counter0 = counter1;

}

}

Thankyou 

BR 

PANDI

0 Kudos
Reply

2,467 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Pandi,

Can you enter FTM2_IRQHandler() if you connect a captured signal?

BR

XiangJun Rong

0 Kudos
Reply

2,464 Views
pandi
Contributor III

hi

@xiangjun_rong 

yes ,In captured frequency forTIM1 but did not working this program FTM2_IRQHandler() to get frequency value not working pls you have to guide me  if any sample program give to me 

                                                Thank you 

BR

PANDI

0 Kudos
Reply

2,460 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

Pls confirm if you can enter the FTM2_IRQHandler()? it does not matter if you computation is correct or not, but it is first step that you can enter FTM2_IRQHandler() if you connect a captured signal to FTM2_CHx pin.

BR

XiangJun Rong

0 Kudos
Reply

2,457 Views
pandi
Contributor III

hi

@xiangjun_rong 

yes,TIM1 output pin to TIM2 input pin captured signal in connected.

BR

PANDI

0 Kudos
Reply

2,453 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Pando,

 

can you enter the FTM2_IRQHandler()?

BR

XiangJun Rong

0 Kudos
Reply

2,451 Views
pandi
Contributor III

Hi

yes

@xiangjun_rong 

void FTM2_IRQHANDLER(void) {

uint32_t intStatus;

/* Reading all interrupt flags of status register */

intStatus = FTM_GetStatusFlags(FTM2_PERIPHERAL);

FTM_ClearStatusFlags(FTM2_PERIPHERAL, intStatus);

 

 

currentCaptureValue = FTM2->CONTROLS[0].CnV;

period = currentCaptureValue - previousCaptureValue;

if (period > 0) {

capturedFrequency =SystemCoreClock/period; //SystemCoreClock / period;

} else {

capturedFrequency = 0;

}

 

 

previousCaptureValue = currentCaptureValue;

/* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F

Store immediate overlapping exception return operation might vector to incorrect interrupt. */

#if defined __CORTEX_M && (__CORTEX_M == 4U)

__DSB();

#endif

}

BR

PANDI

 

0 Kudos
Reply

2,371 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

Okay, you can enter the FTM2_IRQHANDLER(), i suggest you use single edge to test the period, instead of dual edge. In general, the period is constant.

Hope it can help you

BR

XiangJun Rong

0 Kudos
Reply