AnsweredAssumed Answered

How to generate PWM using FTM with RTC Oscillator in K20 USB MCU ?

Question asked by Amit Kumar on Aug 30, 2013
Latest reply on Sep 12, 2013 by Amit Kumar

Hello I am new to Freescale , I am using TWR-K20D50 Development Board. I want to generate a PWM Frequency of 50 Hz Through FTM. I tried using Internal reference clock but within 1-2 minutes the frequency changed to 42 Khz once or twice but I want Exact 50 Hz continuously so I am trying to use XTAL32 i.e RTC crystal attached. The code which I wrote didn't worked. Can anybody figure out what the problem is. Thanks in advance

 

/*****************************************************************************

*    PROJECT      :     PWM Generation

*    BOARD         :    TWR-K20D50M (Freescale)

*    AUTHOR       :    Amit Kumar

*    DATE            :    26/08/2013

*****************************************************************************/

 

#include <MK20D5.H>        // Header file

 

/*****************************************************************************

* Function to initialize RTC Oscillator

*****************************************************************************/

void PWM_RTC_Init(void)

{

    SIM->SOPT1 |= SIM_SOPT1_OSC32KSEL(2);            // Selecting RTC clock

    SIM->SOPT2 &= ~SIM_SOPT2_PLLFLLSEL_SHIFT;   // Selecting FLL Clock for various peripheral

    SIM->SOPT2 |= SIM_SOPT2_CLKOUTSEL(5);         // Selecting 32.768 kHz

  

    MCG->C1 |= MCG_C1_CLKS(0) | MCG_C1_FRDIV(0);   // Output of FLL Selected, FLL External Reference Divider

    MCG->C1 &= ~MCG_C1_IREFS_SHIFT;                               // External reference clock is selected.

    MCG->C4 |= MCG_C4_DMX32_MASK | MCG_C4_DRST_DRS(0);   // 32.768 clock, Fll factor 640, DCO range 20-25 MHz

    MCG->C6 &= ~MCG_C6_PLLS_SHIFT;                //  FLL is selected

    MCG->C7 |= MCG_C7_OSCSEL_MASK;               // Selects 32 kHz RTC Oscillator.

  

    SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK;         // Creating a gating clock for PORT C

    SIM->SCGC6 |= SIM_SCGC6_FTM0_MASK;          // Controls the clock gate to the FTM0 module

    SIM->SOPT4 |= SIM_SOPT4_FTM0TRG0SRC_MASK;       // Selects the source of FTM0 hardware trigger 0

  

    PORTC->PCR[1] |= PORT_PCR_MUX(4);       // Selecting the alternative 4 of port C i.e FTM0_CH0

  

    FTM0->SC |= FTM_SC_CLKS(3);                    // Selecting External Clock for Source clock

    FTM0->SC |= FTM_SC_PS(4);                       // Selecting presaler as 0

    FTM0->MOD = 52560;                                       // Setting Modulo value

    FTM0->CONTROLS[0].CnSC |= ( FTM_CnSC_MSB_MASK | FTM_CnSC_ELSA_MASK); // Rising edge counter

    FTM0->CONTROLS[0].CnV = 0;                   // Starting PWM with initial 0 duty cycle

    FTM0->CNTIN = 0;                                       // Initial Counter value to 0                 

}

 

 

int main(void)

{

    PWM_RTC_Init();            // Calling the function

    while(1)

    {

            FTM0->CONTROLS[0].CnV = 2600;       // Setting the PWM value

    }

}

Outcomes