FTM CnV Update Problem 2

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

FTM CnV Update Problem 2

1,111 Views
Jana_muralidharan
Contributor II

 

Hi, I got another problem, i previously ask and got solve it is the LDOK register that i was wrong

but even after solving that it still not doing what i ask that is to update the CnV and change the Pulse width please help me this is that code i have been working on

 

#include "MKE02Z4.h"

#define FTM_MOD     24      // 25 ticks → 1.25us @ 20MHz
#define TICKS_0      7
#define TICKS_1     14

void FTM_Init(void)
{
    SIM->SCGC |= SIM_SCGC_FTM2_MASK;

    FTM2->MODE = FTM_MODE_WPDIS_MASK | FTM_MODE_FTMEN_MASK;

    FTM2->CNTIN = 0;
    FTM2->CNT   = 0;
    FTM2->MOD   = FTM_MOD;

    FTM2->CONTROLS[0].CnSC =
        FTM_CnSC_MSB_MASK |
        FTM_CnSC_ELSB_MASK;

    FTM2->CONTROLS[0].CnV = TICKS_0;

    /* ENABLE CHANNEL OUTPUT (CRITICAL) */
    FTM2->OUTMASK &= ~(1 << 0);

    /* Initial load */
    *(volatile __UINT32_TYPE__ *)0x4003A098 = 0x0201;

    FTM2->SC = FTM_SC_CLKS(1) | FTM_SC_PS(0);

    *(volatile __UINT32_TYPE__ *)0x4003A064 = 0x00;
}

static inline void wait_ftm_period(void)
{
    while(!(FTM2->SC & FTM_SC_TOF_MASK));
    FTM2->SC &= ~FTM_SC_TOF_MASK;
}

uint32_t word = 0x5555;

int main(void)
{
    FTM_Init();

    while (1)
    {
        for(int i = 0; i < 16; i++)
        {
            if (word & (1UL << i))
                *(volatile __UINT32_TYPE__ *)0x4003A010 = TICKS_1;
            else
            	*(volatile __UINT32_TYPE__ *)0x4003A010 = TICKS_0;

            /* ARM reload for next PWM cycle */
            *(volatile __UINT32_TYPE__ *)0x4003A098 = 0x0201;

            /* WAIT ONE PWM PERIOD */
            wait_ftm_period();
        }
    }
}
0 Kudos
Reply
6 Replies

1,015 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello @Jana_muralidharan 

Please use the FTM_UpdatePwmDutycycle() API to update the PWM duty cycle.
For more details, refer to the ftm_simple_pwm demo, where the PWM duty cycle is periodically updated.
 

/*!

* brief Updates the duty cycle of an active PWM signal.

*

* param base FTM peripheral base address

* param chnlNumber The channel/channel pair number. In combined mode, this represents

* the channel pair number

* param currentPwmMode The current PWM mode set during PWM setup

* param dutyCyclePercent New PWM pulse width; The value should be between 0 to 100

* 0=inactive signal(0% duty cycle)...

* 100=active signal (100% duty cycle)

* return kStatus_Success if the PWM update was successful

* kStatus_Error on failure

*/

status_t FTM_UpdatePwmDutycycle(FTM_Type *base,

ftm_chnl_t chnlNumber,

ftm_pwm_mode_t currentPwmMode,

uint8_t dutyCyclePercent)

 

BR
Alice

 

 

0 Kudos
Reply

936 Views
Jana_muralidharan
Contributor II
But can you please tell me, in how much cycle that the PWM updates
0 Kudos
Reply

927 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello @Jana_muralidharan 

This depends on your application. You can refer to your requirement design.
Regarding this SDK demo, it uses a timer overflow interrupt to update the PWM duty cycle.
Alice_Yang_0-1766732600601.png

 

BR

Alice

 

0 Kudos
Reply

1,108 Views
Jana_muralidharan
Contributor II

Hey I am the writer of the post And i am working in the WS2812B so i though of using PWM to generate the signal so that why thing thing of changing the Duty cycle so and i want to know Is it possible to do that with this 

 

0 Kudos
Reply

1,065 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hi @Jana_muralidharan ]

I'm sorry, I'm a bit confused. Are you asking how to change the duty cycle of the PWM, or are you asking why your duty cycle changed?
 
