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

Option Explicit

'*****************************************************************************
' Module:
'        ADC
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions for generating code for the
'        module 'ADC'.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        1.0   ahe         14.06.00     final version
'
'        ---------------------------------------------------------------------
'        2.0   ahe         20.07.00     final version for C161PI
'                                       - reduzierung von Ch's von 24 auf 4
'                                       - delete P1DIDIS
'                                       - delete ADCON_ADX
'
'        ---------------------------------------------------------------------
'        3.0   ahe         07.08.00     final version for C161CS/JC/JI
'                                       - only AN0..AN7 and AN12..AN15
'
'        ---------------------------------------------------------------------
'        4.0   ahe         20.02.01     final version for Pegasus
'
'        4.1   ahe         08.08.02     no changes
'        4.2   ste         09.12.02     formula for conversion time changed
'                                       (spec. changed)
'
'        ---------------------------------------------------------------------
'        5.0               26.08.04     formula for conversion time changed
'                                       (post calibration made optional)
'        ---------------------------------------------------------------------
'        5.1   Sheshadri   25.05.05     function NAME ADC_vCalOff Changed
'
'*****************************************************************************


'*****************************************************************************
'Description:
'        This functions initiates the code generation for the ADC 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
    Dim sVarPre As String

    sVarPre = "ADC" & vIndex


    ' write files ADC.h
    If (r.v(sVarPre & "_USE_Init") = 1) Then
      If (Not ADCGenerateH(r, vIndex)) Then Exit Function
                   If (Not r.AddFile(r.s(sVarPre & "_HFILE"))) Then Exit Function
    End If

    ' write file ADC.c
    If (r.v(sVarPre & "_USE_Init") = 1) Then
      If (Not ADCGenerateC(r, vIndex)) Then Exit Function
                   If (Not r.AddFile(r.s(sVarPre & "_CFILE"))) Then Exit Function
    End If

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


'*****************************************************************************
'Description:
'        Generates the code for the ADC.c file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function ADCGenerateC(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo ADCGenerateCError
    
    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 n         As String
    Dim Overflow  As UnitType
    Dim sAlternatePinName(1 To 20)  As String

    Dim sVarPre As String
    Dim sRegPre As String
    sVarPre = "ADC" & vIndex
    sRegPre = sVarPre & "_"


    
    ADCGenerateC = False

    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 ADC.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, ""
    
    '------------------------------------------------------------------------------------------------
    ' ADC_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, ""


      If (r.v(sRegPre & "CTR0_MD") = 0) Then
          d = "compatibility mode is selected"
          Print #f, FormatCommentsForInit(1, d)
          Call GererateSourceForCompatibilityMode(f, r, vIndex)
      Else
          d = "enhanced mode is selected"
          Print #f, FormatCommentsForInit(1, d)
          Call GererateSourceForEnhancedMode(f, r, vIndex)
      End If


      Print #f, ""
      '------------------------------------------------------------------------------------------------
      ' Configuration of the used ADC Interrupts:
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of the used " & sVarPre & " Interrupts:")

      Call GenerateIR(f, r, sRegPre & "CIC", "Conv")
      Call GenerateIR(f, r, sRegPre & "EIC", "Error")
      Print #f, ""

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

      For i = 0 To 15
          If (r.Exist(sVarPre & "_PARA_ANA_" & i)) Then
              sAlternatePinName(v + 1) = "AN" & i
              v = v + 1
          End If
      Next i

      Call SetAlternatePinStatements(f, r, sAlternatePinName())

      For i = 0 To 15
          If (r.Regs.BitFind("P5DIDIS_P" & i)) Then
              If (r.v("P5DIDIS_P" & i) = 1) Then
                  d = "digital input stage is disconnected from port line P5." & i
                  Print #f, FormatCommentsForInit(1, d)
              End If
          End If
      Next i
      Print #f, ""

      v = 0
      v = v + r.sfr("P5DIDIS")
      Print #f, FormatCodeStatemente(2, "P5DIDIS", "=", FormatTextOutput(v, "H4"), "load Port 5 digital input disable register")
      Print #f, ""

      If (r.v(sRegPre & "CTR0_MD") = 0 And r.v(sRegPre & "CON_ADST") = 1) Then
          Print #f, "  " & sRegPre & "CON_ADST   =  1;           // set start bit"
      ElseIf (r.v(sRegPre & "CTR0_MD") = 1 And r.v(sRegPre & "CTR0_ADST") = 1) Then
          Print #f, "  " & sRegPre & "CTR0_ADST  =  1;           // set start bit"
      End If

      Print #f, ""
      Print #f, ""
      Print #f, uc.GetUserCode("(" & n & ",3)", 2)
      Print #f, ""
      Print #f, "} " & END_OF_FUNCTION & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""

    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ADC_ConfConv function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ConfConv"
    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, ""
      If (r.v(sRegPre & "CTR0_MD") = 0) Then
          Print #f, "  " & sRegPre & "CON = (" & _
                    sRegPre & "CON & 0xFF80) | (ubMode << 4) | (ubChannel);"
      Else
          Print #f, "  " & sRegPre & "CTR0 = (" & _
                    sRegPre & "CTR0 & 0xFF80) | (ubMode << 5) | (ubChannel);"
      End If
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ADC_ReadConv function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ReadConv"
    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, ""
      
      If (r.v("" & sRegPre & "CIC_IE") = 0) Then
          Print #f, "  " & sRegPre & "CIC_IR = 0;"
      End If
      
      Print #f, "  return(" & sRegPre & "DAT);"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ADC_OverWrite function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "OverWrite"
    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;"
      Print #f, "  if(" & sRegPre & "EIC_IR)"
      Print #f, "  {"
      Print #f, "    " & sRegPre & "EIC_IR = 0;"
      Print #f, "    ubReturnValue = 1;"
      Print #f, "  }"
      Print #f, "  return(ubReturnValue);"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ADC_InjectChan function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "InjectChan"
    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;"
      If (r.v(sRegPre & "CTR0_MD") = 0) Then
          Print #f, "  if(!" & sRegPre & "CON_ADCRQ) // if no injected conversion active"
      Else
          Print #f, "  if(!" & sRegPre & "CTR0_ADCRQ) // if no injected conversion active"
      End If
      Print #f, "  {"
      Print #f, ""
      Print #f, "    " & sRegPre & "DAT2 = ((uword)ubChannel) << 12;   // set the new channel number"
      Print #f, "    ubReturnValue = 1;"
      Print #f, ""
      Print #f, "  }"
      Print #f, "  return(ubReturnValue);"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ADC_ReadInject function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ReadInject"
    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, ""
      
      If (r.v("" & sRegPre & "EIC_IE") = 0) Then
          Print #f, "  " & sRegPre & "EIC_IR = 0;"
      End If
      
      Print #f, "  return(" & sRegPre & "DAT2);"
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ADC_viConv function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "Conv"
    If (IsISRSelected(r, sVarPre, sRegPre & "CIC", 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), sRegPre & "C_INT")
      Print #f, "{"
      Print #f, uc.GetUserCode("(" & n & ",2)", 2)
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ADC_viError function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "Error"
    If (IsISRSelected(r, sVarPre, sRegPre & "EIC", 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), sRegPre & "E_INT")
      Print #f, "{"
      Print #f, uc.GetUserCode("(" & n & ",2)", 2)
      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
    
    ADCGenerateC = True
    Exit Function
    
ADCGenerateCError:
    Close f
    Exit Function
End Function


'*****************************************************************************
'Description:
'        Generates the code for the ADC.h file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function ADCGenerateH(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo ADCGenerateHError
    
    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 n         As String
    Dim sVarPre   As String
    Dim sRegPre   As String
    Dim l         As Long
    Dim v         As Variant

    sVarPre = "ADC" & vIndex
    sRegPre = sVarPre & "_"
   
    ADCGenerateH = False

    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 ADC.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 = "FIXED"
    Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "0")
    n = "FIXED_CONTI"
    Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "1")
    n = "SCAN"
    Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "2")
    n = "SCAN_CONTI"
    Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "3")

    For i = 0 To 15
        If (r.Exist(sVarPre & "_PARA_ANA_" & i)) Then
            n = "ANA_" & i
            Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, Format(i))
        End If
    Next i

    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'

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

      If (r.v(sRegPre & "CTR0_MD") = 0) Then
          Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                    " " & sRegPre & "CON_ADST = 1"
      Else
          Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                    " " & sRegPre & "CTR0_ADST = 1"
      End If

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

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ADC_StopConv macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "StopConv"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      If (r.v(sRegPre & "CTR0_MD") = 0) Then
          Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                    " " & sRegPre & "CON_ADST = 0"
      Else
          Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                    " " & sRegPre & "CTR0_ADST = 0"
      End If
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ADC_ConvReady macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ConvReady"
    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) & _
                " " & sRegPre & "CIC_IR"
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ADC_Busy macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "Busy"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      If (r.v(sRegPre & "CTR0_MD") = 0) Then
          Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                    " " & sRegPre & "CON_ADBSY"
      Else
          Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                    " " & sRegPre & "CTR0_ADBSY"
      End If
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' ADC_CalOff macro
    '------------------------------------------------------------------------------------------------
    n = "CalOff"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
'      If (r.v(sRegPre & "CTR0_MD") = 0) Then
'        Print #f, "#define " & "CALOFF " & "((" & sRegPre & "CON_ADBSY == 0 && " & sRegPre & "CON1_CAL == 0)" & _
'                  "?1:0)"
'      Else
'        Print #f, "#define " & "CALOFF " & "((" & sRegPre & "CTR0_ADBSY == 0 && " & sRegPre & "CON1_CAL == 0)" & _
'                  "?1:0)"
'      End If
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                " " & sRegPre & "CTR0_CALOFF = CALOFF"
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ADC_StartInject macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "StartInject"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      If (r.v(sRegPre & "CTR0_MD") = 0) Then
          Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                    " " & sRegPre & "CON_ADCRQ = 1"
      Else
          Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                    " " & sRegPre & "CTR0_ADCRQ = 1"
      End If
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ADC_InjectReady macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "InjectReady"
    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) & _
                " " & sRegPre & "EIC_IR"
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' Interrupt Vectors
    '------------------------------------------------------------------------------------------------
    Print #f, SECTION_INTERRUPT_VECTORS
    Print #f, ""  'DAvE interrupt vector definitions
    
    n = "Conv"
    If (IsISRSelected(r, sVarPre, sRegPre & "CIC", n)) Then
      v = 40
      Print #f, "#define " & sRegPre & "C_INT    " & FormatTextOutput(v, "H2")
    End If
    
    n = "Error"
    If (IsISRSelected(r, sVarPre, sRegPre & "EIC", n)) Then
      v = 41
      Print #f, "#define " & sRegPre & "E_INT    " & FormatTextOutput(v, "H2")
    End If

    Print #f, ""
    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,8)", 0)
    Print #f, ""
    Print #f, ""
    Print #f, "#endif  // ifndef _" & StripExtension(r.v(sVarPre & "_HFILE")) & "_H_"
    
    'close file
    Close f
    
    ADCGenerateH = True
    Exit Function
    
ADCGenerateHError:
    Close f
    Exit Function
End Function

'*****************************************************************************
'Description:
'        Generates the code for the ADC.C file in compatibility mode.
'-----------------------------------------------------------------------------
'Parameters:
'        f                file handle
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'   none
'*****************************************************************************
Sub GererateSourceForCompatibilityMode(f As Variant, r As ProjectData, vIndex As Variant)
    On Error Resume Next

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

    sVarPre = "ADC" & vIndex
    sRegPre = sVarPre & "_"

    '-------------------------------------------------------- CON_ADM
    If (r.v(sRegPre & "CON_ADM") = 0) Then
        d = "fixed channel single conversion"
    ElseIf (r.v(sRegPre & "CON_ADM") = 1) Then
        d = "fixed channel continuous conversion"
    ElseIf (r.v(sRegPre & "CON_ADM") = 2) Then
        d = "auto scan single conversion"
    ElseIf (r.v(sRegPre & "CON_ADM") = 3) Then
        d = "auto scan continuous conversion"
    End If
    d = d & " mode is selected"
    Print #f, FormatCommentsForInit(1, d)

    '-------------------------------------------------------- CON1_RES
    If (r.v(sRegPre & "CON1_RES") = 0) Then
        d = "10-bit resolution"
    Else
        d = "8-bit resolution"
    End If
    Print #f, FormatCommentsForInit(1, d)

    '-------------------------------------------------------- CON_ADCH
    If (r.v(sRegPre & "CON_ADM") = 0) Then
        ' fixed channel single conversion
        d = "once converts channel " & r.v(sRegPre & "CON_ADCH")
    ElseIf (r.v(sRegPre & "CON_ADM") = 1) Then
        ' fixed channel continuous conversion
        d = "repeatedly converts channel " & r.v(sRegPre & "CON_ADCH")
    ElseIf (r.v(sRegPre & "CON_ADM") = 2) Then
        ' auto scan single conversion
        d = "once converts a sequence beginning with channel " & r.v(sRegPre & "CON_ADCH")
    ElseIf (r.v(sRegPre & "CON_ADM") = 3) Then
        ' auto scan continuous conversion
        d = "repeatedly converts a sequence beginning with channel " & r.v(sRegPre & "CON_ADCH")
    End If
    Print #f, FormatCommentsForInit(1, d)

    '-------------------------------------------------------- CON_ADST
    If (r.v(sRegPre & "CON_ADST") = 0) Then
        d = "ADC start bit is reset"
    Else
        d = "ADC start bit is set"
    End If
    Print #f, FormatCommentsForInit(1, d)


    '-------------------------------------------------------- CON_ADWR
    If (r.v(sRegPre & "CON_ADWR") = 0) Then
        d = "wait for ADDAT read mode is disabled"
        Print #f, FormatCommentsForInit(1, d)
    Else
        d = "wait for ADDAT read mode is enabled"
        Print #f, FormatCommentsForInit(1, d)
    
        '-------------------------------------------------------- CON_ADCIN
        If (r.v(sRegPre & "CON_ADCIN") = 0) Then
            d = "channel injection is disabled"
            Print #f, FormatCommentsForInit(1, d)
        Else
            d = "channel injection is enabled"
            Print #f, FormatCommentsForInit(1, d)

            '-------------------------------------------------------- DAT2_CHNR
            d = "inject channel " & r.v(sRegPre & "DAT2_CHNR")
            Print #f, FormatCommentsForInit(1, d)

        End If
    End If

    If (r.v(sRegPre & "CON1_ICST") = 0) Then
        '-------------------------------------------------------- CON_ADCTC
        d = "converter basic clock tbc is "
        If (r.v(sRegPre & "CON_ADCTC") = 0) Then
            d = d & "fpd / 4"
        ElseIf (r.v(sRegPre & "CON_ADCTC") = 1) Then
            d = d & "fpd / 2"
        ElseIf (r.v(sRegPre & "CON_ADCTC") = 2) Then
            d = d & "fpd / 16"
        ElseIf (r.v(sRegPre & "CON_ADCTC") = 3) Then
            d = d & "fpd / 8"
        End If
        Print #f, FormatCommentsForInit(1, d)

        '-------------------------------------------------------- CON_ADSTC
        d = "sample time tsc is tbc * " & 2 ^ (3 + r.v(sRegPre & "CON_ADSTC"))
        Print #f, FormatCommentsForInit(1, d)
        
        '-------------------------------------------------------- CTR0_CALOFF
        If (r.v(sRegPre & "CTR0_CALOFF") = 1) Then
            d = "post calibration is disabled"
        Else
            d = "post calibration is enabled"
        End If
        Print #f, FormatCommentsForInit(1, d)

'        uTime = ConvertTime(( 2^(3 + r.v(sRegPre & "CON_ADSTC")) * _
'                r.v(sVArPre & "_Tbc_Factor") + r.v(sVArPre & "_Tbc_Factor") * _
'                (48 - 8 * r.v(sRegPre & "CON1_RES")) + 2) / GetADCModuleClock(r, vIndex))
        uTime = ConvertTime((2 ^ (3 + r.v(sRegPre & "CON_ADSTC")) * _
                r.v(sVarPre & "_Tbc_Factor") + r.v(sVarPre & "_Tbc_Factor") * _
                (40 - 8 * r.v(sRegPre & "CON1_RES") + 12 * (r.v(sRegPre & "CTR0_CALOFF") Xor 1)) + 6) / GetADCModuleClock(r, vIndex))
'               (48 - 8 * r.v(sRegPre & "CON1_RES")) + 6) / GetADCModuleClock(r, vIndex))
        d = "conversion time tc = " & Format(uTime.dValue, "0.000") & " " & uTime.sUnit
        Print #f, FormatCommentsForInit(1, d)

    Else
        '-------------------------------------------------------- CON1_ADCTC
        d = "converter basic clock tbc is "
        d = d & "fpd / " & r.v(sRegPre & "CON1_ADCTC") + 1
        Print #f, FormatCommentsForInit(1, d)

        '-------------------------------------------------------- CON1_ADSTC
        d = "sample time tsc is tbc * " & 2 ^ (3 + r.v(sRegPre & "CON1_ADSTC"))
        Print #f, FormatCommentsForInit(1, d)

        '-------------------------------------------------------- CTR0_CALOFF
        If (r.v(sRegPre & "CTR0_CALOFF") = 1) Then
            d = "post calibration is disabled"
        Else
            d = "post calibration is enabled"
        End If
        Print #f, FormatCommentsForInit(1, d)
        
'        uTime = ConvertTime(((4 * r.v(sRegPre & "CON1_ADSTC") + 56 - 8 * _
'                r.v(sRegPre & "CON1_RES")) * (r.v(sRegPre & "CON1_ADCTC") + 1) + 6) _
'                / GetADCModuleClock(r, vIndex))
        uTime = ConvertTime(((4 * r.v(sRegPre & "CON1_ADSTC") + 48 - 8 * _
                 r.v(sRegPre & "CON1_RES") + 12 * (r.v(sRegPre & "CTR0_CALOFF") Xor 1)) * (r.v(sRegPre & "CON1_ADCTC") + 1) + 6) _
                / GetADCModuleClock(r, vIndex))
        d = "conversion time tc = " & Format(uTime.dValue, "0.000") & " " & uTime.sUnit
        Print #f, FormatCommentsForInit(1, d)

    End If
    Print #f, ""


    If (r.v(sRegPre & "CON_ADST") = 1) Then

        r.v(sRegPre & "CON_ADST") = 0 'reset start bit
        v = 0
        v = v + r.sfr(sRegPre & "CON")
        Print #f, FormatCodeStatemente(2, sRegPre & "CON", "=", _
          FormatTextOutput(v, "H4"), "load " & sVarPre & " control register")
        Print #f, ""
        r.v(sRegPre & "CON_ADST") = 1 'set start bit

    Else
        v = 0
        v = v + r.sfr(sRegPre & "CON")
        Print #f, FormatCodeStatemente(2, sRegPre & "CON", "=", _
          FormatTextOutput(v, "H4"), "load " & sVarPre & " control register")
        Print #f, ""
    End If

    v = 0
    v = v + r.sfr(sRegPre & "CON1")
    Print #f, FormatCodeStatemente(2, sRegPre & "CON1", "=", _
      FormatTextOutput(v, "H4"), "load " & sVarPre & " control register 1")
    Print #f, ""

    If (r.v(sRegPre & "CON_ADCIN") = 1) Then
        v = 0
        v = v + r.sfr(sRegPre & "DAT2")
        Print #f, FormatCodeStatemente(2, sRegPre & "DAT2", "=", _
          FormatTextOutput(v, "H4"), "load A/D converter 2 result register")
        Print #f, ""
    End If

    Print #f, ""

End Sub

'*****************************************************************************
'Description:
'        Generates the code for the ADC.C file in enhanced mode.
'-----------------------------------------------------------------------------
'Parameters:
'        f                file handle
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'   none
'*****************************************************************************
Sub GererateSourceForEnhancedMode(f As Variant, r As ProjectData, vIndex As Variant)
    On Error Resume Next

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

    sVarPre = "ADC" & vIndex
    sRegPre = sVarPre & "_"

    '-------------------------------------------------------- CTR0_ADM
    If (r.v(sRegPre & "CTR0_ADM") = 0) Then
        d = "fixed channel single conversion"
    ElseIf (r.v(sRegPre & "CTR0_ADM") = 1) Then
        d = "fixed channel continuous conversion"
    ElseIf (r.v(sRegPre & "CTR0_ADM") = 2) Then
        d = "auto scan single conversion"
    ElseIf (r.v(sRegPre & "CTR0_ADM") = 3) Then
        d = "auto scan continuous conversion"
    End If
    d = d & " mode is selected"
    Print #f, FormatCommentsForInit(1, d)

    '-------------------------------------------------------- CTR2_RES
    If (r.v(sRegPre & "CTR2_RES") = 0) Then
        d = "10-bit resolution"
    Else
        d = "8-bit resolution"
    End If
    Print #f, FormatCommentsForInit(1, d)

    '-------------------------------------------------------- CTR0_ADCH
    If (r.v(sRegPre & "CTR0_ADM") = 0) Then
        ' fixed channel single conversion
        d = "once converts channel " & r.v(sRegPre & "CTR0_ADCH")
    ElseIf (r.v(sRegPre & "CTR0_ADM") = 1) Then
        ' fixed channel continuous conversion
        d = "repeatedly converts channel " & r.v(sRegPre & "CTR0_ADCH")
    ElseIf (r.v(sRegPre & "CTR0_ADM") = 2) Then
        ' auto scan single conversion
        d = "once converts a sequence beginning with channel " & r.v(sRegPre & "CTR0_ADCH")
    ElseIf (r.v(sRegPre & "CTR0_ADM") = 3) Then
        ' auto scan continuous conversion
        d = "repeatedly converts a sequence beginning with channel " & r.v(sRegPre & "CTR0_ADCH")
    End If
    Print #f, FormatCommentsForInit(1, d)

    '-------------------------------------------------------- CTR0_ADST
    If (r.v(sRegPre & "CTR0_ADST") = 0) Then
        d = "ADC start bit is reset"
    Else
        d = "ADC start bit is set"
    End If
    Print #f, FormatCommentsForInit(1, d)


    '-------------------------------------------------------- CTR0_ADWR
    If (r.v(sRegPre & "CTR0_ADWR") = 0) Then
        d = "wait for ADDAT read mode is disabled"
        Print #f, FormatCommentsForInit(1, d)
    Else
        d = "wait for ADDAT read mode is enabled"
        Print #f, FormatCommentsForInit(1, d)
    End If

    '-------------------------------------------------------- CTR2_ADCTC
    d = "converter basic clock tbc is "
    d = d & "fpd / " & r.v(sRegPre & "CTR2_ADCTC") + 1
    Print #f, FormatCommentsForInit(1, d)

    '-------------------------------------------------------- CTR2_ADSTC
    d = "sample time tsc is tbc * " & 2 ^ (3 + r.v(sRegPre & "CTR2_ADSTC"))
    Print #f, FormatCommentsForInit(1, d)
    
    '-------------------------------------------------------- CTR0_CALOFF
    If (r.v(sRegPre & "CTR0_CALOFF") = 1) Then
        d = "post calibration is disabled"
    Else
        d = "post calibration is enabled"
    End If
    Print #f, FormatCommentsForInit(1, d)

'    uTime = ConvertTime(( (4 * r.v(sRegPre & "CTR2_ADSTC") + 56 - 8 * _
'            r.v(sRegPre & "CTR2_RES")) * (r.v(sRegPre & "CTR2_ADCTC") + 1) + 2) _
'            / GetADCModuleClock(r, vIndex))
    uTime = ConvertTime(((4 * r.v(sRegPre & "CTR2_ADSTC") + 56 - 8 * _
            r.v(sRegPre & "CTR2_RES")) * (r.v(sRegPre & "CTR2_ADCTC") + 1) + 6) _
            / GetADCModuleClock(r, vIndex))
    d = "conversion time tc = " & Format(uTime.dValue, "0.000") & " " & uTime.sUnit
    Print #f, FormatCommentsForInit(1, d)


    '-------------------------------------------------------- CTR0_ADCIN
    If (r.v(sRegPre & "CTR0_ADCIN") = 0) Then
        d = "channel injection is disabled"
        Print #f, FormatCommentsForInit(1, d)
    Else
        d = "channel injection is enabled"
        Print #f, FormatCommentsForInit(1, d)

        '-------------------------------------------------------- DAT2_CHNR
        d = "inject channel " & r.v(sRegPre & "DAT2_CHNR")
        Print #f, FormatCommentsForInit(1, d)

        '-------------------------------------------------------- CTR0_ADCTS
        If (r.v(sRegPre & "CTR0_ADCTS") = 0) Then
            d = "no channel injection trigger input by hardware selected"
        ElseIf (r.v(sRegPre & "CTR0_ADCTS") = 1) Then
            d = "trigger input CAPCOM1/2 selected"
        ElseIf (r.v(sRegPre & "CTR0_ADCTS") = 2) Then
            d = "trigger input CCU6 selected"
        End If
        Print #f, FormatCommentsForInit(1, d)

        '-------------------------------------------------------- CTR2IN_RES
        If (r.v(sRegPre & "CTR2IN_RES") = 0) Then
            d = "resolution for injection mode is 10-bit"
        ElseIf (r.v(sRegPre & "CTR2IN_RES") = 1) Then
            d = "resolution for injection mode is 8-bit"
        End If
        Print #f, FormatCommentsForInit(1, d)


        '-------------------------------------------------------- CTR2IN_ADCTC
        d = "converter basic clock tbc for injection mode is "
        d = d & "fpd / " & r.v(sRegPre & "CTR2IN_ADCTC") + 1
        Print #f, FormatCommentsForInit(1, d)

        '-------------------------------------------------------- CTR2IN_ADSTC
        d = "sample time tsc for injection mode is tbc * " & 2 ^ (3 + r.v(sRegPre & "CTR2IN_ADSTC"))
        Print #f, FormatCommentsForInit(1, d)

'        uTime = ConvertTime(((4 * r.v(sRegPre & "CTR2IN_ADSTC") + 56 - 8 * _
'                r.v(sRegPre & "CTR2IN_RES")) * (r.v(sRegPre & "CTR2IN_ADCTC") + 1) + 2) _
'                / GetADCModuleClock(r, vIndex))
        uTime = ConvertTime(((4 * r.v(sRegPre & "CTR2IN_ADSTC") + 56 - 8 * _
                r.v(sRegPre & "CTR2IN_RES")) * (r.v(sRegPre & "CTR2IN_ADCTC") + 1) + 6) _
                / GetADCModuleClock(r, vIndex))
        d = "conversion time in injection mode tc = " & Format(uTime.dValue, "0.000") & _
          " " & uTime.sUnit
        Print #f, FormatCommentsForInit(1, d)

        Print #f, ""

    End If


    If (r.v(sRegPre & "CTR0_ADST") = 1) Then
        r.v(sRegPre & "CTR0_ADST") = 0  'reset start bit
        v = 0
        v = v + r.sfr(sRegPre & "CTR0")
        Print #f, FormatCodeStatemente(2, sRegPre & "CTR0", "=", _
          FormatTextOutput(v, "H4"), "load " & sVarPre & " control register 0")
        Print #f, ""
        r.v(sRegPre & "CTR0_ADST") = 1  'set start bit
    Else
        v = 0
        v = v + r.sfr(sRegPre & "CTR0")
        Print #f, FormatCodeStatemente(2, sRegPre & "CTR0", "=", _
          FormatTextOutput(v, "H4"), "load " & sVarPre & " control register 0")
        Print #f, ""
    End If

    v = 0
    v = v + r.sfr(sRegPre & "CTR2")
    Print #f, FormatCodeStatemente(2, sRegPre & "CTR2", "=", _
      FormatTextOutput(v, "H4"), "load " & sVarPre & " control register 2")
    Print #f, ""

    If (r.v(sRegPre & "CTR0_ADCIN") = 1) Then
        v = 0
        v = v + r.sfr(sRegPre & "DAT2")
        Print #f, FormatCodeStatemente(2, sRegPre & "DAT2", "=", _
          FormatTextOutput(v, "H4"), "load A/D converter 2 result register")
        Print #f, ""

        v = 0
        v = v + r.sfr(sRegPre & "CTR2IN")
        Print #f, FormatCodeStatemente(2, sRegPre & "CTR2IN", "=", _
          FormatTextOutput(v, "H4"), "load " & sVarPre & " injection control register 2")
        Print #f, ""

    End If

    Print #f, ""

  
End Sub
