/*********************************************************************
 *                    Flowcode PWM Component Code
 *
 * File: PIC_PWM.c
 *
 * (c) 2009 Matrix Multimedia Ltd.
 * http://www.matrixmultimedia.com
 *
 * Software License Agreement
 *
 * The software supplied herewith by Matrix Multimedia Ltd (the
 * “Company”) for its Flowcode graphical programming language is
 * intended and supplied to you, the Company’s customer, for use
 * solely and exclusively on the Company's products. The software
 * is owned by the Company, and is protected under applicable
 * copyright laws. All rights are reserved. Any use in violation
 * of the foregoing restrictions may subject the user to criminal
 * sanctions under applicable laws, as well as to civil liability
 * for the breach of the terms and conditions of this licence.
 *
 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *
 * Changelog:
 *
 *  date  | by | description
 * -------+----+-----------------------------------------------------
 * 260207 | ST | Created
 * 010307 | ST | Changed due to 2 PWMs within 1 component
 * 250309 | BR | Added 10-bit PWM functionality
 * 130509 | BR | Added % definitions to DefinesCode section
 *
 ********************************************************************/

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

 Return & parameter types:
   void
   char
   short
   char*

 Pin directions:
   0 = OUTPUT
   2 = INPUT
   3 = BIDIRECTIONAL

**********************************************************************

[Settings]
CLSID={78F46BA6-E118-42D2-AF9E-22B628E3248E}
IsAnalogue=0
MultipleAllowed=0
Description=PWM

**********************************************************************

[Port]
DesiredPort=0
PortMandatory=0
SamePortAsID=0

[Pins]
Count=0
1=
2=

[PinDirection]
1=
2=

[PinDesiredBit]
1=
2=

[BitMandatory]
1=
2=

**********************************************************************

[MacroNames]
Count=5
1=Enable
2=Disable
3=SetDutyCycle
4=ChangePeriod
5=SetDutyCycle10bit

[MacroReturns]
1=void
2=void
3=void
4=void
5=void

[MacroIsPrivate]
1=0
2=0
3=0
4=0
5=0

[MacroParameters_Enable]
Count=1
1=nIdx

[MacroParamTypes_Enable]
1=char

[MacroParameters_Disable]
Count=1
1=nIdx

[MacroParamTypes_Disable]
1=char

[MacroParameters_SetDutyCycle]
Count=2
1=nIdx
2=nDuty

[MacroParamTypes_SetDutyCycle]
1=char
2=char

[MacroParameters_ChangePeriod]
Count=2
1=nPeriodVal
2=nPrescalerVal

[MacroParamTypes_ChangePeriod]
1=char
2=char

[MacroParameters_SetDutyCycle10bit]
Count=2
1=nIdx
2=nDuty

[MacroParamTypes_SetDutyCycle10bit]
1=char
2=short

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

/********************************************************************
 * ADDITIONAL CODE
 ********************************************************************/

/*DefinesCode_Start*/

/**** Macro Substitutions ****
%a = Timer 2 Rollover Value
%b = Timer 2 Prescaler Value
%c = Unused
%d = Alternate Pin Enable
******************************/

/*DefinesCode_End*/

/*InitialisationCode_Start*/
/*InitialisationCode_End*/

/*InterruptCode_Start*/
/*InterruptCode_End*/

/********************************************************************
 * FUNCTIONS
 ********************************************************************/


