I am trying to generate a 38kHz square wave using the MK20DN512VLL10's FTM0 module running under MQX (I don't think MQX would make any difference). The purpose of this is to send out a modulated IR signal for object detection.
The code that I'm using is (and was validated in AN5142 as well as a number of posts on the community forum here):
// Enable Flex Timer Clock
SIM_SCGC6 |= SIM_SCGC6_FTM0_MASK;
FTM0_SC = 0; // Make sure FTM0 is off before writing to it
FTM0_MODE |= FTM_MODE_WPDIS_MASK; /* Disable write protection */
FTM0_MODE |= FTM_MODE_FTMEN_MASK;
FTM0_CONF |= FTM_CONF_BDMMODE(3); // counter run in BDM mode
FTM0_CNTIN = 0x00;
FTM0_CNT = 0; // Reset Counter
FTM0_MOD = 48000000 / 38000; // Set PWM frequency; MODULO = Fclk/Fpwm
FTM0_C7SC |= FTM_CnSC_MSB_MASK; // Set FTM0 Channel 7 Operation
FTM0_C7SC |= FTM_CnSC_ELSB_MASK;
FTM0_C7SC &= ~FTM_CnSC_ELSA_MASK;
FTM0_C7V = FTM0_MOD / 2; // THIS SHOULD BE A 50% duty Cycle
// Status and Control bits
FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0);
PORTD_PCR7 |= PORT_PCR_MUX(4) | PORT_PCR_DSE_MASK; // FTM0 CH7 - high power operation
The PWM period is perfect but the duty cycle is above 77% (Period is 26.4us and the High part of the signal is 20.4us).
I'm taking the period value and dividing it by two - anybody see where my problem is?