How Set FTMx_CNT and FTMy_CNT to 0 (CNTIN) in the same time?

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

How Set FTMx_CNT and FTMy_CNT to 0 (CNTIN) in the same time?

Jump to solution
861 Views
arnogir
Senior Contributor II

:smileyhappy:Hello,

I'm working on a K60.

I would like synchronize FTM0_CNT and FTM3_CNT.

"Synchronize" means that both FTM0 and FTM3 counters have the same value at any instant.

This subject link bellow has an example, but not explain how do it. So in the  code, I don't see any line which could resolve my problem.

Re: How to sync FTM modules on K60.

Then, what is the way to do it? Which register must I configure to do it?

My use is to generate pulse to drive 8 stepper motor. Then I combine FTM0 CH0/CH1(Motor1), CH2/CH3(Motor2), CH4/CH5(Motor3), CH6/CH7(Motor4) and  FTM3 CH0/CH1(Motor5), CH2/CH3(Motor6), CH4/CH5(Motor7), CH6/CH7(Motor8).

But motors must move in the same time, so FTM0 counter and FTM3 counter have to be synchronized.

Thanks

0 Kudos
1 Solution
598 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,Arno,

This is the code I developed based on K40 which can make FTM0 and FTM1 synchronize, pls refer to it.


BR

XiangJun Rong


void FTM1CenterAlign(void)

{

SIM_SCGC6|=0x03000000; //enable FTM0 and FTM0 module clock

SIM_SCGC5=SIM_SCGC5|0x3E00; //enable port A/B/C/D/E clock

FTM1_MODE|=0x05; //enable write the FTM CnV register

FTM1_CONF=0xC0; //set up BDM in 11

FTM1_FMS=0x00; //clear the WPEN so that WPDIS is set in FTM0_MODE reg

FTM1_MOD=1000;

FTM1_C0SC=0x28; //low_High_Low for center-alignment

FTM1_C1SC=0x28;

FTM1_COMBINE=0x22; //complementary mode for CH0&CH1 of FTM1, enable update //CH0/1 of FTM1

FTM1_COMBINE|=0x10; //dead timer insertion enabled  for CH0&CH1 of FTM0

FTM1_DEADTIME=0x00; //deattime is 32 system clock cycles

FTM1_C1V=500;

FTM1_C0V=500;

FTM1_CNTIN=0x00;

//FTM1_SC=0x68; //do not start FTM1

}

void FTM0CenterAlign(void)

{

SIM_SCGC6|=0x03000000; //enable FTM0 and FTM0 module clock

SIM_SCGC5=SIM_SCGC5|0x3E00; //enable port A/B/C/D/E clock

FTM0_MODE|=0x05; //enable write the FTM CnV register

FTM0_CONF=0xC0; //set up BDM in 11

FTM0_FMS=0x00; //clear the WPEN so that WPDIS is set in FTM0_MODE reg

FTM0_MOD=1000;

FTM0_C0SC=0x28; //High_Low_High for center-alignment

FTM0_C1SC=0x28;

FTM0_C2SC=0x28;

FTM0_C3SC=0x28;

FTM0_COMBINE|=0x2020; //enable update the FTM_C0V/FTM0_C1V register

FTM0_COMBINE|=0x0202; //complementary mode for CH0&CH1 of FTM0

FTM0_COMBINE|=0x1010; // dead timer insertion enabled in complementary mode //for CH0&CH1 of FTM0

FTM0_DEADTIME=0x00; //dead time is 0 system clock cycles

FTM0_C1V=500;

FTM0_C0V=500;

FTM0_C2V=500;

FTM0_C3V=500;

FTM0_CNTIN=0x00;

//FTM0_SC=0x68; //do not start FTM0

}

void FTM0SynFTM1(void)

{

//The FTM0 module generates the  GTB signal

//setting GTBEEN bit for both FTM0/FTM1,

FTM0_CONF|=0x200;

FTM1_CONF|=0x200;

      

//enable both FTM0 and FTM1, FTM0/FTM1 waiting for the GTB signal

FTM0_SC=0x68;

FTM1_SC=0x28;

//setting the GTBEOUT of FTM0 will start both FTM0/1, commenting the following line, FTM0/1 will not run

FTM0_CONF|=(0x400);

}

void FTM0_ISR()

{

static bool flag;   

if(FTM0_SC&0x80)

       {

       FTM0_SC&=0x7F; //clear TOF bit   

       if(flag)

       {

              FTM0_C0V=750;

              FTM0_C1V=750;

              FTM0_C2V=750;

              FTM0_C3V=750;

              FTM1_C0V=750;

              FTM1_C1V=750;

       }

       else

       {

              FTM0_C0V=500;

              FTM0_C1V=500;

              FTM0_C2V=500;

              FTM0_C3V=500;

              FTM1_C0V=500;

              FTM1_C1V=500;

       }

       flag=!flag;

       FTM0_PWMLOAD|=0x200;

       FTM1_PWMLOAD|=0x200;

       }

      

}

