I'm trying work with Keil uVision 5 on the KV10Z32 board. However, the FTM won't work properly and performs really weird.
With the following code, I'm trying to configure TPM0 Ch0 to output a PWM signal on PTE24 and generate interrupt every switching period. However, the FTM would start working and count to some arbitrary value and stop working. I also found that when I enabled ADC and constantly sample with the ADC will enable the FTM counter to work. Besides, even when the FTM0 is counting, I can't even reset the overflow bit in FTM0->SC because that would lead to hard fault handler.
I've attached the code. Calling the Init_PWM() in main function doesn't start the FTM counter while calling Init_PWM(), Init_ADC() and then starting ADC with ADC0->SC1 = 0x49 will enable the FTM counter.
I'm desperate for help with this as I've got stuck on this for whole week.
Thanks a lot.
SIM->SCGC6 |= SIM_SCGC6_FTM0_MASK;
SIM->SCGC5 |= (SIM_SCGC5_PORTA_MASK
| SIM_SCGC5_PORTE_MASK );
PORTE->PCR = PORT_PCR_MUX(3);
FTM0->CONTROLS.CnV = 100;
FTM0->SC |= FTM_SC_PS(0) | FTM_SC_CLKS(1) | FTM_SC_TOIE_MASK;
FTM0->CONTROLS.CnSC = FTM_CnSC_MSB_MASK | FTM_CnSC_ELSB_MASK;
SIM->SCGC6 |= (1UL << SIM_SCGC6_ADC0_SHIFT);
ADC0->CFG1 = 0x8C; // Select 16 bit resolution
ADC0->SC2 = 0;
FTM0->SC &= ~FTM_SC_TOF_MASK;
ADC0->SC1 = 0x49;