FTM0 PWM on MK20DX256

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

FTM0 PWM on MK20DX256

802 Views
DustyStew
Contributor V

I'm using a Teensy 3.1 with MK20DX256 MCU.

I'm writing to the metal (not using Teensy libraries).

I can't seem to get PWM working. I think I need a basic example for FTM0. My code below. 

TIA

My init code for the FTM0:

#define PWM_PERIOD 10000

   SIM_SCGC6 |= SIM_SCGC6_FTM0;                 // clock to flex timer 0

    FTM0_SC  = 0;                                // stop clocking the counter

    dummy = FTM0_FMS;                           // enable write to all FTM registers 

    FTM0_MODE    = FTM_MODE_WPDIS;               // disable write protection

    FTM0_MODE    = FTM_MODE_WPDIS | FTM_MODE_FTMEN;

    FTM0_PWMLOAD = 0x02ff;                        // enable loading of compare values for every channel

    FTM0_CNTIN   = 0;                             // counter initial value

    FTM0_CNT     = 0;                             // counter

    FTM0_MOD     = PWM_PERIOD - 1;                // wrap value ie counter modulo

    FTM0_SC      = 0x08;                          // no ISR, up-count, enabled system clock, prescale=1

To enable a particular pin, this code is called:

switch(pin)

    {

        case PTC1:

            PORTC_PCR1  = (uint32_t)(PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE); // FTM0, teensy 22

            GPIOC_PDDR |= 0x00000002;

            FTM0_C0SC   = 0x28;

            break;

        case PTC2:

            PORTC_PCR2  = (uint32_t)(PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE); //       teensy 23

            GPIOC_PDDR |= 0x00000004;

            FTM0_C1SC   = 0x28;

            break;

        case PTC3:

            PORTC_PCR3  = (uint32_t)(PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE); //       teensy  9

            GPIOC_PDDR |= 0x00000008;

            FTM0_C2SC   = 0x28;

            break;

        case PTC4:

            PORTC_PCR4  = (uint32_t)(PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE); //       teensy 10

            GPIOC_PDDR |= 0x00000010;

            FTM0_C3SC   = 0x28;

            break;

        case PTD4:

            PORTD_PCR4  = (uint32_t)(PORT_PCR_MUX(4)); //       teensy  6

            GPIOD_PDDR |= 0x00000010;

            FTM0_C4SC   = 0x28;

            break;

        case PTD5:

      //  uA_tx('E');

            PORTD_PCR5  = (uint32_t)(PORT_PCR_MUX(4)); //       teensy 20

            GPIOD_PDDR |= 0x00000020;

            FTM0_C5SC   = 0x28;

            break;

        case PTD6:

            PORTD_PCR6  = (uint32_t)(PORT_PCR_MUX(4)); //       teensy 21

            GPIOD_PDDR |= 0x00000040;

            FTM0_C6SC   = 0x28;

            break;

        case PTD7:

            PORTD_PCR7  = (uint32_t)(PORT_PCR_MUX(4)); //       teensy  5

            GPIOD_PDDR |= 0x00000080;

            FTM0_C7SC   = 0x28;

    }

To set PWM duty cycle value using a 16 bit variable value:

    switch(pin)

    {

        case PTC1:  FTM0_C0V = value; break; // FTM0, teensy 22 pin 44

        case PTC2:  FTM0_C1V = value; break; //       teensy 23 pin 45

        case PTC3:  FTM0_C2V = value; break; //       teensy  9

        case PTC4:  FTM0_C3V = value; break; //       teensy 10

        case PTD4:  FTM0_C4V = value; break; //       teensy  6

        case PTD5:  FTM0_C5V = value; break;//uA_tx('V');break; //       teensy 20 pin 62

        case PTD6:  FTM0_C6V = value; break; //       teensy 21 pin 63

        case PTD7:  FTM0_C7V = value; break; //       teensy  5

    }

0 Kudos
1 Reply

706 Views
jingpan
NXP TechSupport
NXP TechSupport

Hi Thomas,

I tested your code. It has no problem in register setting. It only need to put duty setting and pin selection code before start.

It should looks like

PWM_Pin_sel(PTD7);     //your second function
PWM_duty(PTD7,PWM_PERIOD/2);   // third function
FTM0_SC = 0x08;

K20 72M version hasn't SDK. But K20 100M device has. The SDK has lots of FTM example. Since FTM module is same, you can refer to those example.

Regards,

Jing

0 Kudos