HI
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
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
hi
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
Hi, Pandi,
Can you enter FTM2_IRQHandler() if you connect a captured signal?
BR
XiangJun Rong
hi
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
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
Hi, Pando,
can you enter the FTM2_IRQHandler()?
BR
XiangJun Rong
Hi
yes
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
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