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

Option Explicit
'*****************************************************************************
' Module:
'        GPT1
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions for generating code for the
'        module 'GPT1'.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ----------------------------------------------------------------------
'        1.0  ahe            27.04.00     final version for C165UTAH, C165H
'
'        ----------------------------------------------------------------------
'        1.1  ahe            28.09.00     final version for C165UTAH, C165H
'                                         - changes discription for TxUD in incremental mode
'        ----------------------------------------------------------------------
'        4.0  ahe            19.04.01     final version for Pegasus
'
'        4.1  ahe            08.08.02     no changes
'        4.2  ste            16.12.02     in functions CheckRotation and CheckEdge
'                                         the initialization of the return value 
'                                         changed
'     
'*****************************************************************************



'*****************************************************************************
'Description:
'        This functions initiates the code generation for the GPT1 module.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function StartGenerateCode(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo StartGenerateCodeError
    StartGenerateCode = False

    ' write files GPT1.h
    If (r.v("GPT1_USE_Init")) Then
      If (Not GPT1GenerateH(r, vIndex)) Then Exit Function
	 	   If (Not r.AddFile(r.s("GPT1_HFILE"))) Then Exit Function
    End if

    ' write file GPT1.c
    If (r.v("GPT1_USE_Init")) Then
      If (Not GPT1GenerateC(r, vIndex)) Then Exit Function
	 	   If (Not r.AddFile(r.s("GPT1_CFILE"))) Then Exit Function
    End if

        
    StartGenerateCode = True
    Exit Function
    
StartGenerateCodeError:
    Errorbox("Code generation for GPT1 failed", Error$)
    Exit Function
End Function


