Creat a PWM Signal

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

Creat a PWM Signal

Jump to solution
908 Views
pascalschröer
Contributor V

Hi, I hope anyone can help me....

First of all my facts:

I'm using CW V.1.4 (without PEX), to program a MK20DX128 (50MHz)

I would like to generate a PWM signal with selectable period and duty cycle. I thought it would

be very easy, so i did it like described in the datasheet:


PERIOD = (MOD-CNTIN+0x0001)


DUTY CYCLE = (CnV-CNTIN)


void PWM_init(void)

{

      FTM0_C1SC  = 0x00                                         /* Clear the FTM0_C1SC register */

                       | FTM_CnSC_MSB_MASK                /* Used for further selections in the channel logic */

                       | FTM_CnSC_ELSB_MASK               /* Edge or Level Select */

                       | FTM_CnSC_ELSA_MASK;              /* Edge or Level Select */

     

      FTM0_SC = 0x00                                              /*  Clear the FTM0_SC register */

                  |FTM_SC_CLKS(0x01);                         /* Selects the System clock */

     

      FTM0_CNTIN = FTM_CNTIN_INIT(0x0000);        /* Initialisation value */

     

      FTM0_CNT = FTM_CNT_COUNT(0x0000);          /* Set counter register */

     

      FTM0_MOD = FTM_MOD_MOD(0x0000);           /* Set up modulo register */

        

      FTM0_C1V = FTM_CnV_VAL(0x8000);              /* Set up the duty cycle */  

}

In this case the duty cycle is 50% how expected... but there isn't any change when I change CNTIN_INIT....

were is my fault here?

The next point is, when I would like to change the Period and change the mod register there isn't a PWM Signal anymore.

Hope anyone can help me :smileyhappy:

regards


Labels (1)
0 Kudos
1 Solution
562 Views
pascalschröer
Contributor V

Hi, and thanks to all

I have solved the problem. The MOD register was too small for the CnV register value, so there was not any PWM signal. Very easy, but I have spent a lot of time to find this problem.

Thanks and regards

Pascal

View solution in original post

0 Kudos
4 Replies
561 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi Pascal,

Please check attached FlexTimer module conuter initial value update test code (especially at void FTM_Counter_Init () function).

Writing to the CNTIN register latches the value into a buffer. The CNTIN register is updated with the value of its write buffer according to Registers updated from write buffers.

More detailed info, please check MK20DX128 reference manual.


Wish it helps.

Best regards,
Ma Hui

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

563 Views
pascalschröer
Contributor V

Hi, and thanks to all

I have solved the problem. The MOD register was too small for the CnV register value, so there was not any PWM signal. Very easy, but I have spent a lot of time to find this problem.

Thanks and regards

Pascal

0 Kudos
561 Views
mjbcswitzerland
Specialist V

Pascal

I am not sure about the FTM_CNTIN since I use only the FTM_CV (for the particular channel) to vary the PWM signal.

However, check the behaviour of the register FTM_CNTIN because writes to it are only propagated through to the counter under certain conditions.

>>when I would like to change the Period and change the mod register there isn't a PWM Signal anymore

Do you mean when debugging and writing to registers to see the effect? If so, check the FTM_CONF register setting since it defines the behaviour of the FlexTimer in debug mode (it can be left to run or to freeze).

regards

Mark

0 Kudos
561 Views
pascalschröer
Contributor V

Hi,

>>Do you mean when debugging and writing to registers to see the effect? If so, check the FTM_CONF register setting since it defines the behaviour of the FlexTimer in debug mode (it can be left to run or to freeze).

No, I have changed the register before debugging...

>>I am not sure about the FTM_CNTIN since I use only the FTM_CV (for the particular channel) to vary the PWM signal.

Have you changed the period too? Or did you only set the PWM duty cycle?

My main question is, what does the MOD register do? I can't understand the describtion in the datasheet...

<<

The Modulo register contains the modulo value for the FTM counter. After the FTM

counter reaches the modulo value, the overflow flag (TOF) becomes set at the next clock,

and the next value of FTM counter depends on the selected counting method;

>>

This paragraph says that the timer counts until the MOD value is reached or?

<<

The Counter Initial Value register contains the initial value for the FTM counter.

Writing to the CNTIN register latches the value into a buffer.

>>

This paragraph says that the timer starts to count from the CNTIN register value or?

<<

The CNT register contains the FTM counter value.

Reset clears the CNT register. Writing any value to COUNT updates the counter with its

initial value, CNTIN.

>>

This paragraph says taht the CNT register shows the counter value at the moment or? So I

don't need to use this register?!

<<

These registers contain the captured FTM counter value for the input modes or the match

value for the output modes.

>>

Last but not least this register says that when I reach the FTM counter value the level is changed from high to low or otherwise or?

Is there a fault in my interpretation?

thanks

Pascal

0 Kudos