How to update FTM_MOD in interrupt, and FTM1_CnV and FTM2CnV?

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

How to update FTM_MOD in interrupt, and FTM1_CnV and FTM2CnV?

1,306 Views
broccolee
Contributor III

Hi,

 

I am using TWR-K60N512, with PK60DN512Z VMD10 chip. I have no problem updating FTMn_MOD in the main code with a short delay, but I couldn't update it smoothly in an interrupt.

 

I have multiple interrupt functions, one for a switch, another for a potentiometer, and the last one for FTM. If I were to update FTMn_MOD in the interrupt function, nothing changes unless I hit the switch. Can't seem to figure out what the problem is.

 

Another question I have is how to update the channel values of different modules specifically FTM1_CnV and FTM2_CnV. I tried it with and without interrupt but nothing seems to be changing. I have no problem updating FTM1_MOD and FTM2_MOD without using interrupt. I also have no problem updating the channel values of FTM0.

 

Any idea how I can troubleshoot the two problems that I have? I have attached my code.

 

Thanks.

Original Attachment has been moved to: PWM.c.zip

Original Attachment has been moved to: pwm_demo.c.zip

0 Kudos
4 Replies

811 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Lim,

There are several methods to update the FTM_MOD/FTM_CnV register, for example updating FTM registers with immediate load, with software triggering, hardware triggering.

I suggest you refer to an5142, or an4560, which focus on the FTM updating. I attach them here.

BR

XiangJun Rong

0 Kudos

811 Views
broccolee
Contributor III

Thanks for your response. However, I don't think you actually read my code nor the contents of my discussion in details... I based my code off of those the attached document. You might have been directly responsible for those documents, because I have seen your responses in similar discussions, which I very much appreciate the documents, they're plenty helpful.

Now, to simplify my question, well a part of my question, for now, I recreated a simpler case. I am only interested in getting one module to work, for now, more questions will ensue when I get this working. My code is presented below.

My questions are:

1) Why does the following code work for when updating FTMx_CnV for "x" equals 0 and only 0 but not for "x" equal 1 and 2? Just a reminder, I am only using one module for this simple case.

     1a) Are there additional registers to set up for FTMx_CnV for "x" equals 1 and 2, or do they just need to be set up slightly differently?

     1b) Does FTM0 module need to be enabled in order for this work although I am only interested in using FTM1?

2) I inspected the connections with an oscilloscope just to make I have set up all the ports correctly, which I have because I was able to, for some reason, update FTM1_MOD and observed the change in PWM period. So this questions is related to (1) why does it update FTM1_MOD but not FTM1_CnV?

3) How can I successfully update FTMx_CnV for "x" equals 1 and 2 when only one module is desired to be operational at the moment?

void InitPWM(void)

{

  SIM_SCGC6 |= SIM_SCGC6_FTM1_MASK;

  PORTA_PCR8 = PORT_PCR_MUX(3); // FTM1_CH0 which is A34

  PORTA_PCR9 = PORT_PCR_MUX(3);

  FTM1_CONF |= FTM_CONF_BDMMODE(4);

  FTM1_FMS &= ~FTM_FMS_WPEN_MASK;

  FTM1_MODE |= ( FTM_MODE_WPDIS_MASK | TM_MODE_FTMEN_MASK );

  FTM1_MODE &= ~FTM_MODE_PWMSYNC_MASK;

  FTM1_MOD = 1000;

  FTM1_C0SC = ( FTM_CnSC_MSB_MASK | FTM_CnSC_ELSB_MASK ); // Edge-aligned PWM

  FTM1_C1SC = ( FTM_CnSC_MSB_MASK | FTM_CnSC_ELSB_MASK );

  FTM1_COMBINE |= FTM_COMBINE_DTEN1_MASK;

  FTM1_COMBINE |= FTM_COMBINE_SYNCEN1_MASK;

  FTM1_C0V = 10;

  FTM1_C1V = 10;

  FTM1_SYNC |= FTM_SYNC_CNTMIN_MASK;

  FTM1_SYNCONF |= FTM_SYNCONF_CNTINC_MASK   |   FTM_SYNCONF_SYNCMODE_MASK;

  FTM1_CNTIN = 0x00;

  FTM1_SC = FTM_SC_CLKS(1);

  FTM1_SC |= FTM_SC_TOIE_MASK;

}

main task:

while(1)

{

   if ( brightness > 900 )

   {

       increase = true;

    }

   else if (brightness < 10 )

  {

     increase = false;

   }

   if(increase)

   {

     brightness++;

   }

   else

   {

     brightness--;

   }

// FTM1_MOD = brightness; // this works

  FTM1_C0V = brightness; // this doesn't work

  FTM1_PWMLOAD |= FTM_PWMLOAD_LDOK_MASK;

     time_delay_ms(50);

}

Thanks.

0 Kudos

811 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

It seems that your code has issue. First of all, if you want to update the FTM1_C0V/FTM1_C1V registers, you should set the SYNCEN0 bit rather than SYNCEN1 in FTM1_COMBINE register. You can add the code: FTM1_COMBINE |= FTM_COMBINE_SYNCEN0_MASK;

Secondly, it seems your code has issue. If the brightness is greater than 900, you should decrease the brightness instead of increase the brightness. Note anytime, the FTM_CnV register must be less than the FTM_MOD register.

So you can change the code as following:

while(1)

{

   if ( brightness > 900 )

   {

       increase = true;

    }

   else if (brightness < 10 )

  {

     increase = false;

   }

   if(increase)

   {

     brightness--;   //Rong modified

   }

   else

   {

     brightness++;  //Rong modified

   }

// FTM1_MOD = brightness; // this works

  FTM1_C0V = brightness; // this doesn't work

  FTM1_PWMLOAD |= FTM_PWMLOAD_LDOK_MASK;

     time_delay_ms(50);

}

Hope it can help you.

BR

XiangJun Rong

811 Views
broccolee
Contributor III

Thanks for the catch, changing SYNCEN1 to SYNCEN0 fixed the issue. I can update FTMx_MOD and FTMx_CnV in the interrupt for all three modules at the same time now! Thank you so much for your help.

0 Kudos