Attribute VB_Name = "Module1"
'#Uses "..\classes\FunctionClass.cls
'#Uses "..\classes\PortPin.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:
'        T21
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions which are create controls for the
'        module 'T21'.
'        Furthermore it contains functions for the event handling of
'        these controls.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        0.1  Sunil          18-01-06    Final version
'
'
'*****************************************************************************

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

    OnLoad = False

    If (Not OnLoadPageTimer21(ps)) Then Exit Function
    If (Not OnLoadPageT2EX(ps)) Then Exit Function
    If (Not OnLoadPageInterrupts(ps)) Then Exit Function
    If (Not OnLoadPageFunctions(ps)) Then Exit Function
    If (Not OnLoadPageParameters(ps)) Then Exit Function
    If (Not OnLoadPageNotes(ps)) Then Exit Function
    
    
    ' call function to initialize the menu
    If (Not InitMenu(ps, "T21\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 successful operation
'        False           else
'*****************************************************************************
Function OnChangingPage(ps As Peripherals, Page As Integer, Pagename As String) As Boolean
    On Error GoTo OnChangingPageError

    OnChangingPage = False
    
    If (Not OnChangingPageTimer21(ps, Pagename)) Then Exit Function
    If (Not OnChangingPageT2EX(ps, Pagename)) Then Exit Function
    If (Not OnChangingPageInterrupts(ps, Pagename)) Then Exit Function
    If (Not OnChangingPageFunctions(ps, Pagename)) Then Exit Function
    If (Not OnChangingPageParameters(ps, Pagename)) Then Exit Function
    If (Not OnChangingPageNotes(ps, Pagename, "T21")) 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 successful operation
'        False           else
'*****************************************************************************
Function OnShow(ps As Peripherals, Controlname As String, v() As Variant, Page As Integer, Pagename As String) As Boolean
    On Error Resume Next

    OnShow = False

    If (Not OnShowPageTimer21(ps, Controlname, Pagename, v)) Then Exit Function
    If (Not OnShowPageT2EX(ps, Controlname, Pagename, v)) Then Exit Function
    If (Not OnShowPageInterrupts(ps, Controlname, Pagename, v)) Then Exit Function
    If (Not OnShowPageFunctions(ps, Controlname, Pagename, v)) Then Exit Function
    If (Not OnShowPageParameters(ps, Controlname, Pagename, v)) Then Exit Function
    If (Not OnShowPageNotes(ps, Controlname, Pagename, "T21", v)) Then Exit Function

    OnShow = True
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 successful operation
'        False           else
'*****************************************************************************
Function OnClick(ps As Peripherals, Controlname As String) As Boolean
    On Error GoTo OnClickError

    OnClick = False
    ' no buttons implemented
    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 successful 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, "T21\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 a page.
'-----------------------------------------------------------------------------
'Parameters:
'        ps               reference to the Peripherals object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageTimer21(ps As Peripherals) As Boolean
  On Error Resume Next
  OnLoadPageTimer21 = False

  If (Not ps.AddPage("Timer 21")) Then Exit Function

  OnLoadPageTimer21 = 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 successful operation
'        False           else
'*****************************************************************************
Public Function OnChangingPageTimer21(ps As Peripherals, Pagename As String) As Boolean
  On Error Resume Next

  OnChangingPageTimer21 = False

  If (Not ps.ExtenderCollection("Timer 21").Loaded) And (Pagename = "Timer 21") Then
    ps.ExtenderCollection("Timer 21").Loaded = True
    
    '---------------------------------------------------------------
    ' Frame : fraMoDisable
    '---------------------------------------------------------------
    If (Not ps.AddFrame("fraMoDisable", 100, 100, 4450, 1100, "Global")) Then Exit Function
        If (Not ps.AddOption("OptEnable", 200, 250, 2000, 350, "Enable Timer21 module")) Then Exit Function
        If (Not ps.AddOption("OptDisable", 200, 600, 2000, 350, "Disable Timer21 module")) Then Exit Function
    
    '---------------------------------------------------------------
    ' Frame : fraC/T2
    '---------------------------------------------------------------
    If (Not ps.AddFrame("fraC/T2", 100, 1300, 4450, 1000, "Timer / Counter Select (C/T2)")) Then Exit Function
        If (Not ps.AddOption("optC/T2_0_timer", 200, 250, 1975, 380, "16-bit timer function")) Then Exit Function
        If (Not ps.AddOption("optC/T2_1_counter", 200, 600, 1975, 380, "16-bit counter function")) Then Exit Function
    
    
    If (Not ps.AddFrame("fraCountPin", 100, 2350, 4450, 900, "T21 Input Select")) Then Exit Function
        If (Not ps.AddOption("optT21_0", 200, 200, 1975, 380, "Select T21_0 (P4.2)")) Then Exit Function
        If (Not ps.AddOption("optT21_1", 200, 500, 1975, 380, "Select T21_1 (P1.6)")) Then Exit Function
    
    '---------------------------------------------------------------
    ' Frame : fraCP/RL2
    '---------------------------------------------------------------
    If (Not ps.AddFrame("fraCP/RL2", 100, 3300, 4450, 1050, "Capture / Reload Select (CP/RL2)")) Then Exit Function
        If (Not ps.AddOption("optCP/RL2_0_reload", 200, 250, 3300, 380, "Automatic reload when timer 21 overflows")) Then Exit Function
        If (Not ps.AddOption("optCP/RL2_1_capture", 200, 600, 3300, 380, "16-bit capture mode (normal timer/counter)")) Then Exit Function
    
    '---------------------------------------------------------------
    ' Frame : fraPreScaler
    '---------------------------------------------------------------
    If (Not ps.AddFrame("fraPreScaler", 4725, 100, 3850, 1100, " Prescaler Options")) Then Exit Function
        If (Not ps.AddCheck("chkPreScaleEnable", 200, 295, 2100, 380, "Enable Prescaler (PREN)")) Then Exit Function
        If (Not ps.AddLabel("lblPreScale", 0, 400, 700, 1200, 315, "Prescaler (PRE)")) Then Exit Function
        If (Not ps.AddCombo("cboPreScale", 1600, 650, 1500, 380)) Then Exit Function
        With ps.ControlCollection("cboPreScale")
            .AddItem "fPCLK"
            .AddItem "fPCLK/2"
            .AddItem "fPCLK/4"
            .AddItem "fPCLK/8"
            .AddItem "fPCLK/16"
        End With
        
    '---------------------------------------------------------------
    ' Frame : fraT2Registers
    '---------------------------------------------------------------
    If (Not ps.AddFrame("fraT2Registers", 4725, 1300, 3850, 1650, "Timer / Counter Register")) Then Exit Function
        If (Not ps.AddText("txtT21x", 2500, 220, 1150, 315)) Then Exit Function
        If (Not ps.AddLabel("lblT21x", 0, 200, 230, 2200, 380, "Timer register (T2H,T2L)")) Then Exit Function
    
        If (Not ps.AddText("txtRC2x", 2500, 540, 1150, 315)) Then Exit Function
        If (Not ps.AddLabel("lblRC2x", 0, 200, 550, 2200, 380, "Reload register (RC2H,RC2L)")) Then Exit Function
    
        If (Not ps.AddLabel("lblOverflow", 1, 2500, 900, 1150, 315)) Then Exit Function
        If (Not ps.AddLabel("lblOverflowLabel", 0, 200, 900, 1450, 380, "Timer overflow [" & TXT_US & "]")) Then Exit Function

        If (Not ps.AddLabel("lblResolution", 1, 2500, 1250, 1150, 315)) Then Exit Function
        If (Not ps.AddLabel("lblResolutionLabel", 0, 200, 1250, 1450, 380, "Timer resolution [" & TXT_US & "]")) Then Exit Function

        If (Not ps.AddLabel("lblUnderflow", 1, 2500, 1250, 1150, 315)) Then Exit Function
        If (Not ps.AddLabel("lblUnderflowLabel", 0, 200, 1250, 1450, 380, "Timer underflow [" & TXT_US & "]")) Then Exit Function
    '-------------------------------
    ' frame: fraEXF2
    '-------------------------------
    If (Not ps.AddFrame("fraEXF2", 4725, 3000, 3850, 1350, "EXF2 Output Pin Selection")) Then Exit Function
        If (Not ps.AddOption("OptEXF2NoPinSel", 200, 175, 1750, 380, "No pins are selected")) Then Exit Function
        If (Not ps.AddOption("OptEXF2SelP3.5", 200, 500, 2200, 380, "Use pin P3.5 (EXF21_0)")) Then Exit Function
        If (Not ps.AddOption("OptEXF2SelP4.3", 200, 800, 2200, 380, "Use pin P4.3 (EXF21_1)")) Then Exit Function
        
    '---------------------------------------------------------------
    ' Frame : fraTR2
    '---------------------------------------------------------------
    If (Not ps.AddFrame("fraTR2", 100, 4400, 4450, 800, "Start / Stop Control")) Then Exit Function
        If (Not ps.AddCheck("chkTR2", 200, 295, 2900, 380, "Start timer 21 after initialization (TR2)")) Then Exit Function
        
    '---------------------------------------------------------------
    ' Frame : fraIntCtrl
    '---------------------------------------------------------------
    If (Not ps.AddFrame("fraIntCtrl", 4725, 4400, 3850, 800, "Interrupt Control")) Then Exit Function
        If (Not ps.AddCheck("chkET2", 200, 295, 3000, 380, "Enable Timer 21 interrupt (IEN1_EX2)")) Then Exit Function

  End If
  OnChangingPageTimer21 = 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 successful operation
'        False           else
'*****************************************************************************
Public Function OnShowPageTimer21(ps As Peripherals, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowError

  OnShowPageTimer21 = False
  Dim SharedInt As Integer
  Dim c As Collection
  Dim r As ProjectData
  Dim e As Collection
  Dim bDisabled As Boolean
  Dim bPinFree As Boolean
  Dim bVisible As Boolean
  'Dim iTemp As Integer
  
  Set c = ps.ControlCollection
  Set r = ps.ProjectData
  Set e = ps.ExtenderCollection
  
  If Pagename = "Timer 21" Then
  
    ' ----------------------------------------------------------------
    ' Page Control (action)
    ' ----------------------------------------------------------------
    Select Case Controlname
        '----------------------------
        ' frame: Module Disable
        '----------------------------
        Case "OptEnable"
            r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 0
            'SetT2Default (r)
        
        Case "OptDisable"
            r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1
            SetT21Default (r)
        '----------------------------
        ' frame: Timer/Counter select
        '----------------------------
        Case "optC/T2_0_timer"
            r.l(T21_PREFIX & "T2CON_C/T2") = 0
            Call ReleaseAlternateInputPin(r, "T21_1")
            'r.v("MODPISEL2_T2IS") = 0
             r.v("MODPISEL2_T21IS") = 0   ' Corrected After Alpha Release
        Case "optC/T2_1_counter"
            r.l(T21_PREFIX & "T2CON_C/T2") = 1
            Call RequestAlternateInputPin(r, "T21_1")
            'r.v("MODPISEL2_T2IS") = 1
             r.v("MODPISEL2_T21IS") = 1   ' Corrected After Alpha Release
        '----------------------------
        ' frame: Capture/Reload Select
        '----------------------------
        Case "optCP/RL2_0_reload"
            r.l(T21_PREFIX & "T2CON_CP/RL2") = 0

        Case "optCP/RL2_1_capture"
            r.l(T21_PREFIX & "T2CON_CP/RL2") = 1
            Call CheckUpDownCounter(r)

        ' ----------------------------------------------------------------
        ' Frame: Prescaler Options
        ' ----------------------------------------------------------------
        Case "chkPreScaleEnable"
            r.v(T21_PREFIX & "T2MOD_PREN") = v(1)
            If v(1) = 0 Then r.l(T21_PREFIX & "T2MOD_T2PRE") = &H0


        Case "cboPreScale"
            Select Case v(1)
                Case "fPCLK"
                    r.l(T21_PREFIX & "T2MOD_T2PRE") = &H0
                Case "fPCLK/2"
                    r.l(T21_PREFIX & "T2MOD_T2PRE") = &H1
                Case "fPCLK/4"
                    r.l(T21_PREFIX & "T2MOD_T2PRE") = &H2
                Case "fPCLK/8"
                    r.l(T21_PREFIX & "T2MOD_T2PRE") = &H3
                Case "fPCLK/16"
                    r.l(T21_PREFIX & "T2MOD_T2PRE") = &H4
            End Select

        '--------------------------------
        ' frame: Timer/Counter registers
        '--------------------------------
        Case "txtT21x"
            If (IsInputFormatOK(v(1), "h", "Timer register (" & T21_PREFIX & "T2H," & T21_PREFIX & "T2L)", 0, &HFFFF)) Then
                r.l(T21_PREFIX & "T2H_THL2") = v(1) \ 256
                r.l(T21_PREFIX & "T2L_THL2") = v(1) Mod 256
            Else
                Exit Function
            End If
        Case "txtRC2x"
            If (IsInputFormatOK(v(1), "h", "Reload register (" & T21_PREFIX & "RC2H," & T21_PREFIX & "RC2L)", 0, &HFFFF)) Then
               If (Ask("Setting Reload register value will also be affecting the Timer register by the same value." & vbCrLf & "Do you want to continue ?")) Then
                  r.l(T21_PREFIX & "RC2H_RC2") = v(1) \ 256
                  r.l(T21_PREFIX & "RC2L_RC2") = v(1) Mod 256
                  r.l(T21_PREFIX & "T2H_THL2") = v(1) \ 256
                  r.l(T21_PREFIX & "T2L_THL2") = v(1) Mod 256
               Else
                  r.l(T21_PREFIX & "RC2H_RC2") = v(1) \ 256
                  r.l(T21_PREFIX & "RC2L_RC2") = v(1) Mod 256
               End If
            Else
                Exit Function
            End If
        '----------------------------
        ' frame: Start/Stop Control
        '----------------------------
        Case "chkTR2"
            r.v(T21_PREFIX & "T2CON_TR2") = v(1)
'        '----------------------------
'        ' frame: Interrupt Control
'        '----------------------------
'        Case "chkET2"
'            If v(1) = 1 Then
'              If r.v(CORE_PREFIX & "IEN1_EX2") = 1 Then
'                If Ask(MSG_SELECT_T21_INTERRUPT) Then
'                    r.v("T21_Interrupt_Select") = 1
'                    r.v("T21_USE_IsrTmr") = 1
'                End If
'              Else
'                r.v("T21_Interrupt_Select") = 1
'                r.v("T21_USE_IsrTmr") = 1
'              End If
'            Else
'                r.v("T21_Interrupt_Select") = 0
'                r.v("T21_USE_IsrTmr") = 0
'            End If
         '----------------------------
        ' frame: Interrupt Control
        '----------------------------
        Case "chkET2"
            If v(1) = 1 Then
             'MsgBox MSG_SELECT_XINTR8_INTERRUPTS
             If (r.v("INTR_MSGEN") = 1) Then
                 SharedInt = MsgBox(MSG_SELECT_XINTR8_INTERRUPTS, vbYesNo + vbQuestion, "DAvE")
                 If (SharedInt = 6) Then
                     r.v("INTR_MSGEN") = 1
                 ElseIf (SharedInt = 7) Then
                     r.v("INTR_MSGEN") = 0
                     SharedInt = MsgBox(MSGBOX_SHAREDINT_ENABLE, vbInformation, "DAvE")
                 End If
             End If
             r.v("T21_Interrupt_Select") = 1
             r.v("T21_USE_IsrTmr") = 1
            Else
               r.v("T21_Interrupt_Select") = 0
               r.v("T21_USE_IsrTmr") = 0
            End If
            
        '------------------------------------------------------------------------------------
        ' Frame "EXF2 output pin selection"
        '------------------------------------------------------------------------------------
        Case "OptEXF2NoPinSel"
            If r.v("Timer21_EXF2_Pin_P3.5_Used") = 1 Then
                Call ReleaseAlternateOutputPin(r, "EXF21_0")
                r.v("Timer21_EXF2_Pin_P3.5_Used") = 0
            End If
            If r.v("Timer21_EXF2_Pin_P4.3_Used") = 1 Then
                Call ReleaseAlternateOutputPin(r, "EXF21_1")
                r.v("Timer21_EXF2_Pin_P4.3_Used") = 0
            End If
        
        Case "OptEXF2SelP3.5"
            r.v("Timer21_EXF2_Pin_P3.5_Used") = 1
            If r.v("Timer21_EXF2_Pin_P4.3_Used") = 1 Then
                r.v("Timer21_EXF2_Pin_P4.3_Used") = 0
                Call ReleaseAlternateOutputPin(r, "EXF21_1")
            End If
            Call RequestAlternateOutputPin(r, "EXF21_0", ALT_FUN2)
        
        Case "OptEXF2SelP4.3"
            r.v("Timer21_EXF2_Pin_P4.3_Used") = 1
            If r.v("Timer21_EXF2_Pin_P3.5_Used") = 1 Then
                r.v("Timer21_EXF2_Pin_P3.5_Used") = 0
                Call ReleaseAlternateOutputPin(r, "EXF21_0")
            End If
            Call RequestAlternateOutputPin(r, "EXF21_1", ALT_FUN1)

    End Select

    ' ----------------------------------------------------------------
    ' Page Control (show)
    ' ----------------------------------------------------------------
        '----------------------------
        ' frame: Module Disable
        '----------------------------
        With c("OptEnable")
            .Value = (r.v(SCU_PREFIX & "PMCON2_T21_DIS") = 0)
        End With
        
        
        With c("OptDisable")
            .Value = (r.v(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
        End With


        '----------------------------
        ' frame: Timer/Counter select
        '----------------------------
        With c("optC/T2_0_timer")
            bDisabled = (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
            .Enabled = Not bDisabled
            .Value = r.l(T21_PREFIX & "T2CON_C/T2") = 0
        End With
    
        With e("optC/T2_0_timer")
            If bDisabled Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
            End If
        End With
        
        With c("optC/T2_1_counter")
            bDisabled = Not IsAlternatePinFree(r, "T21_1") Or (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
            .Enabled = Not bDisabled
            .Value = r.l(T21_PREFIX & "T2CON_C/T2") = 1
        End With
        With e("optC/T2_1_counter")
            If bDisabled Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                If r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1 Then
                    .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
                Else
                    .DisabledTooltipText = .DisabledTooltipText & GetDisabledAlternatePinTooltip(r, "T21_1")
                End If
            End If
        End With
        c("optT21_0").Enabled = False
        e("optT21_0").DisabledTooltipText = FormatDisabledToolTip("This pin is not available")
        
        
        With c("optT21_1")
            bDisabled = (r.l(T21_PREFIX & "T2CON_C/T2") = 0) Or (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
            .Enabled = Not bDisabled
            .Value = 1
        End With
        
        With e("optT21_1")
            If bDisabled Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                If r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1 Then
                    .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
                Else
                    .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("counter mode is not selected")
                End If
            End If
        End With
        '----------------------------
        ' frame: Capture/Reload Select
        '----------------------------
        With c("optCP/RL2_0_reload")
            bDisabled = (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
            .Value = r.l(T21_PREFIX & "T2CON_CP/RL2") = 0
            .Enabled = Not bDisabled
        End With

        With e("optCP/RL2_0_reload")
            If bDisabled Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
            End If
        End With

        With c("optCP/RL2_1_capture")
            bDisabled = (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
            .Enabled = Not bDisabled
            .Value = r.l(T21_PREFIX & "T2CON_CP/RL2") = 1
        End With

        With e("optCP/RL2_1_capture")
            If bDisabled Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                If r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1 Then
                    .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
                End If
            End If
        End With
        
        ' ----------------------------------------------------------------
        ' Frame: Prescaler Options
        ' ----------------------------------------------------------------
        With c("chkPreScaleEnable")
            bDisabled = (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
            .Enabled = Not bDisabled
            .Value = r.v(T21_PREFIX & "T2MOD_PREN")
        End With

        With e("chkPreScaleEnable")
            If bDisabled Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
            End If
        End With

        With c("cboPreScale")
            bDisabled = (r.v(T21_PREFIX & "T2MOD_PREN") = 0) Or (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
            .Enabled = Not bDisabled
            .ListIndex = r.v(T21_PREFIX & "T2MOD_T2PRE")
        End With

        With e("cboPreScale")
            If bDisabled Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                If r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1 Then
                    .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
                Else
                    .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("prescaler option is not selected)")
                End If
            End If
        End With

        With c("lblPreScale")
            bDisabled = (r.v(T21_PREFIX & "T2MOD_PREN") = 0) Or (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
            .Enabled = Not bDisabled
        End With

        '----------------------------
        ' frame: Timer/Counter registers
        '----------------------------
        With c("txtT21x")
            bDisabled = (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
            .Enabled = Not bDisabled
            .Text = FormatTextOutput(r.l(T21_PREFIX & "T2H_THL2") * 256 + r.l(T21_PREFIX & "T2L_THL2"), "H4")
        End With

        With e("txtT21x")
            If bDisabled Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
            End If
        End With
       
        With c("lblT21x")
            .Enabled = Not bDisabled
        End With
        With c("txtRC2x")
            bDisabled = (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
            .Enabled = Not bDisabled
            .Text = FormatTextOutput(r.l(T21_PREFIX & "RC2H_RC2") * 256 + r.l(T21_PREFIX & "RC2L_RC2"), "H4")
        End With

        With e("txtRC2x")
            If bDisabled Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
            End If
        End With

        With c("lblRC2x")
            .Enabled = Not bDisabled
        End With

        bVisible = Not (r.l(T21_PREFIX & "T2CON_CP/RL2") = 0)
        With c("lblResolution")
            .Visible = bVisible
            bDisabled = (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
            .Enabled = Not bDisabled
            .Caption = GetTimer2Resolution(r)
        End With
        c("lblResolutionLabel").Visible = bVisible


        bVisible = Not (r.l(T21_PREFIX & "T2CON_C/T2") = 1)
        With c("lblOverflow")
            .Visible = bVisible
            bDisabled = (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
            .Enabled = Not bDisabled
            .Caption = GetTimer2Overflow(r)
        End With

        With c("lblOverflowLabel")
            .Visible = bVisible
            .Enabled = Not bDisabled
        End With

        bVisible = Not (r.l(T21_PREFIX & "T2MOD_DCEN") = 0)
        With c("lblUnderflow")
            .Visible = bVisible
            bDisabled = (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
            .Enabled = Not bDisabled
            .Caption = GetTimer2Underflow(r)
        End With
        c("lblUnderflowLabel").Visible = bVisible
       
        '----------------------------
        ' frame: Start/Stop Control
        '----------------------------
        With c("chkTR2")
            bDisabled = (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
            .Enabled = Not bDisabled
            .Value = -CInt(r.v(T21_PREFIX & "T2CON_TR2") = 1)
        End With

        With e("chkTR2")
            If bDisabled Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
            End If
        End With

        '----------------------------
        ' frame: Interrupt Control
        '----------------------------
        With c("chkET2")
            bDisabled = (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
            .Enabled = Not bDisabled
            .Value = -CInt(r.v("T21_Interrupt_Select") = 1)
        End With

        With e("chkET2")
            If bDisabled Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                If r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1 Then
                    .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
                End If
            End If
        End With
       

    '------------------------------------------------------------------------------------
    ' Frame "EXF2 Pin Selection"
    '------------------------------------------------------------------------------------
    With c("OptEXF2NoPinSel")
        bDisabled = (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
        .Enabled = Not bDisabled
        .Value = Not (r.v("Timer21_EXF2_Pin_P3.5_Used") Or r.v("Timer21_EXF2_Pin_P4.3_Used"))
    End With
    
    With e("OptEXF2NoPinSel")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
        End If
    End With
    
    With c("OptEXF2SelP3.5")
        bPinFree = IsAlternatePinFree(r, "EXF21_0")
        bDisabled = (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1 Or (Not bPinFree))
        .Enabled = Not bDisabled
        .Value = r.v("Timer21_EXF2_Pin_P3.5_Used")
    End With
    
    With e("OptEXF2SelP3.5")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1 Then
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
            ElseIf (Not bPinFree) Then
                .DisabledTooltipText = .DisabledTooltipText & GetDisabledAlternatePinTooltip(r, "EXF21_0")
            End If
        End If
    End With
        
    With c("OptEXF2SelP4.3")
        bPinFree = IsAlternatePinFree(r, "EXF21_1")
        bDisabled = (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1 Or (Not bPinFree))
        .Enabled = Not bDisabled
        .Value = r.v("Timer21_EXF2_Pin_P4.3_Used")
    End With
    
    With e("OptEXF2SelP4.3")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1 Then
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
            ElseIf (Not bPinFree) Then
                        .DisabledTooltipText = .DisabledTooltipText & GetDisabledAlternatePinTooltip(r, "EXF21_1")
            End If
        End If
    End With
    
  End If
  
  OnShowPageTimer21 = True
  Exit Function

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

'*****************************************************************************
'Description:
'        This subroutine checks if T21 interrupts are possible with the current
'        configuration. If not, it deselects both interrupt enable bit and ISR
'        function.
'-----------------------------------------------------------------------------
'Parameters:
'*****************************************************************************
Private Sub CheckT2Interrupt(r As ProjectData)
    On Error Resume Next

    If (r.v(T21_PREFIX & "T2CON_EXEN2") = 0) Then
        r.l(CORE_PREFIX & "IEN0_ET2") = 0
        r.v("T21_USE_IsrTmr") = 0
    End If
End Sub

'*****************************************************************************
'Description:
'        This subroutine checks if the up/down counter function is possible.
'        If it is not possible, bit DCEN is cleared.
'-----------------------------------------------------------------------------
'Parameters:
'*****************************************************************************
Private Sub CheckUpDownCounter(r As ProjectData)
    On Error Resume Next

    If (r.l(T21_PREFIX & "T2CON_CP/RL2") = 1) Then
        r.l(T21_PREFIX & "T2MOD_DCEN") = 0
        'If (r.v(T21_PREFIX & "T2CON_EXEN2") = 0) Then
        '    Call ReleaseAlternateInputPin(r, "T2EX")
        'End If
    End If
End Sub

'*****************************************************************************
'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 successful operation
'        False           else
'*****************************************************************************
Public Function OnLoadPageT2EX(ps As Peripherals) As Boolean
  On Error Resume Next
  OnLoadPageT2EX = False

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

  OnLoadPageT2EX = 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 successful operation
'        False           else
'*****************************************************************************
Public Function OnChangingPageT2EX(ps As Peripherals, Pagename As String) As Boolean
  On Error Resume Next

  OnChangingPageT2EX = False

  If (Not ps.ExtenderCollection("T2EX").Loaded) And (Pagename = "T2EX") Then
    ps.ExtenderCollection("T2EX").Loaded = True
    
    '---------------------------------------------------------------
    ' Frame : fraT2ExStart
    '---------------------------------------------------------------
    If (Not ps.AddFrame("fraT2ExStart", 100, 100, 4450, 800, "Timer 21 External Start Enable")) Then Exit Function
        If (Not ps.AddCheck("chkT2ExStart", 200, 295, 3100, 400, "Enable external start (T2RHEN)")) Then Exit Function
        
    '---------------------------------------------------------------
    ' Frame : fraExStrtEdge
    '---------------------------------------------------------------
    If (Not ps.AddFrame("fraExStrtEdge", 100, 1000, 4450, 1100, "External Start Egde Select (T2REGS)")) Then Exit Function
        If (Not ps.AddOption("OptExStrtFallEdge", 200, 250, 2900, 350, "Select falling edge at Pin T2EX")) Then Exit Function
        If (Not ps.AddOption("OptExStrtRiseEdge", 200, 600, 2900, 350, "Select rising edge at Pin T2EX")) Then Exit Function
    
    '---------------------------------------------------------------
    ' Frame : fraEXEN2
    '---------------------------------------------------------------
    If (Not ps.AddFrame("fraEXEN2", 4725, 100, 3850, 800, "Timer 21 External Enable Control")) Then Exit Function
        If (Not ps.AddCheck("chkEXEN2_capture_reload", 200, 250, 3400, 400, "Capture or reload when a positive/negative transition occurs on pin T2EX (EXEN2)")) Then Exit Function
    
    '---------------------------------------------------------------
    ' Frame : fraEXEN2Edge
    '---------------------------------------------------------------
    If (Not ps.AddFrame("fraEXEN2Edge", 4725, 1000, 3850, 1100, "Edge Select in Capture/Reload mode (EDGESEL)")) Then Exit Function
        If (Not ps.AddOption("OptEXEN2FallEdge", 200, 250, 2900, 350, "Select falling edge at Pin T2EX")) Then Exit Function
        If (Not ps.AddOption("OptEXEN2RiseEdge", 200, 600, 2900, 350, "Select rising edge at Pin T2EX")) Then Exit Function
        
    If (Not ps.AddFrame("fraT2EXPin", 100, 2150, 4450, 1200, "T2EX Pin Select")) Then Exit Function
        If (Not ps.AddOption("OptT2EXNoPinSel", 200, 175, 1750, 380, "No pins are selected")) Then Exit Function
        If (Not ps.AddOption("OptT2EXSelP3.4", 200, 500, 2200, 380, "Use pin P3.4 (T2EX1_0)")) Then Exit Function
        If (Not ps.AddOption("OptT2EXSelP0.5", 200, 800, 2200, 380, "Use pin P0.5 (T2EX1_1)")) Then Exit Function
    '---------------------------------------------------------------
    ' Frame : fraDCEN
    '---------------------------------------------------------------
    If (Not ps.AddFrame("fraDCEN", 4725, 2150, 3850, 1200, "Up / Down Counter")) Then Exit Function
        If (Not ps.AddCheck("chkDCEN", 200, 295, 3500, 800, "Enable timer 21 as an up/down counter depending on pin T2EX (DCEN)")) Then Exit Function
    

  End If
  OnChangingPageT2EX = 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 successful operation
'        False           else
'*****************************************************************************
Public Function OnShowPageT2EX(ps As Peripherals, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowError

  OnShowPageT2EX = False
  Dim c As Collection
  Dim r As ProjectData
  Dim e As Collection
  Dim bDisabled As Boolean
  Dim bPinFree As Boolean
  Dim bVisible As Boolean
  
  Set c = ps.ControlCollection
  Set r = ps.ProjectData
  Set e = ps.ExtenderCollection
  
  If Pagename = "T2EX" Then
  
    ' ----------------------------------------------------------------
    ' Page Control (action)
    ' ----------------------------------------------------------------
    Select Case Controlname

        '----------------------------
        ' frame: External Enable
        '----------------------------
        Case "chkEXEN2_capture_reload", "chkEXEN2_baudrate"
            r.v(T21_PREFIX & "T2CON_EXEN2") = v(1)
            If (v(1) = 0) Then
                r.v(T21_PREFIX & "T2MOD_EDGESEL") = 0
            End If
            Call CheckT2Interrupt(r)

        Case "chkT2ExStart"
            r.v(T21_PREFIX & "T2MOD_T2RHEN") = v(1)
            If v(1) = 0 Then
                r.v(T21_PREFIX & "T2MOD_T2REGS") = 0
            End If

        '----------------------------
        ' frame: Up/Down Counter
        '----------------------------
        Case "chkDCEN"
            r.v(T21_PREFIX & "T2MOD_DCEN") = v(1)
            If v(1) = 0 Then
                r.v(T21_PREFIX & "T2MOD_EDGESEL") = 0
            End If
            
        '-------------------------------
        ' Edge Selection T2RHEN
        '-------------------------------
        
        Case "OptExStrtFallEdge"
            r.v(T21_PREFIX & "T2MOD_T2REGS") = 0
            
        Case "OptExStrtRiseEdge"
            r.v(T21_PREFIX & "T2MOD_T2REGS") = 1
        
        '-------------------------------
        ' Edge Selection EXEN2
        '-------------------------------
        
        Case "OptEXEN2FallEdge"
            r.v(T21_PREFIX & "T2MOD_EDGESEL") = 0
            
        Case "OptEXEN2RiseEdge"
            r.v(T21_PREFIX & "T2MOD_EDGESEL") = 1
        
        
        Case "OptT2EXNoPinSel"
            If r.v("Timer21_T2EX_Pin_P3.4_Used") = 1 Then
                Call ReleaseAlternateInputPin(r, "T2EX1_0")
                r.v("Timer21_T2EX_Pin_P3.4_Used") = 0
                r.v(CORE_PREFIX & "MODPISEL1_T21EXIS") = 0
            End If
            
            If r.v("Timer21_T2EX_Pin_P0.5_Used") = 1 Then
                Call ReleaseAlternateInputPin(r, "T2EX1_1")
                r.v("Timer21_T2EX_Pin_P0.5_Used") = 0
                r.v(CORE_PREFIX & "MODPISEL1_T21EXIS") = 0
            End If
            
        
        Case "OptT2EXSelP3.4"
            r.v("Timer21_T2EX_Pin_P3.4_Used") = 1
            If r.v("Timer21_T2EX_Pin_P0.5_Used") = 1 Then
                r.v("Timer21_T2EX_Pin_P0.5_Used") = 0
                Call ReleaseAlternateInputPin(r, "T2EX1_1")
            End If
            Call RequestAlternateInputPin(r, "T2EX1_0")
            r.v(CORE_PREFIX & "MODPISEL1_T21EXIS") = 0
        
        Case "OptT2EXSelP0.5"
            r.v("Timer21_T2EX_Pin_P0.5_Used") = 1
            If r.v("Timer21_T2EX_Pin_P3.4_Used") = 1 Then
                r.v("Timer21_T2EX_Pin_P3.4_Used") = 0
                Call ReleaseAlternateInputPin(r, "T2EX1_0")
            End If
            Call RequestAlternateInputPin(r, "T2EX1_1")
            r.v(CORE_PREFIX & "MODPISEL1_T21EXIS") = 1

    End Select

    ' ----------------------------------------------------------------
    ' Page Control (show)
    ' ----------------------------------------------------------------
        
        '----------------------------
        ' frame: External Enable
        '----------------------------
        With c("chkEXEN2_capture_reload")
            bDisabled = (r.v(T21_PREFIX & "T2MOD_DCEN") = 1) Or (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1) _
                        Or (r.v("Timer21_T2EX_Pin_P0.5_Used") = 0 And r.v("Timer21_T2EX_Pin_P3.4_Used") = 0)
            .Enabled = Not bDisabled
            .Value = -CInt(r.v(T21_PREFIX & "T2CON_EXEN2") = 1)
        End With

        With e("chkEXEN2_capture_reload")
            If bDisabled Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                If r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1 Then
                    .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
                ElseIf (r.v(T21_PREFIX & "T2MOD_DCEN") = 1) Then
                    .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("up/down counter mode is selected (DCEN = 1)")
                Else
                        .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("No T2EX pin is selected")
                End If
            End If
        End With
        
        '----------------------------
        ' frame: Up/Down Counter
        '----------------------------
        With c("chkDCEN")
            bDisabled = (r.l(T21_PREFIX & "T2CON_EXEN2") = 1) Or (r.l(T21_PREFIX & "T2CON_CP/RL2") = 1) _
                        Or (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1) Or (r.v("Timer21_T2EX_Pin_P0.5_Used") = 0 And r.v("Timer21_T2EX_Pin_P3.4_Used") = 0)
            .Enabled = Not bDisabled
            .Value = -CInt(r.v(T21_PREFIX & "T2MOD_DCEN") = 1)
        End With

        With e("chkDCEN")
            If bDisabled Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                If (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1) Then
                    .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
                ElseIf (r.l(T21_PREFIX & "T2CON_EXEN2") = 1) Then
                        .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("the external reload is selected (EXEN2 = 1)")
                        ElseIf (r.l(T21_PREFIX & "T2CON_CP/RL2") = 1) Then
                            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("16-bit capture mode is selected (CP/RL2 = 1)")
                        Else
                            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("No T2EX pin is selected")
                End If
            End If
        End With
        
        '----------------------------
        ' frame: T2RHEN
        '----------------------------

        With c("chkT2ExStart")
            bDisabled = (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1) Or (r.v("Timer21_T2EX_Pin_P0.5_Used") = 0 And r.v("Timer21_T2EX_Pin_P3.4_Used") = 0)
            .Enabled = Not bDisabled
            .Value = r.v(T21_PREFIX & "T2MOD_T2RHEN")
        End With
        
        With e("chkT2ExStart")
            If bDisabled Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                If (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1) Then
                    .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
                Else
                    .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("No T2EX pin is selected")
                End If
            End If
        End With
        
        '----------------------------
        ' frame: T2REGS
        '----------------------------
        
        With c("OptExStrtFallEdge")
            bDisabled = (r.v(T21_PREFIX & "T2MOD_T2RHEN") = 0) Or (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
            .Enabled = Not bDisabled
            .Value = (r.v(T21_PREFIX & "T2MOD_T2REGS") = 0)
        End With
        
        With e("OptExStrtFallEdge")
            If bDisabled Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                If r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1 Then
                    .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
                Else
                    .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("External Start Enable is not selected")
                End If
            End If
        End With
        
        With c("OptExStrtRiseEdge")
            bDisabled = (r.v(T21_PREFIX & "T2MOD_T2RHEN") = 0) Or (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
            .Enabled = Not bDisabled
            .Value = (r.v(T21_PREFIX & "T2MOD_T2REGS") = 1)
        End With
        
        With e("OptExStrtRiseEdge")
            If bDisabled Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                If r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1 Then
                    .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
                Else
                    .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("External Start Enable is not selected")
                End If
            End If
        End With
        
        '----------------------------
        ' frame: EDGESEL
        '----------------------------
        
        With c("OptEXEN2FallEdge")
            bDisabled = (r.v(T21_PREFIX & "T2CON_EXEN2") = 0) Or (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
            .Enabled = Not bDisabled
            .Value = (r.v(T21_PREFIX & "T2MOD_EDGESEL") = 0)
        End With
        
        With e("OptEXEN2FallEdge")
            If bDisabled Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                If r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1 Then
                    .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
                Else
                    .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("External Enable pin is not selected")
                End If
            End If
        End With
        
        With c("OptEXEN2RiseEdge")
            bDisabled = (r.v(T21_PREFIX & "T2CON_EXEN2") = 0) Or (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
            .Enabled = Not bDisabled
            .Value = (r.v(T21_PREFIX & "T2MOD_EDGESEL") = 1)
        End With
        
        With e("OptEXEN2RiseEdge")
            If bDisabled Then
                .DisabledTooltipText = TOOLTIP_DISABLED_IF
                If r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1 Then
                    .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
                Else
                    .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("External Enable pin is not selected")
                End If
            End If
        End With
        
        
        '------------------------------------------------------------------------------------
    ' Frame "T2EX Pin Selection"
    '------------------------------------------------------------------------------------
    With c("OptT2EXNoPinSel")
        bDisabled = (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
        .Enabled = Not bDisabled
        .Value = Not (r.v("Timer21_T2EX_Pin_P0.5_Used") Or r.v("Timer21_T2EX_Pin_P3.4_Used"))
    End With
    
    With e("OptT2EXNoPinSel")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
        End If
    End With
    
    With c("OptT2EXSelP3.4")
        bPinFree = IsAlternatePinFree(r, "T2EX1_0")
        bDisabled = (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1 Or (Not bPinFree))
        .Enabled = Not bDisabled
        .Value = r.v("Timer21_T2EX_Pin_P3.4_Used")
    End With
    
    With e("OptT2EXSelP3.4")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1 Then
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
            ElseIf (Not bPinFree) Then
                .DisabledTooltipText = .DisabledTooltipText & GetDisabledAlternatePinTooltip(r, "T2EX1_0")
            End If
        End If
    End With
        
    With c("OptT2EXSelP0.5")
        bPinFree = IsAlternatePinFree(r, "T2EX1_1")
        bDisabled = (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1 Or (Not bPinFree))
        .Enabled = Not bDisabled
        .Value = r.v("Timer21_T2EX_Pin_P0.5_Used")
    End With
    
    With e("OptT2EXSelP0.5")
        If (bDisabled) Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            If r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1 Then
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
            ElseIf (Not bPinFree) Then
                        .DisabledTooltipText = .DisabledTooltipText & GetDisabledAlternatePinTooltip(r, "T2EX1_1")
            End If
        End If
    End With
        
  End If
  
  
  OnShowPageT2EX = True
  Exit Function

OnShowError:
  Errorbox "Can not show page", Err.Description
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 successful 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 successful operation
'        False           else
'*****************************************************************************
Public Function OnChangingPageFunctions(ps As Peripherals, Pagename As String) As Boolean
  On Error Resume Next

  OnChangingPageFunctions = False

  Dim pos As Long

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

    If (Not ps.AddFrame("fraT2_USE_Init", 100, 100, 4150, 700, "Initialization Function")) Then Exit Function
    If (Not ps.AddCheck("chkT2_USE_Init", 200, 270, 1000, 380, "T21_vInit")) Then Exit Function
    If (Not ps.AddText("txtT2_NAME_Init", 2150, 300, 1900, 315)) Then Exit Function

    If (Not ps.AddFrame("fraT2_CFile", 4400, 100, 4150, 700, "Source File")) Then Exit Function
    If (Not ps.AddText("txtT2_CFile", 1500, 300, 2550, 315)) Then Exit Function
    If (Not ps.AddLabel("lblT2_CFile", 0, 200, 300, 1575, 380, "File name")) Then Exit Function

    If (Not ps.AddFrame("fraLibrary1", 100, 900, 4150, 4400, "Function Library")) Then Exit Function
    pos = 0
    ' Functions and Macros
    If (Not ps.AddCheck("chkT2_USE_StartTmr", 200, 270 + pos * 300, 2100, 380, "T21_vStartTmr")) Then Exit Function
    If (Not ps.AddText("txtT2_NAME_StartTmr", 2150, 300 + pos * 300, 1900, 315)) Then Exit Function
    pos = pos + 1

    If (Not ps.AddCheck("chkT2_USE_StopTmr", 200, 270 + pos * 300, 2100, 380, "T21_vStopTmr")) Then Exit Function
    If (Not ps.AddText("txtT2_NAME_StopTmr", 2150, 300 + pos * 300, 1900, 315)) Then Exit Function
    pos = pos + 1

    If (Not ps.AddCheck("chkT2_USE_ReadTmr", 200, 270 + pos * 300, 2100, 380, "T21_uwReadTmr")) Then Exit Function
    If (Not ps.AddText("txtT2_NAME_ReadTmr", 2150, 300 + pos * 300, 1900, 315)) Then Exit Function
    pos = pos + 1

    If (Not ps.AddCheck("chkT2_USE_SetRC2Reg", 200, 270 + pos * 300, 2100, 380, "T21_vSetRC2Reg")) Then Exit Function
    If (Not ps.AddText("txtT2_NAME_SetRC2Reg", 2150, 300 + pos * 300, 1900, 315)) Then Exit Function
    pos = pos + 1

    If (Not ps.AddCheck("chkT2_USE_GetRC2Reg", 200, 270 + pos * 300, 2100, 380, "T21_uwGetRC2Reg")) Then Exit Function
    If (Not ps.AddText("txtT2_NAME_GetRC2Reg", 2150, 300 + pos * 300, 1900, 315)) Then Exit Function
    pos = pos + 1

    'If (Not ps.AddCheck("chkT2_USE_IsrTmr", 200, 270 + pos * 300, 2100, 380, "T21_viTmr")) Then Exit Function
    If (Not ps.AddCheck("chkT2_USE_IsrTmr", 200, 270 + pos * 300, 2100, 380, "SHINT_viXINTR8Isr")) Then Exit Function
    If (Not ps.AddText("txtT2_NAME_IsrTmr", 2150, 300 + pos * 300, 1900, 315)) Then Exit Function
    pos = pos + 1

    
  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 successful operation
'        False           else
'*****************************************************************************
Public Function OnShowPageFunctions(ps As Peripherals, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowError

  OnShowPageFunctions = False
    
  Dim r As ProjectData
  Dim c As Collection
  Dim e As Collection
  Dim bDisabled As Boolean
  Dim func As FunctionClass

  Set r = ps.ProjectData
  Set c = ps.ControlCollection
  Set e = ps.ExtenderCollection
  Set func = New FunctionClass

  If (Pagename = "Functions") Then
    '-------------------------------------------------------------------------
    ' page: Functions (action)
    '-------------------------------------------------------------------------
    Select Case Controlname
        Case "chkT2_USE_Init"
             r.v("T21_USE_Init") = v(1)
    
        Case "txtT2_NAME_Init"
             If (IsInputFormatOK(v(1), FORMAT_FUNCTIONS, c("chkT2_USE_Init").Caption)) Then
                r.v("T21_NAME_Init") = v(1)
             End If
    
        Case "txtT2_CFile"
             If (IsInputFormatOK(v(1), FORMAT_FILES, c("lblT2_CFile").Caption)) Then
                r.v("T21_CFile") = v(1)
             End If
    End Select     ' case Controlname


    If (Not UpdateFunction(ps, Controlname, v, "GetRC2Reg")) Then Exit Function
    If (Not UpdateFunction(ps, Controlname, v, "IsrTmr")) Then Exit Function
    If (Not UpdateFunction(ps, Controlname, v, "ReadTmr")) Then Exit Function
    If (Not UpdateFunction(ps, Controlname, v, "SetRC2Reg")) Then Exit Function
    If (Not UpdateFunction(ps, Controlname, v, "StartTmr")) Then Exit Function
    If (Not UpdateFunction(ps, Controlname, v, "StopTmr")) Then Exit Function
    
    

    '-------------------------------------------------------------------------
    ' page: Functions (show)
    '-------------------------------------------------------------------------
    Call RefreshFunctionDescriptions(func, r)

    '-------------------------------------------------------------------------
    ' control: chkT2_USE_Init (show)
    '-------------------------------------------------------------------------
    With c("chkT2_USE_Init")
        bDisabled = (r.l(SCU_PREFIX & "PMCON2_T21_DIS") = 1)
        .Enabled = Not bDisabled
        .Value = -CInt(r.v("T21_USE_Init") = 1)
    End With
    
    With e("chkT2_USE_Init")
        If bDisabled Then
            .DisabledTooltipText = TOOLTIP_DISABLED_IF
            .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip("T21 module is disabled")
        Else
            .EnabledTooltipText = FormatFunctionToolTip(r, func, "T21_NAME_Init")
        End If
    End With
    
    '-------------------------------------------------------------------------
    ' control: txtT2_NAME_Init (show)
    '-------------------------------------------------------------------------
    With c("txtT2_NAME_Init")
        bDisabled = r.v("T21_USE_Init") = 0
        .Enabled = Not bDisabled
        .Text = FormatTextOutput(r.v("T21_NAME_Init"), "S")
    End With

    With e("txtT2_NAME_Init")
        If (bDisabled) Then
            .DisabledTooltipText = FormatNormalToolTip(TOOLTIP_DISABLED_FUNC_NAME)
        End If
    End With
    
    If (Not ShowFunction(ps, v, func, "GetRC2Reg")) Then Exit Function
    If (Not ShowFunction(ps, v, func, "IsrTmr")) Then Exit Function
    If (Not ShowFunction(ps, v, func, "ReadTmr")) Then Exit Function
    If (Not ShowFunction(ps, v, func, "SetRC2Reg")) Then Exit Function
    If (Not ShowFunction(ps, v, func, "StartTmr")) Then Exit Function
    If (Not ShowFunction(ps, v, func, "StopTmr")) Then Exit Function
  
    '-------------------------------------------------------------------------
    ' control: lblT2_CFile (show)
    '-------------------------------------------------------------------------
    With c("lblT2_CFile")
        bDisabled = r.v("T21_USE_Init") = 0
        .Enabled = Not bDisabled
    End With

    With e("lblT2_CFile")
        If (bDisabled) Then
            .DisabledTooltipText = FormatNormalToolTip(TOOLTIP_DISABLED_CFILE_NAME)
        End If
    End With

    '-------------------------------------------------------------------------
    ' control: txtT2_CFile (show)
    '-------------------------------------------------------------------------
    With c("txtT2_CFile")
      bDisabled = (r.v("T21_USE_Init") = 0)
      .Enabled = Not (bDisabled)
      .Text = FormatTextOutput(r.v("T21_CFile"), "S")
    End With

    With e("txtT2_CFile")
        If (bDisabled) Then
            .DisabledTooltipText = FormatNormalToolTip(TOOLTIP_DISABLED_CFILE_NAME)
        End If
    End With

  End If         ' if Pagename

  OnShowPageFunctions = True
  Exit Function

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

'*****************************************************************************
'Description:
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the Project Data Object
'        Controlname     control name
'        v()             value array
'        sName           function keyword
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function UpdateFunction(ps As Peripherals, Controlname As String, v() As Variant, sName As String) As Boolean
  On Error GoTo ExitLabel
  UpdateFunction = False

  Dim r As ProjectData
  Dim c As Collection

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

  '---------------------------------------------------------------------
  ' control: chkT2_USE_.. (action)
  '---------------------------------------------------------------------
  If (Controlname = "chkT2_USE_" & sName) Then
    r.v("T21_USE_" & sName) = v(1)
  End If

  '---------------------------------------------------------------------
  ' control: txtT2_NAME_.. (action)
  '---------------------------------------------------------------------
  If (Controlname = "txtT2_NAME_" & sName) Then
    If (IsInputFormatOK(v(1), FORMAT_FUNCTIONS, c("chkT2_USE_" & sName).Caption)) Then
        r.v("T21_NAME_" & sName) = v(1)
    Else
        Exit Function
    End If
  End If

  UpdateFunction = True
ExitLabel:
End Function


'*****************************************************************************
'Description:
'-----------------------------------------------------------------------------
'Parameters:
'        ps              reference to the Peripherals object
'        v()             value array
'        func            reference to the function object
'        sName           function keyword
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function ShowFunction(ps As Peripherals, v() As Variant, func As FunctionClass, sName As String) As Boolean
  On Error GoTo ExitLabel
  ShowFunction = False
    
  Dim r As ProjectData
  Dim c As Collection
  Dim e As Collection
  Dim bDisabled As Boolean

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

  '-------------------------------------------------------------------------
  ' control: chkT2_USE_.. (show)
  '-------------------------------------------------------------------------
  With c("chkT2_USE_" & sName)
    Select Case sName

        Case "IsrTmr"      ' ISR functions are always disabled
            bDisabled = True

        Case Else
            bDisabled = (r.v("T21_USE_Init") = 0)

    End Select

    .Enabled = Not bDisabled
    .Value = -CInt((r.v("T21_USE_Init") = 1) And (r.v("T21_USE_" & sName) = 1))
  End With


  With e("chkT2_USE_" & sName)
    If (bDisabled) Then
        ' tooltip if control is disabled
        Select Case sName

            Case "IsrTmr"      ' ISR functions are always disabled
                    .DisabledTooltipText = TOOLTIP_DISABLED_ISR & vbCrLf & FormatFunctionToolTip(r, func, "T21_NAME_" & sName)

            Case Else
                .DisabledTooltipText = GetDisabledFunctionTooltipEntry(func, "T21_NAME_" & sName)
                .DisabledTooltipText = .DisabledTooltipText & FormatDisabledToolTip(TOOLTIP_DESELECTED_INIT)

        End Select
    Else
        ' tooltip if control is enabled
        .EnabledTooltipText = FormatFunctionToolTip(r, func, "T21_NAME_" & sName)
    End If
  End With

  '-------------------------------------------------------------------------
  ' control: txtT2_NAME_... (show)
  '-------------------------------------------------------------------------
  With c("txtT2_NAME_" & sName)
    bDisabled = c("chkT2_USE_" & sName).Value = 0
    '.Enabled = Not bDisabled
    If ((sName = "IsrTmr") And (IsCpuTypeOK(r))) Then
       '.Enabled = bDisabled
       .Enabled = False  ' Added by jag on 6/4/6 in order to make function name to be disabled even if the interrupt is not selected.
    Else
       .Enabled = Not bDisabled
    End If
    .Text = FormatTextOutput(r.s("T21_NAME_" & sName), "S")
  End With

  With e("txtT2_NAME_" & sName)
    If (bDisabled) Then
        '.DisabledTooltipText = FormatNormalToolTip(TOOLTIP_DISABLED_FUNC_NAME)
        If ((sName = "IsrTmr") And (IsCpuTypeOK(r))) Then
          .DisabledTooltipText = FormatNormalToolTip(TOOLTIP_DISABLED_FUNC_SHAREDISR)
        Else
          .DisabledTooltipText = FormatNormalToolTip(TOOLTIP_DISABLED_FUNC_NAME)
        End If
    End If
  End With

  ShowFunction = True
ExitLabel:
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 successful 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 successful operation
'        False           else
'*****************************************************************************
Public Function OnChangingPageParameters(ps As Peripherals, Pagename As String) As Boolean
  On Error Resume Next

  Dim iPar As Integer
  Dim sName As String

  OnChangingPageParameters = False

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

    If (Not ps.AddFrame("fraT2_HFile", 100, 100, 8450, 700, "Header File")) Then Exit Function
    If (Not ps.AddText("txtT2_HFile", 1825, 250, 5000, 315)) Then Exit Function
    If (Not ps.AddLabel("lblT2_HFile", 0, 200, 300, 1575, 380, "File name")) Then Exit Function

  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 successful operation
'        False           else
'*****************************************************************************
Public Function OnShowPageParameters(ps As Peripherals, Controlname As String, Pagename As String, v() As Variant) As Boolean
  On Error GoTo OnShowError

  OnShowPageParameters = False
    
  Dim r As ProjectData
  Dim c As Collection
  Dim e As Collection
  Dim bDisabled As Boolean
  Dim sName As String
  Dim iPar As Integer

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


  If (Pagename = "Parameters") Then
    '-------------------------------------------------------------------------
    ' page: Parameters (action)
    '-------------------------------------------------------------------------
    Select Case Controlname
        Case "txtT2_HFile"
            If (IsInputFormatOK(v(1), FORMAT_FILES, c("lblT2_HFile").Caption)) Then
                r.v("T21_HFile") = v(1)
            Else
                Exit Function
            End If

        Case Else

    End Select

    '-------------------------------------------------------------------------
    ' page: Parameters (show)
    '-------------------------------------------------------------------------
    With c("lblT2_HFile")
        bDisabled = (r.v("T21_USE_Init") = 0)
        .Enabled = Not bDisabled
    End With
    With e("lblT2_HFile")
        If (bDisabled) Then
            .DisabledTooltipText = FormatNormalToolTip(TOOLTIP_DISABLED_HFILE_NAME)
        End If
    End With

    With c("txtT2_HFile")
        bDisabled = (r.v("T21_USE_Init") = 0)
        .Enabled = Not bDisabled
        .Text = FormatTextOutput(r.v("T21_HFile"), "S")
    End With
    With e("txtT2_HFile")
        If (bDisabled) Then
            .DisabledTooltipText = FormatNormalToolTip(TOOLTIP_DISABLED_HFILE_NAME)
        End If
    End With

  End If         ' if Pagename

  OnShowPageParameters = True
  Exit Function

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