Hi,
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[0] = 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.
void Init_PWM(void)
{
SIM->SCGC6 |= SIM_SCGC6_FTM0_MASK;
SIM->SCGC5 |= (SIM_SCGC5_PORTA_MASK
| SIM_SCGC5_PORTB_MASK
| SIM_SCGC5_PORTC_MASK
| SIM_SCGC5_PORTD_MASK
| SIM_SCGC5_PORTE_MASK );
PORTE->PCR[24] = PORT_PCR_MUX(3);
FTM0->MOD=0xffff;
FTM0->CONTROLS[0].CnV = 100;
FTM0->SC |= FTM_SC_PS(0) | FTM_SC_CLKS(1) | FTM_SC_TOIE_MASK;
FTM0->CONTROLS[0].CnSC = FTM_CnSC_MSB_MASK | FTM_CnSC_ELSB_MASK;
NVIC_EnableIRQ(FTM0_IRQn);
}
void Init_ADC(void)
{
SIM->SCGC6 |= (1UL << SIM_SCGC6_ADC0_SHIFT);
ADC0->CFG1 = 0x8C; // Select 16 bit resolution
ADC0->SC2 = 0;
NVIC_EnableIRQ (ADC0_IRQn);
}
void FTM0_IRQHandler(){
NVIC_ClearPendingIRQ(FTM0_IRQn);
FTM0->SC &= ~FTM_SC_TOF_MASK;
}
void ADC0_IRQHandler()
{
int q;
NVIC_ClearPendingIRQ(ADC0_IRQn);
q=ADC0->R[0];
ADC0->SC1[0] = 0x49;
}
Hello Zhi Qu,
Can you create a project to reproduce your problem on a TWR-KV10?
I will try to dig into it.
Regards,
Santiago
I've attached my project. You can comment out the Init_ADC() and ADC0->SC1[0] = 0x49 in main function and then add them back to see my problem with the CNT register in FTM0. I tried to use the micro trace buffer to track the problem but with this project, as long as I load the DBG_MTB.ini that worked for my KL25-Z, the program is locked in the initialization program trying to disable the watchdog timer. I've also attached the file for your reference.
Thanks a lot.
Zhi Qu
Hi Zhi Qu,
I'd like to highly recommend that you get to download the sample code package of the TWR-KV10Z32 board.
And it also contains the demos about the FTM module and I think it will help you to fix this issue, so you can refer to for details.
Have a great day,
Ping
http://cache.freescale.com/files/32bit/software/TWR-KV10Z32_SC.zip
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------