Attribute VB_Name = "Module1"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Const.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\include\Port.inc"
'#Uses "..\classes\FunctionClass.cls
'#Uses "..\classes\ParameterClass.cls
Option Explicit
'*****************************************************************************
' Module:
'        T2CCU
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the
'        T2CCU dialog script and in the T2CCU codegenerator script.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        0.1  SankaraNarayana 07.02.08    Initial version for XC878CLM
'        ---------------------------------------------------------------------
'
'*****************************************************************************


'*****************************************************************************
' @Constants
'*****************************************************************************
' use this constant in the Format statement for all calculated outputs like
' baud rates or deviation

Global Const RATIO_FSYS_T2 = 12          ' RATIO_FSYS_T2  = fSYS / T2CounterRate
Global Const RATIO_FSYS_T2_CAPTURE = 24
'Note: this ratio is only valid for timer function, not for baudrate generation


'*****************************************************************************
'Description:
'        This procedure sets the T2CCU-Module into the default.
'-----------------------------------------------------------------------------
'Parameters:
'        r               As ProjectData
'
'*****************************************************************************
Sub SetT2CCUDefault(r As ProjectData)
    On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  Dim i As Integer
  Dim vIndex As Variant

  'Channel reset
  For i = 0 To (r.v(T2CCU_PREFIX & "MAX_CHANNELS")-1)
    Call ResetT2CCUChannel(r, vIndex,i)
  Next i

  r.v("T2CCU_USE_Init")                  = 0
  r.v("T2CCU_USE_StartTmr")              = 0
  r.v("T2CCU_USE_StopTmr")               = 0
  r.v("T2CCU_USE_GetTmrPeriod")          = 0
  r.v("T2CCU_USE_LoadTmrPeriod")         = 0
  r.v("T2CCU_USE_GetCH0CaptVal")         = 0
  r.v("T2CCU_USE_GetCH1CaptVal")         = 0
  r.v("T2CCU_USE_GetCH2CaptVal")         = 0
  r.v("T2CCU_USE_GetCH3CaptVal")         = 0
  r.v("T2CCU_USE_GetCH4CaptVal")         = 0
  r.v("T2CCU_USE_GetCH5CaptVal")         = 0
  r.v("T2CCU_USE_LoadChCompReg")         = 0
  r.v("T2CCU_USE_EnableShadowTransfer")  = 0
  r.v("T2CCU_USE_EnableCompChOutput")    = 0
  r.v("T2CCU_USE_NodeIsr0") = 0
  r.v("T2CCU_USE_NodeIsr1") = 0

  r.sfr(T2CCU_PREFIX & "CCTBSEL") = &H00
  r.sfr(T2CCU_PREFIX & "CCTRELL") = &H00
  r.sfr(T2CCU_PREFIX & "CCTRELH") = &H00
  r.sfr(T2CCU_PREFIX & "CCTL")    = &H00
  r.sfr(T2CCU_PREFIX & "CCTH")    = &H00
  r.sfr(T2CCU_PREFIX & "CCTCON")  = &H00
  r.sfr(T2CCU_PREFIX & "CCTDTCL") = &H01
  r.sfr(T2CCU_PREFIX & "CCTDTCH") = &H00
  r.sfr(T2CCU_PREFIX & "CCEN")    = &H00
  r.sfr(T2CCU_PREFIX & "COSHDW")  = &H00
  r.sfr(T2CCU_PREFIX & "COCON")   = &H00

End Sub

'*****************************************************************************
'Description:
'        This function checks if the module clock is disabled.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            the module clock is disabled
'        False           else
'*****************************************************************************
Function IsModuleClockDisabled(r As ProjectData, vindex As Variant) As Boolean
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "T2CCU" & vindex
  sRegPre = sVarPre & "_"
  IsModuleClockDisabled = False
  IsModuleClockDisabled = r.v(SCU_PREFIX & "PMCON1_T2CCU_DIS") = 1
End Function


'*****************************************************************************
'Description:
'        This function returns a formated disabled tooltip if the module
'        clock is disabled.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        Formated tooltip
'*****************************************************************************
Function GetTooltipModuleClockDisabled(r As ProjectData, vindex As Variant) As String
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "T2CCU" & vindex
  sRegPre = sVarPre & "_"
  
  GetTooltipModuleClockDisabled = "T2CCU module clock is disabled"
End Function

'*****************************************************************************
'Description:
'       The timer 2 overflow rate is returned as a formatted string.
'       The unit is micro seconds. The value depends on the timer count value
'       and the oscillator frequency.
'-----------------------------------------------------------------------------
'Parameters:
'-----------------------------------------------------------------------------
'Returns:
'       overflow rate   e.g. "780.500"
'*****************************************************************************
Function UpdateTimer2Overflow(r As ProjectData) As String
    On Error Resume Next

''        If r.v(T2_PREFIX & "T2MOD_PREN") = 1 Then
''            UpdateTimer2Overflow = FormatTextOutput(Format$((2 ^ r.l(T2_PREFIX & "T2MOD_T2PRE")) * (65536 - (r.l(T2_PREFIX & "RC2H_RC2") * 256 + _
''                                                                r.l(T2_PREFIX & "RC2L_RC2"))) / GetT2CCU_fFCLK(r, 1), "0.000"), "F")
''            Exit Function
''        End If
''        If r.v(T2_PREFIX & "T2CON_CP/RL2") = 1 Then
''            UpdateTimer2Overflow = FormatTextOutput(Format$(RATIO_FSYS_T2_CAPTURE * (65536 - (r.l(T2_PREFIX & "RC2H_RC2") * 256 + _
''                                                                r.l(T2_PREFIX & "RC2L_RC2"))) / GetT2CCU_fFCLK(r, 1), "0.000"), "F")
''        Else
''            UpdateTimer2Overflow = FormatTextOutput(Format$(RATIO_FSYS_T2 * (65536 - (r.l(T2_PREFIX & "RC2H_RC2") * 256 + _
''                                                                r.l(T2_PREFIX & "RC2L_RC2"))) / GetT2CCU_fFCLK(r, 1), "0.000"), "F")
''        End If
        
        If r.v(T2_PREFIX & "T2CON_CP/RL2") = 1 Then
            If r.v(T2_PREFIX & "T2MOD_PREN") = 1 Then
                UpdateTimer2Overflow = FormatTextOutput(Format$((2 ^ r.l(T2_PREFIX & "T2MOD_T2PRE")) * (65536 / GetT2CCU_fFCLK(r, 1)), "0.000"), "F")
            Else
                UpdateTimer2Overflow = FormatTextOutput(Format$(RATIO_FSYS_T2 * (65536 / GetT2CCU_fFCLK(r, 1)), "0.000"), "F")
            End If
        Else
            If r.v(T2_PREFIX & "T2MOD_PREN") = 1 Then
                UpdateTimer2Overflow = FormatTextOutput(Format$((2 ^ r.l(T2_PREFIX & "T2MOD_T2PRE")) * (65536 - (r.l(T2_PREFIX & "RC2H_RC2") * 256 + _
                                                                r.l(T2_PREFIX & "RC2L_RC2"))) / GetT2CCU_fFCLK(r, 1), "0.000"), "F")
            Else
                UpdateTimer2Overflow = FormatTextOutput(Format$(RATIO_FSYS_T2 * (65536 - (r.l(T2_PREFIX & "RC2H_RC2") * 256 + _
                                                                r.l(T2_PREFIX & "RC2L_RC2"))) / GetT2CCU_fFCLK(r, 1), "0.000"), "F")
            End If
        End If
        
End Function


'*****************************************************************************
'Description:
'       The CCT Timer overflow rate is returned as a formatted string.
'       The unit is micro seconds. The value depends on the timer count value
'       and the oscillator frequency.
'-----------------------------------------------------------------------------
'Parameters:
'-----------------------------------------------------------------------------
'Returns:
'       overflow rate   e.g. "780.500"
'*****************************************************************************
Function GetCCTOverflow(r As ProjectData) As String
    On Error Resume Next
        
	r.v("T2_TmrOverflow") = UpdateTimer2Overflow(r)
        If r.v(T2CCU_PREFIX & "CCTBSEL_CASC") = 1 Then
            GetCCTOverflow = FormatTextOutput(Format$((2 ^ r.l(T2CCU_PREFIX & "CCTCON_CCTPRE")) * (65536 - (r.l(T2CCU_PREFIX & "CCTRELH_CCTREL[15:8]") * 256 + _
                                                                r.l(T2CCU_PREFIX & "CCTRELL_CCTREL[7:0]"))) * r.v("T2_TmrOverflow") / 1, "0.000"), "F")
        Else
            GetCCTOverflow = FormatTextOutput(Format$((2 ^ r.l(T2CCU_PREFIX & "CCTCON_CCTPRE")) * (65536 - (r.l(T2CCU_PREFIX & "CCTRELH_CCTREL[15:8]") * 256 + _
                                                                r.l(T2CCU_PREFIX & "CCTRELL_CCTREL[7:0]"))) / GetT2CCU_fFCLK(r,1), "0.000"), "F")
        End If
