'#Uses "..\classes\FunctionClass.cls"
'#Uses "..\classes\ParameterClass.cls"
'#Uses "..\include\Const.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Pages.inc"
'#Uses "..\include\Port.inc"
'#Uses "Include.inc"

Option Explicit

'*****************************************************************************
' Module:
'        CAPCOMx
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions which are create controls for the
'        module 'CAPCOMx'.
'        Furthermore it contains functions for the event handling of
'        these controls.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        1.0   ahe         05.01.01     final version for S-Gold
'
'        ---------------------------------------------------------------------
'        3.0   ahe         02.05.01     final version for Pegasus
'
'        3.1   ahe         02.07.02     - double register mode for 8 pairs (not only for 4)
'                                       - neu interrupt pages
'        3.2   ste         18.12.02     no changes
'
'*****************************************************************************


'*****************************************************************************
'Description:
'        This function is called from DAvE just before the Peripherals
'        window is shown.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnLoad(ps As Peripherals) As Boolean
    On Error GoTo OnLoadError

    OnLoad = False

    '-------------------------------------------------------------------------
    ' page: Mode
    '-------------------------------------------------------------------------
    If (Not OnLoadPageMode(ps)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: Control
    '-------------------------------------------------------------------------
    If (Not OnLoadPageControl(ps)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: Interrupts
    '-------------------------------------------------------------------------
    If (Not OnLoadPageInterrupts_32(ps, 0)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: Interrupts
    '-------------------------------------------------------------------------
    If (Not OnLoadPageInterrupts_32(ps, 1)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: Functions
    '-------------------------------------------------------------------------
    If (Not OnLoadPageFunctions(ps)) Then Exit Function

    '-------------------------------------------------------------------------
    ' end of pages
    '-------------------------------------------------------------------------
    
    '-------------------------------------------------------------------------
    ' call function to initialize the menu
    '-------------------------------------------------------------------------
    If (Not InitMenu(ps, "CCx\Channel_x")) Then Exit Function

    OnLoad = True
    Exit Function

OnLoadError:
    Exit Function
End Function


'*****************************************************************************
'Description:
'        This function is called from DAvE just before a page
'        is shown.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Page            no. of the page (starts with 1)
'        Pagename        name of the page
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnChangingPage(ps As Peripherals, Page As Integer, Pagename As String) As Boolean
    On Error Goto OnChangingPageError

    OnChangingPage = False
    Dim sVarPre As String
    Dim vIndex  As Variant

    vIndex = ps.Parameters.v("x")
    sVarPre = "CC" & vIndex

    
    '-------------------------------------------------------------------------
    'page: Mode
    '-------------------------------------------------------------------------
    If (Not OnChangingPageMode (ps, Pagename)) Then Exit Function

    '-------------------------------------------------------------------------
    'page: Control
    '-------------------------------------------------------------------------
    If (Not OnChangingPageControl (ps, Pagename)) Then Exit Function

    '-------------------------------------------------------------------------
    'page: Interrupts
    '-------------------------------------------------------------------------
    If (Not OnChangingPageInterrupts_32 (ps, Pagename, 0)) Then Exit Function

    '-------------------------------------------------------------------------
    'page: Interrupts
    '-------------------------------------------------------------------------
    If (Not OnChangingPageInterrupts_32 (ps, Pagename, 1)) Then Exit Function

    '-------------------------------------------------------------------------
    'page: Functions
    '-------------------------------------------------------------------------
    If (Not OnChangingPageFunctions (ps, Pagename)) Then Exit Function


    OnChangingPage = True
    Exit Function

OnChangingPageError:
    Exit Function
End Function


'*****************************************************************************
'Description:
'        This function is called from DAvE in two cases:
'
'        1.
'            The contents of the peripherals window must be refreshed.
'            In this case the 'Controlname' parameter is a empty string
'            and the values array 'v()' contains nothing.
'
'        2.
'            A control on the peripherals has been changed.
'            In this case the 'Controlname' parameter contains the name
'            of the changed control and the values array 'v()' contains the
'            changed value(s).
'
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     name of the changed control (for case 2)
'        v()             array with the changed values (for case 2)
'        Page            no. of the page (starts with 1)
'        Pagename        name of the page
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnShow(ps As Peripherals, Controlname As String, v() As Variant, Page As Integer, Pagename As String) As Boolean
    On Error GoTo OnShowError

    OnShow = False
    
    Dim sVarPre As String
    Dim vIndex  As Variant

    vIndex = ps.Parameters.v("x")
    sVarPre = "CC" & vIndex


    '-------------------------------------------------------------------------
    ' page: Mode
    '-------------------------------------------------------------------------
    If (Not OnShowPageMode(ps, Controlname, Pagename, v)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: Control
    '-------------------------------------------------------------------------
    If (Not OnShowPageControl(ps, Controlname, Pagename, v)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: Interrupts
    '-------------------------------------------------------------------------
    If (Not OnShowPageInterrupts_32(ps, Controlname, v, sVarPre, Pagename, 0)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: Interrupts
    '-------------------------------------------------------------------------
    If (Not OnShowPageInterrupts_32(ps, Controlname, v, sVarPre, Pagename, 1)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: Functions
    '-------------------------------------------------------------------------
    If (Not OnShowPageFunctions(ps, Controlname, Pagename, v)) Then Exit Function


    OnShow = True
    Exit Function

OnShowError:
    Errorbox "Can not show page.", Err.Description
    Exit Function
End Function


'*****************************************************************************
'Description:
'        This function is called from DAvE if the user clicks on a button.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     name of the button
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnClick(ps As Peripherals, Controlname As String) As Boolean
    On Error GoTo OnClickError

    OnClick = False

    Dim np As Peripherals

  

    OnClick = True
    Exit Function

OnClickError:
    Errorbox "Can not Text in OnClick.", Err.Description
    Exit Function
End Function


'*****************************************************************************
'Description:
'        This function is called from DAvE if the user clicks on a menu entry.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Toolname        name of the entry
'        ToolId          id of the entry
'        Tooltext        additional text of the entry
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnMenuClick(ps As Peripherals, Toolname As String, ToolId As Long, Tooltext As String) As Boolean
    On Error GoTo OnMenuClickError

    OnMenuClick = False

    If (Not MenuClick(ps, Toolname, ToolId, Tooltext, "CCx\Channel_x")) Then Exit Function

    OnMenuClick = True
    Exit Function

OnMenuClickError:
    Exit Function
End Function
    


'-----------------------------------------------------------------------------
' local functions
'-----------------------------------------------------------------------------

'*****************************************************************************
'Description:
'        This function is called from the OnLoad function of the associated
'        module in order to add the page 'Channel'.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageMode(ps As Object) As Boolean
  On Error Resume Next

  OnLoadPageMode = False
  Dim vIndex    As Variant
  Dim iChannel  As Integer

  vIndex = ps.Parameters.v("x")
  iChannel = ps.Parameters.v("Channel")

  If (Not ps.AddPage("Mode Selection")) Then Exit Function

  OnLoadPageMode = True
End Function



'*****************************************************************************
'Description:
'        This function is called from the OnChangingPage function of the
'        associated module in order load all controls for the requested page
'        if they are not already loaded.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'        Pagename         requested page name
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnChangingPageMode(ps As Object, Pagename As String) As Boolean
  On Error Resume Next

  OnChangingPageMode = False
  Dim r           As Projectdata
  Dim vIndex      As Variant
  Dim iChannel    As Integer
  Dim iTmr_x      As Integer
  Dim iTmr_y      As Integer
  Dim iCh_Group   As Integer
  Dim sVarPre     As String
  Dim iChannel_for_CC2 As Integer
  Dim sHelp       As String

  vIndex = ps.Parameters.v("x")
  iChannel = ps.Parameters.v("Channel")
  iCh_Group = ps.Parameters.v("Ch_Group")

  Set r = ps.ProjectData

  sVarPre = "CC" & vIndex

  If (vIndex = 1) Then
      iTmr_x = 0
      iTmr_y = 1
      iChannel_for_CC2 = iChannel
  Else
      iTmr_x = 7
      iTmr_y = 8
      iChannel_for_CC2 = iChannel - 16
  End If
  
  If (Not ps.ExtenderCollection("Mode Selection").Loaded) And (Pagename = "Mode Selection") Then
    ps.ExtenderCollection("Mode Selection").Loaded = True
  

    If (r.Exist("MuxFor_" & sVarPre & "_CC" & iChannel & "IO_I") = True) Then
        
        '-------------------------------
        ' frame: frmCCMODx
        '-------------------------------
        If (Not ps.AddFrame("frmCCMOD" & iChannel,100,100, 8475, 3400, _
            "Mode Selection for Capture / Compare Register CC" & iChannel & _
            " (CCMOD" & iChannel & ")")) Then Exit Function
            
            If (Not ps.AddOption("optCCMOD" & iChannel & "_0", 200, 350, 8000, 300, _
                "Disable capture and compare modes")) Then Exit Function
            
            If (Not ps.AddOption("optCCMOD" & iChannel & "_1", 200, 700, 8000, 300, _
                "Capture on positive transition at pin CC" & iChannel & "IO (" & _
                GetPinName(sVarPre & "_CC" & iChannel & "IO_I") & ")")) Then Exit Function
            
            If (Not ps.AddOption("optCCMOD" & iChannel & "_2", 200, 1050, 8000, 300, _
                "Capture on negative transition at pin CC" & iChannel & "IO (" & _
                GetPinName(sVarPre & "_CC" & iChannel & "IO_I") & ")")) Then Exit Function
            
            If (Not ps.AddOption("optCCMOD" & iChannel & "_3", 200, 1400, 8000, 300, _
                "Capture on any transition at pin CC" & iChannel & "IO (" & _
                GetPinName(sVarPre & "_CC" & iChannel & "IO_I") & ")")) Then Exit Function
            
            If (iCh_Group = 1) Then
                If (Not ps.AddOption("optCCMOD" & iChannel & "_4", 200, 1750, 8000, 300, _
                    "Compare mode 0:  Interrupt only")) Then Exit Function
            
                'If (r.Exist("MuxFor_" & sVarPre & "_CC" & iChannel + 8 & "IO_I") = True) Then
                    sHelp = "Compare mode 1: Toggle bit CC" & iChannel_for_CC2 & _
                            "IO and pin CC" & iChannel & "IO (" & _
                            GetPinName(sVarPre & "_CC" & iChannel & "IO_I") & _
                            ") on each match; " & vbcrlf & "(double-register compare mode with CC" & _
                            iChannel + 8 & ")"
                'Else
                '    sHelp = "Compare mode 1: Toggle bit CC" & iChannel_for_CC2 & _
                '            "IO and pin CC" & iChannel & "IO (" & _
                '            GetPinName(sVarPre & "_CC" & iChannel & "IO_I") & _
                '            ") on each match"
                'End If

                If (Not ps.AddOption("optCCMOD" & iChannel & "_5", 200, 2100, 8100, 400, sHelp)) Then Exit Function

                If (Not ps.AddOption("optCCMOD" & iChannel & "_6", 200, 2550, 8000, 300, _
                    "Compare mode 2:  Interrupt only")) Then Exit Function

                If (Not ps.AddOption("optCCMOD" & iChannel & "_7", 200, 2900, 8000, 300, _
                    "Compare mode 3:  Set bit CC" & iChannel_for_CC2 & _
                    "IO and pin CC" & iChannel & "IO (" & _
                    GetPinName(sVarPre & "_CC" & iChannel & "IO_I") & _
                    ") on each match")) Then Exit Function
            Else
                'If (r.Exist("MuxFor_" & sVarPre & "_CC" & iChannel - 8 & "IO_I") = True) Then
                    sHelp = "Compare mode 0:  Interrupt only; (double-register compare mode with CC" & _
                            iChannel - 8 & ")"
                'Else
                '    sHelp = "Compare mode 0:  Interrupt only"
                'End If
                
                If (Not ps.AddOption("optCCMOD" & iChannel & "_4", 200, 1750, 8000, 300, sHelp)) Then Exit Function

                If (Not ps.AddOption("optCCMOD" & iChannel & "_5", 200, 2100, 8100, 300, _
                    "Compare mode 1: Toggle bit CC" & iChannel_for_CC2 & _
                    "IO and pin CC" & iChannel & "IO (" & _
                    GetPinName(sVarPre & "_CC" & iChannel & "IO_I") & _
                    ") on each match")) Then Exit Function
                If (Not ps.AddOption("optCCMOD" & iChannel & "_6", 200, 2450, 8000, 300, _
                    "Compare mode 2:  Interrupt only")) Then Exit Function

                If (Not ps.AddOption("optCCMOD" & iChannel & "_7", 200, 2800, 8000, 300, _
                    "Compare mode 3:  Set bit CC" & iChannel_for_CC2 & _
                    "IO and pin CC" & iChannel & "IO (" & _
                    GetPinName(sVarPre & "_CC" & iChannel & "IO_I") & _
                    ") on each match")) Then Exit Function
            End If
            

        If (iCh_Group = 2) Then
            '-------------------------------
            ' frame: frmDRxM
            '-------------------------------
            If (Not ps.AddFrame("frmDRxM" & iChannel,100,3600, 8475, 1300, _
                "Double Register Compare Mode Selection (DRM" & iChannel_for_CC2 - 8 & _
                ")")) Then Exit Function
                If (Not ps.AddOption("optDRxM" & iChannel & "_0", 200, 250, 4900, 300, _
                    "Double register compare mode is controlled via CC" & _
                    iChannel - 8)) Then Exit Function
                If (Not ps.AddOption("optDRxM" & iChannel & "_1", 200, 600, 4900, 300, _
                    "Double register compare mode is disabled (interrupt only)")) Then Exit Function
                If (Not ps.AddOption("optDRxM" & iChannel & "_2", 200, 950, 4900, 300, _
                    "Double register compare mode is enabled (independent of CC" & iChannel - 8 & _
                    ")")) Then Exit Function
        End If

    Else

        '-------------------------------
        ' frame: frmCCMODx
        '-------------------------------
        If (Not ps.AddFrame("frmCCMOD" & iChannel,100,100, 8475, 1500, _
            "Mode Selection for Capture / Compare Register CC" & iChannel & _
            " (CCMOD" & iChannel & ")")) Then Exit Function
            If (Not ps.AddOption("optCCMOD" & iChannel & "_0", 200, 350, 8000, 300, _
                "Disable capture and compare modes")) Then Exit Function

            sHelp = "Compare mode 0:  Interrupt only; (double-register compare mode with CC" & iChannel - 8 & ")"
            If (Not ps.AddOption("optCCMOD" & iChannel & "_4", 200, 700, 8000, 300, sHelp)) Then Exit Function

            If (Not ps.AddOption("optCCMOD" & iChannel & "_6", 200, 1050, 8000, 300, _
                "Compare Mode 2:  Interrupt only")) Then Exit Function

        If (iCh_Group = 2) Then
            '-------------------------------
            ' frame: frmDRxM
            '-------------------------------
            If (Not ps.AddFrame("frmDRxM" & iChannel,100,1700, 8475, 1300, _
                "Double Register Compare Mode Selection (DRM" & iChannel_for_CC2 - 8 & _
                ")")) Then Exit Function
                If (Not ps.AddOption("optDRxM" & iChannel & "_0", 200, 250, 4900, 300, _
                    "Double register compare mode is controlled via CC" & _
                    iChannel - 8)) Then Exit Function
                If (Not ps.AddOption("optDRxM" & iChannel & "_1", 200, 600, 4900, 300, _
                    "Double register compare mode is disabled (interrupt only)")) Then Exit Function
                If (Not ps.AddOption("optDRxM" & iChannel & "_2", 200, 950, 4900, 300, _
                    "Double register compare mode is enabled (independent of CC" & iChannel - 8 & _
                    ")")) Then Exit Function
        End If

    End If

  End If
  OnChangingPageMode = True
End Function



'*****************************************************************************
'Description:
'        This function is called from the OnShow function of the associated
'        module. See the description for the OnShow function of the
'        associated module for details.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     control name
'        Pagename        current page name
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnShowPageMode(ps As Object, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowPageModeError

  OnShowPageMode = False

  Dim r           As Projectdata
  Dim c           As Object
  Dim e           As Object
  Dim vIndex      As Variant
  Dim iChannel    As Integer
  Dim iTmr_x      As Integer
  Dim iTmr_y      As Integer
  Dim iCh_Group   As Integer
  Dim bDisabled   As Boolean
  Dim lRegPre     As Long
  Dim sREgPre     As String
  Dim sVarPre     As String
  Dim sCCx        As String
  Dim sDisabledTooltip As String
  Dim iChannel_for_CC2 As Integer

  vIndex = ps.Parameters.v("x")
  iChannel = ps.Parameters.v("Channel")
  iCh_Group = ps.Parameters.v("Ch_Group")

  If (vIndex = 1) Then
      iTmr_x = 0
      iTmr_y = 1
      iChannel_for_CC2 = iChannel
  Else
      iTmr_x = 7
      iTmr_y = 8
      iChannel_for_CC2 = iChannel - 16
  End If

  sVarPre = "CC" & vIndex
  sRegPre = sVarPre & "_"

  lRegPre = iChannel \ 4
  sCCx = sRegPre & "M" & lRegPre & "_"

  Set r = ps.ProjectData
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection

  If (Pagename = "Mode Selection") Then
    '-------------------------------------------------------------------------
    ' page: Channel (action)
    '-------------------------------------------------------------------------
    Select Case (Controlname)
        '---------------------------------------------------------------------
        ' control: optCCMODx_0 (action)
        '---------------------------------------------------------------------
        Case "optCCMOD" & iChannel & "_0"
            Call SetChannelxToDefault(r, vIndex, iCh_Group, iChannel)

        '---------------------------------------------------------------------
        ' control: optCCMODx_1 (action)
        '---------------------------------------------------------------------
        Case "optCCMOD" & iChannel & "_1"
            r.v(sCCx & "MOD" & iChannel) = 1
            r.v(sVarPre & "_CC" & iChannel & "_Use") = 1
            Call RequestCCAlternateInputPin(r, vIndex, iChannel)
            r.v(sRegPre & "OUT_CC" & iChannel_for_CC2 & "IO") = 0
            Call DeselectDRM(r, vIndex, iCh_Group, iChannel_for_CC2)

        '---------------------------------------------------------------------
        ' control: optCCMODx_2 (action)
        '---------------------------------------------------------------------
        Case "optCCMOD" & iChannel & "_2"
            r.v(sCCx & "MOD" & iChannel) = 2
            r.v(sVarPre & "_CC" & iChannel & "_Use") = 1
            Call RequestCCAlternateInputPin(r, vIndex, iChannel)
            r.v(sRegPre & "OUT_CC" & iChannel_for_CC2 & "IO") = 0
            Call DeselectDRM(r, vIndex, iCh_Group, iChannel_for_CC2)

        '---------------------------------------------------------------------
        ' control: optCCMODx_3 (action)
        '---------------------------------------------------------------------
        Case "optCCMOD" & iChannel & "_3"
            r.v(sCCx & "MOD" & iChannel) = 3
            r.v(sVarPre & "_CC" & iChannel & "_Use") = 1
            Call RequestCCAlternateInputPin(r, vIndex, iChannel)
            r.v(sRegPre & "OUT_CC" & iChannel_for_CC2 & "IO") = 0
            Call DeselectDRM(r, vIndex, iCh_Group, iChannel_for_CC2)

        '---------------------------------------------------------------------
        ' control: optCCMODx_4 (action)
        '---------------------------------------------------------------------
        Case "optCCMOD" & iChannel & "_4"
            r.v(sCCx & "MOD" & iChannel) = 4
            r.v(sVarPre & "_CC" & iChannel & "_Use") = 1
            Call ReleaseCCAlternatePin(r, vIndex, iChannel)
            r.v(sRegPre & "OUT_CC" & iChannel_for_CC2 & "IO") = 0

        '---------------------------------------------------------------------
        ' control: optCCMODx_5 (action)
        '---------------------------------------------------------------------
        Case "optCCMOD" & iChannel & "_5"
            r.v(sCCx & "MOD" & iChannel) = 5
            r.v(sVarPre & "_CC" & iChannel & "_Use") = 1
            Call RequestCCAlternateOutputPin(r, vIndex, iChannel)
            Call DeselectDRM(r, vIndex, iCh_Group, iChannel_for_CC2)

        '---------------------------------------------------------------------
        ' control: optCCMODx_6 (action)
        '---------------------------------------------------------------------
        Case "optCCMOD" & iChannel & "_6"
            r.v(sCCx & "MOD" & iChannel) = 6
            r.v(sVarPre & "_CC" & iChannel & "_Use") = 1
            Call ReleaseCCAlternatePin(r, vIndex, iChannel)
            Call DeselectDRM(r, vIndex, iCh_Group, iChannel_for_CC2)
            r.v(sRegPre & "OUT_CC" & iChannel_for_CC2 & "IO") = 0

        '---------------------------------------------------------------------
        ' control: optCCMODx_7 (action)
        '---------------------------------------------------------------------
        Case "optCCMOD" & iChannel & "_7"
            r.v(sCCx & "MOD" & iChannel) = 7
            r.v(sVarPre & "_CC" & iChannel & "_Use") = 1
            Call RequestCCAlternateOutputPin(r, vIndex, iChannel)
            Call DeselectDRM(r, vIndex, iCh_Group, iChannel_for_CC2)

      Case Else
    End Select

    If (iCh_Group = 2) Then

        Select Case (Controlname)
            '---------------------------------------------------------------------
            ' control: "optDRxM" & iChannel & "_0" (action)
            '---------------------------------------------------------------------
            Case "optDRxM" & iChannel & "_0"
                r.v(sRegPre & "DRM_DR" & iChannel_for_CC2 - 8 & "M") = 0

            '---------------------------------------------------------------------
            ' control: "optDRxM" & iChannel & "_1" (action)
            '---------------------------------------------------------------------
            Case "optDRxM" & iChannel & "_1"
                r.v(sRegPre & "DRM_DR" & iChannel_for_CC2 - 8 & "M") = 1

            '---------------------------------------------------------------------
            ' control: "optDRxM" & iChannel & "_2" (action)
            '---------------------------------------------------------------------
            Case "optDRxM" & iChannel & "_2"
                r.v(sRegPre & "DRM_DR" & iChannel_for_CC2 - 8 & "M") = 2

          Case Else
        End Select

    End If

    '-------------------------------------------------------------------------
    ' control: optCCMODx_0 (show)
    '-------------------------------------------------------------------------
    With c("optCCMOD" & iChannel & "_0")
        bDisabled = (IsModuleClockDisabled(r, vIndex))
        .Enabled = Not(bDisabled)
        .Value = -CInt(r.v(sCCx & "MOD" & iChannel) = 0)
    End With

    With e("optCCMOD" & iChannel & "_0")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (IsModuleClockDisabled(r, vIndex)) Then
          .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
        End If
      End If
    End With

    If (r.Exist("MuxFor_" & sVarPre & "_CC" & iChannel & "IO_I") = True) Then
        '-------------------------------------------------------------------------
        ' control: optCCMODx_1 (show)
        '-------------------------------------------------------------------------
        With c("optCCMOD" & iChannel & "_1")
            bDisabled = (IsModuleClockDisabled(r, vIndex) Or ( _
                         Not(IsAlternateInputPinFree(r, sVarPre & "_CC" & iChannel & "IO_I")) And _
                         r.v(sCCx & "MOD" & iChannel) <> 5 And _
                         r.v(sCCx & "MOD" & iChannel) <> 7) )

            If (vIndex = 1 And _
                r.Exist("INT_Ex" & iChannel - 8 & "_Use") = True) Then
                
                bDisabled = bDisabled Or r.v("INT_Ex" & iChannel - 8 & "_Use") = 1
            End If

            .Enabled = Not(bDisabled)
            .Value = -CInt(r.v(sCCx & "MOD" & iChannel) = 1)
        End With

        With e("optCCMOD" & iChannel & "_1")
          If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If (IsModuleClockDisabled(r, vIndex)) Then
              .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
            End If
            If (Not(IsAlternateInputPinFree(r, sVarPre & "_CC" & iChannel & "IO_I")) And _
                         r.v(sCCx & "MOD" & iChannel) <> 5 And _
                         r.v(sCCx & "MOD" & iChannel) <> 7) Then
              .DisabledTooltipText = .DisabledTooltipText & _
                GetDisabledAlternatePinTooltip(r, sVarPre & "_CC" & iChannel & "IO_I")
            End If
            If (vIndex = 1 And _
                r.Exist("INT_Ex" & iChannel - 8 & "_Use") = True) Then
                
                If (r.v("INT_Ex" & iChannel - 8 & "_Use") = 1) Then
                    .DisabledTooltipText = .DisabledTooltipText & _
                      FormatDisabledTooltip("ext. interrupt " & iChannel - 8 & _
                      " is used")
                End If
            End If
          End If
        End With

        '-------------------------------------------------------------------------
        ' control: optCCMODx_2 (show)
        '-------------------------------------------------------------------------
        With c("optCCMOD" & iChannel & "_2")
            'bDisabled = see up
            .Enabled = Not(bDisabled)
            .Value = -CInt(r.v(sCCx & "MOD" & iChannel) = 2)
        End With

        With e("optCCMOD" & iChannel & "_2")
            .DisabledTooltipText = e("optCCMOD" & iChannel & "_1").DisabledTooltipText
        End With

        '-------------------------------------------------------------------------
        ' control: optCCMODx_3 (show)
        '-------------------------------------------------------------------------
        With c("optCCMOD" & iChannel & "_3")
            'bDisabled = see up
            .Enabled = Not(bDisabled)
            .Value = -CInt(r.v(sCCx & "MOD" & iChannel) = 3)
        End With

        With e("optCCMOD" & iChannel & "_3")
            .DisabledTooltipText = e("optCCMOD" & iChannel & "_1").DisabledTooltipText
        End With
    End If

    '-------------------------------------------------------------------------
    ' control: optCCMODx_4 (show)
    '-------------------------------------------------------------------------
    With c("optCCMOD" & iChannel & "_4")
        bDisabled = (IsModuleClockDisabled(r, vIndex))
        If (vIndex = 1 And _
            r.Exist("INT_Ex" & iChannel - 8 & "_Use") = True) Then
            
            bDisabled = bDisabled Or r.v("INT_Ex" & iChannel - 8 & "_Use") = 1
        End If
        .Enabled = Not(bDisabled)
        .Value = -CInt(r.v(sCCx & "MOD" & iChannel) = 4)
    End With

    With e("optCCMOD" & iChannel & "_4")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (IsModuleClockDisabled(r, vIndex)) Then
          .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
        End If
        If (vIndex = 1 And _
            r.Exist("INT_Ex" & iChannel - 8 & "_Use") = True) Then
            
            If (r.v("INT_Ex" & iChannel - 8 & "_Use") = 1) Then
                .DisabledTooltipText = .DisabledTooltipText & _
                  FormatDisabledTooltip("ext. interrupt " & iChannel - 8 & _
                  " is used")
            End If
        End If
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: optCCMODx_6 (show)
    '-------------------------------------------------------------------------
    With c("optCCMOD" & iChannel & "_6")
        'bDisabled = see up
        .Enabled = Not(bDisabled)
        .Value = -CInt(r.v(sCCx & "MOD" & iChannel) = 6)
    End With

    With e("optCCMOD" & iChannel & "_6")
        .DisabledTooltipText = e("optCCMOD" & iChannel & "_4").DisabledTooltipText
    End With

    If (r.Exist("MuxFor_" & sVarPre & "_CC" & iChannel & "IO_I") = True) Then
        '-------------------------------------------------------------------------
        ' control: optCCMODx_5 (show)
        '-------------------------------------------------------------------------
        With c("optCCMOD" & iChannel & "_5")
             bDisabled = (IsModuleClockDisabled(r, vIndex) Or _
                          (Not(IsAlternateOutputPinFree(r, sVarPre & "_CC" & iChannel & "IO_O")) And _
                          r.v(sCCx & "MOD" & iChannel) <> 1 And _
                          r.v(sCCx & "MOD" & iChannel) <> 2 And _
                          r.v(sCCx & "MOD" & iChannel) <> 3) _
                          )

            If (vIndex = 1 And _
                r.Exist("INT_Ex" & iChannel - 8 & "_Use") = True) Then
                
                bDisabled = bDisabled Or r.v("INT_Ex" & iChannel - 8 & "_Use") = 1
            End If

            .Enabled = Not(bDisabled)
            .Value = -CInt(r.v(sCCx & "MOD" & iChannel) = 5)
        End With

        With e("optCCMOD" & iChannel & "_5")
          If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If (IsModuleClockDisabled(r, vIndex)) Then
                .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
            End If
            If (Not(IsAlternateOutputPinFree(r, sVarPre & "_CC" & iChannel & "IO_O")) And _
                          r.v(sCCx & "MOD" & iChannel) <> 1 And _
                          r.v(sCCx & "MOD" & iChannel) <> 2 And _
                          r.v(sCCx & "MOD" & iChannel) <> 3) Then
              .DisabledTooltipText = .DisabledTooltipText & _
                GetDisabledAlternatePinTooltip(r, sVarPre & "_CC" & iChannel & "IO_O")
            End If
            If (vIndex = 1 And _
                r.Exist("INT_Ex" & iChannel - 8 & "_Use") = True) Then
                
                If (r.v("INT_Ex" & iChannel - 8 & "_Use") = 1) Then
                    .DisabledTooltipText = .DisabledTooltipText & _
                      FormatDisabledTooltip("ext. interrupt " & iChannel - 8 & _
                      " is used")
                End If
            End If
          End If
        End With

        '-------------------------------------------------------------------------
        ' control: optCCMODx_7 (show)
        '-------------------------------------------------------------------------
        With c("optCCMOD" & iChannel & "_7")
            'bDisabled = see up
            .Enabled = Not(bDisabled)
            .Value = -CInt(r.v(sCCx & "MOD" & iChannel) = 7)
        End With

        With e("optCCMOD" & iChannel & "_7")
            .DisabledTooltipText = e("optCCMOD" & iChannel & "_5").DisabledTooltipText
        End With

    End If 'if vIndex = 2
                
    If (iCh_Group = 2) Then

        '-------------------------------------------------------------------------
        ' control: optDRxM" & iChannel & "_0 (show)
        '-------------------------------------------------------------------------
        With c("optDRxM" & iChannel & "_0")
            bDisabled = (IsModuleClockDisabled(r, vIndex) Or _
                         Not(r.v(sCCx & "MOD" & iChannel) = 4) )

            .Enabled = Not(bDisabled)
            .Value = -CInt(r.v(sRegPre & "DRM_DR" & iChannel_for_CC2 - 8 & "M") = 0)
        End With

        With e("optDRxM" & iChannel & "_0")
          If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If (IsModuleClockDisabled(r, vIndex)) Then
              .DisabledTooltipText = .DisabledTooltipText & _
                GetTooltipModuleClockDisabled(r, vIndex)
            End If
            If (Not(r.v(sCCx & "MOD" & iChannel) = 4)) Then
              .DisabledTooltipText = .DisabledTooltipText & _
                FormatDisabledTooltip("double-register compare mode with CC" & iChannel - 8 & _
                  " is not selected")
            End If
          End If
        End With

        '-------------------------------------------------------------------------
        ' control: optDRxM" & iChannel & "_1 (show)
        '-------------------------------------------------------------------------
        With c("optDRxM" & iChannel & "_1")
            bDisabled = (IsModuleClockDisabled(r, vIndex) Or _
                         Not(r.v(sCCx & "MOD" & iChannel) = 4) )

            .Enabled = Not(bDisabled)
            .Value = -CInt(r.v(sRegPre & "DRM_DR" & iChannel_for_CC2 - 8 & "M") = 1)
        End With

        With e("optDRxM" & iChannel & "_1")
          If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If (IsModuleClockDisabled(r, vIndex)) Then
              .DisabledTooltipText = .DisabledTooltipText & _
                GetTooltipModuleClockDisabled(r, vIndex)
            End If
            If (Not(r.v(sCCx & "MOD" & iChannel) = 4)) Then
              .DisabledTooltipText = .DisabledTooltipText & _
                FormatDisabledTooltip("double-register compare mode with CC" & iChannel - 8 & _
                " is not selected")
            End If
          End If
        End With

        '-------------------------------------------------------------------------
        ' control: optDRxM" & iChannel & "_2 (show)
        '-------------------------------------------------------------------------
        With c("optDRxM" & iChannel & "_2")
            bDisabled = (IsModuleClockDisabled(r, vIndex) Or _
                         Not(r.v(sCCx & "MOD" & iChannel) = 4) )

            .Enabled = Not(bDisabled)
            .Value = -CInt(r.v(sRegPre & "DRM_DR" & iChannel_for_CC2 - 8 & "M") = 2)
        End With

        With e("optDRxM" & iChannel & "_2")
          If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If (IsModuleClockDisabled(r, vIndex)) Then
              .DisabledTooltipText = .DisabledTooltipText & _
                GetTooltipModuleClockDisabled(r, vIndex)
            End If
            If (Not(r.v(sCCx & "MOD" & iChannel) = 4)) Then
              .DisabledTooltipText = .DisabledTooltipText & _
                FormatDisabledTooltip("double-register compare mode with CC" & iChannel - 8 & _
                " is not selected")
            End If
          End If
        End With

    End If



  End If

  OnShowPageMode = True
  Exit Function

OnShowPageModeError:
  Errorbox "Can not show page.", Err.Description
  Exit Function
End Function



'*****************************************************************************
'Description:
'        This function is called from the OnLoad function of the associated
'        module in order to add the page 'Channel'.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageControl(ps As Object) As Boolean
  On Error Resume Next

  OnLoadPageControl = False
  Dim vIndex    As Variant
  Dim iChannel  As Integer

  vIndex = ps.Parameters.v("x")
  iChannel = ps.Parameters.v("Channel")

  If (Not ps.AddPage("Control")) Then Exit Function

  OnLoadPageControl = True
End Function



'*****************************************************************************
'Description:
'        This function is called from the OnChangingPage function of the
'        associated module in order load all controls for the requested page
'        if they are not already loaded.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'        Pagename         requested page name
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnChangingPageControl(ps As Object, Pagename As String) As Boolean
  On Error Resume Next

  OnChangingPageControl = False
  Dim r           As Projectdata
  Dim vIndex      As Variant
  Dim iChannel    As Integer
  Dim iTmr_x      As Integer
  Dim iTmr_y      As Integer
  Dim iCh_Group   As Integer
  Dim sVarPre     As String
  Dim iChannel_for_CC2 As Integer
  Dim sHelp       As String

  vIndex = ps.Parameters.v("x")
  iChannel = ps.Parameters.v("Channel")
  iCh_Group = ps.Parameters.v("Ch_Group")

  Set r = ps.ProjectData

  sVarPre = "CC" & vIndex

  If (vIndex = 1) Then
      iTmr_x = 0
      iTmr_y = 1
      iChannel_for_CC2 = iChannel
  Else
      iTmr_x = 7
      iTmr_y = 8
      iChannel_for_CC2 = iChannel - 16
  End If
  
  If (Not ps.ExtenderCollection("Control").Loaded) And (Pagename = "Control") Then
    ps.ExtenderCollection("Control").Loaded = True
  

    '-------------------------------
    ' frame: frmACCx
    '-------------------------------
    If (Not ps.AddFrame("frmACC" & iChannel,100,100, 4150, 1200, _
        "Allocation Bit (ACC" & iChannel & ")")) Then Exit Function
        If (Not ps.AddOption("optACC" & iChannel & "_0", 200, 300, 2500, 300, _
            "CC" & iChannel & " allocated to timer T" & iTmr_x)) Then Exit Function
        If (Not ps.AddOption("optACC" & iChannel & "_1", 200, 700, 2500, 300, _
            "CC" & iChannel & " allocated to timer T" & iTmr_y)) Then Exit Function

    '-------------------------------
    ' frame: frmSM_x
    '-------------------------------
    If (Not ps.AddFrame("frmSM_" & iChannel & "",4400,100, 4150, 1200, _
        "Single Event Control")) Then Exit Function
        If (Not ps.AddCheck("chkSEM" & iChannel, 200, 300, 3000, 300, _
            "Enable single event mode (SEM" & iChannel_for_CC2 & ")")) Then Exit Function
        If (Not ps.AddCheck("chkSEE" & iChannel, 200, 700, 3000, 300, _
            "Enable single event (SEE" & iChannel_for_CC2 & ")")) Then Exit Function

    '-------------------------------
    ' frame: frIC_x
    '-------------------------------
    If (Not ps.AddFrame("frmIC_" & iChannel & "",100,1400, 4150, 800, _
        "Interrupt Control")) Then Exit Function
        If (Not ps.AddCheck("chkCC" & iChannel & "IE", 200, 300, 3900, 300, _
            "Enable Capture / Compare interrupt (IE)")) Then Exit Function

    '-------------------------------
    ' frame: frmCCx
    '-------------------------------
    If (Not ps.AddFrame("frmCC" & iChannel & "",4400,1400, 4150, 800, _
        "Capture / Compare Register")) Then Exit Function
        If (Not ps.AddText("txtCC" & iChannel & "", 2400, 300, 1000, 300)) Then Exit Function
        If (Not ps.AddLabel("lblCC" & iChannel & "", 0, 200, 350, 2000, 300, _
            "CC register (CC" & iChannel & ")")) Then Exit Function

    If (r.Exist("MuxFor_" & sVarPre & "_CC" & iChannel & "IO_I") = True) Then
        '-------------------------------
        ' frame: frIC_x
        '-------------------------------
        If (Not ps.AddFrame("frmOUT_" & iChannel & "",100,2300, 4150, 800, _
            "Output Register Control")) Then Exit Function
            If (Not ps.AddCheck("chkOUT" & iChannel, 200, 250, 3900, 400, _
                "Set output resgister to high after initialization (CC" & iChannel_for_CC2 & ")")) Then Exit Function
    End If


  End If
  OnChangingPageControl = True
End Function



'*****************************************************************************
'Description:
'        This function is called from the OnShow function of the associated
'        module. See the description for the OnShow function of the
'        associated module for details.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     control name
'        Pagename        current page name
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnShowPageControl(ps As Object, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowPageControlError

  OnShowPageControl = False

  Dim r             As Projectdata
  Dim c             As Object
  Dim e             As Object
  Dim vIndex        As Variant
  Dim iChannel      As Integer
  Dim iTmr_x        As Integer
  Dim iTmr_y        As Integer
  Dim iCh_Group     As Integer
  Dim bDisabled     As Boolean
  Dim lRegPre       As Long
  Dim sREgPre       As String
  Dim sVarPre       As String
  Dim sCCx          As String
  Dim sDisabledTooltip As String
  Dim iChannel_for_CC2 As Integer

  vIndex = ps.Parameters.v("x")
  iChannel = ps.Parameters.v("Channel")
  iCh_Group = ps.Parameters.v("Ch_Group")

  If (vIndex = 1) Then
      iTmr_x = 0
      iTmr_y = 1
      iChannel_for_CC2 = iChannel
  Else
      iTmr_x = 7
      iTmr_y = 8
      iChannel_for_CC2 = iChannel - 16
  End If

  sVarPre = "CC" & vIndex
  sRegPre = sVarPre & "_"

  lRegPre = iChannel \ 4
  sCCx = sRegPre & "M" & lRegPre & "_"

  Set r = ps.ProjectData
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection

  If (Pagename = "Control") Then
    '-------------------------------------------------------------------------
    ' page: Channel (action)
    '-------------------------------------------------------------------------
    Select Case (Controlname)
        '---------------------------------------------------------------------
        ' control: optACCx_0 (action)
        '---------------------------------------------------------------------
        Case "optACC" & iChannel & "_0"
            r.v(sCCx & "ACC" & iChannel) = 0

        '---------------------------------------------------------------------
        ' control: optACCx_1 (action)
        '---------------------------------------------------------------------
        Case "optACC" & iChannel & "_1"
            r.v(sCCx & "ACC" & iChannel) = 1

        '---------------------------------------------------------------------
        ' control: "chkSEM" & iChannel (action)
        '---------------------------------------------------------------------
        Case "chkSEM" & iChannel
            r.v(sRegPre & "SEM_SEM" & iChannel_for_CC2) = v(1)
            If (v(1) = 0) Then
                r.v(sRegPre & "SEE_SEE" & iChannel_for_CC2) = 0
                r.v(sVarPre & "_USE_EnableSingleEvent") = 0
            End If

        '---------------------------------------------------------------------
        ' control: "chkSEE" & iChannel (action)
        '---------------------------------------------------------------------
        Case "chkSEE" & iChannel
            r.v(sRegPre & "SEE_SEE" & iChannel_for_CC2) = v(1)

        '---------------------------------------------------------------------
        ' control: chkCCxIE (action)
        '---------------------------------------------------------------------
        Case "chkCC" & iChannel & "IE"
            r.v(sVarPre & "_Int" & iChannel & "_Use") = v(1)
            If v(1) = 1 Then
              Call EnableSRN(r, sVarPre, sRegPre & "CC" & iChannel & "IC", "CC" & iChannel)
            Else
              Call DisableSRN(r, sVarPre, sRegPre & "CC" & iChannel & "IC", "CC" & iChannel)
            End If

        '---------------------------------------------------------------------
        ' control: txtCCx (action)
        '---------------------------------------------------------------------
        Case "txtCC" & iChannel & ""
                If (IsInputFormatOK(v(1), "H4", c("lblCC" & iChannel).Caption, 0, 65535)) Then
                    r.sfr(sRegPre & "CC" & iChannel) = v(1)
                Else
                    Exit Function
                End If


        '---------------------------------------------------------------------
        ' control: chkCCxIE (action)
        '---------------------------------------------------------------------
        Case "chkOUT" & iChannel
            r.v(sRegPre & "OUT_CC" & iChannel_for_CC2 & "IO") = v(1)


      Case Else
    End Select

    '*************************************************
    'bDisabled for next frames
    '*************************************************
    bDisabled = (IsModuleClockDisabled(r, vIndex) Or r.v(sCCx & "MOD" & iChannel) = 0)

    sDisabledTooltip = ""
    sDisabledTooltip = TOOLTIP_DISABLED_IF

    If (IsModuleClockDisabled(r, vIndex)) Then
        sDisabledTooltip = sDisabledTooltip & GetTooltipModuleClockDisabled(r, vIndex)
    End If
    If (r.v(sCCx & "MOD" & iChannel) = 0) Then
      sDisabledTooltip = sDisabledTooltip & FormatDisabledTooltip("channel " & iChannel & " is disabled")
    End If

    Call SetDefaultPropertiesForAllControlsOnFrame(c("frmCC" & iChannel).Caption, c, e, bDisabled, sDisabledTooltip, "")
    Call SetDefaultPropertiesForAllControlsOnFrame(c("frmACC" & iChannel).Caption, c, e, bDisabled, sDisabledTooltip, "")
    Call SetDefaultPropertiesForAllControlsOnFrame(c("frmIC_" & iChannel).Caption, c, e, bDisabled, sDisabledTooltip, "")

    If (r.Exist("MuxFor_" & sVarPre & "_CC" & iChannel & "IO_I") = True) Then
        bDisabled = bDisabled OR NOT (GetPinFunction(r, sVarPre & "_CC" & iChannel & "IO_O") = 2)
        If (NOT (GetPinFunction(r, sVarPre & "_CC" & iChannel & "IO_O") = 2)) Then
          sDisabledTooltip = sDisabledTooltip & FormatDisabledTooltip("compare mode 1 or 3 is not selected")
        End If
        Call SetDefaultPropertiesForAllControlsOnFrame(c("frmOUT_" & iChannel).Caption, c, e, bDisabled, sDisabledTooltip, "")
    End If
          
    '-------------------------------------------------------------------------
    ' control: txtCCx (show)
    '-------------------------------------------------------------------------
    With c("txtCC" & iChannel & "")
        .Text = FormatTextOutput(r.sfr(sRegPre & "CC" & iChannel), "H4")
    End With

    '-------------------------------------------------------------------------
    ' control: lblCCx (show)
    '-------------------------------------------------------------------------
    With c("lblCC" & iChannel & "")
    End With

    '-------------------------------------------------------------------------
    ' control: optACCx_0 (show)
    '-------------------------------------------------------------------------
    With c("optACC" & iChannel & "_0")
        .Value = -CInt(r.v(sCCx & "ACC" & iChannel) = 0)
    End With

    '-------------------------------------------------------------------------
    ' control: optACCx_1 (show)
    '-------------------------------------------------------------------------
    With c("optACC" & iChannel & "_1")
        .Value = -CInt(r.v(sCCx & "ACC" & iChannel) = 1)
    End With

    '-------------------------------------------------------------------------
    ' control: "chkSEM" & iChannel (show)
    '-------------------------------------------------------------------------
    With c("chkSEM" & iChannel)
        bDisabled = (IsModuleClockDisabled(r, vIndex) Or _
                     r.v(sCCx & "MOD" & iChannel) = 0)
        .Enabled = Not(bDisabled)
        .Value = -CInt(r.v(sRegPre & "SEM_SEM" & iChannel_for_CC2) = 1)
    End With

    With e("chkSEM" & iChannel)
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (IsModuleClockDisabled(r, vIndex)) Then
          .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
        End If
        If (r.v(sCCx & "MOD" & iChannel) = 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("channel " & iChannel & " is disabled")
        End If
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: "chkSEE" & iChannel (show)
    '-------------------------------------------------------------------------
    With c("chkSEE" & iChannel)
        bDisabled = (IsModuleClockDisabled(r, vIndex) Or _
                     r.v(sCCx & "MOD" & iChannel) = 0 Or _
                     r.v(sRegPre & "SEM_SEM" & iChannel_for_CC2) = 0)
        .Enabled = Not(bDisabled)
        .Value = -CInt(r.v(sRegPre & "SEE_SEE" & iChannel_for_CC2) = 1)
    End With

    With e("chkSEE" & iChannel)
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (IsModuleClockDisabled(r, vIndex)) Then
          .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
        End If
        If (r.v(sCCx & "MOD" & iChannel) = 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("channel " & iChannel & " is disabled")
        End If
        If (r.v(sRegPre & "SEM_SEM" & iChannel_for_CC2) = 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("single event mode is disabled")
        End If
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: chkCCxIE (show)
    '-------------------------------------------------------------------------
    With c("chkCC" & iChannel & "IE")
        .Value = -CInt(r.v(sVarPre & "_Int" & iChannel & "_Use") = 1)
    End With

    If (r.Exist("MuxFor_" & sVarPre & "_CC" & iChannel & "IO_I") = True) Then
        '-------------------------------------------------------------------------
        ' control: chkOUT (show)
        '-------------------------------------------------------------------------
        With c("chkOUT" & iChannel)
            .Value = -CInt(r.v(sRegPre & "OUT_CC" & iChannel_for_CC2 & "IO") = 1)
        End With
    End If

  End If

  OnShowPageControl = True
  Exit Function

OnShowPageControlError:
  Errorbox "Can not show page.", Err.Description
  Exit Function
End Function

'*****************************************************************************
'Description:
'        This function is called from the OnLoad function of the associated
'        module in order to add a page.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageFunctions(ps As Peripherals) As Boolean
  On Error Resume Next
  OnLoadPageFunctions = False

  If (Not ps.AddPage("Functions")) Then Exit Function

  OnLoadPageFunctions = True
End Function


'*****************************************************************************
'Description:
'        This function is called from the OnChangingPage function of the
'        associated module in order load all controls for the requested page
'        if they are not already loaded.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'        Pagename         requested page name
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnChangingPageFunctions(ps As Peripherals, Pagename As String) As Boolean
  On Error Resume Next

  OnChangingPageFunctions = False

  Dim vIndex    As Variant
  Dim sVarPre   As String
  Dim n         As String
  Dim i         As Integer
  Dim iChannel  As Integer
  Dim aFunctions(1 To 16) As String

  iChannel = ps.Parameters.v("Channel")

  vIndex = ps.Parameters.v("x")   
  sVarPre = "CC" & vIndex


  If (Not ps.ExtenderCollection("Functions").Loaded) And (Pagename = "Functions") Then
    ps.ExtenderCollection("Functions").Loaded = True

    '-------------------------------
    ' frame: fraLibrary1
    '-------------------------------
    ' initialize the array
    For i = Lbound(aFunctions) To Ubound(aFunctions)
      aFunctions(i) = ""
    Next i

    ' Functions and Macros
    i = Lbound(aFunctions)
    aFunctions(i) = "CC" & iChannel
    i = i + 1

    If (Not ps.AddFrame("fraLibrary1", 100 , 100, 4150, 5200, "Function Library for Interrupt Service Routines")) Then Exit Function
      For i = Lbound(aFunctions) To Ubound(aFunctions)
        If Not(aFunctions(i) = "") Then
          If (Not ps.AddCheck("chk_" & aFunctions(i), 150, 325 + (300 * (i - 1)), 200, 275, "")) Then Exit Function
          If (Not ps.AddText("txt_" & aFunctions(i), 400, 300 + (300 * (i - 1)), 3600, 325)) Then Exit Function
        End If
      Next i


    '###############################################################################################
    ' The global function 'OnShowFunction' needs for each function insert in Include.inc a Checkbox
    ' and a Textbox. For this reason 'CFile' becomes a control. The controls are not used 
    ' and the position is out of the visible area.
    '###############################################################################################

    '-------------------------------
    ' frame: fra_CFile
    '-------------------------------
    If (Not ps.AddFrame("fra_CFile", 4400 , 6000, 4150, 700, CAPTION_FRA_CFILE)) Then Exit Function
      If (Not ps.AddText("txt_CFile", 1475, 250, 2500, 325)) Then Exit Function
      If (Not ps.AddLabel("lbl_CFile", 0, 200, 275, 1250, 275, CAPTION_LBL_CFILE)) Then Exit Function

  End If

  OnChangingPageFunctions = True
End Function


'*****************************************************************************
'Description:
'        This function is called from the OnShow function of the associated
'        module. See the description for the OnShow function of the
'        associated module for details.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     control name
'        Pagename        current page name
'        v()             value array
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnShowPageFunctions(ps As Peripherals, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error Goto OnShowPageFunctionsError

  OnShowPageFunctions = False
    
  Dim r           As ProjectData
  Dim c           As Collection
  Dim e           As Collection
  Dim bDisabled   As Boolean
  Dim func        As FunctionClass
  Dim sName       As String
  Dim sRegPre     As String
  Dim sVarPre     As String
  Dim vIndex      As Variant
  Dim d           As String
  Dim iNrSRN      As Integer
  Dim i           As Integer
  Dim iChannel    As Integer

  iChannel = ps.Parameters.v("Channel")
  vIndex = ps.Parameters.v("x")
  sVarPre = "CC" & vIndex
  sRegPre = sVarPre & "_"

  Set r = ps.ProjectData
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection

  Set func = New FunctionClass

  If (Pagename = "Functions") Then

    '-------------------------------------------------------------------------
    ' page: Functions (action)
    '-------------------------------------------------------------------------
    RefreshFunctionDescriptions(vIndex, func, r, iChannel + 1)

    If (Not UpdateFunction(sVarPre, ps, func , Controlname, v)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: Functions (show)
    '-------------------------------------------------------------------------
    Set func = New FunctionClass
    RefreshFunctionDescriptions(vIndex, func, r, iChannel + 1)

    sName = sVarPre & "_NAME_CC" & iChannel
    ' disable condition
    func.Disabled(sName) = True
    ' tooltip if control is disabled
    d = TOOLTIP_DISABLED_ISR32
    func.DisabledToolTip(sName) = d
    ' value for the control
    func.Value(sName) = IsISRSelected(r, sVarPre, sRegPre & "CC" & iChannel & "IC", "CC" & iChannel)

    If (Not ShowFunction(sVarPre, ps, func , v)) Then Exit Function


  End If         ' if Pagename


  OnShowPageFunctions = True
  Exit Function

OnShowPageFunctionsError:
  Errorbox "Can not show page", Err.Description
  Exit Function
End Function


