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

Option Explicit

'*****************************************************************************
' Module:
'        GPT2
'-----------------------------------------------------------------------------
' Description:
'        This module contains all functions that are used in the 
'        GPT2 dialog script and in the GPT2 codegenerator script.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        2.0   ahe         13.06.00     final version for C167CS
'
'        ----------------------------------------------------------------------
'        2.1   ahe         02.08.00     final version for C167CS and C161CS/JC/JI
'                                       - check if TxEUD exist in functions
'                                         CuntUp and CountDown
'                                       - changes 'GPT2_vCountUp' to 
'                                         r.v(sVarPre & "_USE_CountUp")
'                                       - changes 'GPT2_vCountDown' to 
'                                         r.v(sVarPre & "_USE_CountDown")
'
'              ahe         13.09.00     - changes the functions descriptions
'
'        ---------------------------------------------------------------------
'              ahe         25.10.00    final version for 
'                                      - C167CS
'                                      - C161CS/JC/JI
'                                      - C167CR/SR r2
'
'              ahe         25.10.00    changes IsModuleDisabled (check SYSCON3) 
'
'*****************************************************************************


'*****************************************************************************
' Constants
'*****************************************************************************



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

  Dim sDescription As String
  Dim sReturnValue As String
  Dim sVarPre As String 
  Dim sFunctionName As String
  
  sVarPre = "GPT2"


  RefreshFunctionDescriptions = False

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

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


  '------------------------------------------------------------------------------------------------
  ' Description for GPT2_NAME_Init
  '------------------------------------------------------------------------------------------------
  sDescription = GetGeneralInitDescription(sVarPre)
  sReturnValue = "None" 
  If (Not func.AddFunction(sVarPre & "_NAME_Init", "void", sDescription, sReturnValue, 1)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT2_NAME_StartTmr
  '------------------------------------------------------------------------------------------------
  sFunctionName = "StartTmr"
  sDescription = "This macro starts the selected GPT2 timer. The timer continues " & _
                 "to count from where it had stopped. " & _
                 "The following definitions are available for TimerNr:" & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_5") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_6")
                 
  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "the name of the timer to be started"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT2_NAME_StopTmr
  '------------------------------------------------------------------------------------------------
  sFunctionName = "StopTmr"
  sDescription = "This macro stops the selected GPT2 timer. The contents of " & _
                 "the timer register remain unchanged. " & _
                 "The following definitions are available for TimerNr:" & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_5") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_6")
                 
  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "the name of the timer to be stopped"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT2_NAME_ClearTmr
  '------------------------------------------------------------------------------------------------
  sFunctionName = "ClearTmr"
  sDescription = "This macro stops the selected GPT2 timer and sets the timer " & _
                 "register to 0. " & _
                 "The following definitions are available for TimerNr:" & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_5") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_6")
                
  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "the name of the timer to be cleared"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT2_NAME_ReadTmr
  '------------------------------------------------------------------------------------------------
  sFunctionName = "ReadTmr"
  sDescription = "This macro returns the contents of the selected GPT2 timer. " & _
                 "The timer is not stopped. Access of the CPU to the " & _
                 "registers of the timer is of higher priority than a timer " & _
                 "increment, a reload or a capture event. " & _
                 "The following definitions are available for TimerNr:" & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_5") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_6")

  sReturnValue = "current 16-bit value of the timer"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "the name of the timer to be readed"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT2_NAME_LoadTmr
  '------------------------------------------------------------------------------------------------
  sFunctionName = "LoadTmr"
  sDescription = "This macro loads the selected GPT2 timer with the forwarded " & _
                 "value. The timer is not stopped. The access of the CPU to " & _
                 "the registers of the timers is of higher priority than a " & _
                 "timer increment, a reload or a capture event. " & _
                 "The following definitions are available for TimerNr:" & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_5") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_6")
                
  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "the name of the timer to be loaded"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function
    sDescription = "16-bit value to be loaded"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Para2", "Value", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT2_NAME_ReadCAPREL
  '------------------------------------------------------------------------------------------------
  sFunctionName = "ReadCAPREL"
  sDescription = "This macro returns the content of the CAPREL register. " & _
                 "This can be the capture value of T5 or the reload value of " & _
                 "T6."

  sReturnValue = "current 16-bit value of the CAPREL register"

  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT2_NAME_LoadCAPREL
  '------------------------------------------------------------------------------------------------
  sFunctionName = "LoadCAPREL"
  sDescription = "This macro loads the CAPREL register with the forwarded value."

  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "16-bit value to loaded in the CAPREL register"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Para1", "Value", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT2_NAME_Tmr5
  '------------------------------------------------------------------------------------------------
  sFunctionName = "Tmr5"
  sDescription = "This is the interrupt service routine for the GPT2 timer 5. " & _
                 "It is called up in the case of over or underflow of the " & _
                 "timer 5 register." & vbCrLf & _
                 ""                
                 sDescription = sDescription & vbcrlf & TOOLTIP_ISR_USERCODE

  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "void", sDescription, sReturnValue, 2)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT2_NAME_Tmr6
  '------------------------------------------------------------------------------------------------
  sFunctionName = "Tmr6"
  sDescription = "This is the interrupt service routine for the GPT2 timer 6. " & _
                 "It is called up in the case of over or underflow of the " & _
                 "timer 6 register." & vbCrLf & _
                 ""                
                 sDescription = sDescription & vbcrlf & TOOLTIP_ISR_USERCODE

  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "void", sDescription, sReturnValue, 2)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT2_NAME_CAPREL
  '------------------------------------------------------------------------------------------------
  sFunctionName = "CAPREL"
  sDescription = "This is the interrupt service routine for the GPT2 CAPREL. " & _
                 "It is called up in the case of over or underflow of the " & _
                 "CAPREL register." & vbCrLf & _
                 ""                
                 sDescription = sDescription & vbcrlf & TOOLTIP_ISR_USERCODE
                
  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "void", sDescription, sReturnValue, 2)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT2_NAME_SetToggLatch
  '------------------------------------------------------------------------------------------------
  sFunctionName = "SetToggLatch"
  sDescription = "This macro sets the T6OTL (timer output toggle latch) bit " & _
                 "in T6CON. This bit is toggled on each over/underflow of T6. " & _
                 "If the alternative output function is enabled, the port " & _
                 "T6OUT is set depending on T6OTL. This macro sets the bit " & _
                 "T6OTL on high. This can influence the output signal at " & _
                 "T6OUT by SW." 
                
  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT2_NAME_ClrToggLatch
  '------------------------------------------------------------------------------------------------
  sFunctionName = "ClrToggLatch"
  sDescription = "This macro resets the T6OTL (timer output toggle latch) bit " & _
                 "in T6CON. This bit is toggled on each over/underflow of T6. " & _
                 "If the alternative output function is enabled, the port " & _
                 "T6OUT is set depending on T6OTL. This macro sets the bit " & _
                 "T6OTL on low. This can influence the output signal at " & _
                 "T6OUT by SW."

  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT2_NAME_CountUp
  '------------------------------------------------------------------------------------------------
  sFunctionName = "CountUp"
  sDescription = "This macro sets the counter direction of the selected timer " & _
                 "to up. The timer is not stopped." & vbCrLf & _
                 "Note:" & vbcrlf & _
                 ""
                 If (r.Regs.BitFind("T5CON_T5UDE") = True Or r.Regs.BitFind("T6CON_T6UDE") = True) Then
                     sDescription = sDescription & _
                     "Concerning timer x it is possible to make the counter " & _
                     "direction dependent on the level at TxEUD. If this " & _
                     "functionality has been selected, then function " & r.v(sVarPre & "_NAME_CountUp") & " " & _
                     "set a  0 at TxEUD enabling counting up of timer x and a " & _
                     "1 at TxEUD enabling counting down of timer x. "
                 End If
                 sDescription = sDescription & _
                 "The following definitions are available for TimerNr:" & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_5") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_6")
                
  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "the name of the timer to be used"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function

  '------------------------------------------------------------------------------------------------
  ' Description for GPT2_NAME_CountDown
  '------------------------------------------------------------------------------------------------
  sFunctionName = "CountDown"
  sDescription = "This macro sets the counter direction of the selected timer " & _
                 "to down. The timer is not stopped." & vbCrLf & _
                 "Note:" & vbcrlf & _
                 ""
                 If (r.Regs.BitFind("T5CON_T5UDE") = True Or r.Regs.BitFind("T6CON_T6UDE") = True) Then
                     sDescription = sDescription & _
                     "Concerning timer x it is possible to make the counter " & _
                     "direction dependent on the level at TxEUD. If this " & _
                     "functionality has been selected, then function " & r.v(sVarPre & "_NAME_CountDown") & " " & _
                     "set a 0 at TxEUD enabling counting down of timer x and " & _
                     "a 1 at TxEUD enabling counting up of timer x. "
                 End If
                 sDescription = sDescription & _
                 "The following definitions are available for TimerNr:" & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_5") & vbCrLf & _
                 "" & r.v(sVarPre & "_PARA_TIMER_6")
                
  sReturnValue = "None"
  
  If (Not func.AddFunction(sVarPre & "_NAME_" & sFunctionName, "", sDescription, sReturnValue, 0)) Then Exit Function
    sDescription = "the name of the timer to be used"
    If (Not func.AddParameter(sVarPre & "_NAME_" & sFunctionName & "_Para1", "TimerNr", "", sDescription)) Then Exit Function

 	If (Not func.Sort) Then Exit Function

  RefreshFunctionDescriptions = True
End Function

'*****************************************************************************
'Description:
'        For each parameter that should be used as a constant (on page "Parameters")  
'        a line "If (Not func.AddParameter .." must be insert in this function.
'
'        Note:
'        For each added parameter a project variable with the name 
'        "DUMMY_PARA_Parametername" must be exist in the *.vars file. 
'-----------------------------------------------------------------------------
'Parameters:
'        vIndex           parameter of the module which was added with AddModule
'        func             reference to the FunctionClass object
'        r                reference to the Registers object
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Public Function RefreshParameterDescriptions(vIndex As Variant, para As ParameterClass, r As ProjectData) As Boolean
  On Error Resume Next 

  RefreshParameterDescriptions = False

  Dim sDescription As String
  Dim sVarPre As String
  Dim sRegPre As String
  Dim sParaName As String
  Dim i As Integer

  sVarPre = "GPT2" & vIndex
  sRegPre = sVarPre & "_"


  ' for each parameter of this module the property AddParameter must be called
  '
  ' AddParameter(sParameterName As String, sParameterDescription As String) As Boolean
  '
  ' sParameterName         | String | parameter name as key (use the name of the variable 
  '                                   for the parameter name e.g. SSC_PARA_MODE) 
  ' sParameterDescription  | String | description of the parameter

  For i = 5 To 6
      '------------------------------------------------------------------------------------------------
      ' Description for DUMMY_PARA_xx
      '------------------------------------------------------------------------------------------------
      sDescription = "This parameter identifies " & sVarPre & " timer " & i
      sParaName = "TIMER_" & i
      If (Not para.AddParameter(sVarPre & "_PARA_" & sParaName, sDescription)) Then Exit Function
  Next i
  
 	
 	If (Not para.Sort) Then Exit Function

  RefreshParameterDescriptions = True