'            Exit Function
'        End If
'        If r.v(T2_PREFIX & "T2CON_CP/RL2") = 1 Then
'            GetTimer2Overflow = FormatTextOutput(Format$(RATIO_FSYS_T2_CAPTURE * (65536 - (r.l(T2_PREFIX & "RC2H_RC2") * 256 + _
'                                                                r.l(T2_PREFIX & "RC2L_RC2"))) / GetCoreClock(r), "0.000"), "F")
'        Else
'            GetTimer2Overflow = FormatTextOutput(Format$(RATIO_FSYS_T2 * (65536 - (r.l(T2_PREFIX & "RC2H_RC2") * 256 + _
'                                                                r.l(T2_PREFIX & "RC2L_RC2"))) / GetCoreClock(r), "0.000"), "F")
'        End If
        
End Function


'*****************************************************************************
'Description:
'       The CCT Timer underflow rate is returned as a formatted string.
'       The unit is micro seconds. The value depends on the timer count value
'       and the oscillator frequency.
'-----------------------------------------------------------------------------
'Parameters:
'-----------------------------------------------------------------------------
'Returns:
'       overflow rate   e.g. "780.500"
'*****************************************************************************
Function GetCCTUnderflow(r As ProjectData) As String
    On Error Resume Next
    
'    If r.v(T2_PREFIX & "T2MOD_PREN") = 1 Then
'        GetTimer2Underflow = FormatTextOutput(Format$((2 ^ r.l(T2_PREFIX & "T2MOD_T2PRE")) * (1 + (r.l(T2_PREFIX & "RC2H_RC2") * 256 + _
'                                                        r.l(T2_PREFIX & "RC2L_RC2"))) / GetCoreClock(r), "0.000"), "F")
'    Else
'        GetTimer2Underflow = FormatTextOutput(Format$(RATIO_FSYS_T2 * (1 + (r.l(T2_PREFIX & "RC2H_RC2") * 256 + _
'                                                        r.l(T2_PREFIX & "RC2L_RC2"))) / GetCoreClock(r), "0.000"), "F")
'    End If
    
End Function


