K64F Variable Frequency PWM

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

K64F Variable Frequency PWM

Jump to solution
903 Views
nhinshaw
Contributor III

Hello all,

I am working on an application in Codewarrior for the K64F that requires a variable frequency PWM output on 6 different pins.  I attempted first to use the PWM bean, but it doesnt have any obvious way to change the frequency in run-time.  This led me to the PPG component, but it doesnt share its frequency to other PPG outputs.  My goal is to share a common configurable frequency across the outputs on a certain clock output.  as an example, the test I'm using is FTM0_C0V and FTM0_C1V.  I am attempting to have both of them share a frequency of 17.4768 ms, with a different duty cycle on each output.  Is there a configuration setting I'm missing, or a different way to solve this issue?  Any help would be appreciated.

-Thanks

Nate Hinshaw

0 Kudos
1 Solution
623 Views
mjbcswitzerland
Specialist V

Hi

I just tried the following code and measured 6 PWM outputs on the FRDM-K64F.

    PWM_INTERRUPT_SETUP pwm_setup;
    pwm_setup.int_type = PWM_INTERRUPT;
    pwm_setup.pwm_mode = (PWM_SYS_CLK | PWM_PRESCALER_128 | PWM_EDGE_ALIGNED); // clock PWM timer from the system clock with /128 pre-scaler
    pwm_setup.int_handler = 0;                                           // no user interrupt call-back on PWM cycle
    pwm_setup.pwm_reference = (_TIMER_0 | 3);                            // timer module 0, channel 3
    pwm_setup.pwm_frequency = PWM_FREQUENCY(54, 128);                    // generate 54Hz on PWM output
    pwm_setup.pwm_value   = _PWM_PERCENT(20, pwm_setup.pwm_frequency);   // 20% PWM (high/low)
    fnConfigureInterrupt((void *)&pwm_setup);                            // enter configuration for PWM test
    pwm_setup.pwm_reference = (_TIMER_0 | 2);                            // timer module 0, channel 2
    pwm_setup.pwm_mode |= PWM_POLARITY;                                  // change polarity of following channels
    pwm_setup.pwm_value  = _PWM_TENTH_PERCENT(706, pwm_setup.pwm_frequency); // 70.6% PWM (low/high)
    fnConfigureInterrupt((void *)&pwm_setup);
    pwm_setup.pwm_value = _PWM_TENTH_PERCENT(553, pwm_setup.pwm_frequency); // 55.3% PWM (low/high)
    pwm_setup.pwm_reference = (_TIMER_0 | 1);
    fnConfigureInterrupt((void *)&pwm_setup);
    pwm_setup.pwm_value = _PWM_TENTH_PERCENT(249, pwm_setup.pwm_frequency); // 24.9% PWM (low/high)
    pwm_setup.pwm_reference = (_TIMER_0 | 0);
    fnConfigureInterrupt((void *)&pwm_setup);
    pwm_setup.pwm_value = _PWM_TENTH_PERCENT(129, pwm_setup.pwm_frequency); // 12.9% PWM (low/high)
    pwm_setup.pwm_reference = (_TIMER_0 | 4);
    fnConfigureInterrupt((void *)&pwm_setup);
    pwm_setup.pwm_value = _PWM_TENTH_PERCENT(20, pwm_setup.pwm_frequency); // 2.0% PWM (low/high)
    pwm_setup.pwm_reference = (_TIMER_0 | 5);
    fnConfigureInterrupt((void *)&pwm_setup);

54Hz output frequency on all
FlexTimer 0 - channel 3 = 20% high PTC4 = J2-4
FlexTimer 0 - channel 2 = 70.6% low PTC3 = J1-16
FlexTimer 0 - channel 1 = 55.3% low PTC2 = J1-14
FlexTimer 0 - channel 0 = 24.9% low PTC1 = J1-5
FlexTimer 0 - channel 4 = 12.9% low PTD4 = J6-4
FlexTimer 0 - channel 5 = 2.0% low PTD5 = J6-5

I have attached the binary which can be loaded to the FRDM-K64F to verify.
On its VCOM command line menu the I/O menu (3) can be used to view the FlexTimer registers and check with yours to possibly identify your issue.

This is the display showing registers of interest:

 Input/Output menu
===================
up             go to main menu
md             Memory Display [address] [<l>|<w>|<b>] [num]
mm             Memory Modify [address] [<l>|<w>|<b>] [val]
mf             Memory Fill [address] [<l>|<w>|<b>] [val] [num]
sd             Storage Display {as md}
sm             Storage Modify {as mm}
sf             Storage Fill {as mf}
se             Storage Erase [address] [len-hex]
save           Save port setting as default
help           Display menu specific help
quit           Leave command mode

#md 40038000 l 16
Memory Display
0x40038000     0000008f 00002191 000021e7 000000a4  ......!...!.....
0x40038010     00000871 000000a4 000012c0 000000a4  ...q............
0x40038020     000017f0 000000a8 000006c8 000000a4  ................
0x40038030     0000045f 000000a4 000000ad 00000000  ..._............


Regards

Mark

View solution in original post