'*****************************************************************************
'Description:
'        Generates the code for the GPT1.c file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function GPT1GenerateC(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo GPT1GenerateCError
    
    Dim i         As Integer
    Dim f         As Variant
    Dim v         As Variant
    Dim d         As String
    Dim dc        As DAvECollection
    Dim uc        As UserCode
    Dim func      As FunctionClass
    DIm sVarPre   As String
    Dim n         As String
    Dim sAlternatePinName(1 To 7) As String
    Dim TimerNr   As Integer


    GPT1GenerateC = False

    sVarPre = "GPT1"

    Set dc = ObjectCollection("MODULENAMES")
    Set uc = New UserCode
    Set func = New  FunctionClass

    'refresh the decriptions of the functions and macros  
    RefreshFunctionDescriptions(vIndex, func, r)

    'read usercode
    uc.ReadFile((StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v(sVarPre & "_CFILE"))

    'open GPT1.c
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v(sVarPre & "_CFILE") For Output As f
    
    '------------------------------------------------------------------------------------------------
    ' module header
    '------------------------------------------------------------------------------------------------
    d = FormatModuleDescription(GetModuleHeaderC(sVarPre))
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), dc(sVarPre).ModuleDescription, r.v(sVarPre & "_CFILE"), r.v("MAIN_CPU_TYPE"), r.v("MAIN_COMPILER"), d)
    
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,1)",0)    'USER CODE for application specific descriptions'
    Print #f, ""
    Print #f, Rtf.GenerateNote((r.v(sVarPre & "_Notes")))
    Print #f, ""
    Print #f, SECTION_PROJECT_INCLUDES
    Print #f, ""  'DAvE includes'
    Print #f, "#include """ & r.v("MAIN_HFile") & """"
    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,2)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_MACROS
    Print #f, ""  'DAvE local macro definitions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,3)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_DEFINES
    Print #f, ""  'DAvE local #define definitions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,4)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_TYPEDEFS
    Print #f, ""  'DAvE type defintions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,5)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_IMPORTED_GLOBAL_VARIABLES
    Print #f, ""  'DAvE external declarations of imported global variables'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,6)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_GLOBAL_VARIABLES
    Print #f, ""  'DAvE definitions of global variables'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,7)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_EXTERNAL_PROTOTYPES
    Print #f, ""  'DAvE external declarations of imported functions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,8)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_LOCAL_PROTOTYPES
    Print #f, ""  'DAvE declarations of internal local functions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,9)",0)
    Print #f, ""
    Print #f, ""
    
    '------------------------------------------------------------------------------------------------
    ' GPT1_vInit function
    '------------------------------------------------------------------------------------------------
    n = "Init"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
      Print #f, "{"
      Print #f, uc.GetUserCode("(" & n & ",2)",2) 
      Print #f, ""

      '------------------------------------------------------------------------------------------------
      ' Configuration of Timer Block Prescaler 1:
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of Timer Block Prescaler 1:")

      d = "prescaler for timer block 1 is " & _
          GetBPS1Factor(r.v(GPT12_ & "T3CON_BPS1"))

      Print #f, FormatCommentsForInit(1, d)    
      Print #f, ""
      Print #f, ""

      '------------------------------------------------------------------------------------------------
      ' Configuration of the core timer 3:
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of the " & sVarPre & " Core Timer 3:")

      TimerNr = 3
      Call GetTimerConfig(f, r, TimerNr, vIndex)
      Print #f, ""

      '------------------------------------------------------------------------------------------------
      ' Configuration of the core timer 2:
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of the " & sVarPre & " Auxiliary Timer 2:")

      TimerNr = 2
      Call GetTimerConfig(f, r, TimerNr, vIndex)
      Print #f, ""

      '------------------------------------------------------------------------------------------------
      ' Configuration of the core timer 4:
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of the " & sVarPre & " Auxiliary Timer 4:")

      TimerNr = 4
      Call GetTimerConfig(f, r, TimerNr, vIndex)
      Print #f, ""

      '------------------------------------------------------------------------------------------------
      ' Configuration of the used SSC Port Pins:
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of the used " & sVarPre & " Port Pins:")
      v = 0
      i = 1

      If (GetPinFunction(r, "T3OUT") = 2) Then
        sAlternatePinName(i) = "T3OUT"
        i = i + 1
      End If

      If (GetPinFunction(r, "T2EUD") = 1) Then
        sAlternatePinName(i) = "T2EUD"
        i = i + 1
      End If
      If (GetPinFunction(r, "T2IN") = 1) Then
        sAlternatePinName(i) = "T2IN"
        i = i + 1
      End If
      If (GetPinFunction(r, "T3EUD") = 1) Then
        sAlternatePinName(i) = "T3EUD"
        i = i + 1
      End If
      If (GetPinFunction(r, "T3IN") = 1) Then
        sAlternatePinName(i) = "T3IN"
        i = i + 1
      End If
      If (GetPinFunction(r, "T4EUD") = 1) Then
        sAlternatePinName(i) = "T4EUD"
        i = i + 1
      End If
      If (GetPinFunction(r, "T4IN") = 1) Then
        sAlternatePinName(i) = "T4IN"
        i = i + 1
      End If

      Call SetAlternatePinStatements(f, r, sAlternatePinName())
      Print #f, ""

      '------------------------------------------------------------------------------------------------
      ' Configuration of the used GPT1 Interrupts:
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of the used " & sVarPre & " Interrupts:")
      ' timer 2 interrupt
      TimerNr = 2
      Call GenerateIR(f, r, GPT12_ & "T" & TimerNr & "IC", "timer " & TimerNr)
      ' timer 3 interrupt
      TimerNr = 3
      Call GenerateIR(f, r, GPT12_ & "T" & TimerNr & "IC", "timer " & TimerNr)
      ' timer 4 interrupt
      TimerNr = 4
      Call GenerateIR(f, r, GPT12_ & "T" & TimerNr & "IC", "timer " & TimerNr)


      Print #f, ""
      Print #f, uc.GetUserCode("(" & sVarPre & "_Function,3)",2) 
      Print #f, ""

      TimerNr = 2
      If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "R") = 1) Then
        Print #f, FormatCodeStatemente(2, GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "R", "=", 1, "set timer " & TimerNr & " run bit")
        Print #f, ""
      End If
      If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "RC") = 1) Then
        Print #f, FormatCodeStatemente(2, GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "RC", "=", 1, "set timer " & TimerNr & " remote control bit")
        Print #f, ""
      End If

      TimerNr = 4
      If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "R") = 1) Then
        Print #f, FormatCodeStatemente(2, GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "R", "=", 1, "set timer " & TimerNr & " run bit")
        Print #f, ""
      End If
      If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "RC") = 1) Then
        Print #f, FormatCodeStatemente(2, GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "RC", "=", 1, "set timer " & TimerNr & " remote control bit")
        Print #f, ""
      End If

      TimerNr = 3
      If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "R") = 1) Then
        Print #f, FormatCodeStatemente(2, GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "R", "=", 1, "set timer " & TimerNr & " run bit")
        Print #f, ""
      End If


      Print #f, "} " & END_OF_FUNCTION & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' GPT1_viTmr3 function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    TimerNr = 3
    n = "Tmr" & TimerNr
    If (IsISRSelected(r, sVarPre, GPT12_ & "T" & TimerNr & "IC", n)) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
        Print #f, ""
        Print #f, GenerateIsrDeclaration(r, r.v(sVarPre & "_NAME_" & n), "T" & TimerNr & "INT")
        Print #f, "{"
        Print #f, uc.GetUserCode("(" & n & ",2)",2) 
        Print #f, ""

        If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 6) Then
          Print #f, "  if(" & GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "CHDIR)"
          Print #f, "  {"
          Print #f, ""
          Print #f, uc.GetUserCode("(" & n & ",3)",4) 
          Print #f, ""
          Print #f, "    " & GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "CHDIR = 0;"
          Print #f, "  }"
        End If

        If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 7) Then
          Print #f, "  if(" & GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "EDGE)"
          Print #f, "  {"
          Print #f, ""
          Print #f, uc.GetUserCode("(" & n & ",4)",4) 
          Print #f, ""
          Print #f, "    " & GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "EDGE = 0;"
          Print #f, "  }"
        End If

        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & ",5)",2) 
        Print #f, ""
        Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
        Print #f, ""
        Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' GPT1_viTmr2 function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    TimerNr = 2
    n = "Tmr" & TimerNr
    If (IsISRSelected(r, sVarPre, GPT12_ & "T" & TimerNr & "IC", n)) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, GenerateIsrDeclaration(r, r.v(sVarPre & "_NAME_" & n), "T" & TimerNr & "INT")
      Print #f, "{"
      Print #f, uc.GetUserCode("(" & n & ",2)",2) 
      Print #f, ""

      If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "IRDIS") = 0) Then
        If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 6) Then
          Print #f, "  if(" & GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "CHDIR)"
          Print #f, "  {"
          Print #f, ""
          Print #f, uc.GetUserCode("(" & n & ",3)",4) 
          Print #f, ""
          Print #f, "    " & GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "CHDIR = 0;"
          Print #f, "  }"
        End If

        If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 7) Then
          Print #f, "  if(" & GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "EDGE)"
          Print #f, "  {"
          Print #f, ""
          Print #f, uc.GetUserCode("(" & n & ",4)",4) 
          Print #f, ""
          Print #f, "    " & GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "EDGE = 0;"
          Print #f, "  }"
        End If
      End If

      Print #f, ""
      Print #f, uc.GetUserCode("(" & n & ",5)",2) 
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' GPT1_viTmr4 function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    TimerNr = 4
    n = "Tmr" & TimerNr
    If (IsISRSelected(r, sVarPre, GPT12_ & "T" & TimerNr & "IC", n)) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, GenerateIsrDeclaration(r, r.v(sVarPre & "_NAME_" & n), "T" & TimerNr & "INT")
      Print #f, "{"
      Print #f, uc.GetUserCode("(" & n & ",2)",2) 
      Print #f, ""

      If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "IRDIS") = 0) Then
        If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 6) Then
          Print #f, "  if(" & GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "CHDIR)"
          Print #f, "  {"
          Print #f, ""
          Print #f, uc.GetUserCode("(" & n & ",3)",4) 
          Print #f, ""
          Print #f, "    " & GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "CHDIR = 0;"
          Print #f, "  }"
        End If

        If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 7) Then
          Print #f, "  if(" & GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "EDGE)"
          Print #f, "  {"
          Print #f, ""
          Print #f, uc.GetUserCode("(" & n & ",4)",4) 
          Print #f, ""
          Print #f, "    " & GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "EDGE = 0;"
          Print #f, "  }"
        End If
      End If

      Print #f, ""
      Print #f, uc.GetUserCode("(" & n & ",5)",2) 
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' GPT1_ubCheckRotation function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "CheckRotation"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
      Print #f, "{"
      Print #f, "  ubyte ubReturnValue;"
      Print #f, ""
      Print #f, "  ubReturnValue = 0;"

      For i = 2 To 4
          Print #f, "  if(TimerNr == " & i & " && " & GPT12_ & "T" & i & "CON_T" & i & "CHDIR == 1)"
          Print #f, "  {"
          Print #f, "    ubReturnValue = 1;"
          Print #f, "    " & GPT12_ & "T" & i & "CON_T" & i & "CHDIR = 0;"
          Print #f, "  }"
          Print #f, ""
      Next i

      Print #f, ""
      Print #f, "  return(ubReturnValue);"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' GPT1_ubCheckRotation function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "CheckEdge"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
      Print #f, "{"
      Print #f, "  ubyte ubReturnValue;"
      Print #f, ""
      Print #f, "  ubReturnValue = 0;"

      For i = 2 To 4
          Print #f, "  if(TimerNr == " & i & " && " & GPT12_ & "T" & i & "CON_T" & i & "EDGE == 1)"
          Print #f, "  {"
          Print #f, "    ubReturnValue = 1;"
          Print #f, "    " & GPT12_ & "T" & i & "CON_T" & i & "EDGE = 0;"
          Print #f, "  }"
          Print #f, ""
      Next i

      Print #f, ""
      Print #f, "  return(ubReturnValue);"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' Other function
    '------------------------------------------------------------------------------------------------

    Print #f, ""
    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,10)",0)
    Print #f, ""
    
    'close file
    Close f
    
    GPT1GenerateC = True
    Exit Function
    
GPT1GenerateCError:
    Close f
    Exit Function
End Function


'*****************************************************************************
'Description:
'        Generates the code for the GPT1.h file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function GPT1GenerateH(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo GPT1GenerateHError
    
    Dim i         As Integer
    Dim f         As Variant
    Dim d         As String
    Dim dc        As DAvECollection
    Dim uc        As UserCode
    Dim func      As FunctionClass
    Dim para      As ParameterClass
    Dim sVarPre   As String
    Dim n         As String
    Dim v         As Integer
    Dim l         As Long
   
    GPT1GenerateH = False

    sVarPre = "GPT1"

    Set dc = ObjectCollection("MODULENAMES")
    Set uc = New UserCode
    Set func = New  FunctionClass
    Set para = New ParameterClass

    'refresh the decriptions of the functions and macros  
    RefreshFunctionDescriptions(vIndex, func, r)
    
    'refresh the decriptions of the parameters
    RefreshParameterDescriptions(vIndex, para, r)

    'read usercode
    uc.ReadFile((StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v(sVarPre & "_HFILE"))
    
    'open GPT1.h
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v(sVarPre & "_HFILE") For Output As f
    
    '------------------------------------------------------------------------------------------------
    ' module header
    '------------------------------------------------------------------------------------------------
    d = FormatModuleDescription(GetModuleHeaderH(sVarPre))
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), dc(sVarPre).ModuleDescription, r.v(sVarPre & "_HFILE"), r.v("MAIN_CPU_TYPE"), r.v("MAIN_COMPILER"), d)
            
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,1)",0)
    Print #f, ""
    Print #f, Rtf.GenerateNote((r.v(sVarPre & "_Notes")))
    Print #f, ""
    Print #f, "#ifndef _" & StripExtension(r.v(sVarPre & "_HFILE")) & "_H_"
    Print #f, "#define _" & StripExtension(r.v(sVarPre & "_HFILE")) & "_H_"
    Print #f, ""
    Print #f, SECTION_PROJECT_INCLUDES
    Print #f, ""  'DAvE includes'
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,2)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_MACROS
    Print #f, ""  'DAvE global macro definitions; not used as functions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,3)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_DEFINES
    Print #f, ""  'DAvE global #define definitions'

    n = "TIMER_2"
    Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "2")
    n = "TIMER_3"
    Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "3")
    n = "TIMER_4"
    Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "4")

    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,4)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_TYPEDEFS
    Print #f, ""  'DAvE type definitions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,5)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_IMPORTED_GLOBAL_VARIABLES
    Print #f, ""  'DAvE external declarations of imported global variables'
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,6)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_GLOBAL_VARIABLES
    Print #f, ""  'DAvE definition of global variables'
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,7)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_GLOBAL_PROTOTYPES
    Print #f, ""  'DAvE declarations of functions which should become global'
    
    ' for each function in this 'func' object
    For l = 1 To func.FunctionCount
      ' get the function name which was insert in 'Include.inc' with
      'AddFunction()'
      d = func.FunctionName(l)
      ' get only the third part of the function name (e.g "Init")
      n = Right(d, Len(d) - Instr(d, "NAME") - Len("NAME"))
      ' only if the use variable of the function is selected and the function is a really function
      ' not a macro or a ISR
      If (r.v(sVarPre & "_USE_" & n) = 1 AND func.FunctionSort(d) = 1) Then
            Print #f, FormatFunctionDeclaration(r, func, d) & ";"
      End If
    Next l
    Print #f, ""
    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,8)",0)
    Print #f, ""
    Print #f, ""
                          'DAvE declarations of global macros'
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' GPT1_vStartTmr macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "StartTmr"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " " & r.v(sVarPre & "_NAME_" & n) & "_##TimerNr()" 
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_2") & "() " & GPT12_ & "T2CON_T2R = 1"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_3") & "() " & GPT12_ & "T3CON_T3R = 1"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_4") & "() " & GPT12_ & "T4CON_T4R = 1"
      Print #f, ""                                                                                    
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' GPT1_vStopTmr macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "StopTmr"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " " & r.v(sVarPre & "_NAME_" & n) & "_##TimerNr()" 
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_2") & "() " & GPT12_ & "T2CON_T2R = 0; " & GPT12_ & "T2CON_T2RC = 0"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_3") & "() " & GPT12_ & "T3CON_T3R = 0"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_4") & "() " & GPT12_ & "T4CON_T4R = 0; " & GPT12_ & "T4CON_T4RC = 0"
      Print #f, ""                                                                                    
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' GPT1_vClearTmr macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ClearTmr"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " " & r.v(sVarPre & "_NAME_" & n) & "_##TimerNr()" 
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_2") & _
                "() " & GPT12_ & "T2CON_T2R = 0; " & GPT12_ & "T2 = 0x0000; " & GPT12_ & "T2CON_T2RC = 0"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_3") & _
                "() " & GPT12_ & "T3CON_T3R = 0; " & GPT12_ & "T3 = 0x0000"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_4") & _
                "() " & GPT12_ & "T4CON_T4R = 0; " & GPT12_ & "T4 = 0x0000; " & GPT12_ & "T4CON_T4RC = 0"
      Print #f, ""                                                                                    
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' GPT1_vReadTmr macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ReadTmr"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        

      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " " & r.v(sVarPre & "_NAME_" & n) & "_##TimerNr()" 
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_2") & _
                "() " & GPT12_ & "T2"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_3") & _
                "() " & GPT12_ & "T3"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_4") & _
                "() " & GPT12_ & "T4"

      Print #f, ""                                                                                    
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' GPT1_vLoadTmr macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "LoadTmr"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        

      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " " & r.v(sVarPre & "_NAME_" & n) & "_##TimerNr(Value)" 
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_2") & _
                "(Value) " & GPT12_ & "T2 = Value"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_3") & _
                "(Value) " & GPT12_ & "T3 = Value"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_4") & _
                "(Value) " & GPT12_ & "T4 = Value"

      Print #f, ""                                                                                    
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' GPT1_vSetToggLatch macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetToggLatch"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " " & GPT12_ & "T3CON_T3OTL = 1" 
      Print #f, ""                                                                                    
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' GPT1_vClrToggLatch macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ClrToggLatch"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " " & GPT12_ & "T3CON_T3OTL = 0" 
      Print #f, ""                                                                                    
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' GPT1_vCountUp macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "CountUp"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " " & r.v(sVarPre & "_NAME_" & n) & "_##TimerNr()
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_2") & "() " & GPT12_ & "T2CON_T2UD = 0"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_3") & "() " & GPT12_ & "T3CON_T3UD = 0"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_4") & "() " & GPT12_ & "T4CON_T4UD = 0"
      Print #f, ""                                                                                    
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' GPT1_vCountDown macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "CountDown"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " " & r.v(sVarPre & "_NAME_" & n) & "_##TimerNr()
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_2") & "() " & GPT12_ & "T2CON_T2UD = 1"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_3") & "() " & GPT12_ & "T3CON_T3UD = 1"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_4") & "() " & GPT12_ & "T4CON_T4UD = 1"
      Print #f, ""                                                                                    
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' GPT1_GetCountDirection macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetCountDirection"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " " & r.v(sVarPre & "_NAME_" & n) & "_##TimerNr()
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_2") & "() " & GPT12_ & "T2CON_T2RDIR"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_3") & "() " & GPT12_ & "T3CON_T3RDIR"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_4") & "() " & GPT12_ & "T4CON_T4RDIR"
      Print #f, ""                                                                                    
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' GPT1_vSetTmrRemoteControl macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetTmrRemoteControl"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " " & r.v(sVarPre & "_NAME_" & n) & "_##TimerNr()" 
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_2") & "() " & GPT12_ & "T2CON_T2RC = 1"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_4") & "() " & GPT12_ & "T4CON_T4RC = 1"
      Print #f, ""                                                                                    
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' Interrupt Vectors
    '------------------------------------------------------------------------------------------------
    Print #f, SECTION_INTERRUPT_VECTORS
    Print #f, ""  'DAvE interrupt vector definitions
    
    n = "Tmr2"
    If (IsISRSelected(r, sVarPre, GPT12_ & "T2IC", n)) Then
      v = 34  
      Print #f, "#define T2INT " & FormatTextOutput(v, "H2")
    End If
    
    n = "Tmr3"
    If (IsISRSelected(r, sVarPre, GPT12_ & "T3IC", n)) Then
      v = 35
      Print #f, "#define T3INT " & FormatTextOutput(v, "H2")
    End If
    
    n = "Tmr4"
    If (IsISRSelected(r, sVarPre, GPT12_ & "T4IC", n)) Then
      v = 36
      Print #f, "#define T4INT " & FormatTextOutput(v, "H2")
    End If
    
    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,9)",0)
    Print #f, ""
    Print #f, ""
    Print #f, "#endif  // ifndef _" & StripExtension(r.v(sVarPre & "_HFILE")) & "_H_"


    
    'close file
    Close f
    
    GPT1GenerateH = True
    Exit Function
    
GPT1GenerateHError:
    Close f
    Exit Function
End Function


'*****************************************************************************
'Description:
'        This functions generates the statments for the timer configuration 
'
'        Example:
'        Call GetTimerConfig(f, r, TimerNr, vIndex)
'    
'-----------------------------------------------------------------------------
'Parameters:
'        f                file handle
'        r                reference to the ProjectData object
'        TimerNr          timer numer
'        vIndex
'    
'-----------------------------------------------------------------------------
'Returns:
'       none
'
'*****************************************************************************
Sub GetTimerConfig(f As Variant, r As ProjectData, TimerNr As Variant, vIndex As Variant)
  On Error Resume Next

    Dim i       As Integer
    Dim v       As Variant
    Dim d       As String
    Dim dc      As DAvECollection
    Dim uc      As UserCode
    Dim func    As FunctionClass
    DIm sVarPre As String
    Dim n       As String

    sVarPre = "GPT1"

    '---------------------------------------------------------
    'Timer mode
    '---------------------------------------------------------
    If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 0) Then
      d = "timer " & TimerNr & " works in timer mode"
      Print #f, FormatCommentsForInit(1, d)
      If (TimerNr = 2 Or TimerNr = 4) Then
        If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "RC") = 1) Then 'if T2RC / T4RC bit is set
          d = "timer " & TimerNr & " works in timer mode and is controlled by the run bit (T3R) of timer 3 "
          Print #f, FormatCommentsForInit(1, d)
        End If
      End If
    End If  

    If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 1) Then
      d = "timer " & TimerNr & " works in counter mode"
      Print #f, FormatCommentsForInit(1, d)
      If (TimerNr = 2 Or TimerNr = 4) Then
        If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "RC") = 1) Then 'if T2RC / T4RC bit is set
          d = "timer " & TimerNr & " works in counter mode and is controlled by the run bit (T3R) of timer 3 "
          Print #f, FormatCommentsForInit(1, d)
        End If
      End If
    End If  

    If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 2) Then
      d = "timer " & TimerNr & " works as gated timer with gate active low via T" & TimerNr & "IN (" & GetPinName("T" & TimerNr & "IN") & ")"
      Print #f, FormatCommentsForInit(1, d)
    End If  

    If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 3) Then
      d = "timer " & TimerNr & " works as gated timer with gate active high via T" & TimerNr & "IN (" & GetPinName("T" & TimerNr & "IN") & ")"
      Print #f, FormatCommentsForInit(1, d)
    End If  

    If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 4) Then
      d = "timer " & TimerNr & " works in reload mode"
      Print #f, FormatCommentsForInit(1, d)
    End If  

    If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 5) Then
      d = "timer " & TimerNr & " works in capture mode"
      Print #f, FormatCommentsForInit(1, d)
    End If  

    If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 6) Then
      d = "timer " & TimerNr & " works in incremental interface mode (rotation detection)"
      Print #f, FormatCommentsForInit(1, d)
    End If  

    If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 7) Then
      d = "timer " & TimerNr & " works in incremental interface mode (edge detection)"
      Print #f, FormatCommentsForInit(1, d)
    End If  
        

'    If (TimerNr = 2 Or TimerNr = 3) Then
      '---------------------------------------------------------
      'edge selection on incremental interface mode
      '---------------------------------------------------------
      If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") > 5) Then 
        Select Case (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "I"))
                    Case (0)
                         d = "counter T" & TimerNr & " stops"
                    Case (1)
                         d = "counting by "
                         d = d & "any transition (rising and falling edge) on T" & TimerNr & "IN (" & GetPinName("T" & TimerNr & "IN") & ")"
                    Case (2)
                         d = "counting by "
                         d = d & "any transition (rising and falling edge) on T" & TimerNr & "EUD (" & GetPinName("T" & TimerNr & "EUD") & ")"
                    Case (3) 
                         d = "counting by "
                         d = d & "any transition (rising and falling edge) on T" & TimerNr & "IN (" & GetPinName("T" & TimerNr & "IN") & ") or T" & TimerNr & "EUD (" & GetPinName("T" & TimerNr & "EUD") & ")"
        End Select
          
        Print #f, FormatCommentsForInit(1, d)
      End If
'    End If


'    If ((TimerNr = 3 Or TimerNr = 2) And r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") < 5) Then
    If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") < 5) Then
      '---------------------------------------------------------
      'external count up/down control
      '---------------------------------------------------------
      If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "UDE") = 0) Then
        d = "external up/down control is disabled"
        Print #f, FormatCommentsForInit(1, d)
      End If
      If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "UDE") = 1) Then
        d = "external up/down control via T" & TimerNr & "EUD (" & GetPinName("T" & TimerNr & "EUD") & ") is enabled"
        Print #f, FormatCommentsForInit(1, d)  
      End If
    End If
 
    '---------------------------------------------------------
    'prescaler
    '---------------------------------------------------------
    If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 0 Or r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 2 Or r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 3) Then
      d = "prescaler factor is " & GetBPS1Factor(r.v(GPT12_ & "T3CON_BPS1")) * 2 ^ (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "I")) 
      Print #f, FormatCommentsForInit(1, d)    
    End If  

    '---------------------------------------------------------
    'edge selection
    '---------------------------------------------------------
    If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 1 Or _
        r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 4 Or _
        r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 5) Then
      Select Case (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "I"))
                  Case (0)
                       d = "timer " & TimerNr & " is disabled"
                  Case (1)
                       d = "timer " & TimerNr & " is clocked by positive transition on T" & TimerNr & "IN (" & GetPinName("T" & TimerNr & "IN") & ")"     
                  Case (2)
                       d = "timer " & TimerNr & " is clocked by negative transition on T" & TimerNr & "IN (" & GetPinName("T" & TimerNr & "IN") & ")" 
                  Case (3) 
                       d = "timer " & TimerNr & " is clocked by any transition on T" & TimerNr & "IN (" & GetPinName("T" & TimerNr & "IN") & ")"
                  Case (5)
                       d = "timer " & TimerNr & " is clocked by positive transition of output toggle latch T3OTL"
                  Case (6)
                       d = "timer " & TimerNr & " is clocked by negative transition of output toggle latch T3OTL"     
                  Case (7)
                       d = "timer " & TimerNr & " is clocked by any transition of output toggle latch T3OTL"     
                       
      End Select
      Print #f, FormatCommentsForInit(1, d)    
    End If  

    '---------------------------------------------------------
    'count up/down
    '---------------------------------------------------------
    If Not(r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 4 Or _
        r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") = 5) Then 

      If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "UD") = 0) Then
        d = "up/down control bit is reset"
      End If
      If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "UD") = 1) Then
        d = "up/down control bit is set"
      End If
      Print #f, FormatCommentsForInit(1, d)
    End If

    


    If (TimerNr = 3) Then
      '---------------------------------------------------------
      'alternate output (toggle T3OUT) function is enabled
      '---------------------------------------------------------
      If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "OE") = 0) Then
        d = "alternate output function T3OUT (" & GetPinName("T" & TimerNr & "OUT") & ") is disabled"
      Else
        d = "alternate output function T3OUT (" & GetPinName("T" & TimerNr & "OUT") & ") is enabled"
      End If
      Print #f, FormatCommentsForInit(1, d)

      '---------------------------------------------------------
      'alternate output (toggle T3OUT) function is enabled
      '---------------------------------------------------------
      If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "OTL") = 0) Then
        d = "timer 3 output toggle latch (T3OTL) is set to 0"
      Else
        d = "timer 3 output toggle latch (T3OTL) is set to 1"
      End If
      Print #f, FormatCommentsForInit(1, d)
    End If

    '---------------------------------------------------------
    'start bit
    '---------------------------------------------------------
    If (r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "R") = 0) Then  'if start bit = 0
      d = "timer " & TimerNr & " run bit is reset"
      Print #f, FormatCommentsForInit(1, d)
    End If


    print #f, ""

    v = 0
    v = v + r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "CHDIR") * 2 ^ 14
    v = v + r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "EDGE") * 2 ^ 13
    
    If (TimerNr = 3) Then    
      v = v + r.v(GPT12_ & "T" & TimerNr & "CON_BPS1") * 2 ^ 11
      v = v + r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "OTL") * 2 ^ 10
      v = v + r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "OE") * 2 ^ 9
    End If
    
    If (TimerNr = 2) Then
      v = v + r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "IRDIS") * 2 ^ 12
      'v = v + r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "RC") * 2 ^ 9
    End If

    If (TimerNr = 4) Then
      v = v + r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "IRDIS") * 2 ^ 12
      'v = v + r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "RC") * 2 ^ 9
    End If
    
    v = v + r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "UDE") * 2 ^ 8
    v = v + r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "UD") * 2 ^ 7
    v = v + r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "M") * 2 ^ 3
    v = v + r.v(GPT12_ & "T" & TimerNr & "CON_T" & TimerNr & "I")
    
    Print #f, FormatCodeStatemente(2, GPT12_ & "T" & TimerNr & "CON", "=", FormatTextOutput(v, "H4"), "load timer " & TimerNr & " control register")

    v = 0
    v = r.sfr(GPT12_ & "T" & TimerNr)
    Print #f, FormatCodeStatemente(2, GPT12_ & "T" & TimerNr, "=", FormatTextOutput(v, "H4"), "load timer " & TimerNr & " register")


End Sub