End Function

'*****************************************************************************
'Description:
'          This function returns the module clock (in MHz) of the 
'          GPT2 module depending on the configured modes
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'
'-----------------------------------------------------------------------------
'Returns:
'      GetSystemClock
'
'*****************************************************************************
Function GetGPT2ModuleClock(r As ProjectData, vIndex As Variant) As Double
  On Error Goto GetGPT2ModuleClockError
  GetGPT2ModuleClock = GetSystemClock(r) 
  Exit Function

  GetGPT2ModuleClockError:
  GetGPT2ModuleClock = 1
  Exit Function

End Function


'*****************************************************************************
'Description:
'        This function returns the disable bit name of SYSCON3 register (e.g GPTDIS).
'-----------------------------------------------------------------------------
'Parameters:
'        r               reference to the ProjectData object
'        sVarPre         module name
'
'-----------------------------------------------------------------------------
'Returns:
'       disable bit name
'*****************************************************************************
Function GetGPT2DisebleBitName(r As ProjectData, sVarPre As String) As String
    On Error Resume Next

    If (r.Regs.BitFind("SYSCON3_GPTDIS") = True) Then
        GetGPT2DisebleBitName = "GPTDIS"
    ElseIf (r.Regs.BitFind("SYSCON3_PERDIS3") = True) Then
        GetGPT2DisebleBitName = "PERDIS3"
    Else
        GetGPT2DisebleBitName = ""
    End If


End Function

'*****************************************************************************
'Description:
'        This function check  the SYSCON3 register, whether the modul is disabled
'-----------------------------------------------------------------------------
'Parameters:
'        r               As ProjectData
'        sVarPre         modul name (e.g GPT1)
'-----------------------------------------------------------------------------
'Returns:
'        True  -  if the modul is disabled
'        False -  else
'
'*****************************************************************************
Function IsModuleDisabled(r As ProjectData, sVarPre As String) As Boolean

  IsModuleDisabled = False

  If Not(GetGPT2DisebleBitName(r, sVarPre) = "") Then
      If (r.v("SYSCON3_" & GetGPT2DisebleBitName(r, sVarPre)) = 1) Then
        IsModuleDisabled = True
      End If
  End If

End Function


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

  sVarPre = "GPT2" 
  
  GetTooltipModuleDisabled = ""            
  GetTooltipModuleDisabled =  GetTooltipModuleDisabled & FormatDisabledToolTip("the module is disabled")
End Function

'*****************************************************************************
'Description:
'        This function is called from the OnShowPageGPT2 function 
'        und ist notwendig fuer die Anzeige von cboT6I.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        iExp            der Index aus der For - Next - Schleife 
'-----------------------------------------------------------------------------
'Returns:
'        Text           vom Typ String
'*****************************************************************************
Function GetPrescalerResolution(r As ProjectData, iExp As Integer) As String
  On Error Resume Next
  Dim Exponent As Integer
  Dim vIndex As Variant
  
  GetPrescalerResolution = ""
  Exponent = 2 + iExp
  GetPrescalerResolution = "Fcpu / " & 2^Exponent
  GetPrescalerResolution = GetPrescalerResolution & " (Resolution: " & Format$(2^Exponent / GetGPT2ModuleClock(r, vIndex), "0.000") & " µs)"

  
End Function 


'*****************************************************************************
'Description:
'        This function is called from the OnShowPageCoreTimer3 function 
'        and calculate underflow time.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        TimerNr
'-----------------------------------------------------------------------------
'Returns:
'        underflow time [µs]
'        
'*****************************************************************************
Function GetUnderflow(r As ProjectData, TimerNr As Variant) As Double
  On Error Resume Next
  Dim i As Integer
  Dim vIndex As Variant

  For i = 0 To 7
    If r.v("T" & TimerNr & "CON_T" & TimerNr & "I") = i Then
      GetUnderflow = (r.v("T" & TimerNr & "_T" & TimerNr & "") + 1) * ( (4 * 2^(i) ) / GetGPT2ModuleClock(r, vIndex) )
    End If
  Next i

End Function


'*****************************************************************************
'Description:
'        This function is called from the GPT2 module
'        and calculate overflow time [ms].
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        TimerNr
'-----------------------------------------------------------------------------
'Returns:
'        overflow time [µs]
'
'*****************************************************************************
Function GetOverflow(r As ProjectData, TimerNr As Variant) As Double
  On Error Resume Next
  Dim i As Integer
  Dim vIndex As Variant

  For i = 0 To 7
    If r.v("T" & TimerNr & "CON_T" & TimerNr & "I") = i Then
      GetOverflow = ( 65536 - r.v("T" & TimerNr & "_T" & TimerNr & "")) * ( (4 * 2^(i) ) / GetGPT2ModuleClock(r, vIndex) )
    End If
  Next i

End Function



'*****************************************************************************
'Description:
'        This function is called from the OnShowPageGPT1 function 
'        and calculate the data of the timer gegister T3.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        t               entered time [µs]
'        rIndex          
'             True       for overflow time
'             False      for underflow time    
'        TimerNr
'-----------------------------------------------------------------------------
'Returns:
'        Value of T3
'
'*****************************************************************************
Function GetTimerNrValue(r As ProjectData, t As Variant, rIndex As Boolean, TimerNr As Variant) As Long
  On Error Resume Next
  Dim i As Integer
  Dim vIndex As Variant
  
  If (t > GetMaxResolution(r, TimerNr)) Then
    t = GetMaxResolution(r, TimerNr)
  End If
  If (t < GetMinResolution(r, TimerNr)) Then
    t = GetMinResolution(r, TimerNr)
  End If

  
  For i = 0 To 7
    If r.v("T" & TimerNr & "CON_T" & TimerNr & "I") = i Then
      If (rIndex) Then
        GetTimerNrValue = 65536 - t / ( (4 * 2^(i) ) / GetGPT2ModuleClock(r, vIndex) )
        Exit Function 
      Else
        GetTimerNrValue = t / ( (4 * 2^(i) ) / GetGPT2ModuleClock(r, vIndex) ) - 1
        Exit Function 
      End If
    End If
  Next i

End Function 



'*****************************************************************************
'Description:
'        This function is called from the GPT1 module  
'        and calculate the maximum resolution [ms] for timer clock.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        TimerNr         
'-----------------------------------------------------------------------------
'Returns:
'        Value of the max. resolution [µs]
'
'*****************************************************************************
Function GetMaxResolution(r As ProjectData, TimerNr As Variant) As Double
  On Error Resume Next
  Dim vIndex As Variant
  
  GetMaxResolution = 65536 * 4 * 2^(r.v("T" & TimerNr & "CON_T" & TimerNr & "I")) / GetGPT2ModuleClock(r, vIndex)

End Function 

'*****************************************************************************
'Description:
'        This function is called from the GPT1 module  
'        and calculate the maximum resolution [ms] for timer clock.
'-----------------------------------------------------------------------------
'Parameters:
'        r               Typ von ProjektData
'        TimerNr         
'-----------------------------------------------------------------------------
'Returns:
'        Value of the max. resolution [µs]
'
'*****************************************************************************
Function GetMinResolution(r As ProjectData, TimerNr As Variant) As Double
  On Error Resume Next
  Dim vIndex As Variant
  
  GetMinResolution = 4 * 2^(r.v("T" & TimerNr & "CON_T" & TimerNr & "I")) / GetGPT2ModuleClock(r, vIndex)

End Function 