'*****************************************************************************
'Description:
'        This function is called from the CCT Timer module
'        and calculate overflow time for DTM.
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        vIndex          parameter
'-----------------------------------------------------------------------------
'Returns:
'        overflow time [usec]
'
'*****************************************************************************
Function GetCCTOverflowDTM(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next
  Dim i As Integer
  Dim s As String
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "T2CCU" & vIndex
  sRegPre = sVarPre & "_"

  i = r.v(T2CCU_PREFIX & "CCTCON_CCTPRE")

  GetCCTOverflowDTM = r.l(T2CCU_PREFIX & "CCTDTCL_DTM") * 2 ^ (i) / GetT2CCU_fFCLK(r,1)

End Function

'*****************************************************************************
'Description:
'        This function is called from the CCT Timer module
'        and calculate the maximum resolution for dead time.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        vIndex          ParameterValue
'        iTmr            Timer Nr. (12)
'-----------------------------------------------------------------------------
'Returns:
'        Value of the max. resolution [usec]
'
'*****************************************************************************
Function GetCCTMaxResolutionDTM(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next
  Dim sRegPre As String
  Dim sVarPre As String

  sVarPre = "T2CCU" & vIndex
  sRegPre = sVarPre & "_"

  GetCCTMaxResolutionDTM = 255 * 2 ^ (r.v(sRegPre & "CCTCON_CCTPRE")) / _
                        GetT2CCU_fFCLK(r,1)

End Function

'*****************************************************************************
'Description:
'        This function is called from the CCT Timer module
'        and calculate the minimum resolution for dead time.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        vIndex          ParameterValue
'        iTmr            Timer Nr. (12)
'-----------------------------------------------------------------------------
'Returns:
'        Value of the max. resolution [usec]
'
'*****************************************************************************
Function GetCCTMinResolutionDTM(r As ProjectData, vIndex As Variant) As Double
  On Error Resume Next
  Dim sRegPre As String
  Dim sVarPre As String

  sVarPre = "T2CCU" & vIndex
  sRegPre = sVarPre & "_"

  GetCCTMinResolutionDTM = 2 ^ (r.v(sRegPre & "CCTCON_CCTPRE")) / _
                        GetT2CCU_fFCLK(r,1)

End Function


'*****************************************************************************
'Description:
'        This function is called from the OnShowPage function
'        and calculate the value of the DTM.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        vIndex          ParameterValue
'        t               entered time [usec]
'        rIndex
'             True       for overflow time
'             False      for underflow time
'        TimerNr         timer number (12)
'-----------------------------------------------------------------------------
'Returns:
'        Value of DTM
'
'*****************************************************************************
Function GetCCTTimerNrValueDTM(r As ProjectData, vIndex As Variant, t As Variant, rIndex As Boolean) As Long
  On Error Resume Next
  Dim i As Integer
  Dim s As String
  Dim sRegPre As String
  Dim sVarPre As String

  sVarPre = "T2CCU" & vIndex
  sRegPre = sVarPre & "_"

  If (t > GetCCTMaxResolutionDTM(r, vIndex)) Then
    t = GetCCTMaxResolutionDTM(r, vIndex)
  End If
  If (t < GetCCTMinResolutionDTM(r, vIndex)) Then
    t = GetCCTMinResolutionDTM(r, vIndex)
  End If

  
  i = r.v(sRegPre & "CCTCON_CCTPRE")

  GetCCTTimerNrValueDTM = t / 2 ^ i * GetT2CCU_fFCLK(r,1)

End Function

'*****************************************************************************
'Description:
'        This function is called from the CCT Timer module
'        and calculate the maximum resolution for dead time.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        vIndex          ParameterValue
'        iTmr            Timer Nr. (12)
'-----------------------------------------------------------------------------
'Returns:
'        Value of the max. resolution [usec]
'
'*****************************************************************************
Sub ResetCCT_DTM (r As ProjectData, vIndex As Variant)
  On Error Resume Next
  Dim sRegPre As String
  Dim sVarPre As String

  sVarPre = "T2CCU" & vIndex
  sRegPre = sVarPre & "_"

  r.v(sRegPre & "CCTDTCL_DTM") = 1
  r.sfr(sRegPre & "CCTDTCH") = 0

End Sub

'*****************************************************************************
'Description:
'        This function is called from the CCT Timer module
'        and calculate the maximum resolution for dead time.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        vIndex          ParameterValue
'        iTmr            Timer Nr. (12)
'-----------------------------------------------------------------------------
'Returns:
'        Value of the max. resolution [usec]
'
'*****************************************************************************
Sub ResetCCT_DTMChannel (r As ProjectData, vIndex As Variant, iNr As Integer)
  On Error Resume Next
  Dim sRegPre As String
  Dim sVarPre As String

  sVarPre = "T2CCU" & vIndex
  sRegPre = sVarPre & "_"

  If(iNr < 2) Then
    r.v(sRegPre & "CCTDTCH_DTE0") = 0
    r.v(sRegPre & "CCTDTCH_DTR0") = 0
  ElseIf (iNr < 4) Then
    r.v(sRegPre & "CCTDTCH_DTE1") = 0
    r.v(sRegPre & "CCTDTCH_DTR1") = 0
  ElseIf (iNr < 6) Then
    r.v(sRegPre & "CCTDTCH_DTE2") = 0
    r.v(sRegPre & "CCTDTCH_DTR2") = 0
  End If

End Sub
'*****************************************************************************
'Description:
'        This function converts a time in usec to an other unit
'
'-----------------------------------------------------------------------------
'Parameters:
'        dValue           the time in usec
'-----------------------------------------------------------------------------
'Returns:
'        .dValue          the new value for the time
'        .sUnit           the new unit of the time
'        .bDivider        the new divider of the new time, referring to s
'*****************************************************************************
Function ConvertTime(dValue As Double) As baudratetype
  On Error GoTo ConvertTimeError
  Dim dHelp As Double
  Dim sHelp As String
  Dim dDivider As Double

  dHelp = dValue
  sHelp = "usec"
  dDivider = 1

  If (dHelp < 1) Then
    dHelp = dHelp * 1000
    sHelp = "ns"
    dDivider = dDivider / 1000
  End If

  If (dHelp >= 1000) Then
    dHelp = dHelp / 1000
    sHelp = "ms"
    dDivider = 1000
    If (dHelp >= 1000) Then
      dHelp = dHelp / 1000
      sHelp = "s"
      dDivider = dDivider * 1000
      If (dHelp >= 60) Then
        dHelp = dHelp / 60
        sHelp = "min"
        dDivider = dDivider * 60
        If (dHelp >= 60) Then
          dHelp = dHelp / 60
          sHelp = "h"
          dDivider = dDivider * 60
          If (dHelp >= 24) Then
            dHelp = dHelp / 24
            sHelp = "days"
            dDivider = dDivider * 24
            If (dHelp >= 30) Then
              dHelp = dHelp / 30
              sHelp = "months"
              dDivider = dDivider * 30
              If (dHelp >= 12) Then
                dHelp = dHelp / 12
                sHelp = "years"
                dDivider = dDivider * 12
              End If
            End If
          End If
        End If
      End If
    End If
  End If

  ConvertTime.dValue = dHelp
  ConvertTime.sUnit = sHelp
  ConvertTime.dDivider = dDivider


  Exit Function

ConvertTimeError:
  ConvertTime.dValue = dValue
  ConvertTime.sUnit = "usec"
  ConvertTime.dDivider = 1
  Exit Function
End Function

'*****************************************************************************
'Description:
'        This function sets all necessary registers to release all T2CCU pins from
'        an alternate function which was requested with the function
'        'RequestAlternatePin'.
'-----------------------------------------------------------------------------
'Parameters:
'        r           reference to this object
'        iNr         number of the channel
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Function GetIndexAddT2CCUPin(r As ProjectData, vIndex As Variant, iNr As Integer, cboName As Variant) As Integer
  On Error Resume Next

  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "T2CCU" & vIndex
  sRegPre = sVarPre & "_"


  Dim ListCount As Integer
  Dim sPreCPUType As String

'  sPreCPUType = Mid(r.v("MAIN_CPU_TYPE"),1, 6)
 
' show condition
  cboName.Clear
  cboName.AddItem "None"

'  Select Case (sPreCPUType)
'
'    Case ("XC2287")     'XC22xx series family

      Select Case (iNr)
        Case (0)
          If(IsAlternatePinFree(r, "T2CC0_0(Output)") Or IsAlternatePinFree(r, "T2CC0_0(Input)")) Then
		    cboName.AddItem "P4.0"
          End If
          If(IsAlternatePinFree(r, "T2CC0_1(Output)") Or IsAlternatePinFree(r, "T2CC0_1(Input)")) Then
		    cboName.AddItem "P3.0"
          End If
          If(IsAlternatePinFree(r, "T2CC0_2(Output)") Or IsAlternatePinFree(r, "T2CC0_2(Input)")) Then
		    cboName.AddItem "P5.5"
          End If

        Case (1)
          If(IsAlternatePinFree(r, "T2CC1_0(Output)") Or IsAlternatePinFree(r, "T2CC1_0(Input)")) Then
		    cboName.AddItem "P4.1"
          End If
          If(IsAlternatePinFree(r, "T2CC1_1(Output)") Or IsAlternatePinFree(r, "T2CC1_1(Input)")) Then
		    cboName.AddItem "P3.2"
          End If
          If(IsAlternatePinFree(r, "T2CC1_2(Output)") Or IsAlternatePinFree(r, "T2CC1_2(Input)")) Then
		    cboName.AddItem "P5.6"
          End If

        Case (2)
          If(IsAlternatePinFree(r, "T2CC2_0(Output)") Or IsAlternatePinFree(r, "T2CC2_0(Input)")) Then
		    cboName.AddItem "P4.4"
          End If
          If(IsAlternatePinFree(r, "T2CC2_1(Output)") Or IsAlternatePinFree(r, "T2CC2_1(Input)")) Then
		    cboName.AddItem "P3.3"
          End If
          If(IsAlternatePinFree(r, "T2CC2_2(Output)") Or IsAlternatePinFree(r, "T2CC2_2(Input)")) Then
		    cboName.AddItem "P5.2"
          End If

        Case (3)
          If(IsAlternatePinFree(r, "T2CC3_0(Output)") Or IsAlternatePinFree(r, "T2CC3_0(Input)")) Then
		    cboName.AddItem "P4.5"
          End If
          If(IsAlternatePinFree(r, "T2CC3_1(Output)") Or IsAlternatePinFree(r, "T2CC3_1(Input)")) Then
		    cboName.AddItem "P3.4"
          End If
          If(IsAlternatePinFree(r, "T2CC3_2(Output)") Or IsAlternatePinFree(r, "T2CC3_2(Input)")) Then
		    cboName.AddItem "P5.7"
          End If

        Case (4)
          If(IsAlternatePinFree(r, "T2CC4_0(Output)")) Then
		    cboName.AddItem "P4.6"
          End If

          If(IsAlternatePinFree(r, "T2CC4_1(Output)")) Then
		    cboName.AddItem "P0.6"
          End If

          If(IsAlternatePinFree(r, "T2CC4_2(Output)")) Then
		    cboName.AddItem "P5.4"
          End If

        Case (5)
          If(IsAlternatePinFree(r, "T2CC5_0(Output)")) Then
		    cboName.AddItem "P4.7"
          End If

          If(IsAlternatePinFree(r, "T2CC5_1(Output)")) Then
		    cboName.AddItem "P0.7"
          End If

          If(IsAlternatePinFree(r, "T2CC5_2(Output)")) Then
		    cboName.AddItem "P5.3"
          End If

	    Case Else

      End Select  'End of Inner Select XC2287



'    Case Else  'No MCAN module is available for remaining derivatives
'
'  End Select  'End of Outer Select CPU_TYPE

  For ListCount = 0 To cboName.ListCount-1
    If(cboName.List(ListCount) = r.v(sRegPre & "USE_CH" & iNr & "_PIN")) Then
      GetIndexAddT2CCUPin = ListCount
    End If
  Next ListCount
 
    If ((cboName.ListCount) = 1) Then
'      cboName.RemoveItem 0
'      cboName.AddItem "Pin RXDC" & iNr & " is not available"
      cboName.Enabled = False
    End If

End Function



'*****************************************************************************
'Description:
'        This function sets all necessary registers to release all T2CCU Pins from
'        an alternate function which was requested with the function
'        'RequestAlternatePin'.
'-----------------------------------------------------------------------------
'Parameters:
'        r           reference to this object
'        iNr         number of the channel
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub ReleaseT2CCUPin(r As ProjectData, vIndex As Variant, iNr As Integer, sPinNo As String)
  On Error Resume Next
  Dim sSgnlDir As String
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "T2CCU" & vIndex
  sRegPre = sVarPre & "_"


  If (iNr < 4) Then  'capt/comp mode
    If (r.v(sRegPre & "CCEN_CCM" & iNr) = 2) Then 'compare mode
     Select Case (iNr)
       Case (0)
         Select Case (sPinNo)
           Case "P4.0"
             Call ReleaseAlternateOutputPin(r, "T2CC0_0(Output)")
           Case "P3.0"
              Call ReleaseAlternateOutputPin(r, "T2CC0_1(Output)")
           Case "P5.5"
             Call ReleaseAlternateOutputPin(r, "T2CC0_2(Output)")
           Case Else
         End Select

       Case (1)
         Select Case (sPinNo)
           Case "P4.1"
             Call ReleaseAlternateOutputPin(r, "T2CC1_0(Output)")
           Case "P3.2"
             Call ReleaseAlternateOutputPin(r, "T2CC1_1(Output)")
           Case "P5.6"
             Call ReleaseAlternateOutputPin(r, "T2CC1_2(Output)")
           Case Else
         End Select

       Case (2)
         Select Case (sPinNo)
           Case "P4.4"
             Call ReleaseAlternateOutputPin(r, "T2CC2_0(Output)")
           Case "P3.3"
             Call ReleaseAlternateOutputPin(r, "T2CC2_1(Output)")
           Case "P5.2"
             Call ReleaseAlternateOutputPin(r, "T2CC2_2(Output)")
           Case Else
         End Select

       Case (3)
         Select Case (sPinNo)
           Case "P4.5"
             Call ReleaseAlternateOutputPin(r, "T2CC3_0(Output)")
           Case "P3.4"
             Call ReleaseAlternateOutputPin(r, "T2CC3_1(Output)")
           Case "P5.7"
             Call ReleaseAlternateOutputPin(r, "T2CC3_2(Output)")
           Case Else
         End Select

       Case Else
     End Select

    Else                                             'capture mode
     Select Case (iNr)
       Case (0)
         r.v("MODPISEL4_EXINT3IS") = 0
         Select Case (sPinNo)
           Case "P4.0"
             Call ReleaseAlternateInputPin(r, "T2CC0_0(Input)")
           Case "P3.0"
              Call ReleaseAlternateInputPin(r, "T2CC0_1(Input)")
           Case "P5.5"
             Call ReleaseAlternateInputPin(r, "T2CC0_2(Input)")
           Case Else
         End Select

       Case (1)
         r.v("MODPISEL4_EXINT4IS") = 0
         Select Case (sPinNo)
           Case "P4.1"
             Call ReleaseAlternateInputPin(r, "T2CC1_0(Input)")
           Case "P3.2"
             Call ReleaseAlternateInputPin(r, "T2CC1_1(Input)")
           Case "P5.6"
             Call ReleaseAlternateInputPin(r, "T2CC1_2(Input)")
           Case Else
         End Select

       Case (2)
         r.v("MODPISEL4_EXINT5IS") = 0
         Select Case (sPinNo)
           Case "P4.4"
             Call ReleaseAlternateInputPin(r, "T2CC2_0(Input)")
           Case "P3.3"
             Call ReleaseAlternateInputPin(r, "T2CC2_1(Input)")
           Case "P5.2"
             Call ReleaseAlternateInputPin(r, "T2CC2_2(Input)")
           Case Else
         End Select

       Case (3)
         r.v("MODPISEL1_EXINT6IS") = 0
         Select Case (sPinNo)
           Case "P4.5"
             Call ReleaseAlternateInputPin(r, "T2CC3_0(Input)")
           Case "P3.4"
             Call ReleaseAlternateInputPin(r, "T2CC3_1(Input)")
           Case "P5.7"
             Call ReleaseAlternateInputPin(r, "T2CC3_2(Input)")
           Case Else
         End Select

       Case Else
     End Select
    End If

  Else 'compare mode only
   Select Case (iNr)
     Case (4)
       Select Case (sPinNo)
         Case "P4.6"
           Call ReleaseAlternateOutputPin(r, "T2CC4_0(Output)")
         Case "P0.6"
           Call ReleaseAlternateOutputPin(r, "T2CC4_1(Output)")
         Case "P5.4"
           Call ReleaseAlternateOutputPin(r, "T2CC4_2(Output)")
         Case Else
       End Select
'       r.v(sRegPre & "COCON_CCM4") = 0  'Disable Compare Function for channel4

     Case (5)
       Select Case (sPinNo)
         Case "P4.7"
           Call ReleaseAlternateOutputPin(r, "T2CC5_0(Output)")
         Case "P0.7"
           Call ReleaseAlternateOutputPin(r, "T2CC5_1(Output)")
         Case "P5.3"
           Call ReleaseAlternateOutputPin(r, "T2CC5_2(Output)")
         Case Else
       End Select
'       r.v(sRegPre & "COCON_CCM5") = 0  'Disable Compare Function for channel5

     Case Else
   End Select
  End If
End Sub


'*****************************************************************************
'Description:
'        This function sets all necessary registers to request all T2CCUPins from
'        an alternate function which was requested with the function
'        'RequestAlternatePin'.
'-----------------------------------------------------------------------------
'Parameters:
'        r           reference to this object
'        iNr         number of the channel
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub RequestT2CCUPin(r As ProjectData, vIndex As Variant, iNr As Integer, sPinNo As String)
  On Error Resume Next
  Dim sSgnlDir As String
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "T2CCU" & vIndex
  sRegPre = sVarPre & "_"

  If (iNr < 4) Then  'capt/comp mode
    If (r.v(sRegPre & "CCEN_CCM" & iNr) = 2) Then 'compare mode
     Select Case (iNr)
       Case (0)
         Select Case (sPinNo)
           Case "P4.0"
             Call RequestAlternateOutputPin(r, "T2CC0_0(Output)", ALT_FUN2)
           Case "P3.0"
             Call RequestAlternateOutputPin(r, "T2CC0_1(Output)", ALT_FUN2)
           Case "P5.5"
             Call RequestAlternateOutputPin(r, "T2CC0_2(Output)", ALT_FUN3)
           Case Else
         End Select

       Case (1)
         Select Case (sPinNo)
           Case "P4.1"
             Call RequestAlternateOutputPin(r, "T2CC1_0(Output)", ALT_FUN2)
           Case "P3.2"
             Call RequestAlternateOutputPin(r, "T2CC1_1(Output)", ALT_FUN2)
           Case "P5.6"
             Call RequestAlternateOutputPin(r, "T2CC1_2(Output)", ALT_FUN3)
           Case Else
         End Select

       Case (2)
         Select Case (sPinNo)
           Case "P4.4"
             Call RequestAlternateOutputPin(r, "T2CC2_0(Output)", ALT_FUN2)
           Case "P3.3"
             Call RequestAlternateOutputPin(r, "T2CC2_1(Output)", ALT_FUN2)
           Case "P5.2"
             Call RequestAlternateOutputPin(r, "T2CC2_2(Output)", ALT_FUN1)
           Case Else
         End Select

       Case (3)
         Select Case (sPinNo)
           Case "P4.5"
             Call RequestAlternateOutputPin(r, "T2CC3_0(Output)", ALT_FUN2)
           Case "P3.4"
             Call RequestAlternateOutputPin(r, "T2CC3_1(Output)", ALT_FUN2)
           Case "P5.7"
             Call RequestAlternateOutputPin(r, "T2CC3_2(Output)", ALT_FUN1)
           Case Else
         End Select

       Case Else
     End Select

    Else                                             'capture mode
     Select Case (iNr)
       Case (0)
         Select Case (sPinNo)
           Case "P4.0"
             Call RequestAlternateInputPin(r, "T2CC0_0(Input)")
             r.v("MODPISEL4_EXINT3IS") = 1
           Case "P3.0"
              Call RequestAlternateInputPin(r, "T2CC0_1(Input)")
              r.v("MODPISEL4_EXINT3IS") = 2
           Case "P5.5"
             Call RequestAlternateInputPin(r, "T2CC0_2(Input)")
             r.v("MODPISEL4_EXINT3IS") = 3
           Case Else
         End Select

       Case (1)
         Select Case (sPinNo)
           Case "P4.1"
             Call RequestAlternateInputPin(r, "T2CC1_0(Input)")
             r.v("MODPISEL4_EXINT4IS") = 1
           Case "P3.2"
             Call RequestAlternateInputPin(r, "T2CC1_1(Input)")
             r.v("MODPISEL4_EXINT4IS") = 2
           Case "P5.6"
             Call RequestAlternateInputPin(r, "T2CC1_2(Input)")
             r.v("MODPISEL4_EXINT4IS") = 3
           Case Else
         End Select

       Case (2)
         Select Case (sPinNo)
           Case "P4.4"
             Call RequestAlternateInputPin(r, "T2CC2_0(Input)")
             r.v("MODPISEL4_EXINT5IS") = 1
           Case "P3.3"
             Call RequestAlternateInputPin(r, "T2CC2_1(Input)")
             r.v("MODPISEL4_EXINT5IS") = 2
           Case "P5.2"
             Call RequestAlternateInputPin(r, "T2CC2_2(Input)")
             r.v("MODPISEL4_EXINT5IS") = 3
           Case Else
         End Select

       Case (3)
         Select Case (sPinNo)
           Case "P4.5"
             Call RequestAlternateInputPin(r, "T2CC3_0(Input)")
             r.v("MODPISEL1_EXINT6IS") = 2
           Case "P3.4"
             Call RequestAlternateInputPin(r, "T2CC3_1(Input)")
             r.v("MODPISEL1_EXINT6IS") = 3
           Case "P5.7"
             Call RequestAlternateInputPin(r, "T2CC3_2(Input)")
             r.v("MODPISEL1_EXINT6IS") = 4
           Case Else
         End Select

       Case Else
     End Select
    End If

  Else  'comapare mode only
   Select Case (iNr)
     Case (4)
       Select Case (sPinNo)
         Case "P4.6"
           Call RequestAlternateOutputPin(r, "T2CC4_0(Output)", ALT_FUN2)
         Case "P0.6"
           Call RequestAlternateOutputPin(r, "T2CC4_1(Output)", ALT_FUN1)
         Case "P5.4"
           Call RequestAlternateOutputPin(r, "T2CC4_2(Output)", ALT_FUN1)
         Case Else
       End Select
       r.v(sRegPre & "COCON_CCM4") = 1  'Enable Compare Function for channel4

     Case (5)
       Select Case (sPinNo)
         Case "P4.7"
           Call RequestAlternateOutputPin(r, "T2CC5_0(Output)", ALT_FUN2)
         Case "P0.7"
           Call RequestAlternateOutputPin(r, "T2CC5_1(Output)", ALT_FUN2)
         Case "P5.3"
           Call RequestAlternateOutputPin(r, "T2CC5_2(Output)", ALT_FUN1)
         Case Else
       End Select
       r.v(sRegPre & "COCON_CCM5") = 1  'Enable Compare Function for channel5

     Case Else
   End Select
  End If


End Sub

'*****************************************************************************
'Description:
'        This function sets all necessary registers to request all T2CCUPins from
'        an alternate function which was requested with the function
'        'RequestAlternatePin'.
'-----------------------------------------------------------------------------
'Parameters:
'        r           reference to this object
'        iNr         number of the channel
'        iStatus     status of the interrupt (1-Enable or 0-Disable)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub T2CCUIntrEn(r As ProjectData, vIndex As Variant, iNr As Integer, iStatus)
  On Error Resume Next
  Dim sSgnlDir As String
  Dim sVarPre As String
  Dim sRegPre As String
  Dim sPinNo As String

  sVarPre = "T2CCU" & vIndex
  sRegPre = sVarPre & "_"

  sPinNo = r.v(sRegPre & "USE_CH" & iNr & "_PIN")

     Select Case (iNr)
       Case (0)
         If(iStatus = 0) Then
'           r.v("MODPISEL4_EXINT3IS") = 0
           r.v(T2CCU_PREFIX & "USE_CH" & iNr & "Isr") = 0
         Else         
           r.v(T2CCU_PREFIX & "USE_CH" & iNr & "Isr") = 1
'           Select Case (sPinNo)
'             Case "P4.0"
'                 r.v("MODPISEL4_EXINT3IS") = 1
'             Case "P3.0"
'                 r.v("MODPISEL4_EXINT3IS") = 2
'             Case "P5.5"
'                 r.v("MODPISEL4_EXINT3IS") = 3
'             Case Else
'           End Select
         End If

       Case (1)
         If(iStatus = 0) Then
'           r.v("MODPISEL4_EXINT4IS") = 0
           r.v(T2CCU_PREFIX & "USE_CH" & iNr & "Isr") = 0
         Else         
           r.v(T2CCU_PREFIX & "USE_CH" & iNr & "Isr") = 1
'           Select Case (sPinNo)
'             Case "P4.1"
'                 r.v("MODPISEL4_EXINT4IS") = 1
'             Case "P3.2"
'                 r.v("MODPISEL4_EXINT4IS") = 2
'             Case "P5.6"
'                 r.v("MODPISEL4_EXINT4IS") = 3
'             Case Else
'           End Select
         End If

       Case (2)
         If(iStatus = 0) Then
'           r.v("MODPISEL4_EXINT5IS") = 0
           r.v(T2CCU_PREFIX & "USE_CH" & iNr & "Isr") = 0
         Else         
           r.v(T2CCU_PREFIX & "USE_CH" & iNr & "Isr") = 1
'           Select Case (sPinNo)
'             Case "P4.4"
'                 r.v("MODPISEL4_EXINT5IS") = 1
'             Case "P3.3"
'                 r.v("MODPISEL4_EXINT5IS") = 2
'             Case "P5.2"
'                 r.v("MODPISEL4_EXINT5IS") = 3
'             Case Else
'           End Select
         End If

       Case (3)
         If(iStatus = 0) Then
'           r.v("MODPISEL1_EXINT6IS") = 0
           r.v(T2CCU_PREFIX & "USE_CH" & iNr & "Isr") = 0
         Else         
           r.v(T2CCU_PREFIX & "USE_CH" & iNr & "Isr") = 1
'           Select Case (sPinNo)
'             Case "P4.5"
'           r.v("MODPISEL1_EXINT6IS") = 2
'             Case "P3.4"
'           r.v("MODPISEL1_EXINT6IS") = 3
'             Case "P5.7"
'           r.v("MODPISEL1_EXINT6IS") = 4
'             Case Else
'           End Select
         End If

       Case (4)
         If(iStatus = 0) Then
'           r.v("MODIEN_CM4EN") = 0
           r.v(T2CCU_PREFIX & "USE_CH" & iNr & "Isr") = 0
         Else         
           r.v(T2CCU_PREFIX & "USE_CH" & iNr & "Isr") = 1
'           r.v("MODIEN_CM4EN") = 1
'           Select Case (sPinNo)
'             Case "P4.6"
'             Case "P0.6"
'             Case "P5.4"
'             Case Else
'           End Select
         End If

       Case (5)
         If(iStatus = 0) Then
'           r.v("MODIEN_CM5EN") = 0
           r.v(T2CCU_PREFIX & "USE_CH" & iNr & "Isr") = 0
         Else         
           r.v(T2CCU_PREFIX & "USE_CH" & iNr & "Isr") = 1
'           r.v("MODIEN_CM5EN") = 1
'           Select Case (sPinNo)
'             Case "P4.7"
'             Case "P0.7"
'             Case "P5.3"
'             Case Else
'           End Select
         End If

       Case Else
     End Select

     If(r.v(T2CCU_PREFIX & "USE_CH0Isr")=1) Or (r.v(T2CCU_PREFIX & "USE_CH1Isr")=1) Or _
       (r.v(T2CCU_PREFIX & "USE_CH2Isr")=1) Or (r.v(T2CCU_PREFIX & "USE_CH3Isr")=1) Or _
       (r.v(T2CCU_PREFIX & "USE_CH4Isr")=1) Or (r.v(T2CCU_PREFIX & "USE_CH5Isr")=1) Then
       r.v(T2CCU_PREFIX & "USE_NodeIsr1") = 1
'       r.v("IR_USE_IsrExt3") = 1
     Else
        r.v(T2CCU_PREFIX & "USE_NodeIsr1") = 0
     End If


End Sub


'*****************************************************************************
'Description:
'        This function sets all necessary registers to request all T2CCUPins from
'        an alternate function which was requested with the function
'        'RequestAlternatePin'.
'-----------------------------------------------------------------------------
'Parameters:
'        r           reference to this object
'        iNr         number of the channel
'        iStatus     status of the interrupt (1-Enable or 0-Disable)
'-----------------------------------------------------------------------------
'Returns:
'*****************************************************************************
Sub ResetT2CCUChannel(r As ProjectData, vIndex As Variant,iNr As Integer)
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  sVarPre = "T2CCU" & vIndex
  sRegPre = sVarPre & "_"


  Call ReleaseT2CCUPin (r,vIndex,iNr,r.v("T2CCU_USE_CH" & iNr & "_PIN"))

  If (iNr < 4) Then

    If ((r.v(sRegPre & "CCEN_CCM" & iNr) <> 2) Or (r.v(SCU_PREFIX & "PMCON1_T2CCU_DIS") = 1))Then 'Capture mode (or) module clock is disabled

      If (r.v(sRegPre & "CCEN_CCM" & iNr) = 2) Or (r.v(sRegPre & "CCEN_CCM" & iNr) = 1) Then 'comp or capt on active edge
        'reset EXICON register for edge selection
	    If(iNr = 0) Then
            r.v("EXICON0_EXINT3") = 3 'reset value for ch0 edge
        Else
            r.v("EXICON1_EXINT" & (iNr + 3)) = 3 'reset value for ch1 to ch3 edge
        End If
      End If

      r.v(sRegPre & "CCEN_CCM" & iNr) = 0 'default value for ch(iNr) cap/com node
      r.v("T2CCU_REQ_DUTY_CYCLE_" & iNr) = 100
      r.v(sRegPre & "CC" & iNr & "L_CCVALL") = 0 'reset value for compare regL
      r.v(sRegPre & "CC" & iNr & "H_CCVALH") = 0 'reset value for compare regH
      r.v(sRegPre & "COSHDW_COOUT" & iNr) = 0    'reset value for compare ch(iNr) output
      Call T2CCUIntrEn(r, vIndex, iNr, 0)        'reset value for ch(iNr) interrupt enable
      Call ResetCCT_DTMChannel(r, vIndex, iNr)   'reset value for channel pairx dead time

    End If

  End If

  r.v("T2CCU_USE_CH" & iNr & "_PIN") = "None"

End Sub

'*****************************************************************************
'Description:
'        This function retrund the value of the regiser H+L.
'-----------------------------------------------------------------------------
'Parameters:
'        r
'        sRegName           name of the register (e.g. T12PR)
'-----------------------------------------------------------------------------
'Returns:
'       value of register
'*****************************************************************************
Function GetRegisterValue_L_and_H(r As ProjectData, sRegName As String) As Variant
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String

  GetRegisterValue_L_and_H = r.sfr(sRegName & "H") * 2 ^ 8 + r.sfr(sRegName & "L")

End Function

'*****************************************************************************
'Description:
'        This function sets the low and high register of the CC6 module.
'-----------------------------------------------------------------------------
'Parameters:
'        r
'        sRegName           name of the register (e.g. T2CCU_CC0)
'        dValue             value to be set (0 .. 65535)
'-----------------------------------------------------------------------------
'Returns:
'       none
'*****************************************************************************
Sub SetRegister_L_and_H(r As ProjectData, sRegName As String, dValue As Variant)

    Dim dHigh As Double
    Dim dLow As Double

    dHigh = (dValue \ 2 ^ 8)
    dLow = dValue - dHigh * 2 ^ 8

    r.sfr(sRegName & "H") = dHigh
    r.sfr(sRegName & "L") = dLow

End Sub

'*****************************************************************************
'Description:
'        This function returns a real duty cycle for selected channel.
'-----------------------------------------------------------------------------
'Parameters:
'        r              reference to the ProjectData object
'        vIndex         parameter
'        iNr            channel nuber (0 to 5)
'-----------------------------------------------------------------------------
'Returns:
'       real duty cycle
'*****************************************************************************
Function GetT2CCURealDutyCycle(r As ProjectData, vIndex As Variant, iNr As Integer) As Double
    Dim sVarPre As String
    Dim sRegPre As String
    Dim d As Double
    Dim iONTime As long
    Dim iOFFTime As long

    sVarPre = "T2CCU" & vIndex
    sRegPre = sVarPre & "_"

    If (r.v(T2CCU_PREFIX & "CCTBSEL_CCTB" & iNr) = 0) Then 'Timer 2
      If (GetRegisterValue_L_and_H(r, "T2_RC2") > GetRegisterValue_L_and_H(r, "T2CCU_CC" & iNr)) Then
        d = 0
      Else
        iONTime = 65536 - GetRegisterValue_L_and_H(r, "T2CCU_CC" & iNr)
        iOFFTime = GetRegisterValue_L_and_H(r, "T2CCU_CC" & iNr) - GetRegisterValue_L_and_H(r, "T2_RC2")
        d = (iOnTime * 100) / (iONTime + iOFFTime)
      End If

'        d = (GetRegisterValue_L_and_H(r, "T2_RC2") + 1 - _
'          GetRegisterValue_L_and_H(r, "T2CCU_CC" & iNr)) / _
'          (GetRegisterValue_L_and_H(r, "T2_RC2") + 1) * 100
    Else                                                   'Capture/Compare Timer
      If (GetRegisterValue_L_and_H(r, "T2CCU_CCTREL") > GetRegisterValue_L_and_H(r, "T2CCU_CC" & iNr)) Then
        d = 0
      Else
        iONTime = 65536 - GetRegisterValue_L_and_H(r, "T2CCU_CC" & iNr)
        iOFFTime = GetRegisterValue_L_and_H(r, "T2CCU_CC" & iNr) - GetRegisterValue_L_and_H(r, "T2CCU_CCTREL")
        d = (iOnTime * 100) / (iONTime + iOFFTime)
      End If

'        d = (GetRegisterValue_L_and_H(r, "T2CCU_CCTREL") + 1 - _
'          GetRegisterValue_L_and_H(r, "T2CCU_CC" & iNr)) / _
'          (GetRegisterValue_L_and_H(r, "T2CCU_CCTREL") + 1) * 100
    End If

    If (d < 0) Then
      d = 0
    End If

   GetT2CCURealDutyCycle = d

'   GetRealDutyCecle = d


'    If (iChannel < 3) Then
'        d = (GetT2CCURegisterValue_L_and_H(r, "T12PR") + 1 - _
'          GetRegisterValue_L_and_H(r, "CC6" & iChannel & "SR")) / _
'          (GetRegisterValue_L_and_H(r, "T12PR") + 1) * 100
'    Else
'        d = (GetRegisterValue_L_and_H(r, "T13PR") + 1 - _
'          GetRegisterValue_L_and_H(r, "CC6" & iChannel & "SR")) / _
'          (GetRegisterValue_L_and_H(r, "T13PR") + 1) * 100
'    End If
'    
'    If (d < 0) Then
'      d = 0
'    End If

'   GetRealDutyCecle = d
    

End Function

'*****************************************************************************
'Description:
'        This function updates the register T2CCU_CCx.
'-----------------------------------------------------------------------------
'Parameters:
'        r              reference to the ProjectData object
'        vIndex         parameter
'        iNr            channel nuber (0 to 5)
'-----------------------------------------------------------------------------
'Returns:
'       none
'*****************************************************************************
Sub UpdateT2CCUDutyCycle(r As ProjectData, iNr As Integer)
    Dim sVarPre As String
    Dim sRegPre As String
    Dim l As Long

    If (r.v(T2CCU_PREFIX & "CCTBSEL_CCTB" & iNr) = 0) Then 'Timer 2
      If(r.v(T2CCU_PREFIX & "REQ_DUTY_CYCLE_" & iNr) = 0) And (GetRegisterValue_L_and_H(r, "T2_RC2") <> 0) Then
        l = GetRegisterValue_L_and_H(r, "T2_RC2") - 1
        Call SetRegister_L_and_H(r, "T2CCU_CC" & iNr, l)
        Exit Sub
      ElseIf(r.v(T2CCU_PREFIX & "REQ_DUTY_CYCLE_" & iNr) = 0) And (GetRegisterValue_L_and_H(r, "T2_RC2") = 0) Then
        r.v(T2CCU_PREFIX & "REQ_DUTY_CYCLE_" & iNr) = 100
      End If

      l = 65536 - ((r.v(T2CCU_PREFIX & "REQ_DUTY_CYCLE_" & iNr) * (65536-GetRegisterValue_L_and_H(r, "T2_RC2"))) / 100)

    Else                                                   'Capture/Compare Timer
      If(r.v(T2CCU_PREFIX & "REQ_DUTY_CYCLE_" & iNr) = 0) And (GetRegisterValue_L_and_H(r, "T2CCU_CCTREL") <> 0) Then
        l = GetRegisterValue_L_and_H(r, "T2CCU_CCTREL") - 1
        Call SetRegister_L_and_H(r, "T2CCU_CC" & iNr, l)
        Exit Sub
      ElseIf(r.v(T2CCU_PREFIX & "REQ_DUTY_CYCLE_" & iNr) = 0) And (GetRegisterValue_L_and_H(r, "T2CCU_CCTREL") = 0) Then
        r.v(T2CCU_PREFIX & "REQ_DUTY_CYCLE_" & iNr) = 100
      End If

      l = 65536 - ((r.v(T2CCU_PREFIX & "REQ_DUTY_CYCLE_" & iNr) * (65536-GetRegisterValue_L_and_H(r, "T2CCU_CCTREL"))) / 100)

    End If

    If (l > 65535) Then
        l = 65535
    End If

    Call SetRegister_L_and_H(r, "T2CCU_CC" & iNr, l)

'    If (iChannel < 3) Then
'        l = (GetRegisterValue_L_and_H(r, "T12PR") + 1) * _
'          (1 - r.v(sVarPre & "_REQ_DUTY_CYCLE_" & iChannel) / 100)
'    Else
'        l = (GetRegisterValue_L_and_H(r, "T13PR") + 1) * _
'          (1 - r.v(sVarPre & "_REQ_DUTY_CYCLE_" & iChannel) / 100)
'    End If
'
'    If (l > 65535) Then
'        l = 65535
'    End If
'
'    Call SetCC6Register_L_and_H(r, "CC6" & iChannel & "SR", l)

End Sub

'*****************************************************************************
'Description:
'          This function checks if the pin of the passed alternate function
'          is already in use as general purpose IO or a different alternate function
'          The result depends on the "exclusive" properties of both the passed pin
'          and the pins which are already in use.
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sAlternatePinName     the name of the alternate pin (specified
'                              with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        True                  the alternate pin can be used
'        False                 else
'*****************************************************************************
Function IsEXINT_T2CCU_ModuleFree(r As ProjectData, sModuleName As String, iNr As Integer) As Boolean
  On Error Resume Next
  Dim iChannelNo As Integer

  
  IsEXINT_T2CCU_ModuleFree = False

  If(sModuleName = "T2CCU") Then
    Select Case (iNr)
      Case (0)
        If((r.v("MuxFor_EXINT3_0") = 0) And (r.v("MuxFor_EXINT3_1") = 0) And (r.v("MuxFor_EXINT3_2") = 0) And (r.v("MuxFor_EXINT3_3") = 0)) Then
          IsEXINT_T2CCU_ModuleFree = True
        Else
          IsEXINT_T2CCU_ModuleFree = False
        End If
      Case (1)
        If((r.v("MuxFor_EXINT4_0") = 0) And (r.v("MuxFor_EXINT4_1") = 0) And (r.v("MuxFor_EXINT4_2") = 0) And (r.v("MuxFor_EXINT4_3") = 0)) Then
          IsEXINT_T2CCU_ModuleFree = True
        Else
          IsEXINT_T2CCU_ModuleFree = False
        End If
      Case (2)
        If((r.v("MuxFor_EXINT5_0") = 0) And (r.v("MuxFor_EXINT5_1") = 0) And (r.v("MuxFor_EXINT5_2") = 0) And (r.v("MuxFor_EXINT5_3") = 0)) Then
          IsEXINT_T2CCU_ModuleFree = True
        Else
          IsEXINT_T2CCU_ModuleFree = False
        End If
      Case (3)
        If((r.v("MuxFor_EXINT6_0") = 0) And (r.v("MuxFor_EXINT6_1") = 0) And (r.v("MuxFor_EXINT6_2") = 0) And (r.v("MuxFor_EXINT6_3") = 0) And (r.v("MuxFor_EXINT6_4") = 0)) Then
          IsEXINT_T2CCU_ModuleFree = True
        Else
          IsEXINT_T2CCU_ModuleFree = False
        End If

      Case Else
    End Select

  ElseIf(sModuleName = "EXINT") Then
    Select Case (iNr)
      Case (3)
        If((r.v("MuxFor_T2CC0_0(Input)") = 0) And (r.v("MuxFor_T2CC0_1(Input)") = 0) And (r.v("MuxFor_T2CC0_2(Input)") = 0) And _
           (r.v("MuxFor_T2CC0_0(Output)") = 0) And (r.v("MuxFor_T2CC0_1(Output)") = 0) And (r.v("MuxFor_T2CC0_2(Output)") = 0)) Then
          IsEXINT_T2CCU_ModuleFree = True
        Else
          IsEXINT_T2CCU_ModuleFree = False
        End If
      Case (4)
        If((r.v("MuxFor_T2CC1_0(Input)") = 0) And (r.v("MuxFor_T2CC1_1(Input)") = 0) And (r.v("MuxFor_T2CC1_2(Input)") = 0) And _
           (r.v("MuxFor_T2CC1_0(Output)") = 0) And (r.v("MuxFor_T2CC1_1(Output)") = 0) And (r.v("MuxFor_T2CC1_2(Output)") = 0)) Then
          IsEXINT_T2CCU_ModuleFree = True
        Else
          IsEXINT_T2CCU_ModuleFree = False
        End If
      Case (5)
        If((r.v("MuxFor_T2CC2_0(Input)") = 0) And (r.v("MuxFor_T2CC2_1(Input)") = 0) And (r.v("MuxFor_T2CC2_2(Input)") = 0) And _
           (r.v("MuxFor_T2CC2_0(Output)") = 0) And (r.v("MuxFor_T2CC2_1(Output)") = 0) And (r.v("MuxFor_T2CC2_2(Output)") = 0)) Then
          IsEXINT_T2CCU_ModuleFree = True
        Else
          IsEXINT_T2CCU_ModuleFree = False
        End If
      Case (6)
        If((r.v("MuxFor_T2CC3_0(Input)") = 0) And (r.v("MuxFor_T2CC3_1(Input)") = 0) And (r.v("MuxFor_T2CC3_2(Input)") = 0) And _
           (r.v("MuxFor_T2CC3_0(Output)") = 0) And (r.v("MuxFor_T2CC3_1(Output)") = 0) And (r.v("MuxFor_T2CC3_2(Output)") = 0)) Then
          IsEXINT_T2CCU_ModuleFree = True
        Else
          IsEXINT_T2CCU_ModuleFree = False
        End If

      Case Else
    End Select
  End If

End Function

'*****************************************************************************
'Description:
'          This function set the maximum no. of channels avialable for respective derivatives.
'-----------------------------------------------------------------------------
'Parameters:
'        r                     reference to this object
'        sAlternatePinName     the name of the alternate pin (specified
'                              with function 'AddAlternateName' in install.inc)
'-----------------------------------------------------------------------------
'Returns:
'        True                  the alternate pin can be used
'        False                 else
'*****************************************************************************
Sub SetT2CCU_MAX_Channels(r As ProjectData, vIndex As Variant)
  On Error Resume Next

  r.v("T2CCU_MAX_CHANNELS") = 6

End Sub

'*****************************************************************************
'Description:
'        For each function which should be used for this module a line
'        "If (Not func.AddFunction .." must be insert in this function.
'        For each parameter that should be used for this function a
'        line "If (Not func.AddParameter .." must be insert in this function.
'
'        Note:
'        For each added function a project variable with the name
'        "T2_NAME_Functionname" must be exist in the *.vars file.
'-----------------------------------------------------------------------------
'Parameters:
'        func             reference to the FunctionClass object
'        r                reference to the Registers object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Public Function RefreshFunctionDescriptions(func As FunctionClass, r As ProjectData) As Boolean
  On Error Resume Next

  Dim sDescription As String
  Dim sReturnValue As String
  Dim i As Integer

  RefreshFunctionDescriptions = False

  ' for each function of this module the property AddFunction must be called
  '
  ' AddFunction(sFuncName As String, sFuncType As String, sFuncDescription As String,
  '             sReturnvalue As String, lSort As Long) As Boolean
  '
  ' sFuncName          | String | function name as key (use the name of the variable
  '                               for the function name e.g. SSC_NAME_Init)
  ' sFuncType          | String | type of the function
  ' sFuncDescription   | String | description of the function
  ' sReturnvalue       | String | description of the return value
  ' lSort              | Long   | ' sort of the function (0 => macro, 1 => function, 2 => ISR)

    ' for each parameter of a function the property AddParameter must be called
    '
    'AddParameter(sParName As String, sPar As String, sParType As String, sParDescription As String) As Boolean
    '
    ' sParName         | String | parameter name (key)
    ' sPar             | String | parameter name
    ' sParType         | String | type of the parameter
    ' sParDescription  | String | description of the parameter

  'if variable = (alpha release) or (beta release) Functiona Name
  If ((r.v("T2CCU_NAME_EnableShadowTransfer") = "CCU_vEnableShadowTransfer") Or (r.v("T2CCU_NAME_EnableShadowTransfer") = "CCU_vEnShadTransTimeroverflow")) Then
'    r.v("T2CCU_NAME_EnableShadowTransfer") = "CCU_vEnShadTransTimeroverflow"
    r.v("T2CCU_NAME_EnableShadowTransfer") = "CCU_vWriteTXOV"
  End If

  'New Function for bete increment release(IAR)
  If(r.v("T2CCU_NAME_UserDef0") = "") Then
    r.v("T2CCU_NAME_UserDef0") = "CCU_vShadTransCompareChannels"
  End If

  '------------------------------------------------------------------------------------------------
  ' Description for T2CCU_NAME_Init
  '------------------------------------------------------------------------------------------------
  sDescription = GetGeneralInitDescription("T2CCU")
  sReturnValue = "None"
  If (Not func.AddFunction("T2CCU_NAME_Init", "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T2CCU_NAME_StartTmr
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro starts the Capture/Compare Timer (CCT)." & vbCrLf & _
                 "If bit TIMSYN = 1 and CCTST was 0, then it will also starts " & vbCrLf & _
                 "Timer 2 (if it was not already running)." & vbCrLf & _
                 "The timer continues counting where it stoped."
  sReturnValue = "None"
  If (Not func.AddFunction("T2CCU_NAME_StartTmr", "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T2CCU_NAME_StopTmr
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro stops the Capture/Compare Timer (CCT)." & vbCrLf & _
                 "The content of the timer register remains unchanged."
  sReturnValue = "None"
  If (Not func.AddFunction("T2CCU_NAME_StopTmr", "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T2CCU_NAME_GetTmrPeriod
  '------------------------------------------------------------------------------------------------
  sDescription = "This function returns the content of the Capture/Compare Timer (CCT) register"
  sReturnValue = "16-bit CCT timer register contents"
  If (Not func.AddFunction("T2CCU_NAME_GetTmrPeriod", "uword", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T2CCU_NAME_LoadTmrPeriod
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro loads the CCT Timer register with parameter value"
  sReturnValue = "None"
  If (Not func.AddFunction("T2CCU_NAME_LoadTmrPeriod", "", sDescription, sReturnValue, 0)) Then Exit Function

  sDescription = "16-bit CCT timer value"
  If (Not func.AddParameter("T2CCU_NAME_LoadTmrPeriod_Par1", "Value", "", sDescription)) Then Exit Function

  For i = 0 To 3
    '------------------------------------------------------------------------------------------------
    ' Description for T2CCU_NAME_GetCH(0to5)CaptVal
    '------------------------------------------------------------------------------------------------
    sDescription = "This function returns the contents of the capture register " & i
    sReturnValue = "16-bit channel " & i & " capture register contents"
    If (Not func.AddFunction("T2CCU_NAME_GetCH" & i & "CaptVal", "uword", sDescription, sReturnValue, 1)) Then Exit Function
  Next i

  '------------------------------------------------------------------------------------------------
  ' Description for T2CCU_NAME_LoadChCompReg
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro loads the selected compare register with parameter " & _
                 "value. This Compare register acts as a shadow register to the " & _
                 "channel x internal compare register. The written value is " & _
                 "transferred into the internal compare register when " & _
                 "corresponding bit T2CCU_COSHDW.COOUTx is being set " & vbCrLf & _
                 "(with ENSHDW = 1) or by calling the function CCU_vShadTransCompareChannels."
  sReturnValue = "None"
  If (Not func.AddFunction("T2CCU_NAME_LoadChCompReg", "", sDescription, sReturnValue, 0)) Then Exit Function

  sDescription = "No of the Channel to be load"
  If (Not func.AddParameter("T2CCU_NAME_LoadChCompReg_Par1", "ChNr", "", sDescription)) Then Exit Function
  sDescription = "16-bit compare register value"
  If (Not func.AddParameter("T2CCU_NAME_LoadChCompReg_Par2", "Value", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T2CCU_NAME_EnableShadowTransfer
  '------------------------------------------------------------------------------------------------
'  sDescription = "This macro enables the shadow transfer for all compare channels with COOUTx."
  sDescription = "This macro enables/disables the shadow transfer for all compare channels on overflow of their " & _
                 "respective timer base."
  sReturnValue = "None"
  If (Not func.AddFunction("T2CCU_NAME_EnableShadowTransfer", "", sDescription, sReturnValue, 0)) Then Exit Function

  sDescription = "Level of the shadow transfer. For example:                 " & _
                 "Level = 1 -> enables shadow transfer.                      " & _
                 "Level = 0 -> disables shadow transfer.                     "
  If (Not func.AddParameter("T2CCU_NAME_EnableShadowTransfer_Par1", "Level", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T2CCU_NAME_UserDef0
  '------------------------------------------------------------------------------------------------
  sDescription = "This macro enables the software shadow transfer via ENSHDW and COOUTx bits."
  sReturnValue = "None"
  If (Not func.AddFunction("T2CCU_NAME_UserDef0", "", sDescription, sReturnValue, 0)) Then Exit Function

  sDescription = "Channel number to be enabled for shadow transer. The channel number must be a hexa-decimal value. For Example:   " & _
                 "Bit 0 = 1   ->  enables the channel 0 shadow transfer      " & _
                 "Bit 2 = 1   ->  enables the channel 2 shadow transfer      " & _
                 "Bit 3 = 1   ->  enables the channel 3 shadow transfer      " & _
                 "Bit 6 = 1   ->  enables the channel 6 shadow transfer      "
  If (Not func.AddParameter("T2CCU_NAME_UserDef0_Par1", "ChNr", "", sDescription)) Then Exit Function



  '------------------------------------------------------------------------------------------------
  ' Description for T2CCU_NAME_EnableCompChOutput
  '------------------------------------------------------------------------------------------------
'  sDescription = "This macro transfers the content of the selected compare register into " & _
'                 "corresponding internal compare register when ENSHDW = 1." & vbCrLf & _
'                 "This macro also set the corresponding COOUTx bit of the internal compare" & _
'                 " output register when ENSHDW = 0."
'  sDescription = "This macro changes the COOUTx of the internal compare output register if " & _
'                 "the compare channels are in compare mode 1 or con-current compare mode. Otherwise " & _
'                 "this macro enables the shadow transfer for all compare channels with COOUTx. " & vbCrLf & vbCrLf & _
'                 "Note: The content of the internal compare output register is output on the corresponding " & _
'                 "T2CCx pins when a valid compare match occurs for a channel in compare mode 1 or concurrent compare mode."
'  sDescription = "This macro loads the compare shadow register (SFR:T2CCU_COSHDW), see also below:" & vbCrLf & _
'                 "For Compare Mode 0 : This macro enables the shadow transfer ENSHDW = 1 with COOUTx = 1." & vbCrLf & _
'                 "For Compare Mode 1 and Con-current compare mode: This macro enables the internal compare output register bit COOUTx =1."
  sDescription = "This macro intializes the COOUTx of the internal compare output register (6bit)." & vbCrLf & vbCrLf & _
                 "Note: It is useful for compare mode 1 and con-currect compare mode only."
  sReturnValue = "None"
  If (Not func.AddFunction("T2CCU_NAME_EnableCompChOutput", "", sDescription, sReturnValue, 0)) Then Exit Function

'  sDescription = "Channel number to be enable for compare output. The channel number must be a hexa-decimal value. " & _
'                 "Example: For channel 0 user should pass 0x01, For channels 0 and 2 user should pass 0x05... etc.,"
  sDescription = "Channel number to be enabled for compare output. The channel number must be a hexa-decimal value. For Example:   " & _
                 "Bit 0 = 1   ->  enables the channel 0 for compare output   " & _
                 "Bit 2 = 1   ->  enables the channel 2 for compare output   " & _
                 "Bit 3 = 1   ->  enables the channel 3 for compare output   " & _
                 "Bit 6 = 1   ->  enables the channel 6 for compare output   "
  If (Not func.AddParameter("T2CCU_NAME_EnableCompChOutput_Par1", "ChNr", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T2CCU_NAME_NodeIsr0
  '------------------------------------------------------------------------------------------------
  sDescription = "This is the service routine for the Capture/ Compare Timer (CCT) overflow interrupt. " & _
                TOOLTIP_ISR_USERCODE
  sReturnValue = "none"
  If (Not func.AddFunction("T2CCU_NAME_NodeIsr0", "void", sDescription, sReturnValue, 2)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for T2CCU_NAME_NodeIsr1
  '------------------------------------------------------------------------------------------------
  sDescription = "This is the service routine for all T2CCU Channel's Compare match or " & _
                 "Capture event interrupt. " & _
                TOOLTIP_ISR_USERCODE
  sReturnValue = "none"
  If (Not func.AddFunction("T2CCU_NAME_NodeIsr1", "void", sDescription, sReturnValue, 2)) Then Exit Function

  '------------------------------------------------------------------------------------------------

  If (Not func.Sort) Then Exit Function

  RefreshFunctionDescriptions = True
End Function
