Rainbow color using FTM PWM with KSDK 1.3

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

Rainbow color using FTM PWM with KSDK 1.3

Rainbow color using FTM PWM with KSDK 1.3

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(); }

127378_127378.pngrgb.png

 

  • 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()
hsv2rgb(hue,255,255,&red,&green,&blue,255);

 

  • 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

127754_127754.pngKDS.png

and choose .wsd file

127755_127755.pngKDS_2.png

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

127756_127756.pngKDS_3.png

 

127907_127907.JPGIMG_4157.JPG

 

I hope you will enjoy the demo

 

Iva

Labels (1)
Attachments
%3CLINGO-SUB%20id%3D%22lingo-sub-1103940%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERainbow%20color%20using%20FTM%20PWM%20with%20KSDK%201.3%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1103940%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHi%20all%2C%3C%2FP%3E%3CP%20style%3D%22min-height%3A%208pt%3B%20padding%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethere%20is%20a%20simple%20modification%20of%20ftm%20driver%20example%20based%20on%20KSDK%201.3%2C%20KDS%203.0%20on%20FRDM-K22F.%3C%2FP%3E%3CP%20style%3D%22min-height%3A%208pt%3B%20padding%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%3CUL%3E%3CLI%3Eenabling%20clocks%20for%20PORTs%20in%20hardware_init.c%3C%2FLI%3E%3C%2FUL%3E%3CP%20style%3D%22min-height%3A%208pt%3B%20padding%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22java%22%20name%3D%22code%22%3Evoid%20hardware_init(void)%20%7B%26nbsp%3B%20%26nbsp%3B%20%2F*%20enable%20clock%20for%20PORTs%20*%2F%20%26nbsp%3B%20CLOCK_SYS_EnablePortClock(PORTA_IDX)%3B%20%26nbsp%3B%20CLOCK_SYS_EnablePortClock(PORTD_IDX)%3B%20%26nbsp%3B%20CLOCK_SYS_EnablePortClock(PORTE_IDX)%3B%26nbsp%3B%20%26nbsp%3B%20PORT_HAL_SetMuxMode(PORTA%2C1u%2CkPortMuxAlt3)%3B%2F%2Fred%20%26nbsp%3B%20PORT_HAL_SetMuxMode(PORTA%2C2u%2CkPortMuxAlt3)%3B%2F%2Fgreen%20%26nbsp%3B%20PORT_HAL_SetMuxMode(PORTD%2C5u%2CkPortMuxAlt4)%3B%2F%2Fblue%26nbsp%3B%20%26nbsp%3B%20%2F*%20Init%20board%20clock%20*%2F%20%26nbsp%3B%20BOARD_ClockInit()%3B%20%26nbsp%3B%20dbg_uart_init()%3B%20%7D%3C%2FPRE%3E%3CDIV%20style%3D%22display%3Anone%3B%22%3E%20%3C%2FDIV%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22127378_127378.png%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22127378_127378.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F117415i5EAEE373C25F07C9%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22127378_127378.png%22%20alt%3D%22127378_127378.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22rgb.png%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22rgb.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F111045iD0B391B9E8B8E664%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22rgb.png%22%20alt%3D%22rgb.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%3CUL%3E%3CLI%3Efunction%20for%20converting%20HSV%20to%20RGB%20color%20palette%20downloaded%20from%20%3CA%20_jive_internal%3D%22true%22%20href%3D%22https%3A%2F%2Fcommunity.nxp.com%2Fexternal-link.jspa%3Furl%3Dhttps%253A%252F%252Fcode.google.com%252Fp%252Fshiftpwm%252Fsource%252Fbrowse%252Ftrunk%252Fexamples%252FShiftPWM_Example1%252Fhsv2rgb.cpp%253Fr%253D3%22%20target%3D%22_blank%22%3Ehsv2rgb.cpp%20-%20shiftpwm%20-%20Arduino%20library%20to%20PWM%20many%20outputs%20with%20chained%20shift%20registers.%20-%20Google%20Project%20Hosting%3C%2FA%3E%3C%2FLI%3E%3C%2FUL%3E%3CP%20style%3D%22min-height%3A%208pt%3B%20padding%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%3CUL%3E%3CLI%3Esetting%20ftm%20parameters%20for%20red%2C%20green%20and%20blue%20color%2C%20for%20red%20color%3A%3C%2FLI%3E%3C%2FUL%3E%3CPRE%20class%3D%22java%22%20name%3D%22code%22%3E%20ftm_pwm_param_t%20ftmParamR%20%3D%20%7B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20.mode%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3D%20kFtmEdgeAlignedPWM%2C%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20.edgeMode%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3D%20kFtmLowTrue%2C%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20.uFrequencyHZ%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3D%2024000u%2C%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20.uDutyCyclePercent%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3D%200%2C%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20.uFirstEdgeDelayPercent%20%3D%200%2C%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3B%3C%2FPRE%3E%3CDIV%20style%3D%22display%3Anone%3B%22%3E%20%3C%2FDIV%3E%3CP%20style%3D%22min-height%3A%208pt%3B%20padding%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%3CUL%3E%3CLI%3Esetting%20PWM%20output%20for%20each%20color%20in%20infinite%20loop%3C%2FLI%3E%3C%2FUL%3E%3CPRE%20class%3D%22java%22%20name%3D%22code%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20FTM_DRV_PwmStart(BOARD_FTM_INSTANCE%2C%20%26amp%3BftmParamR%2C%206)%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20FTM_DRV_PwmStart(BOARD_FTM_INSTANCE%2C%20%26amp%3BftmParamG%2C%207)%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20FTM_DRV_PwmStart(BOARD_FTM_INSTANCE%2C%20%26amp%3BftmParamB%2C%205)%3B%3C%2FPRE%3E%3CDIV%20style%3D%22display%3Anone%3B%22%3E%20%3C%2FDIV%3E%3CP%20style%3D%22min-height%3A%208pt%3B%20padding%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%3CUL%3E%3CLI%3Eforwarding%20parameters%20to%20hsv2rgb()%3C%2FLI%3E%3C%2FUL%3E%3CPRE%20class%3D%22java%22%20name%3D%22code%22%3Ehsv2rgb(hue%2C255%2C255%2C%26amp%3Bred%2C%26amp%3Bgreen%2C%26amp%3Bblue%2C255)%3B%3C%2FPRE%3E%3CDIV%20style%3D%22display%3Anone%3B%22%3E%20%3C%2FDIV%3E%3CP%20style%3D%22min-height%3A%208pt%3B%20padding%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%3CUL%3E%3CLI%3Echanging%20hue%20and%20checking%20overflow%20of%20hue%3C%2FLI%3E%3C%2FUL%3E%3CPRE%20class%3D%22java%22%20name%3D%22code%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20hue%2B%2B%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20if(hue%26gt%3B%3D360)hue%3D0%3B%3C%2FPRE%3E%3CDIV%20style%3D%22display%3Anone%3B%22%3E%20%3C%2FDIV%3E%3CP%20style%3D%22min-height%3A%208pt%3B%20padding%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%3CUL%3E%3CLI%3Enormalizing%20from%200-255%20to%200-100%20percent%20of%20PWM%20pulse%20width%20for%20each%20color%3C%2FLI%3E%3C%2FUL%3E%3CPRE%20class%3D%22java%22%20name%3D%22code%22%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ftmParamR.uDutyCyclePercent%20%3D%20(uint8_t)(((float)red%2F255.0)*100.0)%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ftmParamG.uDutyCyclePercent%20%3D%20(uint8_t)(((float)green%2F255.0)*100.0)%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ftmParamB.uDutyCyclePercent%20%3D%20(uint8_t)(((float)blue%2F255.0)*100.0)%3B%3C%2FPRE%3E%3CDIV%20style%3D%22display%3Anone%3B%22%3E%20%3C%2FDIV%3E%3CP%20style%3D%22min-height%3A%208pt%3B%20padding%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThat%C2%B4s%20all!%3C%2FP%3E%3CP%20style%3D%22min-height%3A%208pt%3B%20padding%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%3CP%3EImporting%20example%3C%2FP%3E%3COL%3E%3CLI%3Eextract%20ftm_rainbow.zip%20to%20C%3A%5CFreescale%5CKSDK_1.3.0%5Cexamples%5Cfrdmk22f%5Cdriver_examples%3C%2FLI%3E%3CLI%3EGo%20to%20KDS%2C%20import%20file%3C%2FLI%3E%3C%2FOL%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22127754_127754.png%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22127754_127754.png%22%20style%3D%22width%3A%20525px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F117416i50E66EAA5B884CD7%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22127754_127754.png%22%20alt%3D%22127754_127754.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22KDS.png%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22KDS.png%22%20style%3D%22width%3A%20525px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F35366i8383291B29922DCF%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22KDS.png%22%20alt%3D%22KDS.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3Eand%20choose%20.wsd%20file%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22127755_127755.png%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22127755_127755.png%22%20style%3D%22width%3A%20929px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F117417i0D569AFE53F56577%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22127755_127755.png%22%20alt%3D%22127755_127755.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22KDS_2.png%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22KDS_2.png%22%20style%3D%22width%3A%20929px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F35418iE896A2E86B415585%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22KDS_2.png%22%20alt%3D%22KDS_2.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EAfter%20then%20don%C2%B4t%20forget%20compile%20library%20first%20and%20then%20project.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22127756_127756.png%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22127756_127756.png%22%20style%3D%22width%3A%20357px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F117418iD58EDEFF1C9E72BC%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22127756_127756.png%22%20alt%3D%22127756_127756.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22KDS_3.png%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22KDS_3.png%22%20style%3D%22width%3A%20357px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F111040i2979DECAA72431A6%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22KDS_3.png%22%20alt%3D%22KDS_3.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%20style%3D%22min-height%3A%208pt%3B%20padding%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22127907_127907.JPG%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22127907_127907.JPG%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F117419i738B2511D3F1FA73%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22127907_127907.JPG%22%20alt%3D%22127907_127907.JPG%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22IMG_4157.JPG%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22IMG_4157.JPG%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F111046iE4D959AF0E270A38%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22IMG_4157.JPG%22%20alt%3D%22IMG_4157.JPG%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%20style%3D%22min-height%3A%208pt%3B%20padding%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20hope%20you%20will%20enjoy%20the%20demo%20%3CLI-EMOJI%20id%3D%22lia_slightly-smiling-face%22%20title%3D%22%3Aslightly_smiling_face%3A%22%3E%3C%2FLI-EMOJI%3E%3C%2FP%3E%3CP%20style%3D%22min-height%3A%208pt%3B%20padding%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIva%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1103940%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CLINGO-LABEL%3EGeneral%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
No ratings
Version history
Last update:
‎09-10-2020 01:50 AM
Updated by: