Attribute VB_Name = "Module1"
'#Uses "..\include\Const.inc"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\classes\FunctionClass.cls
'#Uses "Include.inc"
Option Explicit
'*****************************************************************************
' Module:
'        ADC
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions for generating code for the
'        module 'ADC'.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        1.0    sheshadri    03.12.04     initial version for XC866
'        1.1    sheshadri    07.19.05     Changes done for default value for
'                                           ASEN0 & ASEN1
'        1.2    sheshadri    16.12.05     Functions GetResultDatax and GetResultChNumx is
'                                          changed for x = 0-3
'                                         ResultReg FIFO functionality removed
'
'*****************************************************************************

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

    ' write files ADC.h
    If (r.v("ADC_USE_Init")) Then
        If (Not ADCGenerateH(r, vIndex)) Then Exit Function
                   If (Not r.AddFile(r.s("ADC_HFILE"), "H")) Then Exit Function
    End If

    ' write file ADC.c
    If (r.v("ADC_USE_Init")) Then
        If (Not ADCGenerateC(r, vIndex)) Then Exit Function
                   If (Not r.AddFile(r.s("ADC_CFILE"), "C")) 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 successful operation
'        False           else
'*****************************************************************************
Function ADCGenerateC(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo ADCGenerateCError
    
    Dim i As Integer
    Dim ResReg As Integer
    Dim f As Variant
    Dim v As Variant
    Dim d As String
    
'    Dim DW As Integer
    Dim dc As DAvECollection
    Dim uc As UserCode
    Dim func As FunctionClass
    Dim iChannel As Integer
   
    Dim sVarPre As String
    Dim sRegPre As String
    
    Dim temp As Variant
    Dim temp0 As Variant
    Dim temp1 As Variant
    
' Variables added for Default value settings if Src is OFF
    Dim Src0Used As Integer
    Dim Src1Used As Integer
    Src0Used = 1
    Src1Used = 1
    
    sVarPre = "ADC"
    sRegPre = sVarPre & "_"

    ADCGenerateC = False

    Set dc = ObjectCollection("MODULENAMES")
    Set uc = New UserCode
    Set func = New FunctionClass
    
    'refresh the decriptions of the functions and macros
    Call RefreshFunctionDescriptions(func, r)

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

    'open ADC.c
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v("ADC_CFILE") For Output As f
    
    '------------------------------------------------------------------------------------------------
    ' module header
    '------------------------------------------------------------------------------------------------
    d = FormatModuleDescription(GetModuleHeaderC("ADC"))
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), dc("ADC").ModuleDescription, r.v("ADC_CFILE"), r.v("MAIN_CPU_TYPE"), r.v("MAIN_COMPILER"), d)
          
            Print #f, uc.GetUserCode("(ADC_General,1)", 0)   'USER CODE for application specific descriptions'
            Print #f, ""
            Print #f, Rtf.GenerateNote((r.v("ADC_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("(ADC_General,2)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_MACROS
            Print #f, ""  'DAvE local macro definitions'
            Print #f, uc.GetUserCode("(ADC_General,3)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_DEFINES
            Print #f, ""  'DAvE local #define definitions'
            Print #f, uc.GetUserCode("(ADC_General,4)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_TYPEDEFS
            Print #f, ""  'DAvE type defintions'
            Print #f, uc.GetUserCode("(ADC_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("(ADC_General,6)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_GLOBAL_VARIABLES
            Print #f, ""  'DAvE definitions of global variables'
            Print #f, uc.GetUserCode("(ADC_General,7)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_EXTERNAL_PROTOTYPES
            Print #f, ""  'DAvE external declarations of imported functions'
            Print #f, uc.GetUserCode("(ADC_General,8)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_LOCAL_PROTOTYPES
            Print #f, ""  'DAvE declarations of internal local functions'
            Print #f, uc.GetUserCode("(ADC_General,9)", 0)
            Print #f, ""
            Print #f, ""
    '------------------------------------------------------------------------------------------------
    ' ADC_Init function
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_Init") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_Init")
            Print #f, uc.GetUserCode("(ADC_Init,1)", 0)     'USER CODE for application specific descriptions'
            Print #f, ""
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_Init")
            Print #f, "{"
            Print #f, uc.GetUserCode("(ADC_Init,2)", 2)
            Print #f, ""

'------------------------------------------------------------------------------------------------
' Following code is added for SRC Enable/Disable to set codegeneration for Default/UserSettings
'------------------------------------------------------------------------------------------------

        If ((r.v(sRegPre & "PRAR_ASEN0") = 0) Or (r.v(sRegPre & "QMR0_ENGT") = 0)) Then
            Src0Used = 0
        End If
            
        If ((r.v(sRegPre & "PRAR_ASEN1") = 0) Or (r.v(sRegPre & "CRMR1_ENGT") = 0)) Then
            Src1Used = 0
        End If
            
            '------------------------------------------------------------------------------------------------
            ' Configuration of Global Control:
            '------------------------------------------------------------------------------------------------
            Print #f, FormatSectionHeader(2, 1, "Configuration of Global Control:")
             If (r.v(SCU_PREFIX & "PMCON1_ADC_DIS") = 1) Then
                   d = "the ADC module clock is disabled"
             Else
                   d = "the ADC module clock is enabled"
             End If
             Print #f, FormatCommentsForInit(1, d)
             
             d = "the ADC module clock = " & Format(GetADCModuleClock(r), "00.00") & " MHz"
             Print #f, FormatCommentsForInit(1, d)
             Print #f, FormatComments(2, 1, "", "", " ")
             
             '----------------------GLOBCTR------------------------------------------
             If (r.v(sRegPre & "GLOBCTR_DW") = 1) Then
                   d = "the result is 8 bits wide"
             Else
                   d = "the result is 10 bits wide"
             End If
             Print #f, FormatCommentsForInit(1, d)
             Print #f, FormatComments(2, 1, "", "", "--- Conversion Timing -----------------")
             Print #f, FormatComments(2, 1, "", "", "- conversion time (CTC)    = " & Format(GetAdcConversionTime(r), "00.00") & " us")
             Print #f, ""
             Print #f, FormatComments(2, 1, "", "", "- Configure global control functions ")
             Print #f, ""
             
                  Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, noSST);", "", "", "switch to page 0")
                  Print #f, ""
             
                v = 0
                v = v + r.sfr(sRegPre & "GLOBCTR")
                Print #f, FormatCodeStatemente(2, sRegPre & "GLOBCTR", "=", FormatTextOutput(v, "H2"), _
                          "load global control register")
             Print #f, ""
             
             
            '------------------------------------------------------------------------------------------------
            ' Configuration of Priority and Arbitration:
            '------------------------------------------------------------------------------------------------
            Print #f, FormatSectionHeader(2, 1, "Configuration of Priority and Arbitration:")
       
             If (r.v(sRegPre & "PRAR_PRIO0") = 1 And Src0Used = 1) Then
                   d = "the priority of request source 0 is high"
             Else
                   d = "the priority of request source 0 is low"
             End If
             Print #f, FormatCommentsForInit(1, d)

             If (r.v(sRegPre & "PRAR_CSM0") = 1 And Src0Used = 1) Then
                   d = "the cancel-injection-repeat mode is selected for source 0"
             Else
                   d = "the wait-for-start mode is selected for source 0"
             End If
             Print #f, FormatCommentsForInit(1, d)

             If (r.v(sRegPre & "PRAR_PRIO1") = 1 And Src1Used = 1) Then
                   d = "the priority of request source 1 is high"
             Else
                   d = "the priority of request source 1 is low"
             End If
             Print #f, FormatCommentsForInit(1, d)

             If (r.v(sRegPre & "PRAR_CSM1") = 1 And Src1Used = 1) Then
                   d = "the cancel-injection-repeat mode is selected for source 1"
             Else
                   d = "the wait-for-start mode is selected for source 1"
             End If
             Print #f, FormatCommentsForInit(1, d)

             If (r.v(sRegPre & "PRAR_ARBM") = 1) Then
                   d = "the arbitration started by pending conversion request is selected"
             Else
                   d = "the permanent arbitration mode is selected"
             End If
             Print #f, FormatCommentsForInit(1, d)

             If (r.v(sRegPre & "PRAR_ASEN0") = 1) Then
                   d = "Arbitration Slot 0 is enabled"
             Else
                   d = "Arbitration Slot 0 is disabled"
             End If

             Print #f, FormatCommentsForInit(1, d)
             If (r.v(sRegPre & "PRAR_ASEN1") = 1) Then
                   d = "Arbitration Slot 1 is enabled"
             Else
                   d = "Arbitration Slot 1 is disabled"
             End If
             Print #f, FormatCommentsForInit(1, d)
            Print #f, ""
            
        If ((Src0Used = 1) And (Src1Used = 1)) Then
            temp = r.sfr(sRegPre & "PRAR")
        ElseIf ((Src0Used = 1) And (Src1Used = 0)) Then
            v = r.sfr(sRegPre & "PRAR")
            temp = ADC_BitMask(v, 2)
            temp = ADC_BitMask(temp, 3)
        ElseIf ((Src0Used = 0) And (Src1Used = 1)) Then
            v = r.sfr(sRegPre & "PRAR")
            temp = ADC_BitMask(v, 0)
            temp = ADC_BitMask(temp, 1)
        End If
                Print #f, FormatCodeStatemente(2, sRegPre & "PRAR", "=", FormatTextOutput(temp, "H2"), _
                          "load Priority and Arbitration register")
                Print #f, ""
       
            i = 0
         For iChannel = 0 To 7
           If (r.v("ADC_USE_CHANNEL_" & iChannel) = 1) Then
                i = 1
            End If
         Next iChannel
            
         If (i = 1) Then
            '------------------------------------------------------------------------------------------------
                  Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "1, noSST);", "", "", "switch to page 1")
                  Print #f, ""
            '------------------------------------------------------------------------------------------------
        End If
            '------------------------------------------------------------------------------------------------
            ' Configuration of Channel Control Registers:
            '------------------------------------------------------------------------------------------------
            Print #f, FormatSectionHeader(2, 1, "Configuration of Channel Control Registers:")
         For iChannel = 0 To 7
           If (r.v("ADC_USE_CHANNEL_" & iChannel) = 1) Then
                Print #f, FormatComments(2, 1, "", "", "Configuration of Channel " & iChannel)
                v = 0
                v = v + r.v(sRegPre & "CHCTR" & iChannel & "_RESRSEL")
                Print #f, FormatComments(2, 1, "", "", "- the result register" & v & " is selected")
                v = 0
                v = v + r.v(sRegPre & "CHCTR" & iChannel & "_LCC")
                Print #f, FormatComments(2, 1, "", "", "- the limit check " & v & " is selected")
                Print #f, ""
                v = 0
                v = v + r.sfr(sRegPre & "CHCTR" & iChannel)
                Print #f, FormatCodeStatemente(2, sRegPre & "CHCTR" & iChannel, "=", FormatTextOutput(v, "H2"), _
                          "load channel control register")
                Print #f, ""
           End If
         Next iChannel
            
            If (i = 0) Then
                Print #f, FormatComments(2, 1, "", "", "- No ADC Channels are selected")
                Print #f, ""
            End If
            
                  Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, noSST);", "", "", "switch to page 0")
                  Print #f, ""
            
            '------------------------------------------------------------------------------------------------
            ' Configuration of Sample Time Control:
            '------------------------------------------------------------------------------------------------
            Print #f, FormatSectionHeader(2, 1, "Configuration of Sample Time Control:")
            Print #f, ""
            v = 0
            v = v + r.sfr(sRegPre & "INPCR0")
            Print #f, FormatCodeStatemente(2, sRegPre & "INPCR0", "=", FormatTextOutput(v, "H2"), _
                      "load input class register")
            Print #f, ""
            
                  Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "4, noSST);", "", "", "switch to page 4")
                  Print #f, ""
            
            '------------------------------------------------------------------------------------------------
            ' Configuration of Result Control Registers:
            '------------------------------------------------------------------------------------------------
            Print #f, FormatSectionHeader(2, 1, "Configuration of Result Control Registers:")
          For i = 0 To 3
            Print #f, FormatComments(2, 1, "", "", "Configuration of Result Control Register " & i)
            If (r.v(sRegPre & "RCR" & i & "_DRCTR") = 1) Then
                Print #f, FormatComments(2, 1, "", "", "- the data reduction filter is enabled")
              Else
                Print #f, FormatComments(2, 1, "", "", "- the data reduction filter is disabled")
            End If
            If (r.v(sRegPre & "RCR" & i & "_IEN") = 1) Then
                Print #f, FormatComments(2, 1, "", "", "- the event interrupt is enabled")
              Else
                Print #f, FormatComments(2, 1, "", "", "- the event interrupt is disabled")
            End If
            If (r.v(sRegPre & "RCR" & i & "_WFR") = 1) Then
                Print #f, FormatComments(2, 1, "", "", "- the wait-for-read mode is enabled")
              Else
                Print #f, FormatComments(2, 1, "", "", "- the wait-for-read mode is disabled")
            End If
            If (r.v(sRegPre & "RCR" & i & "_VFCTR") = 1) Then
               Print #f, FormatComments(2, 1, "", "", "- the VF reset by read access to RESRxH/RESRAxH")
             Else
               Print #f, FormatComments(2, 1, "", "", "- the VF unchaned by read access to RESRxH/RESRAxH")
            End If
            
            Print #f, ""
            v = 0
            v = v + r.sfr(sRegPre & "RCR" & i)
            Print #f, FormatCodeStatemente(2, sRegPre & "RCR" & i, "=", FormatTextOutput(v, "H2"), _
                      "load result control register " & i)
            Print #f, ""
          Next i

                  Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "5, noSST);", "", "", "switch to page 5")
                  Print #f, ""
            
            '------------------------------------------------------------------------------------------------
            ' Configuration of Channel Interrupt Node Pointer Register:
            '------------------------------------------------------------------------------------------------
            Print #f, FormatSectionHeader(2, 1, "Configuration of Channel Interrupt Node Pointer Register:")
          For i = 0 To 7
            v = 0
            v = v + r.v(sRegPre & "CHINPR_CHINP" & i)
               Print #f, FormatComments(2, 1, "", "", "- the SR " & v & " line become activated if channel " & i & " interrupt is generated")
          Next i
            
            Print #f, ""
            v = 0
            v = v + r.sfr(sRegPre & "CHINPR")
            Print #f, FormatCodeStatemente(2, sRegPre & "CHINPR", "=", FormatTextOutput(v, "H2"), _
                      "load channel interrupt node pointer register ")
            Print #f, ""
            
            '------------------------------------------------------------------------------------------------
            ' Configuration of Event Interrupt Node Pointer Registers:
            '------------------------------------------------------------------------------------------------
            Print #f, FormatSectionHeader(2, 1, "Configuration of Event Interrupt Node Pointer Registers:")
            For i = 0 To 1
              v = 0
              v = v + r.v(sRegPre & "EVINPR_EVINP" & i)
                 Print #f, FormatComments(2, 1, "", "", "- the SR " & v & " line become activated if the event " & i & " interrupt is generated")
            Next i
            For i = 4 To 7
              v = 0
              v = v + r.v(sRegPre & "EVINPR_EVINP" & i)
                 Print #f, FormatComments(2, 1, "", "", "- the SR " & v & " line become activated if the event " & i & " interrupt is generated")
            Next i
            
            Print #f, ""
            v = 0
            v = v + r.sfr(sRegPre & "EVINPR")
            Print #f, FormatCodeStatemente(2, sRegPre & "EVINPR", "=", FormatTextOutput(v, "H2"), _
                      "load event interrupt set flag register ")
            Print #f, ""
            
                  Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, noSST);", "", "", "switch to page 0")
                  Print #f, ""
            
            '------------------------------------------------------------------------------------------------
            ' Configuration of Limit Check Boundary:
            '------------------------------------------------------------------------------------------------
            Print #f, FormatSectionHeader(2, 1, "Configuration of Limit Check Boundary:")
            Print #f, ""
            v = 0
            v = v + r.sfr(sRegPre & "LCBR")
            Print #f, FormatCodeStatemente(2, sRegPre & "LCBR", "=", FormatTextOutput(v, "H2"), _
                      "load limit check boundary register")
            Print #f, ""

            '------------------------------------------------------------------------------------------------
            ' Configuration of External Trigger Control:
            '------------------------------------------------------------------------------------------------
            Print #f, FormatSectionHeader(2, 1, "Configuration of External Trigger Control:")
            
            If (Src0Used = 1) Then
                v = r.v(sRegPre & "ETRCR_ETRSEL0")
            Else
                v = 0
            End If
                Print #f, FormatComments(2, 1, "", "", "- the trigger input ETR0" & v & " is selected for Source 0")
            
            If (Src1Used = 1) Then
                v = r.v(sRegPre & "ETRCR_ETRSEL1")
            Else
                v = 0
            End If
                Print #f, FormatComments(2, 1, "", "", "- the trigger input ETR1" & v & " is selected for Source 1")
             
             If (r.v(sRegPre & "ETRCR_SYNEN0") = 1 And Src0Used = 1) Then
                   temp0 = &H40
                   d = "the synchronizing stage is in external trigger input REQTR0 path"
             Else
                   temp0 = 0
                   d = "the synchronizing stage is not in external trigger input REQTR0 path"
             End If
             Print #f, FormatCommentsForInit(1, d)
             
             If (r.v(sRegPre & "ETRCR_SYNEN1") = 1 And Src1Used = 1) Then
                   temp1 = &H80
                   d = "the synchronizing stage is in external trigger input REQTR1 path"
             Else
                   temp1 = 0
                   d = "the synchronizing stage is not in external trigger input REQTR1 path"
             End If
             Print #f, FormatCommentsForInit(1, d)
             
            Print #f, ""
            
            If (Src0Used = 1 And Src1Used = 1) Then
                v = r.sfr(sRegPre & "ETRCR")
            ElseIf (Src0Used = 1 And Src1Used = 0) Then
                v = temp0 + r.v(sRegPre & "ETRCR_ETRSEL0")
            ElseIf (Src0Used = 0 And Src1Used = 1) Then
                v = temp1 + (r.v(sRegPre & "ETRCR_ETRSEL1") * (2 ^ 3))
            End If
            
            Print #f, FormatCodeStatemente(2, sRegPre & "ETRCR", "=", FormatTextOutput(v, "H2"), _
                      "load external trigger control register")
            Print #f, ""
            
    
            If ((r.v(sRegPre & "PRAR_ASEN0") = 1) Or (r.v(sRegPre & "PRAR_ASEN1") = 1)) Then
                  Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "6, noSST);", "", "", "switch to page 6")
                  Print #f, ""
            End If
            
            '------------------------------------------------------------------------------------------------
            ' Configuration of Queue Mode Register:
            '------------------------------------------------------------------------------------------------
            Print #f, FormatSectionHeader(2, 1, "Configuration of Conversion Queue Mode Register:")
        
        If ((r.v(sRegPre & "PRAR_ASEN0") = 1) And (r.v(sRegPre & "QMR0_ENGT") = 1)) Then
            v = 0
            v = r.v(sRegPre & "QMR0_ENGT")
            Print #f, FormatComments(2, 1, "", "", "- the gating line is permanently " & v)
            
            If (r.v(sRegPre & "QMR0_ENTR") = 1) Then
              Print #f, FormatComments(2, 1, "", "", "- the external trigger is enabled")
            Else
              Print #f, FormatComments(2, 1, "", "", "- the external trigger is disabled")
            End If
            
'            v = r.v(sRegPre & "QMR0_TRMD")
'            Print #f, FormatComments(2, 1, "", "", "- the trigger mode " & v & " is selected")
            
            v = r.sfr(sRegPre & "QMR0")
       Else
            v = 0
            Print #f, FormatComments(2, 1, "", "", "- the gating line is permanently " & v)
            Print #f, FormatComments(2, 1, "", "", "- the external trigger is disabled")
            Print #f, FormatComments(2, 1, "", "", "- the trigger mode " & v & " is selected")
       End If
       
            Print #f, ""
            Print #f, FormatCodeStatemente(2, sRegPre & "QMR0", "=", FormatTextOutput(v, "H2"), _
                      "load queue mode register")
            
            Print #f, ""
            
            '------------------------------------------------------------------------------------------------
            ' Configuration of Channel Control Registers:
            '------------------------------------------------------------------------------------------------
            Print #f, FormatSectionHeader(2, 1, "Configuration of Conversion Request Mode Registers:")
            
        If ((r.v(sRegPre & "PRAR_ASEN1") = 1) And (r.v(sRegPre & "CRMR1_ENGT") = 1)) Then
            v = 0
            v = v + r.v(sRegPre & "CRMR1_ENGT")
            Print #f, FormatComments(2, 1, "", "", "- the gating line is permanently " & v)
            
            If (r.v(sRegPre & "CRMR1_ENTR") = 1) Then
              Print #f, FormatComments(2, 1, "", "", "- the external trigger is enabled")
            Else
              Print #f, FormatComments(2, 1, "", "", "- the external trigger is disabled")
            End If
            
            If (r.v(sRegPre & "CRMR1_ENSI") = 1) Then
              Print #f, FormatComments(2, 1, "", "", "- the source interrupt is enabled")
            Else
              Print #f, FormatComments(2, 1, "", "", "- the source interrupt is disabled")
            End If
            
            If (r.v(sRegPre & "CRMR1_SCAN") = 1) Then
              Print #f, FormatComments(2, 1, "", "", "- the autoscan functionality is enabled")
            Else
              Print #f, FormatComments(2, 1, "", "", "- the autoscan functionality is disabled")
            End If

            v = r.sfr(sRegPre & "CRMR1")
        Else
            v = 0
            Print #f, FormatComments(2, 1, "", "", "- the gating line is permanently " & v)
            Print #f, FormatComments(2, 1, "", "", "- the external trigger is disabled")
            Print #f, FormatComments(2, 1, "", "", "- the source interrupt is disabled")
            Print #f, FormatComments(2, 1, "", "", "- the autoscan functionality is disabled")
        End If
            Print #f, ""
            Print #f, FormatCodeStatemente(2, sRegPre & "CRMR1", "=", FormatTextOutput(v, "H2"), _
                      "load conversion request mode register 1")
                      
            Print #f, ""
            
                  Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, noSST);", "", "", "switch to page 0")
                  Print #f, ""
            '------------------------------------------------------------------------------------------------
            ' Turn on Analog part
            '------------------------------------------------------------------------------------------------
            Print #f, FormatCodeStatemente(2, sRegPre & "GLOBCTR", "|=", FormatTextOutput(&H80, "H2"), _
                      "turn on Analog part")
            Print #f, ""
            
            If (r.v(CORE_PREFIX & "IEN1_EADC") = 1) Then
                'Print #f, FormatCodeStatemente(2, "IEN1", "|=", FormatTextOutput(&H1, "H2"), " - enable ADC-Interrupt (EADC)")
                 Print #f, FormatComments(2, 1, "", "", "- ADC -Interrupt enable bit is set in SHINT_vInit() function")
            Else
                Print #f, FormatComments(2, 1, "", "", "- ADC-Interrupt (EADC) remains disabled")
            Print #f, ""
            End If
            
            Print #f, ""
            Print #f, uc.GetUserCode("(ADC_Init,3)", 2)
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_Init")
            Print #f, ""
            Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' ADC_SeqCEV function
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_SeqCEV") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_SeqCEV")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_SeqCEV")
            Print #f, "{"
            Print #f, ""
                  Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "6, SST1);", "", "", "switch to page 6")
            Print #f, ""
            Print #f, "  " & SfrBitStatement(r, sRegPre & "QMR0_CEV", 1)
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_SeqCEV")
            Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' ADC_SeqTREV function
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_SeqTREV") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_SeqTREV")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_SeqTREV")
            Print #f, "{"
            Print #f, ""
                  Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "6, SST1);", "", "", "switch to page 6")
            Print #f, ""
            Print #f, "  " & SfrBitStatement(r, sRegPre & "QMR0_TREV", 1)
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_SeqTREV")
            Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' ADC_SeqFLUSH function
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_SeqFLUSH") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_SeqFLUSH")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_SeqFLUSH")
            Print #f, "{"
            Print #f, ""
                  Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "6, SST1);", "", "", "switch to page 6")
            Print #f, ""
            Print #f, "  " & SfrBitStatement(r, sRegPre & "QMR0_FLUSH", 1)
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_SeqFLUSH")
            Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' ADC_SeqCLRV function
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_SeqCLRV") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_SeqCLRV")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_SeqCLRV")
            Print #f, "{"
            Print #f, ""
                  Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "6, SST1);", "", "", "switch to page 6")
            Print #f, ""
            Print #f, "  " & SfrBitStatement(r, sRegPre & "QMR0_CLRV", 1)
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_SeqCLRV")
            Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' ADC_SeqREQCHNR
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_SeqREQCHNR") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_SeqREQCHNR")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_SeqREQCHNR")
            Print #f, "{"
            Print #f, ""
            Print #f, "  ubyte ubVal = 0;"
            Print #f, "  if (ubExtTrg == 1){ ubVal = 0x80;}"
            Print #f, "  if (ubEnIntr == 1){ ubVal = ubVal + 0x40;}"
            Print #f, "  if (ubRFill == 1){ ubVal = ubVal + 0x20;}"
            Print #f, "  ubVal = ubVal + (ubChannelNum & 0x07);"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "6, SST1);", "", "", "switch to page 6")
            Print #f, ""
            Print #f, "  " & sRegPre & "QINR0 = ubVal; // requested channel "
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_SeqREQCHNR")
            Print #f, ""
            Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' ADC_SeqSrcFillLeve
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_SeqSrcFillLevel") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_SeqSrcFillLevel")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_SeqSrcFillLevel")
            Print #f, "{"
            Print #f, "  ubyte ubResult = 0x04;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "6, SST1);", "", "", "switch to page 6")
            Print #f, ""
            Print #f, "  if ((" & sRegPre & "QSR0 & 0x20) == 0)"
            Print #f, "  {"
            Print #f, "    ubResult = " & sRegPre & "QSR0" & " & 0x03; "
            Print #f, "  }"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "  return(ubResult);"
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_SeqSrcFillLevel")
            Print #f, ""
            Print #f, ""
    End If
    
    
    
    '------------------------------------------------------------------------------------------------
    ' ADC_SeqSrcEvent
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_SeqSrcEvent") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_SeqSrcEvent")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_SeqSrcEvent")
            Print #f, "{"
            Print #f, "  ubyte ubResult = 0;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "6, SST1);", "", "", "switch to page 6")
            Print #f, ""
            Print #f, "  ubResult = (" & sRegPre & "QSR0" & " & " & _
                FormatTextOutput(BitMask(r, sRegPre & "QSR0", "EV"), "H2") & ") >> 0x04;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "  return(ubResult);"
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_SeqSrcEvent")
            Print #f, ""
            Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' ADC_SeqSrcQueueEmpty
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_SeqSrcQueueEmpty") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_SeqSrcQueueEmpty")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_SeqSrcQueueEmpty")
            Print #f, "{"
            Print #f, "  ubyte ubResult = 0;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "6, SST1);", "", "", "switch to page 6")
            Print #f, ""
            Print #f, "  ubResult = (" & sRegPre & "QSR0" & " & " & _
                FormatTextOutput(BitMask(r, sRegPre & "QSR0", "EMPTY"), "H2") & ") >> 0x05;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "  return(ubResult);"
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_SeqSrcQueueEmpty")
            Print #f, ""
            Print #f, ""
    End If
    
    
    
    '------------------------------------------------------------------------------------------------
    ' ADC_SeqSrcReqChNum
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_SeqSrcReqChNum") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_SeqSrcReqChNum")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_SeqSrcReqChNum")
            Print #f, "{"
            Print #f, "  ubyte ubResult = 0;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "6, SST1);", "", "", "switch to page 6")
            Print #f, ""
            Print #f, "  ubResult = " & sRegPre & "Q0R0" & " & 0x07;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "  return(ubResult);"
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_SeqSrcReqChNum")
            Print #f, ""
            Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' ADC_SeqSrcReqChValid
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_SeqSrcReqChValid") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_SeqSrcReqChValid")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_SeqSrcReqChValid")
            Print #f, "{"
            Print #f, "  ubyte ubResult = 0;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "6, SST1);", "", "", "switch to page 6")
            Print #f, ""
            Print #f, "  ubResult = (" & sRegPre & "Q0R0" & " & " & _
                FormatTextOutput(BitMask(r, sRegPre & "Q0R0", "V"), "H2") & ") >> 0x04;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "  return(ubResult);"
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_SeqSrcReqChValid")
            Print #f, ""
            Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' ADC_SeqSrcQueExtTrg
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_SeqSrcQueExtTrg") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_SeqSrcQueExtTrg")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_SeqSrcQueExtTrg")
            Print #f, "{"
            Print #f, "  ubyte ubResult = 0;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "6, SST1);", "", "", "switch to page 6")
            Print #f, ""
            Print #f, "  ubResult = (" & sRegPre & "Q0R0" & " & " & _
                FormatTextOutput(BitMask(r, sRegPre & "Q0R0", "EXTR"), "H2") & ") >> 0x07;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "  return(ubResult);"
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_SeqSrcQueExtTrg")
            Print #f, ""
            Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' ADC_ParLDEV function
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_ParLDEV") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_ParLDEV")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_ParLDEV")
            Print #f, "{"
            Print #f, ""
                  Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "6, SST1);", "", "", "switch to page 6")
            Print #f, ""
            Print #f, "  " & SfrBitStatement(r, sRegPre & "CRMR1_LDEV", 1)
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_ParLDEV")
            Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' ADC_ParCLRPND function
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_ParCLRPND") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_ParCLRPND")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_ParCLRPND")
            Print #f, "{"
            Print #f, ""
                  Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "6, SST1);", "", "", "switch to page 6")
            Print #f, ""
            Print #f, "  " & SfrBitStatement(r, sRegPre & "CRMR1_CLRPND", 1)
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_ParCLRPND")
            Print #f, ""
    End If
    
    
    '------------------------------------------------------------------------------------------------
    ' ADC_ParREQCH
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_ParREQCH") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_ParREQCH")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_ParREQCH")
            Print #f, "{"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "6, SST1);", "", "", "switch to page 6")
            Print #f, ""
            Print #f, "  " & sRegPre & "CRPR1 |= ubChannelNum ;          //  requested channel number "
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_ParREQCH")
            Print #f, ""
            Print #f, ""
    End If
        
    '------------------------------------------------------------------------------------------------
    ' ADC_ParConvPending
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_ParConvPending") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_ParConvPending")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_ParConvPending")
            Print #f, "{"
            Print #f, "  ubyte ubResult = 0;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "6, SST1);", "", "", "switch to page 6")
            Print #f, ""
            Print #f, "  ubResult = (" & sRegPre & "CRPR1" & " & 0xF0 );"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "  return(ubResult);"
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_ParConvPending")
            Print #f, ""
            Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' ADC_Busy function
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_Busy") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_Busy")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_Busy")
            Print #f, "{"
            Print #f, "  ubyte ubResult = 0;"
            Print #f, ""
                  Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, SST1);", "", "", "switch to page 0")
            Print #f, ""
            Print #f, "  ubResult = " & sRegPre & "GLOBSTR" & " & " & _
                    FormatTextOutput(BitMask(r, sRegPre & "GLOBSTR", "BUSY"), "H2") & ";"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "  return(ubResult);"
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_Busy")
            Print #f, ""
            Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' ADC_bSamplePhase function
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_SamplePhase") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_SamplePhase")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_SamplePhase")
            Print #f, "{"
            Print #f, "  ubyte ubResult = 0;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, SST1);", "", "", "switch to page 0")
            Print #f, ""
            Print #f, "  ubResult = (" & sRegPre & "GLOBSTR" & " & " & _
                FormatTextOutput(BitMask(r, sRegPre & "GLOBSTR", "SAMPLE"), "H2") & ") >> 0x01;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "  return(ubResult);"
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_SamplePhase")
            Print #f, ""
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' ADC_GetConvChNum
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_GetConvChNum") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_GetConvChNum")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_GetConvChNum")
            Print #f, "{"
            Print #f, "  ubyte ubResult = 0;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, SST1);", "", "", "switch to page 0")
            Print #f, ""
            Print #f, "  ubResult = (" & sRegPre & "GLOBSTR" & " & 0x38) >> 3 ;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "  return(ubResult);"
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_GetConvChNum")
            Print #f, ""
            Print #f, ""
    End If
       
    
    '------------------------------------------------------------------------------------------------
    ' ADC_ClrValidFlag
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_ClrValidFlag") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_ClrValidFlag")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_ClrValidFlag")
            Print #f, "{"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "4, SST1);", "", "", "switch to page 4")
            Print #f, ""
                Print #f, "  ADC_VFCR  |=  ubRegisterNum ;          //  Clear Valid Flag Register "
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_ClrValidFlag")
            Print #f, ""
            Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' ADC_GetChIntrFlag
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_GetChIntrFlag") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_GetChIntrFlag")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_GetChIntrFlag")
            Print #f, "{"
            Print #f, "  ubyte ubResult = 0;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "5, SST1);", "", "", "switch to page 5")
            Print #f, ""
            Print #f, "  ubResult = ADC_CHINFR ;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "  return(ubResult);"
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_GetChIntrFlag")
            Print #f, ""
            Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' ADC_SetChIntr
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_SetChIntr") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_SetChIntr")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_SetChIntr")
            Print #f, "{"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "5, SST1);", "", "", "switch to page 5")
            Print #f, ""
            Print #f, "  ADC_CHINSR = ubChannelNum;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_SetChIntr")
            Print #f, ""
            Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' ADC_ClrChIntr
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_ClrChIntr") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_ClrChIntr")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_ClrChIntr")
            Print #f, "{"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "5, SST1);", "", "", "switch to page 5")
            Print #f, ""
            Print #f, "  ADC_CHINCR = ubChannelNum;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_ClrChIntr")
            Print #f, ""
            Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' ADC_GetEventIntrFlag
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_GetEventIntrFlag") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_GetEventIntrFlag")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_GetEventIntrFlag")
            Print #f, "{"
            Print #f, "  ubyte ubResult = 0;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "5, SST1);", "", "", "switch to page 5")
            Print #f, ""
            Print #f, "  ubResult = ADC_EVINFR ;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "  return(ubResult);"
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_GetEventIntrFlag")
            Print #f, ""
            Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' ADC_SetEventIntr
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_SetEventIntr") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_SetEventIntr")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_SetEventIntr")
            Print #f, "{"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "5, SST1);", "", "", "switch to page 5")
            Print #f, ""
            Print #f, "  ADC_EVINSR = ubChannelNum;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_SetEventIntr")
            Print #f, ""
            Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' ADC_ClrEventIntrFlag
    '------------------------------------------------------------------------------------------------
    If (r.v("ADC_USE_ClrEventIntrFlag") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_ClrEventIntrFlag")
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_ClrEventIntrFlag")
            Print #f, "{"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "5, SST1);", "", "", "switch to page 5")
            Print #f, ""
            Print #f, "  ADC_EVINCR = ubChannelNum;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_ClrEventIntrFlag")
            Print #f, ""
            Print #f, ""
    End If
    '------------------------------------------------------------------------------------------------
    ' ADC_ReadResultDatax
    '------------------------------------------------------------------------------------------------
    For ResReg = 0 To 3
        If (r.v("ADC_USE_ReadResultData" & ResReg) = 1) Then
        Print #f, FormatFunctionHeader(r, func, "ADC_NAME_ReadResultData" & ResReg)
           Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_ReadResultData" & ResReg)
           Print #f, "{"
           Print #f, "  uword uwResult = 0;"
           Print #f, ""
                
    '----------------------------------------------------------------------------------------------------
    ' if Data Reduction is not enabled
    '----------------------------------------------------------------------------------------------------
           If (r.v(sRegPre & "RCR" & ResReg & "_DRCTR") = 0) Then
               Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "2, SST1);", "", "", "switch to page 2")
               Print #f, ""
               Print #f, "    if ( ADC_RESR" & ResReg & "L & 0x10 )       //  if Result Register" & ResReg & " contains valid data"
               Print #f, "    {"
               If (r.v(sRegPre & "GLOBCTR_DW") = 1) Then      'if Data Width 8 bit
                    Print #f, "    // 8-bit conversion (without accumulation)"
                    Print #f, "    uwResult = ADC_RESR" & ResReg & "H;  // Result Register" & ResReg & " High"
               Else
                    Print #f, "    // 10-bit conversion (without accumulation)"
'                    Print #f, "    uwResult = (uword)((ADC_RESR" & ResReg & "H << 2) + ((ADC_RESR" & ResReg & "L >> 6)& 0x03));   // for 10-bit resolution (without accumulation)"
                    Print #f, "    uwResult = ((ADC_RESR" & ResReg & "L >> 6) & 0x03); // Result Register" & ResReg & " Low"
                    Print #f, "    uwResult = (((uword)(ADC_RESR" & ResReg & "H << 2)) + uwResult); // Result Register" & ResReg & " High"
               End If
               Print #f, "    }"
            Else
    '----------------------------------------------------------------------------------------------------
    ' if Data Reduction is enabled
    '----------------------------------------------------------------------------------------------------
               Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "3, SST1);", "", "", "switch to page 3")
               Print #f, ""
               Print #f, "    if ( ADC_RESRA" & ResReg & "L & 0x10 )      //  if Result Register" & ResReg & " contains valid data"
               Print #f, "    {"
               If (r.v(sRegPre & "GLOBCTR_DW") = 1) Then      'if Data Width 8 bit
'                  Print #f, "    uwResult = (uword)((ADC_RESRA" & ResReg & "H << 1) + ((ADC_RESRA" & ResReg & "L >> 7)& 0x01)); // for 8-bit resolution (with accumulation)"
                    Print #f, "    // 8-bit resolution (with accumulated 9-bit))"
                    Print #f, "    uwResult = ((ADC_RESRA" & ResReg & "L >> 7) & 0x01); // Result Register" & ResReg & " Low"
                    Print #f, "    uwResult = (((uword)(ADC_RESRA" & ResReg & "H << 1)) + uwResult); // Result Register" & ResReg & " High"
               Else
'                  Print #f, "    uwResult = (uword)((ADC_RESRA" & ResReg & "H << 3) + ((ADC_RESRA" & ResReg & "L >> 5)& 0x07)); // for 10-bit resolution (with accumulation)"
                    Print #f, "    // 10-bit resolution (with accumulated 11-bit))"
                    Print #f, "    uwResult = ((ADC_RESRA" & ResReg & "L >> 5) & 0x07); // Result Register" & ResReg & " Low"
                    Print #f, "    uwResult = (((uword)(ADC_RESRA" & ResReg & "H << 3)) + uwResult); // Result Register" & ResReg & " High"
               End If
               Print #f, "    }"
            End If
               Print #f, ""
               Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
               Print #f, ""
               Print #f, "  return(uwResult);"
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_ReadResultData" & ResReg)
        End If
        Print #f, ""
    Next ResReg
  
    '------------------------------------------------------------------------------------------------
    ' ADC_ReadResultChNumx
    '------------------------------------------------------------------------------------------------
    For ResReg = 0 To 3
        If (r.v("ADC_USE_ReadResultChNum" & ResReg) = 1) Then
            Print #f, FormatFunctionHeader(r, func, "ADC_NAME_ReadResultChNum" & ResReg)
            Print #f, FormatFunctionDeclaration(r, func, "ADC_NAME_ReadResultChNum" & ResReg)
            Print #f, "{"
            Print #f, "  ubyte ubResult = 0;"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "2, SST1);", "", "", "switch to page 2")
            Print #f, ""
                Print #f, "    if ( ADC_RESR" & ResReg & "L & 0x10 )          //  if Result Register" & ResReg & " contains valid data"
                Print #f, "    {"
                Print #f, "      ubResult = ADC_RESR" & ResReg & "L & 0x07;    //  Result Register" & ResReg & "Channel Number"
                Print #f, "    }"
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & ADC_PAGE_NAME & "0, RST1);", "", "", "restore the old ADC page")
            Print #f, ""
            Print #f, "  return(ubResult);"
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_ReadResultChNum" & ResReg)
            Print #f, ""
        End If
    Next ResReg
    
    '------------------------------------------------------------------------------------------------
    ' ADC_Isr function
    '------------------------------------------------------------------------------------------------
'    If (r.v("ADC_USE_Isr") = 1) Then
'    Print #f, FormatFunctionHeader(r, func, "ADC_NAME_Isr")
'            Print #f, uc.GetUserCode("(ADC_Isr,1)", 0)     'USER CODE for application specific descriptions'
'            Print #f, ""
'                      '<--                     max. 78 characters                                -->'
'            Print #f, IsrDeclaration(r, r.v("ADC_NAME_Isr"), "ADCINT")
'            Print #f, "{"
''            Print #f, "  ubyte ubChannel;"
'            Print #f, ""
'            Print #f, uc.GetUserCode("(ADC_Isr,2)", 2)
'            Print #f, ""
'
'            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST2);", "", "", "switch to page 0")
'            Print #f, ""
'            Print #f, FormatComments(2, 0, "", "", "Check Interrupt Request 0 Flag")
'            Print #f, "  if (" & SfrBitTest(r, SCU_PREFIX & "IRCON1_ADCSR0") & ")"
'            Print #f, "  {"
'            Print #f, "    " & SfrBitStatement(r, SCU_PREFIX & "IRCON1_ADCSR0", 0)
'            Print #f, ""
'            Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST2);", "", "", "restore the old SCU page")
'            Print #f, ""
'            Print #f, uc.GetUserCode("(ADC_Isr,3)", 4)
'            Print #f, "  }"
'            Print #f, ""
'
'            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST2);", "", "", "switch to page 0")
'            Print #f, ""
'            Print #f, FormatComments(2, 0, "", "", "Check Interrupt Request 1 Flag")
'            Print #f, "  if (" & SfrBitTest(r, SCU_PREFIX & "IRCON1_ADCSR1") & ")"
'            Print #f, "  {"
'            Print #f, "    " & SfrBitStatement(r, SCU_PREFIX & "IRCON1_ADCSR1", 0)
'            Print #f, ""
'            Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST2);", "", "", "restore the old SCU page")
'            Print #f, ""
'            Print #f, uc.GetUserCode("(ADC_Isr,4)", 4)
'            Print #f, "  }"
'            Print #f, ""
'
'            Print #f, ""
'            Print #f, uc.GetUserCode("(ADC_Isr,5)", 2)
'            Print #f, ""
'            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_Isr")
'            Print #f, ""
'            Print #f, ""
'    End If
If (Not IsCpuTypeOK(r)) Then
If (r.v("ADC_USE_Isr") = 1) Then
    If (r.v("ISR_CHOICE") = 1) Then
            Print #f, FormatFunctionHeader(r, func, "ADC_NAME_Isr")
            Call GetDescriptionForISR(r, f)
            Print #f, uc.GetUserCode("(ADC_Isr,1)", 0)     'USER CODE for application specific descriptions'
            Print #f, ""
                      '<--                     max. 78 characters                                -->'
            Print #f, IsrDeclaration(r, r.v("ADC_NAME_Isr"), "ADCINT")
            Print #f, "{"
'            Print #f, "  ubyte ubChannel;"
            Print #f, ""
            Print #f, uc.GetUserCode("(ADC_Isr,2)", 2)
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "_push_(SYSCON0);", "", "", "push the current RMAP")
            Print #f, FormatCodeStatemente(2, "RESET_RMAP();", "", "", "resets RMAP")
            Print #f, ""
            Print #f, ""
            If ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 0) Then
                Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST0);", "", "", "switch to page 0")
            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 1) Then
                Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST1);", "", "", "switch to page 0")
            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 2) Then
                Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST2);", "", "", "switch to page 0")
            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 3) Then
                Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST3);", "", "", "switch to page 0")
            End If
            Print #f, ""
            Print #f, FormatComments(2, 0, "", "", "Check Interrupt Request 0 Flag")
            Print #f, "  if (" & SfrBitTest(r, SCU_PREFIX & "IRCON1_ADCSR0") & ")"
            Print #f, "  {"
            Print #f, "    " & SfrBitStatement(r, SCU_PREFIX & "IRCON1_ADCSR0", 0)
            Print #f, ""
'            If ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 0) Then
'                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST1);", "", "", "restore the old SCU page")
'            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 1) Then
'                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST2);", "", "", "restore the old SCU page")
'            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 2) Then
'                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST3);", "", "", "restore the old SCU page")
'            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 3) Then
'                Print #f, FormatCodeStatemente(4, "_pop_(" & SCU_PAGE_NAME & "0);", "", "", "restore the old SCU page")
'            End If
            Print #f, ""
            Print #f, uc.GetUserCode("(ADC_Isr,3)", 4)
            Print #f, "  }"
            Print #f, ""
'            If ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 0) Then
'                Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST1);", "", "", "switch to page 0")
'            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 1) Then
'                Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST2);", "", "", "switch to page 0")
'            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 2) Then
'                Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST3);", "", "", "switch to page 0")
'            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 3) Then
'                Print #f, FormatCodeStatemente(2, "_push_(" & SCU_PAGE_NAME & "0);", "", "", "switch to page 0")
'            End If
            Print #f, ""
            Print #f, FormatComments(2, 0, "", "", "Check Interrupt Request 1 Flag")
            Print #f, "  if (" & SfrBitTest(r, SCU_PREFIX & "IRCON1_ADCSR1") & ")"
            Print #f, "  {"
            Print #f, "    " & SfrBitStatement(r, SCU_PREFIX & "IRCON1_ADCSR1", 0)
            Print #f, ""
'            If ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 0) Then
'                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST1);", "", "", "restore the old SCU page")
'            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 1) Then
'                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST2);", "", "", "restore the old SCU page")
'            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 2) Then
'                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST3);", "", "", "restore the old SCU page")
'            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 3) Then
'                Print #f, FormatCodeStatemente(4, "_pop_(" & SCU_PAGE_NAME & "0);", "", "", "restore the old SCU page")
'            End If
'
            Print #f, ""
            Print #f, uc.GetUserCode("(ADC_Isr,4)", 4)
            Print #f, "  }"
            Print #f, ""
            If ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 0) Then
                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST0);", "", "", "restore the old SCU page")
            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 1) Then
                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST1);", "", "", "restore the old SCU page")
            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 2) Then
                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST2);", "", "", "restore the old SCU page")
            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 3) Then
                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST3);", "", "", "restore the old SCU page")
            End If
            Print #f, ""
            Print #f, uc.GetUserCode("(ADC_Isr,5)", 2)
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "_pop_(SYSCON0);", "", "", "restore the old RMAP")
            Print #f, ""
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_Isr")
            Print #f, ""
            Print #f, ""
     ElseIf (r.v("ISR_CHOICE") = 2) Then
            Print #f, FormatFunctionHeader(r, func, "ADC_NAME_Isr")
            Call GetDescriptionForISR(r, f)
            Print #f, uc.GetUserCode("(ADC_Isr,1)", 0)     'USER CODE for application specific descriptions'
            Print #f, ""
                      '<--                     max. 78 characters                                -->'
            Print #f, IsrDeclaration(r, r.v("ADC_NAME_Isr"), "ADCINT")
            Print #f, "{"
'            Print #f, "  ubyte ubChannel;"
            Print #f, ""
            Print #f, uc.GetUserCode("(ADC_Isr,2)", 2)
            Print #f, ""
      
            If ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 0) Then
                Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST0);", "", "", "switch to page 0")
            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 1) Then
                Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST1);", "", "", "switch to page 0")
            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 2) Then
                Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST2);", "", "", "switch to page 0")
            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 3) Then
                 Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST3);", "", "", "switch to page 0")
            End If
            Print #f, ""
            Print #f, FormatComments(2, 0, "", "", "Check Interrupt Request 0 Flag")
            Print #f, "  if (" & SfrBitTest(r, SCU_PREFIX & "IRCON1_ADCSR0") & ")"
            Print #f, "  {"
            Print #f, "    " & SfrBitStatement(r, SCU_PREFIX & "IRCON1_ADCSR0", 0)
            Print #f, ""
'            If ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 0) Then
'                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST1);", "", "", "restore the old SCU page")
'            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 1) Then
'                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST2);", "", "", "restore the old SCU page")
'            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 2) Then
'                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST3);", "", "", "restore the old SCU page")
'            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 3) Then
'                Print #f, FormatCodeStatemente(4, "_pop_(" & SCU_PAGE_NAME & "0);", "", "", "restore the old SCU page")
'            End If
            Print #f, ""
            Print #f, uc.GetUserCode("(ADC_Isr,3)", 4)
            Print #f, "  }"
            Print #f, ""
            
'            If ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 0) Then
'                Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST1);", "", "", "switch to page 0")
'            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 1) Then
'                Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST2);", "", "", "switch to page 0")
'            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 2) Then
'                Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST3);", "", "", "switch to page 0")
'            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 3) Then
'                Print #f, FormatCodeStatemente(2, "_push_(" & SCU_PAGE_NAME & "0);", "", "", "switch to page 0")
'            End If
            Print #f, ""
            Print #f, FormatComments(2, 0, "", "", "Check Interrupt Request 1 Flag")
            Print #f, "  if (" & SfrBitTest(r, SCU_PREFIX & "IRCON1_ADCSR1") & ")"
            Print #f, "  {"
            Print #f, "    " & SfrBitStatement(r, SCU_PREFIX & "IRCON1_ADCSR1", 0)
            Print #f, ""
'            If ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 0) Then
'                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST1);", "", "", "restore the old SCU page")
'            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 1) Then
'                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST2);", "", "", "restore the old SCU page")
'            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 2) Then
'                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST3);", "", "", "restore the old SCU page")
'            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 3) Then
'                Print #f, FormatCodeStatemente(4, "_pop_(" & SCU_PAGE_NAME & "0);", "", "", "restore the old SCU page")
'            End If
            Print #f, ""
            Print #f, uc.GetUserCode("(ADC_Isr,4)", 4)
            Print #f, "  }"
            Print #f, ""
           
            If ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 0) Then
                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST0);", "", "", "restore the old SCU page")
            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 1) Then
                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST1);", "", "", "restore the old SCU page")
            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 2) Then
                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST2);", "", "", "restore the old SCU page")
            ElseIf ((r.v(CORE_PREFIX & "IPH1_PADCH") * 2 ^ r.v(CORE_PREFIX & "IPH1_PADCH") + r.v(CORE_PREFIX & "IP1_PADC")) = 3) Then
                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST3);", "", "", "restore the old SCU page")
            End If
            Print #f, ""
            Print #f, uc.GetUserCode("(ADC_Isr,5)", 2)
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("ADC_NAME_Isr")
            Print #f, ""
            Print #f, ""
     End If
  End If
End If
    '------------------------------------------------------------------------------------------------
            Print #f, uc.GetUserCode("(ADC_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 successful 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 iChannel As Integer
    Dim l As Long
    Dim sName As String
   
    ADCGenerateH = False

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

    'refresh the decriptions of the functions and macros
    Call RefreshFunctionDescriptions(func, r)
    
    'read usercode
    uc.ReadFile ((StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v("ADC_HFILE"))
    
    'open ADC.h
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v("ADC_HFILE") For Output As f
    
    '------------------------------------------------------------------------------------------------
    ' module header
    '------------------------------------------------------------------------------------------------
    d = FormatModuleDescription(GetModuleHeaderH("ADC"))
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), dc("ADC").ModuleDescription, r.v("ADC_HFILE"), r.v("MAIN_CPU_TYPE"), r.v("MAIN_COMPILER"), d)
            
            Print #f, uc.GetUserCode("(ADC_Header,1)", 0)
            Print #f, ""
            Print #f, Rtf.GenerateNote((r.v("ADC_Notes")))
            Print #f, ""
            Print #f, "#ifndef _" & StripExtension(r.v("ADC_HFILE")) & "_H_"
            Print #f, "#define _" & StripExtension(r.v("ADC_HFILE")) & "_H_"
            Print #f, ""
            Print #f, SECTION_PROJECT_INCLUDES
            Print #f, ""  'DAvE includes'
            Print #f, uc.GetUserCode("(ADC_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("(ADC_Header,3)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_DEFINES
            Print #f, ""  'DAvE global #define definitions'
            Print #f, uc.GetUserCode("(ADC_Header,4)", 0)
            Print #f, ""

     For iChannel = 0 To 7
       If (r.v("ADC_USE_CHANNEL_" & iChannel) = 1) Then

            Print #f, "#define " & r.v("ADC_PARA_CHANNEL_" & iChannel) & " " & iChannel

       End If
     Next iChannel

            Print #f, ""
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_TYPEDEFS
            Print #f, ""  'DAvE type definitions'
            Print #f, uc.GetUserCode("(ADC_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("(ADC_Header,6)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_GLOBAL_VARIABLES
            Print #f, ""  'DAvE definition of global variables'
            Print #f, uc.GetUserCode("(ADC_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")
        sName = 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("ADC_USE_" & sName) = 1) And (func.FunctionSort(d) = 1)) Then
            Print #f, FormatFunctionDeclaration(r, func, d) & ";"
        End If
    Next l

            Print #f, ""
            Print #f, uc.GetUserCode("(ADC_Header,8)", 0)
            Print #f, ""
            Print #f, ""
                          'DAvE declarations of global macros'

    '------------------------------------------------------------------------------------------------
            Print #f, SECTION_INTERRUPT_VECTORS
            Print #f, ""  'DAvE interrupt vector definitions
            Print #f, uc.GetUserCode("(ADC_Header,9)", 0)
            Print #f, ""
    If (r.v("ADC_USE_Isr") = 1) Then
            If (Not IsCpuTypeOK(r)) Then
               Print #f, "#define ADCINT " & IntNumber(&H33)
            Else
               Print #f, FormatComments(0, 0, "", "", "Shared interrupt vector definitions are defined in SHARED_INT.H")
            End If
    End If
            Print #f, ""
            Print #f, "#endif  // ifndef _" & StripExtension(r.v("ADC_HFILE")) & "_H_"
    
    'close file
    Close f
    
    ADCGenerateH = True
    Exit Function
    
ADCGenerateHError:
    Close f
    Exit Function
End Function


'------------------------------------------------------------------------------------------------
' Function definition to  Mask the specified bit .
'------------------------------------------------------------------------------------------------
Private Function ADC_BitMask(RegVal As Variant, RegBitPos As Integer) As Integer
  
  ADC_BitMask = RegVal Xor RegVal And (2 ^ RegBitPos)

End Function


