S32K144 USES FTM0 to output two-channel PWM and compares the output.To calculate the waveform by interruption, use CH0 to interrupt.but never can enter interrupt.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

S32K144 USES FTM0 to output two-channel PWM and compares the output.To calculate the waveform by interruption, use CH0 to interrupt.but never can enter interrupt.

506 Views
1751640975
Contributor I

S32K144 USES FTM0 to output two-channel PWM and compares the output.To calculate the waveform by interruption, use CH0 to interrupt.

Case:PWM always has output, but never can enter interrupt.

Analysis: 1. Is it configured incorrectly?

               2. Wrong interrupt service function?

int main(void)

{

WDOG_disable(); /* Disable WatchDog */
SOSC_init_8MHz(); /* Initialize system oscillator for 8 MHz xtal */

SPLL_init_160MHz(); /* Initialize SPLL to 160 MHz with 8 MHz SOSC */
NormalRUNmode_40MHz(); /* Init clocks: 80 MHz sysclk & core, 40 MHz bus, 20 MHz flash */

 FTM0_NVIC_init_IRQs();
FTM0_init_40MHZ(); /* Init FTM0 in 40MHZ*/

PORT_init(); /* Configure ports */
FTM0_CH0_init(); /* Init FTM0 CH0 */
FTM0_CH1_init(); /* Init FTM0 CH1 */
for(;;)
{
/* Do nothing */
}

}

void FTM0_Ch0_Ch1_IRQHandler (void)
{
if(count<1000)
{
count++;

if(count==128)
{
FTM0->SC &= ~(FTM_SC_PWMEN1_MASK | FTM_SC_PWMEN0_MASK);
count = 0;
}
}
FTM0->CONTROLS[0].CnSC &= ~FTM_CnSC_CHF_MASK;
}

void PORT_init(void)
{
/*PWM0--FTM0*/
PCC->PCCn[PCC_PORTD_INDEX ]|=PCC_PCCn_CGC_MASK; /* Enable clock for PORTD */
PORTD->PCR[15]|=PORT_PCR_MUX(2); /* Port D15: MUX = ALT2 CH0*/
PORTD->PCR[16]|=PORT_PCR_MUX(2); /* Port D15: MUX = ALT2 CH1*/
//PTD->PDDR |= 1<<15; /* Port D0: Data Direction= output */
//PORTD->PCR[15] = 0x00000100; /* Port D0: MUX = GPIO */
//PTD-> PDOR |= 1<<15;
}

void FTM0_init_40MHZ(void)
{
PCC->PCCn[PCC_FTM0_INDEX] &= ~PCC_PCCn_CGC_MASK; /* Ensure clk disabled for config */
PCC->PCCn[PCC_FTM0_INDEX] |= PCC_PCCn_PCS(0) | PCC_PCCn_CGC_MASK;
/* Clock is off */
/* Enable clock for FTM regs */
FTM0->MODE |= FTM_MODE_WPDIS_MASK; /* Write protect to registers disabled (default) */
FTM0->MODE |= FTM_MODE_FTMEN_MASK; /* Enable write the FTM CnV register */
FTM0->SC = 0;
FTM0->CNTIN = 0;
FTM0->SC |= FTM_SC_PWMEN1_MASK | FTM_SC_PWMEN0_MASK | FTM_SC_PS(0);/**/
/* Enable PWM channel 0,1 output*/
/* TOIE (Timer Overflow Interrupt Ena) = 0 (default) */
/* CPWMS (Center aligned PWM Select) = 0 (default, up count) */
/* CLKS (Clock source) = 0 (default, no clock; FTM disabled) */
/* PS (Prescaler factor) = 0. Prescaler = 1 */
FTM0->COMBINE = 0x00000000;/* FTM mode settings used: DECAPENx, MCOMBINEx, COMBINEx=0 */
FTM0->POL = 0x00000000; /* Polarity for all channels is active high (default) */
FTM0->MOD = 160 -1 ; /* FTM1 counter final value (used for PWM mode) */
/* FTM1 Period = MOD-CNTIN+0x0001 ~= 2 ctr clks */
/* 40MHz / 1 = 40MHz 1/40MHZ = 0.025us */
//FTM0->SC |= FTM_SC_CPWMS_MASK;
}

void FTM0_CH0_init(void)
{
FTM0->CONTROLS[0].CnSC |= FTM_CnSC_MSA_MASK | FTM_CnSC_CHIE_MASK | FTM_CnSC_ELSA_MASK ; //0x00000054;//设置为输出比较 中断使能

FTM0->CONTROLS[0].CnSC &= ~FTM_CnSC_CHF_MASK;

FTM0->CONTROLS[0].CnV = 10;
}

void FTM0_CH1_init(void)
{
FTM0->CONTROLS[1].CnSC |= FTM_CnSC_MSA_MASK | FTM_CnSC_ELSA_MASK;//设置为输出比较

FTM0->CONTROLS[1].CnV = 30;
}

void start_FTM0_counter(char clock_Selection)
{
FTM0->SC |= FTM_SC_CLKS(clock_Selection); //
/* Start FTM0 counter with clk source = FTM System clock*/
}

void FTM0_NVIC_init_IRQs (void)
{
S32_NVIC->ICPR[1] = 1 << (99 % 32); /* IRQ99-FTM0 ch0: clr any pending IRQ*/
S32_NVIC->ISER[1] = 1 << (99 % 32); /* IRQ99-FTM0 ch0: enable IRQ */
S32_NVIC->IP[99] = 0x9; /* IRQ99-FTM0 ch0: priority 10 of 0-15*/
}

0 Kudos
0 Replies