Attribute VB_Name = "Module1"
'#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.inc"
Option Explicit
'*****************************************************************************
' Module:
'        RTC
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions which are create controls for the
'        module 'RTC'.
'        Furthermore it contains functions for the event handling of
'        these controls.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'             ahe            25.01.00    final version for C165 UTAH
'             ste            28.02.00    many changes for the TC1775
'        2.1  ste            09.03.00    new handling for pages "Functions" and
'                                        "Parameters" added
'        ----------------------------------------------------------------------
'        4.0  ahe            24.04.01    final version Pegasus
'
'        4.1  ahe            08.08.02    - neu interrupt pages
'        ----------------------------------------------------------------------
'             ste            13.10.03    the XC164 uses the Main OSC as RTC
'                                        input clock
'
'*****************************************************************************


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

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

    '-------------------------------------------------------------------------
    ' page: CNT0-1
    '-------------------------------------------------------------------------
    If (Not OnLoadPageCNTx(ps, 0)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: CNT2-3
    '-------------------------------------------------------------------------
    If (Not OnLoadPageCNTx(ps, 2)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: InterruptControl
    '-------------------------------------------------------------------------
    If (Not OnLoadPageInterruptControl(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, "RTC\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

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

    '-------------------------------------------------------------------------
    'page: T14
    '-------------------------------------------------------------------------
    If (Not OnChangingPageT14(ps, Pagename)) Then Exit Function

    '-------------------------------------------------------------------------
    'page: CNT0-1
    '-------------------------------------------------------------------------
    If (Not OnChangingPageCNTx(ps, Pagename, 0)) Then Exit Function

    '-------------------------------------------------------------------------
    'page: CNT2-3
    '-------------------------------------------------------------------------
    If (Not OnChangingPageCNTx(ps, Pagename, 2)) Then Exit Function

    '-------------------------------------------------------------------------
    'page: InterruptControl
    '-------------------------------------------------------------------------
    If (Not OnChangingPageInterruptControl(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, "RTC")) 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

    Dim sVarPre As String
    sVarPre = "RTC"

    OnShow = False

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

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

    '-------------------------------------------------------------------------
    ' page: CNT0-1
    '-------------------------------------------------------------------------
    If (Not OnShowPageCNTx(ps, Controlname, Pagename, v, 0)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: CNT2-3
    '-------------------------------------------------------------------------
    If (Not OnShowPageCNTx(ps, Controlname, Pagename, v, 2)) Then Exit Function

    '-------------------------------------------------------------------------
    ' page: InterruptControl
    '-------------------------------------------------------------------------
    If (Not OnShowPageInterruptControl(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, "RTC", 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

    OnClick = True
    Exit Function

OnClickError:
    Errorbox "Can not process the click", 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, "RTC\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 '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
  Dim sVarPre As String
  Dim vIndex  As Variant

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

  OnChangingPageControl = False
  
  If (Not ps.ExtenderCollection("Control").Loaded) And (Pagename = "Control") Then
    ps.ExtenderCollection("Control").Loaded = True
  
    '-------------------------------
    ' frame: frmOSC
    '-------------------------------
    If (Not ps.AddFrame("frmOSC", 100, 100, 4150, 1000, _
      sVarPre & " Input Frequency")) Then Exit Function
        If (Not ps.AddLabel("txtOSC", 1, 2700, 400, 1100, 315)) Then Exit Function
        If (Not ps.AddLabel("lblOSC", 0, 200, 450, 2300, 300, _
          sVarPre & " input frequency [kHz]")) Then Exit Function

    '-------------------------------
    ' frame: frmPRE
    '-------------------------------
    If (Not ps.AddFrame("frmPRE", 4400, 100, 4200, 1000, _
      sVarPre & " Input Clock Prescaler")) Then Exit Function
        If (Not ps.AddCheck("chkPRE", 200, 400, 3900, 300, _
          "Divide the " & sVarPre & " counting clock by factor 8 (PRE)")) Then Exit Function

    '-------------------------------
    ' frame: frmClock
    '-------------------------------
    If (Not ps.AddFrame("frmClock", 100, 1200, 4150, 1100, "Input Clock")) Then Exit Function
        If (Not ps.AddLabel("txtClock", 1, 2700, 400, 1100, 315, "")) Then Exit Function
        If (Not ps.AddLabel("lblClock", 0, 200, 400, 2300, 400, _
          "Resulting input clock of the " & sVarPre & " module [kHz]")) Then Exit Function

    '-------------------------------
    ' frame: frmRUN
    '-------------------------------
    If (Not ps.AddFrame("frmRUN", 4400, 1200, 4200, 1100, "Run Control")) Then Exit Function
        If (Not ps.AddCheck("chkRUN", 200, 450, 3200, 300, _
          "Start counting after initialization (RUN)")) Then Exit Function

    '-------------------------------
    ' frame: frmRTCACCEN
    '-------------------------------
    If (Not ps.AddFrame("frmRTCCM", 100, 2450, 4200, 1200, _
      "Mode Selection (RTCCM)")) Then Exit Function
        If (Not ps.AddOption("optRTCCM_0", 200, 300, 3950, 300, _
          "RTC is operating in synchronous mode")) Then Exit Function
        If (Not ps.AddOption("optRTCCM_1", 200, 700, 3950, 300, _
          "RTC is operating in asynchronous mode")) 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 sRegPre   As String
  Dim sVarPre   As String
  Dim vIndex    As Variant

  Dim dHelp     As Double
  Dim Overflow  As UnitType
  Dim sHelp     As String
  Dim dDivider  As Double
  Dim bDisabled As Boolean
  Dim i         As Integer

  vIndex = ps.Parameters.v("x")
  sVarPre = "RTC" & 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: txtOSC (action)
        '---------------------------------------------------------------------
        Case "txtOSC"
               If (IsInputFormatOK(v(1), "F", c("lblOSC").Caption, 1, 40000)) Then
                  r.d(sVarPre & "_OSC") = CDbl(v(1)) / 1000  ' save in MHz
                  Call UpdateRTC(r, vIndex)
               Else
                   Exit Function
               End If

        '---------------------------------------------------------------------
        ' control: chkPRE (action)
        '---------------------------------------------------------------------
        Case "chkPRE"
             r.v(sRegPre & "CON_PRE") = v(1)
             Call UpdateRTC(r, vIndex)

        '---------------------------------------------------------------------
        ' control: chkRUN (action)
        '---------------------------------------------------------------------
        Case "chkRUN"
             r.v(sRegPre & "CON_RUN") = v(1)

        '---------------------------------------------------------------------
        ' control: optRTCCM_0 (action)
        '---------------------------------------------------------------------
        Case "optRTCCM_0"
             r.v("SYSCON0_RTCCM") = 0

        '---------------------------------------------------------------------
        ' control: optRTCCM_1 (action)
        '---------------------------------------------------------------------
        Case "optRTCCM_1"
             r.v("SYSCON0_RTCCM") = 1

      Case Else
    End Select

    '-------------------------------------------------------------------------
    ' page: Control (show)
    '-------------------------------------------------------------------------

    '-------------------------------------------------------------------------
    ' control: txtOSC (show)
    '-------------------------------------------------------------------------
    With c("txtOSC")
      bDisabled = IsModuleClockDisabled(r, vIndex)
      .Enabled = Not (bDisabled)
      If (GetRTC_RefClk_Implementation(r, vIndex) = 1) Then        ' for XC164
        .Caption = Format((r.d("MAIN_OSC") / 32) * 1000, "0.000")  ' show kHz
      ElseIf (GetRTC_RefClk_Implementation(r, vIndex) = 2) Then    ' for XC167, XC161
        .Caption = Format(r.d(sVarPre & "_OSC") * 1000, "0.000")   ' show kHz
      End If
    End With

    With e("txtOSC")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
      Else
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: lblOSC (show)
    '-------------------------------------------------------------------------
    With c("lblOSC")
      ' same as for txtOSC
      .Enabled = Not (bDisabled)
    End With

    With e("lblOSC")
      If (bDisabled) Then
        .DisabledTooltipText = e("txtOSC").DisabledTooltipText
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: chkPRE (show)
    '-------------------------------------------------------------------------
    With c("chkPRE")
      bDisabled = IsModuleClockDisabled(r, vIndex)
      .Enabled = Not (bDisabled)
      .Value = -CInt(r.v(sRegPre & "CON_PRE") = 1)
    End With
  
    With e("chkPRE")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: txtClock (show)
    '-------------------------------------------------------------------------
    With c("txtClock")
      bDisabled = IsModuleClockDisabled(r, vIndex)
      .Enabled = Not (bDisabled)
      .Caption = Format(GetRTCModuleClock(r, vIndex) * 1000, "0.000")  ' show kHz
    End With

    With e("txtClock")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: lblClock (show)
    '-------------------------------------------------------------------------
    With c("lblClock")
      ' samr as for txtClock
      .Enabled = Not (bDisabled)
    End With

    With e("lblClock")
      If (bDisabled) Then
        .DisabledTooltipText = e("txtClock").DisabledTooltipText
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: chkRUN (show)
    '-------------------------------------------------------------------------
    With c("chkRUN")
      bDisabled = IsModuleClockDisabled(r, vIndex)
      .Enabled = Not (bDisabled)
      .Value = -CInt(r.v(sRegPre & "CON_RUN") = 1)
    End With
  
    With e("chkRUN")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: optRTCCM_0 (show)   async. mode
    '-------------------------------------------------------------------------
    With c("optRTCCM_0")
      bDisabled = IsModuleClockDisabled(r, vIndex)
      .Enabled = Not (bDisabled)
      .Value = (r.v("SYSCON0_RTCCM") = 0)
    End With
  
    With e("optRTCCM_0")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: optRTCCM_1 (show)   sync. mode
    '-------------------------------------------------------------------------
    With c("optRTCCM_1")
      bDisabled = True
      .Enabled = Not (bDisabled)
      .Value = (r.v("SYSCON0_RTCCM") = 1)
    End With
  
    With e("optRTCCM_1")
      If (bDisabled) Then
        .DisabledTooltipText = _
          FormatNormalTooltip("The asynchronous mode can only be selected via function " & _
          r.v(sVarPre & "_NAME_SetAsynchronousMode") & ".")
      End If
    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 OnLoad function of the associated
'        module in order to add the page 'Control'.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageT14(ps As Object) As Boolean
  On Error Resume Next

  OnLoadPageT14 = False

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

  OnLoadPageT14 = 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 OnChangingPageT14(ps As Object, Pagename As String) As Boolean
  On Error Resume Next
  Dim sVarPre As String
  Dim vIndex  As Variant

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

  OnChangingPageT14 = False
  
  If (Not ps.ExtenderCollection("T14").Loaded) And (Pagename = "T14") Then
    ps.ExtenderCollection("T14").Loaded = True

    '-------------------------------
    ' frame: frmT14
    '-------------------------------
    If (Not ps.AddFrame("frmT14", 100, 100, 8450, 2100, sVarPre & " Time Base")) Then Exit Function
        If (Not ps.AddText("txtReqTime", 2500, 350, 1000, 315)) Then Exit Function
        If (Not ps.AddLabel("lblReqTime", 0, 200, 350, 1725, 385, "")) Then Exit Function
        If (Not ps.AddLabel("lbl_box_RealTime", 1, 7200, 350, 1000, 300, "")) Then Exit Function
        If (Not ps.AddLabel("lbl_txt_RealTime", 0, 4500, 350, 1500, 385, "")) Then Exit Function
        If (Not ps.AddText("txtT14REL", 7200, 950, 1000, 315)) Then Exit Function
        If (Not ps.AddLabel("lblT14REL", 0, 4500, 950, 1500, 385, "Reload value (RTC_T14REL)")) Then Exit Function
        If (Not ps.AddLabel("lbl_box_MinTime", 1, 2500, 950, 1000, 300, "")) Then Exit Function
        If (Not ps.AddLabel("lbl_txt_MinTime", 0, 200, 1000, 1725, 300, "")) Then Exit Function
        If (Not ps.AddLabel("lbl_box_MaxTime", 1, 2500, 1550, 1000, 300, "")) Then Exit Function
        If (Not ps.AddLabel("lbl_txt_MaxTime", 0, 200, 1600, 1725, 300, "")) Then Exit Function

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

  OnShowPageT14 = False

  Dim r         As ProjectData
  Dim c         As Object
  Dim e         As Object
  Dim sRegPre   As String
  Dim sVarPre   As String
  Dim dHelp     As Double
  Dim Overflow  As UnitType
  Dim sHelp     As String
  Dim dDivider  As Double
  Dim bDisabled As Boolean
  Dim vIndex    As Variant

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

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

  If (Pagename = "T14") Then
    '-------------------------------------------------------------------------
    ' page: Control (action)
    '-------------------------------------------------------------------------
    Select Case (Controlname)
        '---------------------------------------------------------------------
        ' control: txtReqTime (action)
        '---------------------------------------------------------------------
        Case "txtReqTime"
             Overflow = ConvertTime(r.v(sVarPre & "_Base"))
             sHelp = Overflow.sUnit
             dDivider = Overflow.dDivider
             If (IsInputFormatOK(v(1), "F", c("lblReqTime").Caption, Format(GetMinTimeBase(r, vIndex) / dDivider, "0.0000"), _
                 Format(GetMaxTimeBase(r, vIndex) / dDivider, "0.0000"))) Then
               r.v(sVarPre & "_Base") = (v(1) * dDivider)   'us
               Call UpdateRTC(r, vIndex)
             Else
                 Exit Function
             End If

        '---------------------------------------------------------------------
        ' control: txtT14REL (action)
        '---------------------------------------------------------------------
        Case "txtT14REL"
             If (IsInputFormatOK(v(1), "H4", c("lblT14REL").Caption, 0, 65535)) Then
               r.sfr(sRegPre & "T14REL") = v(1)
               r.v(sVarPre & "_Base") = GetRealTimeBase(r, vIndex)
               Call UpdateRTC(r, vIndex)
             Else
                 Exit Function
             End If

      Case Else
    End Select

    '-------------------------------------------------------------------------
    ' page: Control (show)
    '-------------------------------------------------------------------------

    '-------------------------------------------------------------------------
    ' control: txtReqTime (show)
    '-------------------------------------------------------------------------
    With c("txtReqTime")
      bDisabled = IsModuleClockDisabled(r, vIndex)
      .Enabled = Not (bDisabled)
      dHelp = r.v(sVarPre & "_Base")
      Overflow = ConvertTime(dHelp)
      .Text = Format(Overflow.dValue, "0.0000")
      c("lblReqTime").Caption = "Required time base for the real time clock [" & Overflow.sUnit & "]"
    End With

    With e("txtReqTime")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: lblReqTimel (show)
    '-------------------------------------------------------------------------
    With c("lblReqTime")
      ' same as for txtReqTime
      .Enabled = Not (bDisabled)
    End With

    With e("lblReqTime")
      If (bDisabled) Then
        .DisabledTooltipText = e("txtReqTime").DisabledTooltipText
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: lbl_box_RealTime (show)
    '-------------------------------------------------------------------------
    With c("lbl_box_RealTime")
      bDisabled = IsModuleClockDisabled(r, vIndex)
      .Enabled = Not (bDisabled)
      dHelp = GetRealTimeBase(r, vIndex)
      Overflow = ConvertTime(dHelp)
      .Caption = Format(Overflow.dValue, "0.0000")
      c("lbl_txt_RealTime").Caption = "Real time base [" & Overflow.sUnit & "]  (Interrupt period)"
    End With

    With e("lbl_box_RealTime")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: lbl_txt_RealTime (show)
    '-------------------------------------------------------------------------
    With c("lbl_txt_RealTime")
      ' same as for lbl_box_RealTime
      .Enabled = Not (bDisabled)
    End With

    With e("lbl_txt_RealTime")
      If (bDisabled) Then
        .DisabledTooltipText = e("lbl_box_RealTime").DisabledTooltipText
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: txtT14REL (show)
    '-------------------------------------------------------------------------
    With c("txtT14REL")
      bDisabled = IsModuleClockDisabled(r, vIndex)
      .Enabled = Not (bDisabled)
      .Text = FormatTextOutput(r.sfr(sRegPre & "T14REL"), "H4")
    End With

    With e("txtT14REL")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: lblT14REL (show)
    '-------------------------------------------------------------------------
    With c("lblT14REL")
      ' same as for txtT14REL
      .Enabled = Not (bDisabled)
    End With

    With e("lblT14REL")
      If (bDisabled) Then
        .DisabledTooltipText = e("txtT14REL").DisabledTooltipText
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: lbl_box_MinTime (show)
    '-------------------------------------------------------------------------
    With c("lbl_box_MinTime")
      bDisabled = IsModuleClockDisabled(r, vIndex)
      .Enabled = Not (bDisabled)
      dHelp = GetMinTimeBase(r, vIndex)
      Overflow = ConvertTime(dHelp)
      .Caption = Format(Overflow.dValue, "0.0000")
      c("lbl_txt_MinTime").Caption = "Min. time base [" & Overflow.sUnit & "]"
    End With

    With e("lbl_box_MinTime")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: lbl_txt_MinTime (show)
    '-------------------------------------------------------------------------
    With c("lbl_txt_MinTime")
      ' same as for lbl_box_MinTime
      .Enabled = Not (bDisabled)
    End With

    With e("lbl_txt_MinTime")
      If (bDisabled) Then
        .DisabledTooltipText = e("lbl_box_MinTime").DisabledTooltipText
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: lbl_box_MaxTime (show)
    '-------------------------------------------------------------------------
    With c("lbl_box_MaxTime")
      bDisabled = IsModuleClockDisabled(r, vIndex)
      .Enabled = Not (bDisabled)
      dHelp = GetMaxTimeBase(r, vIndex)
      Overflow = ConvertTime(dHelp)
      .Caption = Format(Overflow.dValue, "0.0000")
      c("lbl_txt_MaxTime").Caption = "Max. time base [" & Overflow.sUnit & "]"
    End With

    With e("lbl_box_MaxTime")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
      End If
    End With

    '-------------------------------------------------------------------------
    ' control: lbl_txt_MaxTime (show)
    '-------------------------------------------------------------------------
    With c("lbl_txt_MaxTime")
      ' same as for lbl_box_MaxTime
      .Enabled = Not (bDisabled)
    End With

    With e("lbl_txt_MaxTime")
      If (bDisabled) Then
        .DisabledTooltipText = e("lbl_box_MaxTime").DisabledTooltipText
      End If
    End With


  End If

  OnShowPageT14 = True
  Exit Function

OnShowPageT14Error:
  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 'RTCL'.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'        iNumber          number of the first CNTINT (0, 2)
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageCNTx(ps As Object, iNumber As Integer) As Boolean
  On Error Resume Next

  OnLoadPageCNTx = False
  Dim sL_or_H As String

  If (iNumber = 0) Then
      sL_or_H = "L"
  ElseIf (iNumber = 2) Then
      sL_or_H = "H"
  End If

  If (Not ps.AddPage("RTC" & sL_or_H)) Then Exit Function

  OnLoadPageCNTx = 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
'        iNumber          number of the first CNTINT (0, 2)
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function OnChangingPageCNTx(ps As Object, Pagename As String, iNumber As Integer) As Boolean
  On Error Resume Next
  Dim sVarPre       As String
  Dim sRegPre       As String
  Dim vIndex        As Variant
  Dim i             As Integer
  Dim iTopPosition  As Integer
  Dim sCurrentBits  As String
  Dim sL_or_H       As String

  If (iNumber = 0) Then
      sL_or_H = "L"
  ElseIf (iNumber = 2) Then
      sL_or_H = "H"
  End If

  OnChangingPageCNTx = False

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

  If (Not ps.ExtenderCollection("RTC" & sL_or_H).Loaded) And (Pagename = "RTC" & sL_or_H) Then
    ps.ExtenderCollection("RTC" & sL_or_H).Loaded = True
  
    For i = iNumber To iNumber + 1
      '-------------------------------
      ' frame: frmRTCx
      '-------------------------------
      Select Case i
          Case 0
              iTopPosition = 100
              sCurrentBits = "[9-0]"
          Case 1
              iTopPosition = 2300
              sCurrentBits = "[15-10]"
          Case 2
              iTopPosition = 100
              sCurrentBits = "[5-0]"
          Case 3
              iTopPosition = 2300
              sCurrentBits = "[15-6]"
      End Select

      If (Not ps.AddFrame("frmRTC" & i, 100, iTopPosition, 8450, 2100, _
        "Overflow Generation (CNTINT" & i & ")")) Then Exit Function
          If (Not ps.AddText("txtReqTime_RTC" & i, 2500, 350, 1000, 315)) Then Exit Function
          If (Not ps.AddLabel("lblReqTime_RTC" & i, 0, 200, 400, 2250, 310, "")) Then Exit Function
          If (Not ps.AddLabel("lbl_box_RealOv_RTC" & i, 1, 7200, 350, 1000, 300, "")) Then Exit Function
          If (Not ps.AddLabel("lbl_txt_RealOv_RTC" & i, 0, 4500, 350, 1775, 395, "")) Then Exit Function
          If (Not ps.AddText("txtRTC" & i, 7200, 950, 1000, 315)) Then Exit Function
          If (Not ps.AddLabel("lblRTC" & i, 0, 4500, 950, 1850, 410, _
            "Reload value                    (" & sRegPre & "REL" & sL_or_H & " " & sCurrentBits & ")")) Then Exit Function
          If (Not ps.AddLabel("lbl_box_MinTime_RTC" & i, 1, 2500, 950, 1000, 300, "")) Then Exit Function
          If (Not ps.AddLabel("lbl_txt_MinTime_RTC" & i, 0, 200, 1000, 1725, 310, "")) Then Exit Function
          If (Not ps.AddLabel("lbl_box_MaxTime_RTC" & i, 1, 2500, 1550, 1000, 300, "")) Then Exit Function
          If (Not ps.AddLabel("lbl_txt_MaxTime_RTC" & i, 0, 200, 1600, 1725, 310, "")) Then Exit Function
    Next i

  End If

  OnChangingPageCNTx = 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
'        iNumber         number of the first CNTINT (0, 2)
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnShowPageCNTx(ps As Object, Controlname As String, Pagename As String, v() As Variant, iNumber As Integer) As Boolean
  On Error GoTo OnShowPageCNTxError

  OnShowPageCNTx = False

  Dim r         As ProjectData
  Dim c         As Object
  Dim e         As Object
  Dim sRegPre   As String
  Dim sVarPre   As String
  Dim dHelp     As Double
  Dim Overflow  As UnitType
  Dim sHelp     As String
  Dim dDivider  As Double
  Dim dValue    As Double
  Dim bDisabled As Boolean
  Dim vIndex    As Variant
  Dim sL_or_H   As String

  If (iNumber = 0) Then
      sL_or_H = "L"
  ElseIf (iNumber = 2) Then
      sL_or_H = "H"
  End If

  Dim i As Integer
  Dim lMaxValue As Long

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

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

  If (Pagename = "RTC" & sL_or_H) Then
    '-------------------------------------------------------------------------
    ' page: CNTx-y (action)
    '-------------------------------------------------------------------------

    For i = iNumber To iNumber + 1
      '---------------------------------------------------------------------
      ' control: txtReqTime_RTCx (action)  (x = 0-3)
      '---------------------------------------------------------------------
      If (Controlname = "txtReqTime_RTC" & i) Then
        Overflow = ConvertTime(r.v(sVarPre & "_Time" & i))
        sHelp = Overflow.sUnit
        dDivider = Overflow.dDivider
        If (IsInputFormatOK(v(1), "F", c("lblReqTime_RTC" & i), Format(GetMinTime(r, vIndex, i) / dDivider, "0.0000"), _
            Format(GetMaxTime(r, vIndex, i) / dDivider, "0.0000"))) Then
          r.v(sVarPre & "_Time" & i) = (v(1) * dDivider)
          Call UpdateRTC(r, vIndex)
        Else
          Exit Function
        End If

      End If

      '---------------------------------------------------------------------
      ' control: txtRTCx (action)
      '---------------------------------------------------------------------
      If (Controlname = "txtRTC" & i) Then
        If (i = 0 Or i = 3) Then
          lMaxValue = &H3FF
        Else
          lMaxValue = &H3F
        End If
        If (IsInputFormatOK(v(1), "H3", c("lblRTC" & i), 0, lMaxValue)) Then
          r.l(sVarPre & "_RTC" & i) = v(1)
          r.v(sVarPre & "_Time" & i) = GetRealTime(r, vIndex, i)
          Call UpdateRTC(r, vIndex)
        Else
          Exit Function
        End If
      End If
    Next i

    '-------------------------------------------------------------------------
    ' page: CNTx-y (show)
    '-------------------------------------------------------------------------

    For i = iNumber To iNumber + 1
      '-------------------------------------------------------------------------
      ' control: txtReqTime_RTCx (show) (x = 0,3)
      '-------------------------------------------------------------------------
      With c("txtReqTime_RTC" & i)
        bDisabled = IsModuleClockDisabled(r, vIndex)
        .Enabled = Not (bDisabled)
        dHelp = r.v(sVarPre & "_Time" & i)
        Overflow = ConvertTime(dHelp)
        .Text = Format(Overflow.dValue, "0.0000")
        c("lblReqTime_RTC" & i).Caption = "Req. overflow time [" & Overflow.sUnit & "]"
      End With

      With e("txtReqTime_RTC" & i)
        If (bDisabled) Then
          .DisabledTooltipText = TOOLTIP_DISABLED_IF
          .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
        End If
      End With

      '-------------------------------------------------------------------------
      ' control: lblReqTime_RTCx (show) (x = 0,3)
      '-------------------------------------------------------------------------
      With c("lblReqTime_RTC" & i)
        ' same as for txtReqTime_RTCx
        .Enabled = Not (bDisabled)
      End With

      With e("lblReqTime_RTC" & i)
        If (bDisabled) Then
          .DisabledTooltipText = e("txtReqTime_RTC" & i).DisabledTooltipText
        End If
      End With

      '-------------------------------------------------------------------------
      ' control: lbl_box_RealOv_RTCx (show) (x = 0, 3)
      '-------------------------------------------------------------------------
      With c("lbl_box_RealOv_RTC" & i)
        bDisabled = IsModuleClockDisabled(r, vIndex)
        .Enabled = Not (bDisabled)
        dHelp = GetRealTime(r, vIndex, i)
        Overflow = ConvertTime(dHelp)
        .Caption = Format(Overflow.dValue, "0.0000")
        c("lbl_txt_RealOv_RTC" & i).Caption = "Real overflow time [" & Overflow.sUnit & "] (Interrupt period)"
      End With

      With e("lbl_box_RealOv_RTC" & i)
        If (bDisabled) Then
          .DisabledTooltipText = TOOLTIP_DISABLED_IF
          .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
        End If
      End With

      '-------------------------------------------------------------------------
      ' control: lbl_txt_RealOv_RTCx (show) (x = 0, 3)
      '-------------------------------------------------------------------------
      With c("lbl_txt_RealOv_RTC" & i)
        ' same as for lbl_box_RealOv_RTCx
        .Enabled = Not (bDisabled)
      End With

      With e("lbl_txt_RealOv_RTC" & i)
        If (bDisabled) Then
          .DisabledTooltipText = e("lbl_box_RealOv_RTC" & i).DisabledTooltipText
        End If
      End With

      '-------------------------------------------------------------------------
      ' control: txtRTCx (show)
      '-------------------------------------------------------------------------
      With c("txtRTC" & i)
        bDisabled = IsModuleClockDisabled(r, vIndex)
        .Enabled = Not (bDisabled)
        If (i = 0 Or i = 3) Then
          .Text = FormatTextOutput(r.v(sVarPre & "_RTC" & i), "H3")
        Else
          .Text = FormatTextOutput(r.v(sVarPre & "_RTC" & i), "H2")
        End If
      End With

      With e("txtRTC" & i)
        If (bDisabled) Then
          .DisabledTooltipText = TOOLTIP_DISABLED_IF
          .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
        End If
      End With

      '-------------------------------------------------------------------------
      ' control: lblRTCx (show)
      '-------------------------------------------------------------------------
      With c("lblRTC" & i)
        ' same as for txtRTCx
        .Enabled = Not (bDisabled)
      End With

      With e("lblRTC" & i)
        If (bDisabled) Then
          .DisabledTooltipText = e("txtRTC" & i).DisabledTooltipText
        End If
      End With

      '-------------------------------------------------------------------------
      ' control: lbl_box_MinTime_RTCx (show)
      '-------------------------------------------------------------------------
      With c("lbl_box_MinTime_RTC" & i)
        bDisabled = IsModuleClockDisabled(r, vIndex)
        .Enabled = Not (bDisabled)
        dHelp = GetMinTime(r, vIndex, i)
        Overflow = ConvertTime(dHelp)
        .Caption = Format(Overflow.dValue, "0.0000")
        c("lbl_txt_MinTime_RTC" & i).Caption = "Min. time [" & Overflow.sUnit & "]"
      End With

      With e("lbl_box_MinTime_RTC" & i)
        If (bDisabled) Then
          .DisabledTooltipText = TOOLTIP_DISABLED_IF
          .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
        End If
      End With

      '-------------------------------------------------------------------------
      ' control: lbl_txt_MinTime_RTCx (show)
      '-------------------------------------------------------------------------
      With c("lbl_txt_MinTime_RTC" & i)
        ' same as for lbl_box_MinTime_RTCx
        .Enabled = Not (bDisabled)
      End With

      With e("lbl_txt_MinTime_RTC" & i)
        If (bDisabled) Then
          .DisabledTooltipText = e("lbl_box_MinTime_RTC" & i).DisabledTooltipText
        End If
      End With

      '-------------------------------------------------------------------------
      ' control: lbl_box_MaxTime_RTCx (show)
      '-------------------------------------------------------------------------
      With c("lbl_box_MaxTime_RTC" & i)
        bDisabled = IsModuleClockDisabled(r, vIndex)
        .Enabled = Not (bDisabled)
        dHelp = GetMaxTime(r, vIndex, i)
        Overflow = ConvertTime(dHelp)
        .Caption = Format(Overflow.dValue, "0.0000")
        c("lbl_txt_MaxTime_RTC" & i).Caption = "Max. time [" & Overflow.sUnit & "]"
      End With

      With e("lbl_box_MaxTime_RTC" & i)
        If (bDisabled) Then
          .DisabledTooltipText = TOOLTIP_DISABLED_IF
          .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
        End If
      End With

      '-------------------------------------------------------------------------
      ' control: lbl_txt_MaxTime_RTCx (show)
      '-------------------------------------------------------------------------
      With c("lbl_txt_MaxTime_RTC" & i)
        ' same as for lbl_box_MaxTime_RTCx
        .Enabled = Not (bDisabled)
      End With

      With e("lbl_txt_MaxTime_RTC" & i)
        If (bDisabled) Then
          .DisabledTooltipText = e("lbl_box_MaxTime_RTC" & i).DisabledTooltipText
        End If
      End With

    Next i

  End If

  OnShowPageCNTx = True
  Exit Function

OnShowPageCNTxError:
  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 'InterruptControl'.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageInterruptControl(ps As Object) As Boolean
  On Error Resume Next

  OnLoadPageInterruptControl = False

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

  OnLoadPageInterruptControl = 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 OnChangingPageInterruptControl(ps As Object, Pagename As String) As Boolean
  On Error Resume Next
  Dim sVarPre As String
  Dim sRegPre As String
  Dim vIndex  As Variant

  OnChangingPageInterruptControl = False

  vIndex = ps.Parameters.v("x")
  sVarPre = "RTC" & vIndex
  sRegPre = sVarPre & "_"
  
  If (Not ps.ExtenderCollection("Interrupt Control").Loaded) And (Pagename = "Interrupt Control") Then
    ps.ExtenderCollection("Interrupt Control").Loaded = True
  
    '-------------------------------
    ' frame: frmRTCISNC
    '-------------------------------
    If (Not ps.AddFrame("frmRTCISNC", 100, 100, 4500, 3000, "Interrupt Sub Node Control")) Then Exit Function
        If (Not ps.AddCheck("chkT14IE", 200, 350, 3700, 300, "Request interrupt on T14 overflow (T14IE)")) Then Exit Function
        If (Not ps.AddCheck("chkCNT0IE", 200, 775, 3700, 300, "Request interrupt on CNT0 overflow (0IE)")) Then Exit Function
        If (Not ps.AddCheck("chkCNT1IE", 200, 1300, 3700, 300, "Request interrupt on CNT1 overflow (1IE)")) Then Exit Function
        If (Not ps.AddCheck("chkCNT2IE", 200, 1825, 3700, 300, "Request interrupt on CNT2 overflow (2IE)")) Then Exit Function
        If (Not ps.AddCheck("chkCNT3IE", 200, 2400, 3700, 300, "Request interrupt on CNT3 overflow (3IE)")) Then Exit Function

    '-------------------------------
    ' frame: frmRTC_IC
    '-------------------------------
    If (Not ps.AddFrame("frmRTC_IC", 100, 3200, 4500, 900, "Interrupt Control")) Then Exit Function
        If (Not ps.AddCheck("chkRTCIE", 200, 350, 2700, 300, "Enable " & sVarPre & " interrupt (IE)")) Then Exit Function

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

  OnShowPageInterruptControl = False

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

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

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

  If (Pagename = "Interrupt Control") Then

    '-------------------------------------------------------------------------
    ' page: InterruptControl (action)
    '-------------------------------------------------------------------------
    For i = 0 To 3
      '---------------------------------------------------------------------
      ' control: chkCNTxIE (action) (x = 0-3)
      '---------------------------------------------------------------------
      If (Controlname = "chkCNT" & i & "IE") Then
        r.v(sRegPre & "ISNC_" & "CNT" & i & "IE") = v(1)
        Call DisableRTCInt(r, vIndex)
        Exit For
      End If
    Next i

    Select Case (Controlname)
        '---------------------------------------------------------------------
        ' control: chkT14IE (action)
        '---------------------------------------------------------------------
        Case "chkT14IE"
             r.v(sRegPre & "ISNC_T14IE") = v(1)
             Call DisableRTCInt(r, vIndex)

        '---------------------------------------------------------------------
        ' control: chkRTCIE (action)
        '---------------------------------------------------------------------
        Case "chkRTCIE"
             If (v(1) = 1) Then
               Call EnableSRN(r, sVarPre, sRegPre & "IC", "RTC")
             Else
               Call DisableSRN(r, sVarPre, sRegPre & "IC", "RTC")
             End If

      Case Else
    End Select

    '-------------------------------------------------------------------------
    ' page: InterruptControl (show)
    '-------------------------------------------------------------------------

    '-------------------------------------------------------------------------
    ' control: chkT14IE (show)
    '-------------------------------------------------------------------------
    With c("chkT14IE")
      bDisabled = IsModuleClockDisabled(r, vIndex)
      .Enabled = Not (bDisabled)
      .Value = -CInt(r.v(sRegPre & "ISNC_T14IE") = 1)
    End With

    With e("chkT14IE")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
      End If
    End With

    For i = 0 To 3
      '-------------------------------------------------------------------------
      ' control: chkCNTxIE (show) (x = 0-3)
      '-------------------------------------------------------------------------
      With c("chkCNT" & i & "IE")
        bDisabled = IsModuleClockDisabled(r, vIndex)
        .Enabled = Not (bDisabled)
        .Value = -CInt(r.v(sRegPre & "ISNC_" & "CNT" & i & "IE") = 1)
      End With

      With e("chkCNT" & i & "IE")
        If (bDisabled) Then
          .DisabledTooltipText = TOOLTIP_DISABLED_IF
          .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
        End If
      End With
    Next i

    '-------------------------------------------------------------------------
    ' control: chkRTCIE (show)
    '-------------------------------------------------------------------------
    With c("chkRTCIE")
      bDisabled = (IsModuleClockDisabled(r, vIndex) Or IsSubNodeIntDisabled(r, vIndex))
      .Enabled = Not (bDisabled)
      .Value = -CInt(r.v(sRegPre & "IC_IE") = 1)
    End With

    With e("chkRTCIE")
      If (bDisabled) Then
        .DisabledTooltipText = TOOLTIP_DISABLED_IF
        .DisabledTooltipText = .DisabledTooltipText & GetTooltipModuleClockDisabled(r, vIndex)
        If (IsSubNodeIntDisabled(r, vIndex)) Then
          .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("no sub node interrupt is selected")
        End If
      End If
    End With

  End If

  OnShowPageInterruptControl = True
  Exit Function

OnShowPageInterruptControlError:
  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 vIndex  As Variant
  Dim sVarPre As String
  Dim n       As String
  Dim i       As Integer
  Dim aFunctions(1 To 16) As String

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


  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, 275, 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) = "RTCRun"
    i = i + 1
    aFunctions(i) = "RTCStop"
    i = i + 1
    aFunctions(i) = "DecrementT14"
    i = i + 1
    aFunctions(i) = "IncrementT14"
    i = i + 1
    aFunctions(i) = "RTC"
    i = i + 1
    aFunctions(i) = "SetTime"
    i = i + 1
    aFunctions(i) = "GetTime"
    i = i + 1
    aFunctions(i) = "Reset"
    i = i + 1
    aFunctions(i) = "SetAsynchronousMode"
    i = i + 1
    aFunctions(i) = "SetSynchronousMode"

    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

  vIndex = ps.Parameters.v("x")
  sVarPre = "RTC" & 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) = IsModuleClockDisabled(r, vIndex)
    ' tooltip if control is disabled
    d = GetDisabledFunctionToolTipEntry(func, sName)
    d = d & GetTooltipModuleClockDisabled(r, vIndex)
    func.DisabledToolTip(sName) = d

    sName = sVarPre & "_NAME_RTC"
    ' 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 & "IC", "RTC")

    sName = sVarPre & "_NAME_RTCRun"
    ' 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

    sName = sVarPre & "_NAME_RTCStop"
    ' 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

    sName = sVarPre & "_NAME_DecrementT14"
    ' 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

    sName = sVarPre & "_NAME_IncrementT14"
    ' 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

    sName = sVarPre & "_NAME_SetTime"
    ' 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

    sName = sVarPre & "_NAME_GetTime"
    ' 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

    sName = sVarPre & "_NAME_Reset"
    ' 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

    sName = sVarPre & "_NAME_SetAsynchronousMode"
    ' 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

    sName = sVarPre & "_NAME_SetSynchronousMode"
    ' 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


    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
  Dim vIndex  As Variant
  Dim sVarPre As String
  Dim i       As Integer
  Dim aParameters(1 To 16) As String

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


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

    '-------------------------------
    ' frame: fra_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, 275, 1250, 275, 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) = "Para1"                                                                                  */
'     i = i + 1                                                                                                 */
'     aParameters(i) = "Para2"                                                                                  */
'                                                                                                               */
'     If (Not ps.AddFrame("fraParameters1", 100 , 900, 4150, 4400, 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, 300 * i, 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 = "RTC" & 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