void Enable(char nIdx)
{
/*Macro_Enable_Start*/

  //error checking
  #ifndef MX_PWM
    #warning "This chip does not have PWM capability"
  #else
    #ifndef MX_PWM_CNT
      #pragma error FCD file error (no MX_PWM_CNT)
    #endif
    #if (MX_PWM_CNT < 1)
      #pragma error FCD file error (MX_PWM_CNT < 1)
    #endif
    #if (MX_PWM_CNT > 2)
      #pragma error FCD file error (MX_PWM_CNT > 2)
    #endif
    #ifndef MX_PWM_TRIS1
      #pragma error FCD file error (no MX_PWM_TRIS1)
    #endif
    #ifndef MX_PWM_1
      #pragma error FCD file error (no MX_PWM_1)
    #endif
    #if (MX_PWM_CNT == 2)
      #ifndef MX_PWM_TRIS2
        #pragma error FCD file error (no MX_PWM_TRIS2)
      #endif
      #ifndef MX_PWM_2
        #pragma error FCD file error (no MX_PWM_2)
      #endif
    #endif
    #if (%d == 1)
      #ifndef MX_PWM_TRIS1a
        #pragma error PWM component error (using alternative, but no MX_PWM_TRIS1a)
        #define MX_PWM_ALT_ERROR
      #endif
      #ifndef MX_PWM_1a
        #pragma error PWM component error (using alternative, but no MX_PWM_1a)
        #define MX_PWM_ALT_ERROR
      #endif
    #endif
    #if (%d == 2)
      #ifndef MX_PWM_TRIS2a
        #pragma error PWM component error (using alternative, but no MX_PWM_TRIS2a)
        #define MX_PWM_ALT_ERROR
      #endif
      #ifndef MX_PWM_2a
        #pragma error PWM component error (using alternative, but no MX_PWM_2a)
        #define MX_PWM_ALT_ERROR
      #endif
    #endif
  #endif

 #ifdef MX_PWM
    pr2 = %a;
    t2con = %b;

  #if (MX_PWM_CNT >= 1)
    if (nIdx == 1)
    {
        ccp1con = 0x0C;

      #if (%d == 1)
       #ifndef MX_PWM_ALT_ERROR
        clear_bit(MX_PWM_TRIS1a, MX_PWM_1a);
       #endif
      #else
        clear_bit(MX_PWM_TRIS1, MX_PWM_1);
      #endif
	}
  #endif

  #if (MX_PWM_CNT >= 2)
    if (nIdx == 2)
    {
        ccp2con = 0x0C;
      #if (%d == 2)
       #ifndef MX_PWM_ALT_ERROR
        clear_bit(MX_PWM_TRIS2a, MX_PWM_2a);
       #endif
      #else
        clear_bit(MX_PWM_TRIS2, MX_PWM_2);
      #endif
   }
  #endif
 #endif

 #ifdef MX_PWM_ALT_ERROR
   #undef MX_PWM_ALT_ERROR
 #endif

/*Macro_Enable_End*/
}



void Disable(char nIdx)
{
/*Macro_Disable_Start*/

  //error checking
  #ifndef MX_PWM
    #warning "This chip does not have PWM capability"
  #endif

 #ifdef MX_PWM
  #if (MX_PWM_CNT >= 1)
    if (nIdx == 1)
    {
        ccp1con = 0x00;
    }
  #endif

  #if (MX_PWM_CNT >= 2)
    if (nIdx == 2)
    {
        ccp2con = 0x00;
    }
  #endif
 #endif

/*Macro_Disable_End*/
}


void SetDutyCycle(char nIdx, char nDuty)
{
/*Macro_SetDutyCycle_Start*/

  //error checking
  #ifndef MX_PWM
    #warning "This chip does not have PWM capability"
  #endif

 #ifdef MX_PWM
  #if (MX_PWM_CNT >= 1)
    if (nIdx == 1)
    {
        ccpr1l = nDuty;
    }
  #endif

  #if (MX_PWM_CNT >= 2)
    if (nIdx == 2)
    {
        ccpr2l = nDuty;
    }
  #endif
 #endif

/*Macro_SetDutyCycle_End*/
}



void ChangePeriod(char nPeriodVal, char nPrescalerVal)
{
/*Macro_ChangePeriod_Start*/

  //error checking
  #ifndef MX_PWM
    #warning "This chip does not have PWM capability"
  #endif

  #ifdef MX_PWM
    pr2 = nPeriodVal;

    switch (nPrescalerVal)
    {
        case 1:
            t2con = 0x04;
            break;

        case 4:
            t2con = 0x05;
            break;

        case 16:
            t2con = 0x06;
            break;
    }
  #endif

/*Macro_ChangePeriod_End*/
}


void SetDutyCycle10bit(char nIdx, short nDuty)
{
/*Macro_SetDutyCycle10bit_Start*/

	char nCCPxCONtemp;

  //error checking
  #ifndef MX_PWM
    #warning "This chip does not have PWM capability"
  #endif

 #ifdef MX_PWM
  #if (MX_PWM_CNT >= 1)
    if (nIdx == 1)
    {
        ccpr1l = (nDuty & 0x3FC) >> 2;

    	nDuty = (nDuty & 0x03) << 4;
    	nCCPxCONtemp = (ccp1con & 0xCF) | nDuty;
    	ccp1con = nCCPxCONtemp;
    }

  #endif

  #if (MX_PWM_CNT >= 2)
    if (nIdx == 2)
    {
        ccpr2l = (nDuty & 0x3FC) >> 2;

    	nDuty = (nDuty & 0x03) << 4;
    	nCCPxCONtemp = (ccp2con & 0xCF) | nDuty;
    	ccp2con = nCCPxCONtemp;
    }

  #endif
 #endif

/*Macro_SetDutyCycle10bit_End*/
}

