'#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:
'        ADC
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions which are create controls for the
'        module 'ADC'.
'        Furthermore it contains functions for the event handling of
'        these controls.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        1.0   ahe         14.06.00     final version
'
'        ---------------------------------------------------------------------
'        2.0   ahe         20.07.00     final version for C161PI
'                                       - reduzierung von Ch's von 24 auf 4
'                                       - delete P1DIDIS
'                                       - delete ADCON_ADX
'             
'        ---------------------------------------------------------------------
'        3.0   ahe         07.08.00     final version for C161CS/JC/JI
'                                       - only AN0..AN7 and AN12..AN15
'
'        ---------------------------------------------------------------------
'        4.0   ahe         20.02.01     final version for Pegasus
'
'        4.1   ahe         08.08.02     - neu interrupt page
'        4.2   ste         09.12.02     formula for conversion time changed 
'                                       (spec. changed)
'                                       16 analog inputs
'        4.3   ste         24.02.03    caption for module clock changed
'
'*****************************************************************************
Dim CONTROL As Peripherals


'*****************************************************************************
'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: ModuleClock
    '-------------------------------------------------------------------------
    If (Not OnLoadPageModuleClock(ps)) Then Exit Function

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

    '-------------------------------------------------------------------------
    ' page: PortControl
    '-------------------------------------------------------------------------
    If (Not OnLoadPagePortControl(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: PEC
    '-------------------------------------------------------------------------
    If (Not OnLoadPagePEC(ps)) Then Exit Function

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

    '-------------------------------------------------------------------------
    ' page: Parameters
    '-------------------------------------------------------------------------
    If (Not OnLoadPageParameters(ps)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: Notes
    '-------------------------------------------------------------------------
    If (Not OnLoadPageNotes(ps)) Then Exit Function

    '-------------------------------------------------------------------------
    ' end of pages
    '-------------------------------------------------------------------------
    
    '-------------------------------------------------------------------------
    ' call function to initialize the menu
    '-------------------------------------------------------------------------
    If (Not InitMenu(ps, "ADC\Dialog")) 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 = "ADC" & vIndex

    
    '-------------------------------------------------------------------------
    'page: ModuleClock
    '-------------------------------------------------------------------------
    If (Not OnChangingPageModuleClock (ps, Pagename)) Then Exit Function

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

    '-------------------------------------------------------------------------
    'page: PortControl
    '-------------------------------------------------------------------------
    If (Not OnChangingPagePortControl (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: PEC
    '-------------------------------------------------------------------------
    If (Not OnChangingPagePEC (ps, Pagename)) Then Exit Function

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

    '-------------------------------------------------------------------------
    'page: Parameters
    '-------------------------------------------------------------------------
    If (Not OnChangingPageParameters (ps, Pagename)) Then Exit Function

    '-------------------------------------------------------------------------
    'page: Notes
    '-------------------------------------------------------------------------
    If (Not OnChangingPageNotes (ps, Pagename, sVarPre)) 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 = "ADC" & vIndex


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

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

    '-------------------------------------------------------------------------
    ' page: PortControl
    '-------------------------------------------------------------------------
    If (Not OnShowPagePortControl(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: PEC
    '-------------------------------------------------------------------------
    If (Not OnShowPagePEC(ps, Controlname, v, Pagename)) Then Exit Function

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

    '-------------------------------------------------------------------------
    ' page: Parameters
    '-------------------------------------------------------------------------
    If (Not OnShowPageParameters(ps, Controlname, Pagename, v)) Then Exit Function 
       
    '-------------------------------------------------------------------------
    ' page: Notes
    '-------------------------------------------------------------------------
    If (Not OnShowPageNotes(ps, Controlname, Pagename, sVarPre, 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

    '-------------------------------------------------------------------------
    ' buttons: PEC
    '-------------------------------------------------------------------------
    If (Not OnClickPagePEC(ps, Controlname)) Then Exit Function

    '-------------------------------------------------------------------------
    ' buttons: Control
    '-------------------------------------------------------------------------
    If (Not OnButtonClickOnPageControl(ps, Controlname)) Then Exit Function


    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, "ADC\Dialog")) 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 'ModuleClock'.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageModuleClock(ps As Object) As Boolean
  On Error Resume Next

  OnLoadPageModuleClock = False

  If (Not ps.AddPage("Module Clock")) Then Exit Function

  OnLoadPageModuleClock = 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 OnChangingPageModuleClock(ps As Object, Pagename As String) As Boolean
  On Error Resume Next

  OnChangingPageModuleClock = False
  Dim sVarPre As String
  Dim vIndex  As Variant
  Dim r       As ProjectData

  vIndex = ps.Parameters.v("x")
  sVarPre = "ADC" & vIndex
  
  Set r = ps.ProjectData

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

    '-------------------------------
    ' frame: frmDISx
    '-------------------------------
    If (Not ps.AddFrame("frmDISx",100,100, 5400, 1375, sVarPre & " Disabled Flag (" & GetADCDisebleBitName(r, vIndex) & ")")) Then Exit Function
        If (Not ps.AddOption("optDISx_0", 200, 350, 4800, 300, "Enable module; the peripheral is supplied with the clock signal")) Then Exit Function
        If (Not ps.AddOption("optDISx_1", 200, 825, 4800, 300, "Disable module; the clock input of peripheral is disabled")) Then Exit Function

    '-------------------------------
    ' frame: frmInputClock
    '-------------------------------
    If (Not ps.AddFrame("frmInputClock",100,1600, 5400, 1000, "Input Clock")) Then Exit Function
        If (Not ps.AddLabel("lbl_txt_InputClock", 0, 200, 450, 3225, 300, "Input clock of the " & sVarPre & " module [MHz]")) Then Exit Function
        If (Not ps.AddLabel("lbl_box_InputClock", 1, 3550, 400, 1000, 300, "")) Then Exit Function

  End If
  OnChangingPageModuleClock = 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 OnShowPageModuleClock(ps As Object, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowPageModuleClockError

  OnShowPageModuleClock = False

  Dim r         As ProjectData
  Dim c         As Object
  Dim e         As Object
  Dim bDisabled As Boolean
  Dim sVarPre   As String
  Dim vIndex    As Variant
  Dim sRegPre   As String
  Dim i         As Integer

  vIndex = ps.Parameters.v("x")
  sVarPre = "ADC" & vIndex
  sRegPre = sVarPre & "_"

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

  If (Pagename = "Module Clock") Then
    '-------------------------------------------------------------------------
    ' page: ModuleClock (action)
    '-------------------------------------------------------------------------
    Select Case (Controlname)
        '---------------------------------------------------------------------
        ' control: optDISx_0 (action)
        '---------------------------------------------------------------------
        Case "optDISx_0"
             r.v("SYSCON3_" & GetADCDisebleBitName(r, vIndex)) = 0

        '---------------------------------------------------------------------
        ' control: optDISx_1 (action)
        '---------------------------------------------------------------------
        Case "optDISx_1"
             r.v("SYSCON3_" & GetADCDisebleBitName(r, vIndex)) = 1
             r.sfr("P5DIDIS") = 0
             r.v(sVarPre & "_DIC_P5") = 0
             r.v(sRegPre & "CON_ADM") = 0
             r.v(sRegPre & "CON_ADST") = 0
             r.v(sRegPre & "CON_ADWR") = 0
             r.v(sRegPre & "CON_ADCIN") = 0
             r.v(sRegPre & "CTR0_ADST") = 0
             r.v(sRegPre & "CTR0_ADWR") = 0
             r.v(sRegPre & "CTR0_ADCIN") = 0
             r.v(sRegPre & "CON1_ICST") = 0
             
             Call DisableSRN(r, sVarPre, sRegPre & "CIC", "Conv")
             Call DisableSRN(r, sVarPre, sRegPre & "EIC", "Error")
             
             For i = 0 To 15
                 If (r.Exist("MuxFor_AN" & i)) Then
                     If (GetPinFunction(r, "AN" & i) = 1) Then
                         Call ReleaseAlternateInputPin(r, "AN" & i) 
                     End If
                 End If
             Next i
             
             r.v(sVarPre & "_USE_Init") = 0
             r.v(sVarPre & "_USE_InjectChan") = 0
             r.v(sVarPre & "_USE_StartInject") = 0
             r.v(sVarPre & "_USE_ReadInject") = 0
             r.v(sVarPre & "_USE_InjectReady") = 0


      Case Else
    End Select

    '-------------------------------------------------------------------------
    ' page: ModuleClock (show)
    '-------------------------------------------------------------------------

    '-------------------------------------------------------------------------
    ' control: optDISx_0 (show)
    '-------------------------------------------------------------------------
    With c("optDISx_0")
      bDisabled = False
      .Enabled = Not(bDisabled)
      .Value = -CInt(r.v("SYSCON3_" & GetADCDisebleBitName(r, vIndex)) = 0)
    End With

    With e("optDISx_0")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("")
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: optDISx_1 (show)
    '-------------------------------------------------------------------------
    With c("optDISx_1")
      bDisabled = False
      .Enabled = Not(bDisabled)
      .Value = -CInt(r.v("SYSCON3_" & GetADCDisebleBitName(r, vIndex)) = 1)
    End With

    With e("optDISx_1")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("")
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: lbl_txt_InputClock (show)
    '-------------------------------------------------------------------------
    With c("lbl_txt_InputClock")
      bDisabled = (IsModuleDisabled(r, vIndex))
      .Enabled = Not(bDisabled)
    End With

    With e("lbl_txt_InputClock")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (IsModuleDisabled(r, vIndex)) Then
          .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleDisabled(r, vIndex)
        End If
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: lbl_box_InputClock (show)
    '-------------------------------------------------------------------------
    With c("lbl_box_InputClock")
      bDisabled = (IsModuleDisabled(r, vIndex))
      .Enabled = Not(bDisabled)
      If (GetADCModuleClock(r, vIndex) > GetMaxSystemClock(r)) Then
          .Caption = TO_HIGH
      Else
          .Caption = GetADCModuleClock(r, vIndex)
      End If
    End With

    With e("lbl_box_InputClock")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (IsModuleDisabled(r, vIndex)) Then
          .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleDisabled(r, vIndex)
        End If
      End If
    End With


  End If

  OnShowPageModuleClock = True
  Exit Function

OnShowPageModuleClockError:
  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 'ADC_Control'.
'-----------------------------------------------------------------------------
'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

  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
  
  If (Not ps.ExtenderCollection("Control").Loaded) And (Pagename = "Control") Then
    ps.ExtenderCollection("Control").Loaded = True
  

    '-------------------------------
    ' frame: frmMD
    '-------------------------------
    If (Not ps.AddFrame("frmMD",100,100, 4150, 1000, "Mode (MD)")) Then Exit Function
        If (Not ps.AddOption("optMD_0", 200, 250, 2900, 300, "Compatibility mode")) Then Exit Function
        If (Not ps.AddOption("optMD_1", 200, 600, 2900, 300, "Enhanced mode")) Then Exit Function

    '-------------------------------
    ' frame: frmCC
    '-------------------------------
    If (Not ps.AddFrame("frmCC",100,1200, 4150, 1000, "Configuration of Compatibility Mode")) Then Exit Function
        If (Not ps.AddButton("btnCC", 1000, 400, 2100, 380, "Compatibility Mode")) Then Exit Function

    '-------------------------------
    ' frame: frmEC
    '-------------------------------
    If (Not ps.AddFrame("frmEC",100,2300, 4150, 1000, "Configuration of Enhanced Mode")) Then Exit Function
        If (Not ps.AddButton("btnEC", 1000, 400, 2100, 380, "Enhanced Mode")) Then Exit Function

    '-------------------------------
    ' frame: frmIC
    '-------------------------------
    If (Not ps.AddFrame("frmIC",100,3350, 4150, 1400, "Interrupt Control")) Then Exit Function
        If (Not ps.AddCheck("chkADCIE", 200, 300, 3500, 380, _
            "Enable end-of-conversion interrupt (IE)")) Then Exit Function
        If (Not ps.AddCheck("chkADEIE", 200, 800, 3500, 380, _
            "Enable error / end-of-injected-conversion interrupt (IE)")) Then Exit Function
  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 sVarPre           As String
  Dim vIndex            As Variant
  Dim sRegPre           As String
  Dim bDisabled         As Boolean
  Dim i                 As Integer
  Dim sDisabledTooltip  As String

  vIndex = ps.Parameters.v("x")
  sVarPre = "ADC" & vIndex
  sRegPre = sVarPre & "_"

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

  If (Pagename = "Control") Then
    '-------------------------------------------------------------------------
    ' page: Control (action)
    '-------------------------------------------------------------------------
    Select Case (Controlname)
        '---------------------------------------------------------------------
        ' control: optMD_0 (action)
        '---------------------------------------------------------------------
        Case "optMD_0"
            r.v(sRegPre & "CTR0_MD") = 0
            r.v(sRegPre & "CTR0_ADST") = 0
            r.v(sRegPre & "CTR0_ADWR") = 0
            r.v(sRegPre & "CTR0_ADCIN") = 0
            
            r.v(sVarPre & "_USE_InjectChan") = 0
            r.v(sVarPre & "_USE_StartInject") = 0
            r.v(sVarPre & "_USE_ReadInject") = 0
            r.v(sVarPre & "_USE_InjectReady") = 0
            r.v(sVarPre & "_USE_OverWrite") = 0

        '---------------------------------------------------------------------
        ' control: optMD_1 (action)
        '---------------------------------------------------------------------
        Case "optMD_1"
            r.v(sRegPre & "CTR0_MD") = 1
            r.v(sRegPre & "CON1_ICST") = 0
            r.v(sRegPre & "CON_ADST") = 0
            r.v(sRegPre & "CON_ADWR") = 0
            r.v(sRegPre & "CON_ADCIN") = 0

            r.v(sVarPre & "_USE_InjectChan") = 0
            r.v(sVarPre & "_USE_StartInject") = 0
            r.v(sVarPre & "_USE_ReadInject") = 0
            r.v(sVarPre & "_USE_InjectReady") = 0
            r.v(sVarPre & "_USE_OverWrite") = 0

        '---------------------------------------------------------------------
        ' control: chkADCIE (action)
        '---------------------------------------------------------------------
        Case "chkADCIE"
            If (v(1) = 1) Then
                r.v(sVarPre & "_USE_ConvReady") = 0
                Call EnableSRN(r, sVarPre, sRegPre & "CIC", "Conv")
            Else
                Call DisableSRN(r, sVarPre, sRegPre & "CIC", "Conv")
            End If

        '---------------------------------------------------------------------
        ' control: chkADEIE (action)
        '---------------------------------------------------------------------
        Case "chkADEIE"
            If (v(1) = 1) Then
                r.v(sVarPre & "_USE_OverWrite") = 0
                r.v(sVarPre & "_USE_InjectReady") = 0
                Call EnableSRN(r, sVarPre, sRegPre & "EIC", "Error")
            Else
                Call DisableSRN(r, sVarPre, sRegPre & "EIC", "Error")
            End If

      Case Else
    End Select

    '-------------------------------------------------------------------------
    ' page: Control (show)
    '-------------------------------------------------------------------------
    bDisabled = (IsModuleDisabled(r, vIndex))

    sDisabledTooltip = ""
    sDisabledTooltip = TOOLTIP_DISABLED_IF

    If (IsModuleDisabled(r, vIndex)) Then
        sDisabledTooltip = sDisabledTooltip & GetTooltipModuleDisabled(r, vIndex)
    End If
                                               
    SetDefaultPropertiesForAllControlsOnFrame(c("frmMD").Caption, c, e, bDisabled, sDisabledTooltip, "")
    SetDefaultPropertiesForAllControlsOnFrame(c("frmIC").Caption, c, e, bDisabled, sDisabledTooltip, "")


    '-------------------------------------------------------------------------
    ' control: optADM_0 (show)
    '-------------------------------------------------------------------------
    With c("optMD_0")
        .Value = -CInt(r.v(sRegPre & "CTR0_MD") = 0)
    End With

    '-------------------------------------------------------------------------
    ' control: optADM_1 (show)
    '-------------------------------------------------------------------------
    With c("optMD_1")
        .Value = -CInt(r.v(sRegPre & "CTR0_MD") = 1)
    End With

    '-------------------------------------------------------------------------
    ' control: btnCC (show)
    '-------------------------------------------------------------------------
    With c("btnCC")
        bDisabled = IsModuleDisabled(r, vIndex) Or r.v(sRegPre & "CTR0_MD") = 1
        .Enabled = nOt(bDisabled)
    End With

    With e("btnCC")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (IsModuleDisabled(r, vIndex)) Then
          .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleDisabled(r, vIndex)
        End If
        If (r.v(sRegPre & "CTR0_MD") = 1) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("enhanced mode is selected")
        End If
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: btnEC (show)
    '-------------------------------------------------------------------------
    With c("btnEC")
        bDisabled = IsModuleDisabled(r, vIndex) Or r.v(sRegPre & "CTR0_MD") = 0
        .Enabled = nOt(bDisabled)
    End With

    With e("btnEC")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        If (IsModuleDisabled(r, vIndex)) Then
          .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleDisabled(r, vIndex)
        End If
        If (r.v(sRegPre & "CTR0_MD") = 0) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledTooltip("compatibility mode is selected")
        End If
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: chkADCIE (show)
    '-------------------------------------------------------------------------
    With c("chkADCIE")
        .Value = -CInt(r.v(sRegPre & "CIC_IE") = 1)
    End With

    '-------------------------------------------------------------------------
    ' control: chkADEIE (show)
    '-------------------------------------------------------------------------
    With c("chkADEIE")
        .Value = -CInt(r.v(sRegPre & "EIC_IE") = 1)
    End With

  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 OnClick function of the associated
'        module if a button on the page 'Control' will be clicked.
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        Controlname     control name of the button
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnButtonClickOnPageControl(ps As Object, Controlname As String) As Boolean
  On Error GoTo OnButtonClickOnPageControlError

  OnButtonClickOnPageControl = False
  Dim vIndex    As Variant
  Dim iChannel  As Integer

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

  If (Controlname = "btnCC") Then
    '---------------------------------------------------------------------
    ' control: btnCh_i (action)
    '---------------------------------------------------------------------
    Set CONTROL = New Peripherals
    CONTROL.Parameters.Add ("x")
    CONTROL.Parameters.v("x") = vIndex
    Set CONTROL.ProjectData = ps.ProjectData
    CONTROL.Caption = "ADC Compatibility Mode"
    CONTROL.Scriptfile = DefGetPath("Scripts") & "ADC\NormalControl.DAVE"
    CONTROL.Show
  End If

  If (Controlname = "btnEC") Then
    '---------------------------------------------------------------------
    ' control: btnCh_i (action)
    '---------------------------------------------------------------------
    Set CONTROL = New Peripherals
    CONTROL.Parameters.Add ("x")
    CONTROL.Parameters.v("x") = vIndex
    Set CONTROL.ProjectData = ps.ProjectData
    CONTROL.Caption = "ADC Enhanced Mode"
    CONTROL.Scriptfile = DefGetPath("Scripts") & "ADC\EnhancedControl.dave"
    CONTROL.Show
  End If




  OnButtonClickOnPageControl = True
  Exit Function

OnButtonClickOnPageControlError:
  Exit Function
End Function

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

  OnLoadPagePortControl = False

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

  OnLoadPagePortControl = 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 OnChangingPagePortControl(ps As Object, Pagename As String) As Boolean
  On Error Resume Next

  OnChangingPagePortControl = False
  Dim r   As ProjectData
  Dim i   As Integer
  Dim ii  As Integer
  
  Set r = ps.ProjectData
 
  If (Not ps.ExtenderCollection("Port Control").Loaded) And (Pagename = "Port Control") Then
    ps.ExtenderCollection("Port Control").Loaded = True
  

    '-------------------------------
    ' frame: frmDIC_P5
    '-------------------------------
    If (Not ps.AddFrame("frmDIC_P5",100,100, 3800, 1400, "Digital Input Control for all enabled Pins on Port P5")) Then Exit Function
        If (Not ps.AddOption("optDIC_P5_0", 200, 350, 2800, 380, "Connect all digital input stages to port P5")) Then Exit Function
        If (Not ps.AddOption("optDIC_P5_1", 200, 850, 2800, 380, "Disconnect all digital input stages from port P5")) Then Exit Function

    '-------------------------------
    ' frame: frmP5DIDIS
    '-------------------------------
    If (Not ps.AddFrame("frmP5DIDIS",4000,100, 4650, 5200, "Digital Input Control (P5DIDIS)")) Then Exit Function
        ii = 0
        For i = 0 To 15
            If (r.Regs.BitFind("P5DIDIS_P" & i)) Then
              If (Not ps.AddCheck("chkP5D_" & i, 200, 300 + ii * 300, 3950, 300, "Disconnect digital input stage from pin P5." & i & "/AN" & i)) Then Exit Function
              ii = ii + 1
            End If
        Next i


  End If
  OnChangingPagePortControl = 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 OnShowPagePortControl(ps As Object, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowPagePortControlError

  OnShowPagePortControl = False

  Dim r                 As ProjectData
  Dim c                 As Object
  Dim e                 As Object
  Dim sVarPre           As String
  Dim vIndex            As Variant
  Dim sRegPre           As String
  Dim bDisabled         As Boolean
  Dim i                 As Integer
  Dim sDisabledTooltip  As String

  vIndex = ps.Parameters.v("x")
  sVarPre = "ADC" & vIndex
  sRegPre = sVarPre & "_"

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

  If (Pagename = "Port Control") Then
    '-------------------------------------------------------------------------
    ' page: PortControl (action)
    '-------------------------------------------------------------------------

    '---------------------------------------------------------------------
    ' control: chkP5D_x (action)
    '---------------------------------------------------------------------
    For i = 0 To 15
        If (r.Regs.BitFind("P5DIDIS_P" & i)) Then
            If (Controlname = "chkP5D_" & i) Then
                If (v(1) = 1) Then
                    r.v("P5DIDIS_P" & i) = 1
                    Call RequestAlternateInputPin(r, "AN" & i) 
                Else
                    r.v("P5DIDIS_P" & i) = 0
                    Call ReleaseAlternateInputPin(r, "AN" & i) 
                End If
            End If
        End If
    Next i

    '---------------------------------------------------------------------
    ' control: optDIC_P5_0 (action)
    '---------------------------------------------------------------------
    If (Controlname = "optDIC_P5_0") Then
        r.v(sVarPre & "_DIC_P5") = 0
        r.sfr("P5DIDIS") = 0
        For i = 0 To 15
           If (r.Regs.BitFind("P5DIDIS_P" & i)) Then
               If (GetPinFunction(r, "AN" & i) = 1) Then
                   Call ReleaseAlternateInputPin(r, "AN" & i) 
               End If
           End If
        Next i
    End If

    '---------------------------------------------------------------------
    ' control: optDIC_P5_1 (action)
    '---------------------------------------------------------------------
    If (Controlname = "optDIC_P5_1") Then
        r.v(sVarPre & "_DIC_P5") = 1
        For i = 0 To 15
           If (r.Regs.BitFind("P5DIDIS_P" & i)) Then
               If (IsAlternateInputPinFree(r, "AN" & i)) Then
                   r.v("P5DIDIS_P" & i) = 1
                   Call RequestAlternateInputPin(r, "AN" & i) 
              End If
           End If
        Next i
    End If

    '-------------------------------------------------------------------------
    ' page: PortControl (show)
    '-------------------------------------------------------------------------

    '-------------------------------------------------------------------------
    ' control: chkP5D_x (show)
    '-------------------------------------------------------------------------
    For i = 0 To 15
        If (r.Regs.BitFind("P5DIDIS_P" & i)) Then
            With c("chkP5D_" & i)
                bDisabled = (IsModuleDisabled(r, vIndex) Or Not(IsAlternateInputPinFree(r, "AN" & i)))
                .Enabled = Not(bDisabled)
                .Value = -CInt(r.v("P5DIDIS_P" & i) = 1)
            End With

            With e("chkP5D_" & i)
              If (bDisabled) Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                If (IsModuleDisabled(r, vIndex)) Then
                  .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleDisabled(r, vIndex)
                End If
                If (Not(IsAlternateInputPinFree(r, "AN" & i))) Then
                  .DisabledTooltipText = .DisabledTooltipText & GetDisabledAlternatePinTooltip(r, "AN" & i)
                End If
              End If
            End With
        End If
    Next i

    '-------------------------------------------------------------------------------------------------------
    '-------------------------------------------------------------------------------------------------------
    'DisabledTooltip for all next controls
    '-------------------------------------------------------------------------------------------------------
    '-------------------------------------------------------------------------------------------------------
    
    bDisabled = (IsModuleDisabled(r, vIndex))

    sDisabledTooltip = ""
    sDisabledTooltip = TOOLTIP_DISABLED_IF

    If (IsModuleDisabled(r, vIndex)) Then
        sDisabledTooltip = sDisabledTooltip & GetTooltipModuleDisabled(r, vIndex)
    End If

    SetDefaultPropertiesForAllControlsOnFrame(c("frmDIC_P5").Caption, c, e, bDisabled, sDisabledTooltip, "")

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

    '-------------------------------------------------------------------------
    ' control: optDIC_P5_0 (show)
    '-------------------------------------------------------------------------
    With c("optDIC_P5_0")
        .Value = -CInt(r.v(sVarPre & "_DIC_P5") = 0)
    End With

    '-------------------------------------------------------------------------
    ' control: optDIC_P5_1 (show)
    '-------------------------------------------------------------------------
    With c("optDIC_P5_1")
        .Value = -CInt(r.v(sVarPre & "_DIC_P5") = 1)
    End With

  End If

  OnShowPagePortControl = True
  Exit Function

OnShowPagePortControlError:
  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

  Dim i                   As Integer
  Dim aFunctions(1 To 16) As String


  OnChangingPageFunctions = False

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

    '-------------------------------
    ' frame: fra_Init
    '-------------------------------
    If (Not ps.AddFrame("fra_Init", 100 , 100, 4150, 700, CAPTION_FRA_USE_INIT)) Then Exit Function
      If (Not ps.AddCheck("chk_Init", 150, 275, 200, 275, "")) Then Exit Function
      If (Not ps.AddText("txt_Init", 400, 250, 3600, 325)) Then Exit Function

    '-------------------------------
    ' frame: fra_CFile
    '-------------------------------
    If (Not ps.AddFrame("fra_CFile", 4400 , 100, 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, 300, 1250, 275, CAPTION_LBL_CFILE)) Then Exit Function


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

    ' Functions and Macros
    i = Lbound(aFunctions)

    aFunctions(i) = "StartConv"
    i = i + 1
    aFunctions(i) = "StopConv"
    i = i + 1
    aFunctions(i) = "ConfConv"
    i = i + 1
    aFunctions(i) = "ConvReady"
    i = i + 1
    aFunctions(i) = "ReadConv"
    i = i + 1
    aFunctions(i) = "OverWrite"
    i = i + 1
    aFunctions(i) = "Busy"
    i = i + 1
    aFunctions(i) = "Conv"
    i = i + 1
    aFunctions(i) = "Error"
    i = i + 1
    aFunctions(i) = "InjectChan"
    i = i + 1
    aFunctions(i) = "StartInject"
    i = i + 1
    aFunctions(i) = "ReadInject"
    i = i + 1
    aFunctions(i) = "InjectReady"
    i = i + 1

    If (Not ps.AddFrame("fraLibrary1", 100 , 900, 4150, 4400, CAPTION_FRA_LIBRARY1)) 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


  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 d         As String
  Dim sVarPre   As String
  Dim sRegPre   As String
  Dim sName     As String
  Dim vIndex    As Variant
  Dim sFctName  As String

  vIndex = ps.Parameters.v("x")
  sVarPre = "ADC" & 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)

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

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

    sName = sVarPre & "_NAME_Init" 
    ' disable condition
    func.Disabled(sName) = (IsModuleDisabled(r, vIndex))
    ' tooltip if control is disabled
    d = GetDisabledFunctionToolTipEntry(func, sName)
    If (IsModuleDisabled(r, vIndex)) Then
      d = d & GetTooltipModuleDisabled(r, vIndex)
    End If
    func.DisabledToolTip(sName) = d
    
    sFctName = "StartConv"
    sName = sVarPre & "_NAME_" & sFctName
    ' disable condition
    func.Disabled(sName) = (r.v(sVarPre & "_USE_Init") = 0)
    ' tooltip if control is disabled
    d = GetDisabledFunctionToolTipEntry(func, sName)
    If (r.v(sVarPre & "_USE_Init") = 0) Then 
      d = d & FormatDisabledToolTip(TOOLTIP_DESELECTED_INIT)
    End If
    func.DisabledToolTip(sName) = d

    sFctName = "ConfConv"
    sName = sVarPre & "_NAME_" & sFctName
    ' disable condition
    func.Disabled(sName) = (r.v(sVarPre & "_USE_Init") = 0)
    ' tooltip if control is disabled
    d = GetDisabledFunctionToolTipEntry(func, sName)
    If (r.v(sVarPre & "_USE_Init") = 0) Then 
      d = d & FormatDisabledToolTip(TOOLTIP_DESELECTED_INIT)
    End If
    func.DisabledToolTip(sName) = d

    sFctName = "StopConv"
    sName = sVarPre & "_NAME_" & sFctName
    ' disable condition
    func.Disabled(sName) = (r.v(sVarPre & "_USE_Init") = 0)
    ' tooltip if control is disabled
    d = GetDisabledFunctionToolTipEntry(func, sName)
    If (r.v(sVarPre & "_USE_Init") = 0) Then 
      d = d & FormatDisabledToolTip(TOOLTIP_DESELECTED_INIT)
    End If
    func.DisabledToolTip(sName) = d

    sFctName = "ConvReady"
    sName = sVarPre & "_NAME_" & sFctName
    ' disable condition
    func.Disabled(sName) = (r.v(sVarPre & "_USE_Init") = 0 Or _
                            r.v(sRegPre & "CIC_IE") = 1)
    ' tooltip if control is disabled
    d = GetDisabledFunctionToolTipEntry(func, sName)
    If (r.v(sVarPre & "_USE_Init") = 0) Then 
      d = d & FormatDisabledToolTip(TOOLTIP_DESELECTED_INIT)
    End If
    If (r.v(sRegPre & "CIC_IE") = 1) Then 
      d = d & FormatDisabledToolTip("end-of-conversion interrupt is enabled")
    End If
    func.DisabledToolTip(sName) = d

    sFctName = "ReadConv"
    sName = sVarPre & "_NAME_" & sFctName
    ' disable condition
    func.Disabled(sName) = (r.v(sVarPre & "_USE_Init") = 0)
    ' tooltip if control is disabled
    d = GetDisabledFunctionToolTipEntry(func, sName)
    If (r.v(sVarPre & "_USE_Init") = 0) Then 
      d = d & FormatDisabledToolTip(TOOLTIP_DESELECTED_INIT)
    End If
    func.DisabledToolTip(sName) = d

    sFctName = "OverWrite"
    sName = sVarPre & "_NAME_" & sFctName
    ' disable condition
    func.Disabled(sName) = (r.v(sVarPre & "_USE_Init") = 0 Or _
                            r.v(sRegPre & "CON_ADWR") = 1 Or _
                            r.v(sRegPre & "CTR0_ADWR") = 1 Or _
                            r.v(sRegPre & "EIC_IE") = 1)
    ' tooltip if control is disabled
    d = GetDisabledFunctionToolTipEntry(func, sName)
    If (r.v(sVarPre & "_USE_Init") = 0) Then 
      d = d & FormatDisabledToolTip(TOOLTIP_DESELECTED_INIT)
    End If
    If (r.v(sRegPre & "EIC_IE") = 1) Then 
      d = d & FormatDisabledToolTip("error / end-of-injected-conversion interrupt is enabled")
    End If
    If (r.v(sRegPre & "CON_ADWR") = 1 Or r.v(sRegPre & "CTR0_ADWR") = 1) Then 
      d = d & FormatDisabledToolTip("wait for ADDAT read mode is enabled")
    End If
    func.DisabledToolTip(sName) = d

    sFctName = "Busy"
    sName = sVarPre & "_NAME_" & sFctName
    ' disable condition
    func.Disabled(sName) = (r.v(sVarPre & "_USE_Init") = 0)
    ' tooltip if control is disabled
    d = GetDisabledFunctionToolTipEntry(func, sName)
    If (r.v(sVarPre & "_USE_Init") = 0) Then 
      d = d & FormatDisabledToolTip(TOOLTIP_DESELECTED_INIT)
    End If
    func.DisabledToolTip(sName) = d

    sFctName = "InjectChan"
    sName = sVarPre & "_NAME_" & sFctName
    ' disable condition
    func.Disabled(sName) = (r.v(sVarPre & "_USE_Init") = 0 Or _
                            (r.v(sRegPre & "CON_ADWR") = 0 And _
                            r.v(sRegPre & "CTR0_ADWR") = 0) Or _
                            (r.v(sRegPre & "CON_ADCIN") = 0 And _
                            r.v(sRegPre & "CTR0_ADCIN") = 0) )
    ' tooltip if control is disabled
    d = GetDisabledFunctionToolTipEntry(func, sName)
    If (r.v(sVarPre & "_USE_Init") = 0) Then 
      d = d & FormatDisabledToolTip(TOOLTIP_DESELECTED_INIT)
    End If
    If (r.v(sRegPre & "CON_ADWR") = 0 And r.v(sRegPre & "CTR0_ADWR") = 0) Then 
      d = d & FormatDisabledToolTip("wait for ADDAT read mode is disabled")
    End If
    If (r.v(sRegPre & "CON_ADCIN") = 0 And r.v(sRegPre & "CTR0_ADCIN") = 0) Then 
      d = d & FormatDisabledToolTip("channel injection is disabled")
    End If
    func.DisabledToolTip(sName) = d

    sFctName = "StartInject"
    sName = sVarPre & "_NAME_" & sFctName
    ' disable condition
    func.Disabled(sName) = (r.v(sVarPre & "_USE_Init") = 0 Or _
                            (r.v(sRegPre & "CON_ADWR") = 0 And _
                            r.v(sRegPre & "CTR0_ADWR") = 0) Or _
                            (r.v(sRegPre & "CON_ADCIN") = 0 And _
                            r.v(sRegPre & "CTR0_ADCIN") = 0) )
    ' tooltip if control is disabled
    d = GetDisabledFunctionToolTipEntry(func, sName)
    If (r.v(sVarPre & "_USE_Init") = 0) Then 
      d = d & FormatDisabledToolTip(TOOLTIP_DESELECTED_INIT)
    End If
    If (r.v(sRegPre & "CON_ADWR") = 0 And r.v(sRegPre & "CTR0_ADWR") = 0) Then 
      d = d & FormatDisabledToolTip("wait for ADDAT read mode is disabled")
    End If
    If (r.v(sRegPre & "CON_ADCIN") = 0 And r.v(sRegPre & "CTR0_ADCIN") = 0) Then 
      d = d & FormatDisabledToolTip("channel injection is disabled")
    End If
    func.DisabledToolTip(sName) = d

    sFctName = "ReadInject"
    sName = sVarPre & "_NAME_" & sFctName
    ' disable condition
    func.Disabled(sName) = (r.v(sVarPre & "_USE_Init") = 0 Or _
                            (r.v(sRegPre & "CON_ADWR") = 0 And _
                            r.v(sRegPre & "CTR0_ADWR") = 0) Or _
                            (r.v(sRegPre & "CON_ADCIN") = 0 And _
                            r.v(sRegPre & "CTR0_ADCIN") = 0) )
    ' tooltip if control is disabled
    d = GetDisabledFunctionToolTipEntry(func, sName)
    If (r.v(sVarPre & "_USE_Init") = 0) Then 
      d = d & FormatDisabledToolTip(TOOLTIP_DESELECTED_INIT)
    End If
    If (r.v(sRegPre & "CON_ADWR") = 0 And r.v(sRegPre & "CTR0_ADWR") = 0) Then 
      d = d & FormatDisabledToolTip("wait for ADDAT read mode is disabled")
    End If
    If (r.v(sRegPre & "CON_ADCIN") = 0 And r.v(sRegPre & "CTR0_ADCIN") = 0) Then 
      d = d & FormatDisabledToolTip("channel injection is disabled")
    End If
    func.DisabledToolTip(sName) = d

    sFctName = "InjectReady"
    sName = sVarPre & "_NAME_" & sFctName
    func.Disabled(sName) = (r.v(sVarPre & "_USE_Init") = 0 Or _
                            (r.v(sRegPre & "CON_ADWR") = 0 And _
                            r.v(sRegPre & "CTR0_ADWR") = 0) Or _
                            (r.v(sRegPre & "CON_ADCIN") = 0 And _
                            r.v(sRegPre & "CTR0_ADCIN") = 0) Or _
                            r.v(sRegPre & "EIC_IE") = 1)
    ' tooltip if control is disabled
    d = GetDisabledFunctionToolTipEntry(func, sName)
    If (r.v(sVarPre & "_USE_Init") = 0) Then 
      d = d & FormatDisabledToolTip(TOOLTIP_DESELECTED_INIT)
    End If
    If (r.v(sRegPre & "CON_ADWR") = 0 And r.v(sRegPre & "CTR0_ADWR") = 0) Then 
      d = d & FormatDisabledToolTip("wait for ADDAT read mode is disabled")
    End If
    If (r.v(sRegPre & "CON_ADCIN") = 0 And r.v(sRegPre & "CTR0_ADCIN") = 0) Then 
      d = d & FormatDisabledToolTip("channel injection is disabled")
    End If
    If (r.v(sRegPre & "EIC_IE") = 1) Then 
      d = d & FormatDisabledToolTip("error / end-of-injected-conversion interrupt is enabled")
    End If
    func.DisabledToolTip(sName) = d

    sFctName = "Conv"
    sName = sVarPre & "_NAME_" & sFctName
    ' 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 & "CIC", sFctName)

    sFctName = "Error"
    sName = sVarPre & "_NAME_" & sFctName
    ' 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 & "EIC", sFctName)


    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


'*****************************************************************************
'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 OnLoadPageParameters(ps As Peripherals) As Boolean
  On Error Resume Next
  OnLoadPageParameters = False

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

  OnLoadPageParameters = 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 OnChangingPageParameters(ps As Peripherals, Pagename As String) As Boolean
  On Error Resume Next

  OnChangingPageParameters = False

  Dim r                     As ProjectData
  Dim i                     As Integer
  Dim aParameters(1 To 16)  As String
  Dim iANA                  As Integer
  Dim sVarPre               As String
  Dim vIndex                As Variant

  vIndex = ps.Parameters.v("x")
  sVarPre = "ADC" & vIndex
  iANA = 0

  Set r = ps.ProjectData

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

    '-------------------------------------------------------------------------
    ' frame: frmCAPCOMx_HFile
    '-------------------------------------------------------------------------
    If (Not ps.AddFrame("fra_HFile", 100 , 100, 4150, 700, CAPTION_FRA_HFILE)) Then Exit Function
      If (Not ps.AddText("txt_HFile", 1475, 250, 2500, 325)) Then Exit Function
      If (Not ps.AddLabel("lbl_HFile",0 , 200 , 300, 1250, 300, CAPTION_LBL_HFILE)) Then Exit Function   

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

    i = Lbound(aParameters)
    aParameters(i) = "FIXED"
    i = i + 1
    aParameters(i) = "FIXED_CONTI"
    i = i + 1
    aParameters(i) = "SCAN"
    i = i + 1
    aParameters(i) = "SCAN_CONTI"
    i = i + 1
    aParameters(i) = "ANA_" & iANA
    iANA = iANA + 1
    i = i + 1
    aParameters(i) = "ANA_" & iANA
    iANA = iANA + 1
    i = i + 1
    aParameters(i) = "ANA_" & iANA
    iANA = iANA + 1
    i = i + 1
    aParameters(i) = "ANA_" & iANA
    iANA = iANA + 1
    i = i + 1
    aParameters(i) = "ANA_" & iANA
    iANA = iANA + 1
    i = i + 1
    aParameters(i) = "ANA_" & iANA

    If (Not ps.AddFrame("fraParameters1", 100 , 900, 4150, 4500, CAPTION_FRA_PARAMETERS1)) Then Exit Function
      For i = Lbound(aParameters) To Ubound(aParameters)
        If Not(aParameters(i) = "") Then
          If (Not ps.AddText("txt_" & aParameters(i), 200, 200 + 300 * (i - 1), 3800, 325)) Then Exit Function
        End If
      Next i

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

    i = Lbound(aParameters)
    For iANA = 6 To 15
      If (r.Exist(sVarPre & "_PARA_ANA_" & iANA)) Then
          aParameters(i) = "ANA_" & iANA
          i = i + 1
      End If
    Next iANA

    If (Not ps.AddFrame("fraParameters2", 4400 , 900, 4150, 4500, CAPTION_FRA_PARAMETERS1)) Then Exit Function
      For i = Lbound(aParameters) To Ubound(aParameters)
        If Not(aParameters(i) = "") Then
          If (Not ps.AddText("txt_" & aParameters(i), 200, 200 + 300 * (i - 1), 3800, 325)) Then Exit Function
        End If
      Next i


  End If

  OnChangingPageParameters = 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 OnShowPageParameters(ps As Peripherals, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error Goto OnShowPageParametersError
  OnShowPageParameters = False
    
  Dim r         As ProjectData
  Dim c         As Collection
  Dim e         As Collection
  Dim para      As ParameterClass
  Dim vIndex    As Variant
  Dim sVarPre   As String
  Dim sRegPre   As String
  Dim sName     As String
  Dim d         As String
  Dim i         As Integer

  vIndex = ps.Parameters.v("x")   
  sVarPre = "ADC" & vIndex
  sRegPre = sVarPre & "_"

  Set r = ps.ProjectData
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection
  Set para = New ParameterClass


  If (Pagename = "Parameters") Then

    '-------------------------------------------------------------------------
    ' page: Parameters (action)
    '-------------------------------------------------------------------------
    RefreshParameterDescriptions(vIndex, para, r)

    If (Not UpdateParameter(sVarPre, ps, para , Controlname, v)) Then Exit Function


    '-------------------------------------------------------------------------
    ' page: Parameters (show)
    '-------------------------------------------------------------------------

    Set para = New ParameterClass
    RefreshParameterDescriptions(vIndex, para, r)

'    sName = sIndex & "_PARA_T2" 
'    ' disable condition
'    para.Disabled(sName) = True
'    ' tooltip if control is disabled
'    d = FormatDisabledTooltip("Fred ...")
'    para.DisabledToolTip(sName) = d

    If (Not ShowParameter(sVarPre, ps, para, v)) Then Exit Function

  End If         ' if Pagename

  OnShowPageParameters = True
  Exit Function

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





