Advertisement
Guest User

Untitled

a guest
May 25th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.91 KB | None | 0 0
  1. #include "derivative.h"
  2.  
  3. void PWM_Init(FTM_Type *pFTM, uint32_t u32clock, uint32_t u32periodUS)
  4. {
  5.      /* SIM_SCGC: pFTM=1 */
  6.      SIM_SCGC |= ((pFTM == FTM0) ? SIM_SCGC_FTM0_MASK : (pFTM == FTM1 ? SIM_SCGC_FTM1_MASK : SIM_SCGC_FTM2_MASK));
  7.  
  8.      if ( pFTM == FTM2 )
  9.      {
  10.           /* Set up mode register */
  11.           /* FTM_MODE: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,FAULTIE=0,FAULTM=0,CAPTEST=0,PWMSYNC=0,WPDIS=1,INIT=0,FTMEN=0 */
  12.           pFTM->MODE = (FTM_MODE_FAULTM(0x00) | FTM_MODE_WPDIS_MASK);
  13.  
  14.           /* Clear counter initial register */
  15.           /* FTM_CNTIN: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,INIT=0 */
  16.           pFTM->CNTIN = FTM_CNTIN_INIT( 0x00 );
  17.      }
  18.  
  19.      /* Clear status and control register */
  20.      /* FTM_SC: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,TOF=0,TOIE=0,CPWMS=0,CLKS=0,PS=0 */
  21.      pFTM->SC = (FTM_SC_CLKS(0x00) | FTM_SC_PS( 0x00 ));
  22.  
  23.      /* Reset counter register */
  24.      /* FTM_CNT: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,COUNT=0 */
  25.      pFTM->CNT = FTM_CNT_COUNT( 0x00 );
  26.  
  27.      /* Clear channel status and control register */
  28.      /* FTM_C0SC: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,CHF=0,CHIE=0,MSB=0,MSA=0,ELSB=0,ELSA=0,??=0,??=0 */
  29.      FTM_CnSC_REG(pFTM, 0) = 0x00U;
  30.  
  31.      /* Clear channel status and control register */
  32.      /* FTM_C1SC: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,CHF=0,CHIE=0,MSB=0,MSA=0,ELSB=0,ELSA=0,??=0,??=0 */
  33.      FTM_CnSC_REG(pFTM, 1) = 0x00U;
  34.  
  35.      if ( pFTM == FTM2 )          // only FTM2 have more than 2 Channels
  36.      {
  37.           /* Clear channel status and control register */
  38.           /* FTM_C2SC: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,CHF=0,CHIE=0,MSB=0,MSA=0,ELSB=0,ELSA=0,??=0,??=0 */
  39.           FTM_CnSC_REG(pFTM, 2) = 0x00U;
  40.  
  41.           /* Clear channel status and control register */
  42.           /* FTM_C3SC: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,CHF=0,CHIE=0,MSB=0,MSA=0,ELSB=0,ELSA=0,??=0,??=0 */
  43.           FTM_CnSC_REG(pFTM, 3) = 0x00U;
  44.  
  45.           /* Clear channel status and control register */
  46.           /* FTM_C4SC: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,CHF=0,CHIE=0,MSB=0,MSA=0,ELSB=0,ELSA=0,??=0,??=0 */
  47.           FTM_CnSC_REG(pFTM, 4) = 0x00U;
  48.  
  49.           /* Clear channel status and control register */
  50.           /* FTM_C5SC: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,CHF=0,CHIE=0,MSB=0,MSA=0,ELSB=0,ELSA=0,??=0,??=0 */
  51.           FTM_CnSC_REG(pFTM, 5) = 0x00U;
  52.      }
  53.  
  54.      /* Set up modulo register */
  55.      /* FTM_MOD: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,MOD=0x3E7F */
  56.      //pFTM->MOD = FTM_MOD_MOD(0x3E7F);
  57.      pFTM->MOD = FTM_MOD_MOD( u32clock/2/(1000000/u32periodUS) );
  58.  
  59.      if ( pFTM == FTM2 )
  60.      {
  61.           /* Set up Initial State for Channel Output register */
  62.           /* FTM_OUTINIT: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,CH7OI=0,CH6OI=0,CH5OI=0,CH4OI=0,CH3OI=0,CH2OI=0,CH1OI=0,CH0OI=1 */
  63.           pFTM->OUTINIT = FTM_OUTINIT_CH0OI_MASK;
  64.  
  65.           /* Initialize the Output Channels */
  66.           /* FTM_MODE: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,FAULTIE=0,FAULTM=0,CAPTEST=0,PWMSYNC=0,WPDIS=1,INIT=1,FTMEN=0 */
  67.           pFTM->MODE = (FTM_MODE_FAULTM(0x00) | FTM_MODE_WPDIS_MASK | FTM_MODE_INIT_MASK);
  68.      }
  69. }
  70.  
  71. void PWM_Enable(FTM_Type *pFTM, uint8_t channel)
  72. {
  73.      /* Set up channel status and control register */
  74.      /* FTM_C0SC: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,CHF=0,CHIE=0,MSB=1,MSA=0,ELSB=1,ELSA=0,??=0,??=0 */
  75.      FTM_CnSC_REG(pFTM, channel) = (FTM_CnSC_MSB_MASK | FTM_CnSC_ELSB_MASK);
  76.  
  77.      /* Set up channel value register - 0 default duty--cycle*/
  78.      /* FTM_C0V: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,VAL=0 */
  79.      FTM_CnV_REG(pFTM, channel) = FTM_CnV_VAL( 0x00 );
  80.  
  81.      /* Initialize clocks, prescalers and other timer stuff */
  82.      pFTM->SC |= 1 << 5;     // CPWMS (Center-Aligned PWM Select): 0 = counting up, 1 = counting down
  83.      pFTM->SC |= 0 << 6;     // TOIE (Timer Overflow Interrupt Enable): 0 = disabled, 1 = enabled
  84.      pFTM->SC |= 0 << 4;     // CLKS (Clock Source Selection) 4-3: 00 - No clock selected, 01 - System clock
  85.      pFTM->SC |= 1 << 3;     // CLKS (Clock Source Selection) 4-3: 10 - Fixed frequency clock, 11 - External clock
  86.      pFTM->SC |= 0 << 2;     // PS (Prescale Factor Selection):
  87.      pFTM->SC |= 1 << 1;     // PS (Prescale Factor Selection):
  88.      pFTM->SC |= 1 << 0;     // PS (Prescale Factor Selection):
  89. //                    321
  90. //                    000 Divide by 1
  91. //                    001 Divide by 2
  92. //                    010 Divide by 4
  93. //                    011 Divide by 8               V
  94. //                    100 Divide by 16
  95. //                    101 Divide by 32
  96. //                    110 Divide by 64
  97. //                    111 Divide by 128
  98.  
  99.      /* Select PIN automatically */
  100.      if ( pFTM == FTM0 )
  101.      {
  102.           SIM_PINSEL |= ((channel == 0) ? (SIM_PINSEL_FTM0PS0_MASK) :( SIM_PINSEL_FTM0PS1_MASK));
  103.      }
  104.      else if ( pFTM == FTM1 )
  105.      {
  106.           SIM_PINSEL |= ((channel == 0) ? (SIM_PINSEL_FTM1PS0_MASK) : ( SIM_PINSEL_FTM1PS1_MASK));
  107.      }
  108.      else if ( pFTM == FTM2 )
  109.      {
  110.           if ( channel == 0 )
  111.           {
  112.                SIM_PINSEL &= (uint32_t)~(uint32_t)(SIM_PINSEL_FTM2PS0_MASK);
  113.           }
  114.           else if ( channel == 1 )
  115.           {
  116.                SIM_PINSEL &= (uint32_t)~(uint32_t)(SIM_PINSEL_FTM2PS1_MASK);
  117.           }
  118.           else if ( channel == 2 )
  119.           {
  120.                SIM_PINSEL &= (uint32_t)~(uint32_t)(SIM_PINSEL_FTM2PS2_MASK);
  121.           }
  122.           else if ( channel == 3 )
  123.           {
  124.                SIM_PINSEL &= (uint32_t)~(uint32_t)(SIM_PINSEL_FTM2PS3_MASK);
  125.           }
  126.           else if ( channel == 4 || channel == 5 )
  127.           {
  128.                // Not pinsel masks for this SKEAZN642 :(
  129.                ;
  130.           }
  131.      }
  132. }
  133.  
  134. void PWM_SetRatio16(FTM_Type *pFTM, uint8_t ftmChannel, uint16_t ratio)
  135. {
  136.      uint16_t period;
  137.      uint16_t duty;
  138.  
  139.      // Retrieve period from FTM module
  140.      period = (uint16_t)(pFTM->MOD) + 1;
  141.  
  142.      if ( period == 0U )
  143.      {
  144.           duty = ratio;
  145.      }
  146.      else
  147.      {
  148.           duty = (uint16_t)((((uint32_t)(period) * ratio) + 0x8000) >> 0x10);
  149.      }
  150.  
  151.      // Write new duty
  152.      FTM_CnV_REG(pFTM, ftmChannel) = (uint32_t)duty;
  153. }
  154.  
  155. int main()
  156. {
  157.      /* FTM module and it's channel */
  158.      FTM_Type *pFTM = FTM2;
  159.      uint8_t channel = 0;
  160.  
  161.      /* Init pwm driver */
  162.      PWM_Init( pFTM, 16000000/8 /*Clock in Hz with selected prescaler*/, 1000 /*Period in US*/ );
  163.  
  164.      /* Emable PWM on a given channel*/
  165.      PWM_Enable( pFTM, channel );
  166.  
  167.      int i, j;
  168.      while ( 1 )
  169.      {
  170.           for ( i = 0; i <= 65535; i++ )
  171.           {
  172.                PWM_SetRatio16( pFTM, channel, i );
  173.                for ( int j = 0; j <= 10; j++ )
  174.                     ;
  175.           }
  176.  
  177.           for ( i = 65534; i >= 1; i-- )
  178.           {
  179.                PWM_SetRatio16( pFTM, channel, i );
  180.                for ( int j = 0; j <= 10; j++ )
  181.                     ;
  182.           }
  183.      }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement