Helps on PWM output from K64F

cancel
Showing results for 
Search instead for 
Did you mean: 

Helps on PWM output from K64F

1,591 Views
linye
Contributor I


Hi All,

I am new to K64F. Recently,  I am trying to use K64F to output a PWM signal from an GPIO ( PTB 18) with KDS 3.2 and KSDK 2.0.

So far, I have no idea on how to FTM with KSDK 2.0 to generate PWM signal. I saw lots of demo applications from KSDS 2.0, those demo applications helps a lot. But I can not find any example or demo on how to use FTM to generate PWM output.

Is there any application note available on how to use FTM with KSDK 2.0 ?

Or any demo code? It really helps a lot!

Thank you so much for helping me,

Tags (3)
0 Kudos
2 Replies

410 Views
isaacavila
NXP Employee
NXP Employee

Hello,

If you download the SDK 2.0 package for MK64FN1M0xxx12, you can find an example for PWM implementation that is available for TWR-K64F120M board, this example is located at: <SDK_2.0_MK64FN1M0xxx12>\boards\twrk64f120m\driver_examples\ftm\pwm_twochannel.

Basically, FTM implementation is configured as follows:

void delay(void)

{

    volatile uint32_t i = 0U;

    for (i = 0U; i < 800000U; ++i)

    {

        __asm("NOP"); /* delay */

    }

}

/*!

* @brief Main function

*/

int main(void)

{

    bool brightnessUp = true; /* Indicate LEDs are brighter or dimmer */

    ftm_config_t ftmInfo;

    uint8_t updatedDutycycle = 0U;

    ftm_chnl_pwm_signal_param_t ftmParam[2];

    /* Configure ftm params with frequency 24kHZ */

    ftmParam[0].chnlNumber = (ftm_chnl_t)BOARD_FIRST_FTM_CHANNEL;

    ftmParam[0].level = kFTM_LowTrue;

    ftmParam[0].dutyCyclePercent = 0U;

    ftmParam[0].firstEdgeDelayPercent = 0U;

    ftmParam[1].chnlNumber = (ftm_chnl_t)BOARD_SECOND_FTM_CHANNEL;

    ftmParam[1].level = kFTM_LowTrue;

    ftmParam[1].dutyCyclePercent = 0U;

    ftmParam[1].firstEdgeDelayPercent = 0U;

    /* Board pin, clock, debug console init */

    BOARD_InitPins();

    BOARD_BootClockRUN();

    BOARD_InitDebugConsole();

    /* Print a note to terminal */

    PRINTF("\r\nFTM example to output PWM on 2 channels\r\n");

    PRINTF("\r\nYou will see a change in LED brightness if an LED is connected to the FTM pin");

    PRINTF("\r\nIf no LED is connected to the FTM pin, then probe the signal using an oscilloscope");

    /*

     * ftmInfo.prescale = kFTM_Prescale_Divide_1;

     * ftmInfo.bdmMode = kFTM_BdmMode_0;

     * ftmInfo.pwmSyncMode = kFTM_SoftwareTrigger;

     * ftmInfo.reloadPoints = 0;

     * ftmInfo.faultMode = kFTM_Fault_Disable;

     * ftmInfo.faultFilterValue = 0;

     * ftmInfo.deadTimePrescale = kFTM_Deadtime_Prescale_1;

     * ftmInfo.deadTimeValue = 0;

     * ftmInfo.extTriggers = 0;

     * ftmInfo.chnlInitState = 0;

     * ftmInfo.chnlPolarity = 0;

     * ftmInfo.useGlobalTimeBase = false;

     */

    FTM_GetDefaultConfig(&ftmInfo);

    /* Initialize FTM module */

    FTM_Init(BOARD_FTM_BASEADDR, &ftmInfo);

    FTM_SetupPwm(BOARD_FTM_BASEADDR, ftmParam, 2U, kFTM_EdgeAlignedPwm, 24000U, FTM_SOURCE_CLOCK);

    FTM_StartTimer(BOARD_FTM_BASEADDR, kFTM_SystemClock);

    while (1)

    {

        /* Delay to see the change of LEDs brightness */

        delay();

        if (brightnessUp)

        {

            /* Increase duty cycle until it reach limited value */

            if (++updatedDutycycle == 100U)

            {

                brightnessUp = false;

            }

        }

        else

        {

            /* Decrease duty cycle until it reach limited value */

            if (--updatedDutycycle == 0U)

            {

                brightnessUp = true;

            }

        }

        /* Start PWM mode with updated duty cycle */

        FTM_UpdatePwmDutycycle(BOARD_FTM_BASEADDR, (ftm_chnl_t)BOARD_FIRST_FTM_CHANNEL, kFTM_EdgeAlignedPwm,

                               updatedDutycycle);

        FTM_UpdatePwmDutycycle(BOARD_FTM_BASEADDR, (ftm_chnl_t)BOARD_SECOND_FTM_CHANNEL, kFTM_EdgeAlignedPwm,

                               updatedDutycycle);

        /* Software trigger to update registers */

        FTM_SetSoftwareTrigger(BOARD_FTM_BASEADDR, true);

    }

}

I hope this can help you!

Regards,

Isaac

0 Kudos

410 Views
linye
Contributor I

Hi Isaac,

Thanks a lot!

0 Kudos