Rainbow color using FTM PWM with KSDK 1.3

Document created by Iva Dorazinova Employee on Jan 13, 2016Last modified by Diana Torres on Feb 28, 2017
Version 3Show Document
  • View in full screen mode

Hi all,


there is a simple modification of ftm driver example based on KSDK 1.3, KDS 3.0 on FRDM-K22F.


  • enabling clocks for PORTs in hardware_init.c


void hardware_init(void) {    /* enable clock for PORTs */   CLOCK_SYS_EnablePortClock(PORTA_IDX);   CLOCK_SYS_EnablePortClock(PORTD_IDX);   CLOCK_SYS_EnablePortClock(PORTE_IDX);    PORT_HAL_SetMuxMode(PORTA,1u,kPortMuxAlt3);//red   PORT_HAL_SetMuxMode(PORTA,2u,kPortMuxAlt3);//green   PORT_HAL_SetMuxMode(PORTD,5u,kPortMuxAlt4);//blue    /* Init board clock */   BOARD_ClockInit();   dbg_uart_init(); }



  • setting ftm parameters for red, green and blue color, for red color:
 ftm_pwm_param_t ftmParamR = {         .mode                   = kFtmEdgeAlignedPWM,         .edgeMode               = kFtmLowTrue,         .uFrequencyHZ           = 24000u,         .uDutyCyclePercent      = 0,         .uFirstEdgeDelayPercent = 0,     };


  • setting PWM output for each color in infinite loop
        FTM_DRV_PwmStart(BOARD_FTM_INSTANCE, &ftmParamR, 6);         FTM_DRV_PwmStart(BOARD_FTM_INSTANCE, &ftmParamG, 7);         FTM_DRV_PwmStart(BOARD_FTM_INSTANCE, &ftmParamB, 5);


  • forwarding parameters to hsv2rgb()


  • changing hue and checking overflow of hue
        hue++;         if(hue>=360)hue=0;


  • normalizing from 0-255 to 0-100 percent of PWM pulse width for each color
        ftmParamR.uDutyCyclePercent = (uint8_t)(((float)red/255.0)*100.0);         ftmParamG.uDutyCyclePercent = (uint8_t)(((float)green/255.0)*100.0);         ftmParamB.uDutyCyclePercent = (uint8_t)(((float)blue/255.0)*100.0);


That´s all!


Importing example

  1. extract ftm_rainbow.zip to C:\Freescale\KSDK_1.3.0\examples\frdmk22f\driver_examples
  2. Go to KDS, import file


and choose .wsd file


After then don´t forget compile library first and then project.





I hope you will enjoy the demo :-)