Thank you.
 
BR
Alice
0 Kudos
Reply

1,021 Views
Jana_muralidharan
Contributor II
Hello @Alice_Yang,
Yeah i am trying to change the PWM duty cycle for every cycle like for 1st period 30% duty cycle then in the 2nd period 70% duty cycle like that

Is there any procedure to change the PWM duty change

It just stuck at the initially set value that the issue here
0 Kudos
Reply
%3CLINGO-SUB%20id%3D%22lingo-sub-2267521%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EFTM%20CnV%20Update%20Problem%202%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2267521%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CBR%20%2F%3E%3CP%3EHi%2C%20I%20got%20another%20problem%2C%20i%20previously%20ask%20and%20got%20solve%20it%20is%20the%20LDOK%20register%20that%20i%20was%20wrong%3C%2FP%3E%3CP%3Ebut%20even%20after%20solving%20that%20it%20still%20not%20doing%20what%20i%20ask%20that%20is%20to%20update%20the%20CnV%20and%20change%20the%20Pulse%20width%20please%20help%20me%20this%20is%20that%20code%20i%20have%20been%20working%20on%3C%2FP%3E%3CBR%20%2F%3E%3CPRE%20class%3D%22lia-code-sample%20language-cpp%22%3E%3CCODE%3E%23include%20%22MKE02Z4.h%22%0A%0A%23define%20FTM_MOD%20%20%20%20%2024%20%20%20%20%20%20%2F%2F%2025%20ticks%20%E2%86%92%201.25us%20%40%2020MHz%0A%23define%20TICKS_0%20%20%20%20%20%207%0A%23define%20TICKS_1%20%20%20%20%2014%0A%0Avoid%20FTM_Init(void)%0A%7B%0A%20%20%20%20SIM-%26gt%3BSCGC%20%7C%3D%20SIM_SCGC_FTM2_MASK%3B%0A%0A%20%20%20%20FTM2-%26gt%3BMODE%20%3D%20FTM_MODE_WPDIS_MASK%20%7C%20FTM_MODE_FTMEN_MASK%3B%0A%0A%20%20%20%20FTM2-%26gt%3BCNTIN%20%3D%200%3B%0A%20%20%20%20FTM2-%26gt%3BCNT%20%20%20%3D%200%3B%0A%20%20%20%20FTM2-%26gt%3BMOD%20%20%20%3D%20FTM_MOD%3B%0A%0A%20%20%20%20FTM2-%26gt%3BCONTROLS%5B0%5D.CnSC%20%3D%0A%20%20%20%20%20%20%20%20FTM_CnSC_MSB_MASK%20%7C%0A%20%20%20%20%20%20%20%20FTM_CnSC_ELSB_MASK%3B%0A%0A%20%20%20%20FTM2-%26gt%3BCONTROLS%5B0%5D.CnV%20%3D%20TICKS_0%3B%0A%0A%20%20%20%20%2F*%20ENABLE%20CHANNEL%20OUTPUT%20(CRITICAL)%20*%2F%0A%20%20%20%20FTM2-%26gt%3BOUTMASK%20%26amp%3B%3D%20~(1%20%26lt%3B%26lt%3B%200)%3B%0A%0A%20%20%20%20%2F*%20Initial%20load%20*%2F%0A%20%20%20%20*(volatile%20__UINT32_TYPE__%20*)0x4003A098%20%3D%200x0201%3B%0A%0A%20%20%20%20FTM2-%26gt%3BSC%20%3D%20FTM_SC_CLKS(1)%20%7C%20FTM_SC_PS(0)%3B%0A%0A%20%20%20%20*(volatile%20__UINT32_TYPE__%20*)0x4003A064%20%3D%200x00%3B%0A%7D%0A%0Astatic%20inline%20void%20wait_ftm_period(void)%0A%7B%0A%20%20%20%20while(!(FTM2-%26gt%3BSC%20%26amp%3B%20FTM_SC_TOF_MASK))%3B%0A%20%20%20%20FTM2-%26gt%3BSC%20%26amp%3B%3D%20~FTM_SC_TOF_MASK%3B%0A%7D%0A%0Auint32_t%20word%20%3D%200x5555%3B%0A%0Aint%20main(void)%0A%7B%0A%20%20%20%20FTM_Init()%3B%0A%0A%20%20%20%20while%20(1)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20for(int%20i%20%3D%200%3B%20i%20%26lt%3B%2016%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(word%20%26amp%3B%20(1UL%20%26lt%3B%26lt%3B%20i))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20*(volatile%20__UINT32_TYPE__%20*)0x4003A010%20%3D%20TICKS_1%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%09*(volatile%20__UINT32_TYPE__%20*)0x4003A010%20%3D%20TICKS_0%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20ARM%20reload%20for%20next%20PWM%20cycle%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20*(volatile%20__UINT32_TYPE__%20*)0x4003A098%20%3D%200x0201%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F*%20WAIT%20ONE%20PWM%20PERIOD%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20wait_ftm_period()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2267892%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20FTM%20CnV%20Update%20Problem%202%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2267892%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F258109%22%20target%3D%22_blank%22%3E%40Jana_muralidharan%3C%2FA%3E%26nbsp%3B%5D%3C%2FP%3E%0A%3CDIV%3EI'm%20sorry%2C%20I'm%20a%20bit%20confused.%20Are%20you%20asking%20how%20to%20change%20the%20duty%20cycle%20of%20the%20PWM%2C%20or%20are%20you%20asking%20why%20your%20duty%20cycle%20changed%3F%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3EThank%20you.%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3EBR%3C%2FDIV%3E%0A%3CDIV%3EAlice%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2267535%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20FTM%20CnV%20Update%20Problem%202%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2267535%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHey%20I%20am%20the%20writer%20of%20the%20post%20And%20i%20am%20working%20in%20the%20WS2812B%20so%20i%20though%20of%20using%20PWM%20to%20generate%20the%20signal%20so%20that%20why%20thing%20thing%20of%20changing%20the%20Duty%20cycle%20so%20and%20i%20want%20to%20know%20Is%20it%20possible%20to%20do%20that%20with%20this%26nbsp%3B%3C%2FP%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2268657%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20FTM%20CnV%20Update%20Problem%202%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2268657%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHello%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F258109%22%20target%3D%22_blank%22%3E%40Jana_muralidharan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3EPlease%20use%20the%20%3CCODE%3EFTM_UpdatePwmDutycycle()%3C%2FCODE%3E%20API%20to%20update%20the%20PWM%20duty%20cycle.%3C%2FDIV%3E%0A%3CDIV%3EFor%20more%20details%2C%20refer%20to%20the%20%3CCODE%3Eftm_simple_pwm%3C%2FCODE%3E%20demo%2C%20where%20the%20PWM%20duty%20cycle%20is%20periodically%20updated.%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%0A%3CDIV%20style%3D%22background-color%3A%20%23ffffff%3B%20padding%3A%200px%200px%200px%202px%3B%22%3E%0A%3CDIV%20style%3D%22color%3A%20%23000000%3B%20background-color%3A%20%23ffffff%3B%20font-family%3A%20'Courier%20New'%3B%20font-size%3A%2010pt%3B%20white-space%3A%20pre%3B%22%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%20style%3D%22margin%3A%200%3B%22%3E%3CSPAN%3E%2F*!%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200%3B%22%3E%3CSPAN%3E%20*%20brief%20Updates%20the%20duty%20cycle%20of%20an%20active%20PWM%20signal.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200%3B%22%3E%3CSPAN%3E%20*%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200%3B%22%3E%3CSPAN%3E%20*%20%3C%2FSPAN%3E%3CSPAN%3Eparam%3C%2FSPAN%3E%3CSPAN%3E%20base%20FTM%20peripheral%20base%20address%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200%3B%22%3E%3CSPAN%3E%20*%20%3C%2FSPAN%3E%3CSPAN%3Eparam%3C%2FSPAN%3E%3CSPAN%3E%20chnlNumber%20The%20channel%2Fchannel%20pair%20number.%20In%20combined%20mode%2C%20this%20represents%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200%3B%22%3E%3CSPAN%3E%20*%20the%20channel%20pair%20number%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200%3B%22%3E%3CSPAN%3E%20*%20%3C%2FSPAN%3E%3CSPAN%3Eparam%3C%2FSPAN%3E%3CSPAN%3E%20currentPwmMode%20The%20current%20PWM%20mode%20set%20during%20PWM%20setup%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200%3B%22%3E%3CSPAN%3E%20*%20%3C%2FSPAN%3E%3CSPAN%3Eparam%3C%2FSPAN%3E%3CSPAN%3E%20dutyCyclePercent%20New%20PWM%20pulse%20width%3B%20The%20value%20should%20be%20between%200%20to%20100%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200%3B%22%3E%3CSPAN%3E%20*%200%3Dinactive%20signal(0%25%20duty%20cycle)...%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200%3B%22%3E%3CSPAN%3E%20*%20100%3Dactive%20signal%20(100%25%20duty%20cycle)%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200%3B%22%3E%3CSPAN%3E%20*%20return%20kStatus_Success%20if%20the%20PWM%20update%20was%20successful%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200%3B%22%3E%3CSPAN%3E%20*%20kStatus_Error%20on%20failure%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200%3B%22%3E%3CSPAN%3E%20*%2F%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200%3B%22%3E%3CSPAN%3Estatus_t%3C%2FSPAN%3E%20%3CSPAN%3EFTM_UpdatePwmDutycycle%3C%2FSPAN%3E%3CSPAN%3E(%3C%2FSPAN%3E%3CSPAN%3EFTM_Type%3C%2FSPAN%3E%3CSPAN%3E%20*base%2C%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200%3B%22%3E%3CSPAN%3Eftm_chnl_t%3C%2FSPAN%3E%3CSPAN%3E%20chnlNumber%2C%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200%3B%22%3E%3CSPAN%3Eftm_pwm_mode_t%3C%2FSPAN%3E%3CSPAN%3E%20currentPwmMode%2C%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22margin%3A%200%3B%22%3E%3CSPAN%3Euint8_t%3C%2FSPAN%3E%3CSPAN%3E%20dutyCyclePercent)%3C%2FSPAN%3E%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CP%20style%3D%22margin%3A%200%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CDIV%3EBR%3C%2FDIV%3E%0A%3CDIV%3EAlice%3C%2FDIV%3E%0A%3CBR%20%2F%3E%0A%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2268517%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20FTM%20CnV%20Update%20Problem%202%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2268517%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EHello%20%3CA%20href%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F27788%22%20target%3D%22_blank%22%3E%40Alice_Yang%3C%2FA%3E%2C%3CBR%20%2F%3EYeah%20i%20am%20trying%20to%20change%20the%20PWM%20duty%20cycle%20for%20every%20cycle%20like%20for%201st%20period%2030%25%20duty%20cycle%20then%20in%20the%202nd%20period%2070%25%20duty%20cycle%20like%20that%3CBR%20%2F%3E%3CBR%20%2F%3EIs%20there%20any%20procedure%20to%20change%20the%20PWM%20duty%20change%3CBR%20%2F%3E%3CBR%20%2F%3EIt%20just%20stuck%20at%20the%20initially%20set%20value%20that%20the%20issue%20here%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2269086%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20FTM%20CnV%20Update%20Problem%202%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2269086%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EBut%20can%20you%20please%20tell%20me%2C%20in%20how%20much%20cycle%20that%20the%20PWM%20updates%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2269122%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20FTM%20CnV%20Update%20Problem%202%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2269122%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHello%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F258109%22%20target%3D%22_blank%22%3E%40Jana_muralidharan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%3EThis%20depends%20on%20your%20application.%20You%20can%20refer%20to%20your%20requirement%20design.%3C%2FDIV%3E%0A%3CDIV%3ERegarding%20this%20SDK%20demo%2C%20it%20uses%20a%20timer%20overflow%20interrupt%20to%20update%20the%20PWM%20duty%20cycle.%3C%2FDIV%3E%0A%3CDIV%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Alice_Yang_0-1766732600601.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22Alice_Yang_0-1766732600601.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F371223i55C338270FCC583E%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Alice_Yang_0-1766732600601.png%22%20alt%3D%22Alice_Yang_0-1766732600601.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%0A%3CBR%20%2F%3E%0A%3CP%3EBR%3C%2FP%3E%0A%3CP%3EAlice%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3C%2FDIV%3E%3C%2FLINGO-BODY%3E