View solution in original post

0 Kudos
3 Replies
599 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,Arno,

This is the code I developed based on K40 which can make FTM0 and FTM1 synchronize, pls refer to it.


BR

XiangJun Rong


void FTM1CenterAlign(void)

{

SIM_SCGC6|=0x03000000; //enable FTM0 and FTM0 module clock

SIM_SCGC5=SIM_SCGC5|0x3E00; //enable port A/B/C/D/E clock

FTM1_MODE|=0x05; //enable write the FTM CnV register

FTM1_CONF=0xC0; //set up BDM in 11

FTM1_FMS=0x00; //clear the WPEN so that WPDIS is set in FTM0_MODE reg

FTM1_MOD=1000;

FTM1_C0SC=0x28; //low_High_Low for center-alignment

FTM1_C1SC=0x28;

FTM1_COMBINE=0x22; //complementary mode for CH0&CH1 of FTM1, enable update //CH0/1 of FTM1

FTM1_COMBINE|=0x10; //dead timer insertion enabled  for CH0&CH1 of FTM0

FTM1_DEADTIME=0x00; //deattime is 32 system clock cycles

FTM1_C1V=500;

FTM1_C0V=500;

FTM1_CNTIN=0x00;

//FTM1_SC=0x68; //do not start FTM1

}

void FTM0CenterAlign(void)

{

SIM_SCGC6|=0x03000000; //enable FTM0 and FTM0 module clock

SIM_SCGC5=SIM_SCGC5|0x3E00; //enable port A/B/C/D/E clock

FTM0_MODE|=0x05; //enable write the FTM CnV register

FTM0_CONF=0xC0; //set up BDM in 11

FTM0_FMS=0x00; //clear the WPEN so that WPDIS is set in FTM0_MODE reg

FTM0_MOD=1000;

FTM0_C0SC=0x28; //High_Low_High for center-alignment

FTM0_C1SC=0x28;

FTM0_C2SC=0x28;

FTM0_C3SC=0x28;

FTM0_COMBINE|=0x2020; //enable update the FTM_C0V/FTM0_C1V register

FTM0_COMBINE|=0x0202; //complementary mode for CH0&CH1 of FTM0

FTM0_COMBINE|=0x1010; // dead timer insertion enabled in complementary mode //for CH0&CH1 of FTM0

FTM0_DEADTIME=0x00; //dead time is 0 system clock cycles

FTM0_C1V=500;

FTM0_C0V=500;

FTM0_C2V=500;

FTM0_C3V=500;

FTM0_CNTIN=0x00;

//FTM0_SC=0x68; //do not start FTM0

}

void FTM0SynFTM1(void)

{

//The FTM0 module generates the  GTB signal

//setting GTBEEN bit for both FTM0/FTM1,

FTM0_CONF|=0x200;

FTM1_CONF|=0x200;

      

//enable both FTM0 and FTM1, FTM0/FTM1 waiting for the GTB signal

FTM0_SC=0x68;

FTM1_SC=0x28;

//setting the GTBEOUT of FTM0 will start both FTM0/1, commenting the following line, FTM0/1 will not run

FTM0_CONF|=(0x400);

}

void FTM0_ISR()

{

static bool flag;   

if(FTM0_SC&0x80)

       {

       FTM0_SC&=0x7F; //clear TOF bit   

       if(flag)

       {

              FTM0_C0V=750;

              FTM0_C1V=750;

              FTM0_C2V=750;

              FTM0_C3V=750;

              FTM1_C0V=750;

              FTM1_C1V=750;

       }

       else

       {

              FTM0_C0V=500;

              FTM0_C1V=500;

              FTM0_C2V=500;

              FTM0_C3V=500;

              FTM1_C0V=500;

              FTM1_C1V=500;

       }

       flag=!flag;

       FTM0_PWMLOAD|=0x200;

       FTM1_PWMLOAD|=0x200;

       }

      

}

0 Kudos
598 Views
arnogir
Senior Contributor II

Hi,

After test, this work correctly.

Thank:smileyhappy:

0 Kudos
598 Views
arnogir
Senior Contributor II

Hi!

I will test it, If I understand, the important lines for me are:

FTM0_CONF|=0x200;

FTM1_CONF|=0x200;

      

//enable both FTM0 and FTM1, FTM0/FTM1 waiting for the GTB signal

FTM0_SC=0xnn;

FTM1_SC=0xmm;

//setting the GTBEOUT of FTM0 will start both FTM0/1, commenting the following line, FTM0/1 will not run

FTM0_CONF|=(0x400);


I will come nack here to inform you.


Thank

:smileyhappy: