Maybe this can help you.
Remember to set the correct vector in your interrupt table.
File main.c
/*
* This application implements Input Capture mode for FTM0 with interrupt
* Autor: Graziano Pagani
* Partially stolen from Mr. Adrian Sanchez Cano's code
* Novasis
*/
#include "derivative.h" /* include peripheral declarations */
#include "main.h"
/* tower module LED defines */
#define LED0_EN (PORTA_PCR11 = PORT_PCR_MUX(1))
#define LED0_TOGGLE (GPIOA_PTOR = (1<<11))
#define LED0_OFF (GPIOA_PSOR = (1<<11))
#define LED0_ON (GPIOA_PCOR = (1<<11))
int count = 0;
int main(void)
{
int counter = 0;
SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTC_MASK | SIM_SCGC5_PORTD_MASK | SIM_SCGC5_PORTE_MASK;
GPIOA_PDDR |= (1<<11);
LED0_EN;
LED0_ON;
ftm0_init();
nvic_init();
for(;;)
{
counter++;
}
return 0;
}
void ftm0_init(void)
{
//Enable FlexTimer 0 Clock
SIM_SCGC6 |= SIM_SCGC6_FTM0_MASK; /* Enable clock for FTM2 */
PORTC_PCR3|= (0|PORT_PCR_ISF_MASK|PORT_PCR_MUX(4));
// FTM0_CH2
FTM0_MODE |= FTM_MODE_WPDIS_MASK;
FTM0_CNT = 0; //reset the counter to zero
FTM0_MOD = 0xffff ; //Set the overflow rate
FTM0_CNTIN = 0; //Set the Counter Initial Value to 0
FTM0_MODE |= FTM_MODE_FTMEN_MASK;
// FLEX Timer0 configuration
FTM0_SC |= (FTM_SC_PS(0)| FTM_SC_CLKS(1));
// TOF=0 TOIE=0 CPWMS=0 CLKS=01 (system clock) (divide by 1)
FTM0_C1SC = 0; /* Clear channel status and control register */
FTM0_C2SC = 0; /* Clear channel status and control register */
FTM0_C3SC = 0; /* Clear channel status and control register */
FTM0_C4SC = 0; /* Clear channel status and control register */
FTM0_C5SC = 0; /* Clear channel status and control register */
FTM0_C6SC = 0; /* Clear channel status and control register */
FTM0_C7SC = 0; /* Clear channel status and control register */
FTM0_C2SC |= (0|FTM_CnSC_ELSA_MASK | FTM_CnSC_CHIE_MASK);
FTM0_MODE |= FTM_MODE_FTMEN_MASK;
FTM0_COMBINE = 0;
FTM0_QDCTRL = 0;
//Enable the counter to run in the BDM mode
FTM0_CONF |= FTM_CONF_BDMMODE(0);
}
void FTM0_isr(void)
{
int event = 0;
int overflow = 0;
int status = 0;
LED0_TOGGLE; //Toggle LED
status = FTM0_SC;
if (FTM0_C2SC & FTM_CnSC_CHF_MASK)
{
FTM0_C2SC &= ~FTM_CnSC_CHF_MASK;
event = 1;
count = FTM0_C2V & FTM_CnV_VAL_MASK;
FTM0_CNT = 0; //reset the counter to zero
}
if (FTM0_SC & FTM_SC_TOF_MASK)
{
FTM0_SC &= ~FTM_SC_TOF_MASK;
overflow = 1;
}
}
void nvic_init (void)
{
EnableInterrupts;
enable_irq(INT_FTM0-16);
}
void enable_irq (int irq)
{
int div;
/* Determine which of the NVICISERs corresponds to the irq */
div = irq/32;
switch (div)
{
case 0x0:
NVICICPR0 |= 1 << (irq%32);
NVICISER0 |= 1 << (irq%32);
break;
case 0x1:
NVICICPR1 |= 1 << (irq%32);
NVICISER1 |= 1 << (irq%32);
break;
case 0x2:
NVICICPR2 |= 1 << (irq%32);
NVICISER2 |= 1 << (irq%32);
break;
}
}
File main.h
/*
* This application implements Input Capture mode for FTM0 with interrupt
* Autor: Graziano Pagani
* Partially stolen from Mr. Adrian Sanchez Cano's code
* Novasis
*/
#ifndef MAIN_H_
#define MAIN_H_
#define EnableInterrupts asm(" CPSIE i");
void FTM0_isr(void);
void ftm0_init(void);
void enable_irq (int irq);
void set_irq_priority (int irq, int prio);
void nvic_init (void);
#endif /* MAIN_H_ */