4 Replies
623 Views
nhinshaw
Contributor III

Sorry for the late response,  I got caught up on other projects.  Moving back to this, i manually set the registers rather than letting processor expert handle it, and it allowed me to change the frequencies on the fly.  I am unsure why processor expert doesn't let you do this already, but i suspect they have some reason

-Thanks for the help

Nate Hinshaw

0 Kudos
623 Views
nhinshaw
Contributor III

I understand that they are shared, but I can't seem to configure that shared frequency at run-time

-Nate

0 Kudos
624 Views
mjbcswitzerland
Specialist V

Hi

I just tried the following code and measured 6 PWM outputs on the FRDM-K64F.

    PWM_INTERRUPT_SETUP pwm_setup;
    pwm_setup.int_type = PWM_INTERRUPT;
    pwm_setup.pwm_mode = (PWM_SYS_CLK | PWM_PRESCALER_128 | PWM_EDGE_ALIGNED); // clock PWM timer from the system clock with /128 pre-scaler
    pwm_setup.int_handler = 0;                                           // no user interrupt call-back on PWM cycle
    pwm_setup.pwm_reference = (_TIMER_0 | 3);                            // timer module 0, channel 3
    pwm_setup.pwm_frequency = PWM_FREQUENCY(54, 128);                    // generate 54Hz on PWM output
    pwm_setup.pwm_value   = _PWM_PERCENT(20, pwm_setup.pwm_frequency);   // 20% PWM (high/low)
    fnConfigureInterrupt((void *)&pwm_setup);                            // enter configuration for PWM test
    pwm_setup.pwm_reference = (_TIMER_0 | 2);                            // timer module 0, channel 2
    pwm_setup.pwm_mode |= PWM_POLARITY;                                  // change polarity of following channels
    pwm_setup.pwm_value  = _PWM_TENTH_PERCENT(706, pwm_setup.pwm_frequency); // 70.6% PWM (low/high)
    fnConfigureInterrupt((void *)&pwm_setup);
    pwm_setup.pwm_value = _PWM_TENTH_PERCENT(553, pwm_setup.pwm_frequency); // 55.3% PWM (low/high)
    pwm_setup.pwm_reference = (_TIMER_0 | 1);
    fnConfigureInterrupt((void *)&pwm_setup);
    pwm_setup.pwm_value = _PWM_TENTH_PERCENT(249, pwm_setup.pwm_frequency); // 24.9% PWM (low/high)
    pwm_setup.pwm_reference = (_TIMER_0 | 0);
    fnConfigureInterrupt((void *)&pwm_setup);
    pwm_setup.pwm_value = _PWM_TENTH_PERCENT(129, pwm_setup.pwm_frequency); // 12.9% PWM (low/high)
    pwm_setup.pwm_reference = (_TIMER_0 | 4);
    fnConfigureInterrupt((void *)&pwm_setup);
    pwm_setup.pwm_value = _PWM_TENTH_PERCENT(20, pwm_setup.pwm_frequency); // 2.0% PWM (low/high)
    pwm_setup.pwm_reference = (_TIMER_0 | 5);
    fnConfigureInterrupt((void *)&pwm_setup);

54Hz output frequency on all
FlexTimer 0 - channel 3 = 20% high PTC4 = J2-4
FlexTimer 0 - channel 2 = 70.6% low PTC3 = J1-16
FlexTimer 0 - channel 1 = 55.3% low PTC2 = J1-14
FlexTimer 0 - channel 0 = 24.9% low PTC1 = J1-5
FlexTimer 0 - channel 4 = 12.9% low PTD4 = J6-4
FlexTimer 0 - channel 5 = 2.0% low PTD5 = J6-5

I have attached the binary which can be loaded to the FRDM-K64F to verify.
On its VCOM command line menu the I/O menu (3) can be used to view the FlexTimer registers and check with yours to possibly identify your issue.

This is the display showing registers of interest:

 Input/Output menu
===================
up             go to main menu
md             Memory Display [address] [<l>|<w>|<b>] [num]
mm             Memory Modify [address] [<l>|<w>|<b>] [val]
mf             Memory Fill [address] [<l>|<w>|<b>] [val] [num]
sd             Storage Display {as md}
sm             Storage Modify {as mm}
sf             Storage Fill {as mf}
se             Storage Erase [address] [len-hex]
save           Save port setting as default
help           Display menu specific help
quit           Leave command mode

#md 40038000 l 16
Memory Display
0x40038000     0000008f 00002191 000021e7 000000a4  ......!...!.....
0x40038010     00000871 000000a4 000012c0 000000a4  ...q............
0x40038020     000017f0 000000a8 000006c8 000000a4  ................
0x40038030     0000045f 000000a4 000000ad 00000000  ..._............


Regards

Mark

623 Views
mjbcswitzerland
Specialist V

Hi

All PWM outputs on a FlexTimer share a common frequency and can each have  differnet mark-space ratio.
You may prefer to set up all before enabling the counter to avoid any complications.

You may get some ideas from http://www.utasker.com/docs/uTasker/uTaskerHWTimers.PDF.

Regards

Mark

0 Kudos