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

Option Explicit

'*****************************************************************************
' Module:
'        CAPCOMx
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions for generating code for the
'        module 'CAPCOMx'.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        1.0   ahe         05.01.01     final version for S-Gold
'
'        ---------------------------------------------------------------------
'        3.0   ahe         02.05.01     final version for Pegasus
'
'        3.1   ahe         02.07.02     - no pin exist check (from 4 to 8 pairs of double register mode)
'                                       - set OUT-Register
'        3.2   ste         18.12.02     spelling correction
'                          22.01.03     bits PDS and ORSEL deleted
'
'        ---------------------------------------------------------------------
'        4.0   ahe         26.08.04     final version for XC164CM Pegasus light
'
'
'*****************************************************************************


'*****************************************************************************
'Description:
'        This functions initiates the code generation for the CAPCOMx 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 = "CC" & vIndex


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

    ' write file CAPCOMx.c
    If (r.v(sVarPre & "_USE_Init") = 1) Then
      If (Not CAPCOMxGenerateC(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 " & sVarPre & " failed", Error$)
    Exit Function
End Function


'*****************************************************************************
'Description:
'        Generates the code for the CAPCOMx.c file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function CAPCOMxGenerateC(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo CAPCOMxGenerateCError
    
    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 iTmr_x      As Integer
    Dim iTmr_y      As Integer
    Dim iTmr        As Integer
    Dim iCh_low     As Integer
    Dim iCh_high    As Integer
    Dim iChannel    As Integer
    Dim lRegPre     As Long
    Dim sVarPre     As String
    Dim sRegPre     As String
    Dim sCCx        As String
    Dim sCAPx       As String
    Dim sCAPCOMx    As String

    Dim sAlternatePinName(1 To 36)  As String
    Dim iChannel_for_CC2            As Integer

    sVarPre = "CC" & vIndex
    sRegPre = sVarPre & "_"
    sCCx = "CC" & vIndex
    sCAPx = "CAP" & vIndex
    sCAPCOMx = "CAPCOM" & vIndex
    
    If (vIndex = 1) Then
        iCh_low = 0
        iCh_high = 7 + CH_HIGH
        iTmr_x = 0
        iTmr_y = 1
    Else
        iCh_low = 16
        iCh_high = 23 + CH_HIGH
        iTmr_x = 7
        iTmr_y = 8
    End If

    
    CAPCOMxGenerateC = 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, 0)
    If (vIndex = 1) Then
        For i = 1 To 16
            RefreshFunctionDescriptions(vIndex, func, r, i)
        Next i
    End If
    If (vIndex = 2) Then
        For i = 17 To 32
            RefreshFunctionDescriptions(vIndex, func, r, i)
        Next i
    End If

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

    'open CAPCOMx.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, ""
    
    '------------------------------------------------------------------------------------------------
    ' CAPCOMx_vInit function
    '------------------------------------------------------------------------------------------------
    n = "Init"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)
      Print #f, "{"
      Print #f, uc.GetUserCode("(" & n & ",2)", 2)
      Print #f, ""
    
      '------------------------------------------------------------------------------------------------
      ' Configuration of " & sCAPCOMx & " Control:
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of " & sCAPCOMx & " Control:")

      'If (r.v(sRegPre & "IOC_ORSEL") = 0) Then
      '      d = "the contents of the port register is visble at output port"
      'Else
            d = "the contents of the CC" & vIndex & "_OUT register is visble at output port"
      'End If
      Print #f, FormatCommentsForInit(1, d)
      
      
      'If (r.v(sRegPre & "IOC_ORSEL") = 1) Then
          If (r.v(sRegPre & "IOC_PL") = 0) Then
                d = "the contents of the port register is changed by the " & sCAPCOMx & " unit"
                Print #f, FormatCommentsForInit(1, d)
          Else
                d = "the contents of the port register is not changed by the " & sCAPCOMx & " unit. "
                d = d & "Use the macro " & r.v(sVarPre & "_NAME_PortUnlock") & _
                    "() for unlock the port."
                Print #f, FormatCommentsForInit(2, d)
          End If
      'End If

      'If (r.v(sRegPre & "IOC_ORSEL") = 0) Then
      '    If (r.v(sRegPre & "IOC_STAG") = 0) Then
      '          d = "staggered mode is enabled"
      '    Else
      '          d = "staggered mode is disabled"
      '    End If
      '    Print #f, FormatCommentsForInit(1, d)
      'End If

      Print #f, ""
      v = 0
      v = v + r.sfr(sRegPre & "IOC")
      Print #f, FormatCodeStatemente(2, sRegPre & "IOC", "=", FormatTextOutput(v, "H4"), _
                "load " & sCAPCOMx & " I/O control register")
      Print #f, ""

      For iTmr = iTmr_x To iTmr_y
          '------------------------------------------------------------------------------------------------
          ' Configuration of " & sCAPCOMx & " Timer " & iTmr & ":
          '------------------------------------------------------------------------------------------------
          Print #f, FormatSectionHeader(2, 1, "Configuration of " & sCAPCOMx & " Timer " & iTmr & ":")

          '-------------------------------------------------------------- TxyCON_TiM
          If (r.v(sRegPre & "T" & iTmr_x & iTmr_y & "CON_T" & iTmr & "M") = 0) Then
              d = "timer " & iTmr & " works in timer mode"
              Print #f, FormatCommentsForInit(1, d)
              '-------------------------------------------------------------- TxyCON_TiI
              d = "prescaler factor is " & _
                  2 ^ (r.v(sRegPre & "T" & iTmr_x & iTmr_y & "CON_T" & iTmr & "I") + 3 * (1 - r.v(sRegPre & "IOC_STAG")))
              Print #f, FormatCommentsForInit(1, d)
          Else
              d = "timer " & iTmr & " works in counter mode, counted by:"
              Print #f, FormatCommentsForInit(1, d)
              
              '-------------------------------------------------------------- TxyCON_TiI
              If (r.v(sRegPre & "T" & iTmr_x & iTmr_y & "CON_T" & iTmr & "I") = 0) Then
                  d = "Overflow/Underflow of timer T6"
              ElseIf (r.v(sRegPre & "T" & iTmr_x & iTmr_y & "CON_T" & iTmr & "I") = 1) Then
                  d = "positive transition (rising edge) on T" & iTmr & "IN (" & _
                      GetPinName(sCCx & "_T" & iTmr & "IN") & ")"
              ElseIf (r.v(sRegPre & "T" & iTmr_x & iTmr_y & "CON_T" & iTmr & "I") = 2) Then
                  d = "negative transition (falling edge) on T" & iTmr & "IN (" & _
                      GetPinName(sCCx & "_T" & iTmr & "IN") & ")"
              ElseIf (r.v(sRegPre & "T" & iTmr_x & iTmr_y & "CON_T" & iTmr & "I") = 3) Then
                  d = "any transition (rising and falling edge) on T" & iTmr & "IN (" & _
                      GetPinName(sCCx & "_T" & iTmr & "IN") & ")"
              End If
              Print #f, FormatCommentsForInit(1, d)
          End If

          '-------------------------------------------------------------- TxyCON_TiR
          If (r.v(sRegPre & "T" & iTmr_x & iTmr_y & "CON_T" & iTmr & "R") = 0) Then
              d = "timer " & iTmr & " run bit is reset"
          Else
              d = "timer " & iTmr & " run bit is set"
          End If
          Print #f, FormatCommentsForInit(1, d)
          Print #f, ""

      Next iTmr
    
      Print #f, ""
    
      v = 0
      'v = v + r.v("T" & iTmr_x & iTmr_y & "CON_T" & iTmr_y & "R") * 2^14
      v = v + r.v(sRegPre & "T" & iTmr_x & iTmr_y & "CON_T" & iTmr_y & "M") * 2 ^ 11
      v = v + r.v(sRegPre & "T" & iTmr_x & iTmr_y & "CON_T" & iTmr_y & "I") * 2 ^ 8
      'v = v + r.v("T" & iTmr_x & iTmr_y & "CON_T" & iTmr_x & "R") * 2^6
      v = v + r.v(sRegPre & "T" & iTmr_x & iTmr_y & "CON_T" & iTmr_x & "M") * 2 ^ 3
      v = v + r.v(sRegPre & "T" & iTmr_x & iTmr_y & "CON_T" & iTmr_x & "I")
      Print #f, FormatCodeStatemente(2, sRegPre & "T" & iTmr_x & iTmr_y & "CON", "=", _
                FormatTextOutput(v, "H4"), _
                "load CAPCOM" & vIndex & " timer " & iTmr_x & " and timer " & iTmr_y & _
                " control register")
      Print #f, ""

      For iTmr = iTmr_x To iTmr_y
          v = 0
          v = v + r.sfr(sRegPre & "T" & iTmr)
          Print #f, FormatCodeStatemente(2, sRegPre & "T" & iTmr, "=", FormatTextOutput(v, "H4"), _
                    "load CAPCOM" & vIndex & " timer " & iTmr & " register")
          Print #f, ""

          v = 0
          v = v + r.sfr(sRegPre & "T" & iTmr & "REL")
          Print #f, FormatCodeStatemente(2, sRegPre & "T" & iTmr & "REL", "=", _
                    FormatTextOutput(v, "H4"), _
                    "load CAPCOM" & vIndex & " timer " & iTmr & " reload register")
          Print #f, ""
      Next iTmr

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

      If (r.Exist("MuxFor_" & sVarPre & "_T" & iTmr_x & "IN") = True) Then
          sAlternatePinName(1) = sVarPre & "_T" & iTmr_x & "IN"
      End If
      'sAlternatePinName(2) = sVarPre & "_T" & iTmr_y & "IN"

      Call SetAlternatePinStatements(f, r, sAlternatePinName())

      Print #f, ""

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

      Call GenerateIR(f, r, sRegPre & "T" & iTmr_x & "IC", "Tmr" & iTmr_x)
      Print #f, ""
      Call GenerateIR(f, r, sRegPre & "T" & iTmr_y & "IC", "Tmr" & iTmr_y)
      Print #f, ""

      For iChannel = iCh_low To iCh_high
          If (vIndex = 1) Then
              iChannel_for_CC2 = iChannel
          Else
              iChannel_for_CC2 = iChannel - 16
          End If

          lRegPre = iChannel \ 4
          sCCx = sRegPre & "M" & lRegPre & "_"
          '------------------------------------------------------------------------------------------------
          ' Configuration of the used " & sCAPCOMx & " Channel :
          '------------------------------------------------------------------------------------------------
          Print #f, FormatSectionHeader(2, 1, "Configuration of the used " & sCAPCOMx & " Channel " & iChannel & ":")

          If (r.Exist("MuxFor_" & sVarPre & "_CC" & iChannel & "IO_I") = True) Then
              '---------------------------------------------------------- CCMODi
              If (r.v(sCCx & "MOD" & iChannel) = 0) Then
                  d = "channel " & iChannel & " is disabled"
                  Print #f, FormatCommentsForInit(1, d)
              Else
                  If (r.v(sCCx & "MOD" & iChannel) = 1) Then
                      d = "capture on positive transition (rising edge) at pin CC" & iChannel & _
                          "IO (" & GetPinName(sVarPre & "_CC" & iChannel & "IO_O") & ")"

                  ElseIf (r.v(sCCx & "MOD" & iChannel) = 2) Then
                      d = "capture on positive transition (falling edge) at pin CC" & iChannel & _
                          "IO (" & GetPinName(sVarPre & "_CC" & iChannel & "IO_O") & ")"

                  ElseIf (r.v(sCCx & "MOD" & iChannel) = 3) Then
                      d = "capture on positive transition (both edges) at pin CC" & iChannel & _
                          "IO (" & GetPinName(sVarPre & "_CC" & iChannel & "IO_O") & ")"

                  ElseIf (r.v(sCCx & "MOD" & iChannel) = 4) Then
                      If (iChannel < (iCh_high - 7)) Then
                          d = "compare mode 0: interrupt only"
                      Else
                          d = "compare mode 0: interrupt only; " & _
                              "(double-register compare mode with CC" & iChannel - 8 & ")"
                      End If

                  ElseIf (r.v(sCCx & "MOD" & iChannel) = 5) Then
                      If (iChannel < (iCh_high - 7)) Then
                          d = "compare mode 1: toggle bit CC" & iChannel_for_CC2 & _
                              "IO and pin CC" & iChannel & "IO (" & _
                              GetPinName(sVarPre & "_CC" & iChannel & "IO_I") & _
                              ") on each match; (double-register compare mode with CC" & _
                              iChannel + 8 & ")"
                      Else
                          d = "compare mode 1: toggle bit CC" & iChannel_for_CC2 & _
                              "IO and pin CC" & iChannel & "IO (" & _
                              GetPinName(sVarPre & "_CC" & iChannel & "IO_I") & _
                              ") on each match"
                      End If
                  ElseIf (r.v(sCCx & "MOD" & iChannel) = 6) Then
                      d = "compare mode 2:  interrupt only"
                  ElseIf (r.v(sCCx & "MOD" & iChannel) = 7) Then
                      d = "compare mode 3:  set bit CC" & iChannel_for_CC2 & _
                          "IO and pin CC" & iChannel & "IO (" & _
                          GetPinName(sVarPre & "_CC" & iChannel & "IO_I") & _
                          ") on each match"
                  End If
                  Print #f, FormatCommentsForInit(1, d)

                  '---------------------------------------------------------- DRM
                  If ((r.v(sCCx & "MOD" & iChannel) = 4) And (iChannel > (iCh_low + 7))) Then
                      If (r.v(sRegPre & "DRM_DR" & iChannel_for_CC2 - 8 & "M") = 0) Then
                          d = "double register compare mode is controlled via CC" & iChannel - 8
                      ElseIf (r.v(sRegPre & "DRM_DR" & iChannel_for_CC2 - 8 & "M") = 1) Then
                          d = "double register compare mode is disabled (interrupt only)"
                      ElseIf (r.v(sRegPre & "DRM_DR" & iChannel_for_CC2 - 8 & "M") = 2) Then
                          d = "double register compare mode is enabled (independent of CC" & iChannel - 8 & ")"
                      End If
                      Print #f, FormatCommentsForInit(1, d)
                  End If
              End If
              
          End If

          If (r.v(sCCx & "MOD" & iChannel) > 0) Then
              '---------------------------------------------------------- ACCi
              If (r.v(sCCx & "ACC" & iChannel) = 0) Then
                  d = "CC" & iChannel & " allocated to CAPCOM" & vIndex & " timer " & iTmr_x
              Else
                  d = "CC" & iChannel & " allocated to CAPCOM" & vIndex & " timer " & iTmr_y
              End If
              Print #f, FormatCommentsForInit(1, d)

              '---------------------------------------------------------- SEE
              If (r.v(sRegPre & "SEM_SEM" & iChannel_for_CC2) = 0) Then
                  d = "single event mode is disabled"
              Else
                  d = "single event mode is enabled"
              End If
              Print #f, FormatCommentsForInit(1, d)

              '---------------------------------------------------------- SEM
              If (r.v(sRegPre & "SEM_SEM" & iChannel_for_CC2) = 1) Then
                  
                  If (r.v(sRegPre & "SEE_SEE" & iChannel_for_CC2) = 0) Then
                      d = "single event is disabled"
                  Else
                      d = "single event is enabled"
                  End If
                  Print #f, FormatCommentsForInit(1, d)

              End If

              '---------------------------------------------------------- OUT_CCxIO
              If (r.v(sRegPre & "OUT_CC" & iChannel_for_CC2 & "IO") = 1) Then
                  d = "register output bit of channel " & iChannel & " is set to high"
                  Print #f, FormatCommentsForInit(1, d)
              End If

              Print #f, ""


          End If

          Print #f, ""

          v = 0
          v = v + r.sfr(sRegPre & "CC" & iChannel)
          Print #f, FormatCodeStatemente(2, sRegPre & "CC" & iChannel, "=", _
                    FormatTextOutput(v, "H4"), _
                    "load CAPCOM" & vIndex & " channel " & iChannel & " register")

          Print #f, ""
      Next iChannel

      'only for 8 channels in one group !!!
      For i = (iCh_low / 4) To (((iCh_high + 1) / 4) - 1)
         v = 0
         v = v + r.sfr(sRegPre & "M" & i)
         Print #f, FormatCodeStatemente(2, sRegPre & "M" & i, "=", _
                   FormatTextOutput(v, "H4"), _
                   "load " & sCAPCOMx & " mode register " & i)
      Next i
      Print #f, ""

      v = 0
      v = v + r.sfr(sRegPre & "DRM")
      Print #f, FormatCodeStatemente(2, sRegPre & "DRM", "=", _
                FormatTextOutput(v, "H4"), _
                "load " & sCAPCOMx & " double-register mode register")
      Print #f, ""

      v = 0
      v = v + r.sfr(sRegPre & "SEM")
      Print #f, FormatCodeStatemente(2, sRegPre & "SEM", "=", _
                FormatTextOutput(v, "H4"), _
                "load " & sCAPCOMx & " single event mode register")
      Print #f, ""

      v = 0
      v = v + r.sfr(sRegPre & "SEE")
      Print #f, FormatCodeStatemente(2, sRegPre & "SEE", "=", _
                FormatTextOutput(v, "H4"), _
                "load " & sCAPCOMx & " single event enable register")
      Print #f, ""

      v = 0
      v = v + r.sfr(sRegPre & "OUT")
      Print #f, FormatCodeStatemente(2, sRegPre & "OUT", "=", _
                FormatTextOutput(v, "H4"), _
                "load " & sCAPCOMx & " compare output register")
      Print #f, ""


      If (vIndex = 2 Or Not (DefGetValue("Name") = "XC164CM")) Then
          '------------------------------------------------------------------------------------------------
          ' Configuration of the used CAPCOMx Port Pins:
          '------------------------------------------------------------------------------------------------
          Print #f, FormatSectionHeader(2, 1, "Configuration of the used " & sCAPCOMx & " Channel Port Pins:")
          v = 0
      
          i = 1
          For iChannel = iCh_low To iCh_high
              If (r.Exist("MuxFor_" & sVarPre & "_CC" & iChannel & "IO_I") = True) Then
                  sAlternatePinName(i) = sVarPre & "_CC" & iChannel & "IO_I"
                  i = i + 1
              End If
              If (r.Exist("MuxFor_" & sVarPre & "_CC" & iChannel & "IO_O") = True) Then
                  sAlternatePinName(i) = sVarPre & "_CC" & iChannel & "IO_O"
                  i = i + 1
              End If
          Next iChannel
      
          Call SetAlternatePinStatements(f, r, sAlternatePinName())
      
          Print #f, ""

      End If

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

      For iChannel = iCh_low To iCh_high
          If (r.v(sVarPre & "_Int" & iChannel & "_Use") = 1) Then
              Call GenerateIR(f, r, sRegPre & "CC" & iChannel & "IC", "CC" & iChannel)
              Print #f, ""
          End If
      Next iChannel

      Print #f, ""
      If (r.v(sRegPre & "T" & iTmr_x & iTmr_y & "CON_T" & iTmr_x & "R") = 1) Then     'if timer x run bit is set
          Print #f, "  " & sRegPre & "T" & iTmr_x & iTmr_y & "CON_T" & iTmr_x & "R    = 1;    // set CAPCOM" & vIndex & " timer " & iTmr_x & " run bit"
          Print #f, ""
      End If

      If (r.v(sRegPre & "T" & iTmr_x & iTmr_y & "CON_T" & iTmr_y & "R") = 1) Then     'if timer y run bit is set
          Print #f, "  " & sRegPre & "T" & iTmr_x & iTmr_y & "CON_T" & iTmr_y & "R    = 1;    // set CAPCOM" & vIndex & " timer " & iTmr_y & " run bit"
          Print #f, ""
      End If

      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

    For iTmr = iTmr_x To iTmr_y
        '------------------------------------------------------------------------------------------------
        '------------------------------------------------------------------------------------------------
        ' CCx_viTmr.. function
        '------------------------------------------------------------------------------------------------
        '------------------------------------------------------------------------------------------------
        n = "Tmr" & iTmr
        If (IsISRSelected(r, sVarPre, sRegPre & "T" & iTmr & "IC", n)) Then
          Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
          Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
          Print #f, ""
          Print #f, GenerateIsrDeclaration(r, r.v(sVarPre & "_NAME_" & n), _
                      sVarPre & "_T" & iTmr & "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
    
    Next iTmr

    For iChannel = iCh_low To iCh_high
        '------------------------------------------------------------------------------------------------
        '------------------------------------------------------------------------------------------------
        ' CCx_viCC.. function
        '------------------------------------------------------------------------------------------------
        '------------------------------------------------------------------------------------------------
        If (r.v(sVarPre & "_Int" & iChannel & "_Use") = 1) Then
            n = "CC" & iChannel
            If (IsISRSelected(r, sVarPre, sRegPre & "CC" & iChannel & "IC", n)) Then
              Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
              Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
              Print #f, ""
              Print #f, GenerateIsrDeclaration(r, r.v(sVarPre & "_NAME_" & n), _
                          sVarPre & "_CC" & iChannel & "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
        End If
    
    Next iChannel

    '------------------------------------------------------------------------------------------------
    ' Other function
    '------------------------------------------------------------------------------------------------

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


'*****************************************************************************
'Description:
'        Generates the code for the CAPCOMx.h file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function CAPCOMxGenerateH(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo CAPCOMxGenerateHError
    
    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 l           As Long
    Dim v           As Variant
    Dim iTmr_x      As Integer
    Dim iTmr_y      As Integer
    Dim iTmr        As Integer
    Dim iCh_low     As Integer
    Dim iCh_high    As Integer
    Dim iChannel    As Integer
    Dim sRegPre     As String
    Dim sCCx        As String
    
    sVarPre = "CC" & vIndex
    sRegPre = sVarPre & "_"
    sCCx = "CC" & vIndex
    
    If (vIndex = 1) Then
        iCh_low = 0
        iCh_high = 7 + CH_HIGH
        iTmr_x = 0
        iTmr_y = 1
    Else
        iCh_low = 16
        iCh_high = 23 + CH_HIGH
        iTmr_x = 7
        iTmr_y = 8
    End If
   
    CAPCOMxGenerateH = 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, 0)
    If (vIndex = 1) Then
        For i = 1 To 16
            RefreshFunctionDescriptions(vIndex, func, r, i)
        Next i
    End If
    If (vIndex = 2) Then
        For i = 17 To 32
            RefreshFunctionDescriptions(vIndex, func, r, i)
        Next i
    End If
    
    'refresh the decriptions of the parameters
    RefreshParameterDescriptions(vIndex, para, r)

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


    n = "TIMER_" & iTmr_x
    Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "" & iTmr_x)

    n = "TIMER_" & iTmr_y
    Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "" & iTmr_y)

    For i = iCh_low To iCh_high
      If (r.v(sVarPre & "_CC" & i & "_Use") = 1) Then
        n = "CC_" & i
        Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "" & 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'

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_StartTmr macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "StartTmr"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & " " _
                & r.v(sVarPre & "_NAME_" & n) & "_##TimerNr()"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_" & iTmr_x) & "() " & _
                sRegPre & "T" & iTmr_x & iTmr_y & "CON_T" & iTmr_x & "R = 1"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_" & iTmr_y) & "() " & _
                sRegPre & "T" & iTmr_x & iTmr_y & "CON_T" & iTmr_y & "R = 1"
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_StopTmr macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "StopTmr"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & " " _
                & r.v(sVarPre & "_NAME_" & n) & "_##TimerNr()"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_" & iTmr_x) & "() " & _
                sRegPre & "T" & iTmr_x & iTmr_y & "CON_T" & iTmr_x & "R = 0"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_" & iTmr_y) & "() " & _
                sRegPre & "T" & iTmr_x & iTmr_y & "CON_T" & iTmr_y & "R = 0"
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_ClearTmr macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ClearTmr"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & " " & _
                r.v(sVarPre & "_NAME_" & n) & "_##TimerNr()"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_" & iTmr_x) & "() " & _
                sRegPre & "T" & iTmr_x & iTmr_y & "CON_T" & iTmr_x & "R = 0; " & _
                sRegPre & "T" & iTmr_x & " = 0x0000"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_" & iTmr_y) & "() " & _
                sRegPre & "T" & iTmr_x & iTmr_y & "CON_T" & iTmr_y & "R = 0; " & _
                sRegPre & "T" & iTmr_y & " = 0x0000"
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_ReadTmr macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ReadTmr"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & " " & _
                r.v(sVarPre & "_NAME_" & n) & "_##TimerNr()"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_" & iTmr_x) & "() " & _
                sRegPre & "T" & iTmr_x
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_" & iTmr_y) & "() " & _
                sRegPre & "T" & iTmr_y
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_LoadTmr macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "LoadTmr"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & " " & _
                r.v(sVarPre & "_NAME_" & n) & "_##TimerNr(Value)"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_" & iTmr_x) & "(Value) " & _
                sRegPre & "T" & iTmr_x & " = Value"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_" & iTmr_y) & "(Value) " & _
                sRegPre & "T" & iTmr_y & " = Value"
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_SetReload macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetReload"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & " " & _
                r.v(sVarPre & "_NAME_" & n) & "_##TimerNr(Value)"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_" & iTmr_x) & "(Value) " & _
                sRegPre & "T" & iTmr_x & "REL = Value"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_" & iTmr_y) & "(Value) " & _
                sRegPre & "T" & iTmr_y & "REL = Value"
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_SetCCxReg macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetCCxReg"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & " " & _
                r.v(sVarPre & "_NAME_" & n) & "_##RegName(Value)"

      For i = iCh_low To iCh_high
          Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                    r.v(sVarPre & "_PARA_CC_" & i) & "(Value) " & _
                    sRegPre & "CC" & i & " = Value"
      Next i

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

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_GetCCxReg macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetCCxReg"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & " " & r.v(sVarPre & "_NAME_" & n) & "_##RegName()"

      For i = iCh_low To iCh_high
          Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                    r.v(sVarPre & "_PARA_CC_" & i) & "() " & _
                    sRegPre & "CC" & i
      Next i

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

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_PortLock macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "PortLock"
    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 & "IOC_PL = 1"
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_PortUnlock macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "PortUnlock"
    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 & "IOC_PL = 0"
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_EnableSingleEvent macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "EnableSingleEvent"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & " " & r.v(sVarPre & "_NAME_" & n) & "_##Channel()"
      If (vIndex = 1) Then
          For i = iCh_low To iCh_high
              Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                        r.v(sVarPre & "_PARA_CC_" & i) & "() " & sRegPre & "SEE |= ((uword)0x0001 << " & i & ")"
          Next i

      Else
          For i = iCh_low To iCh_high
              Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                        r.v(sVarPre & "_PARA_CC_" & i) & "() " & sRegPre & "SEE |= ((uword)0x0001 << " & i - 16 & ")"
          Next i

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

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_SelectDoubleRegisterCompareMode macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SelectDoubleRegisterCompareMode"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & " " & r.v(sVarPre & "_NAME_" & n) & "_##Channel(Mode)"
      If (vIndex = 1) Then
          Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                    r.v(sVarPre & "_PARA_CC_8") & "(Mode) " & _
                    sRegPre & "DRM = (" & sRegPre & "DRM & ~(uword)0x0003) | (Mode)"

          Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                    r.v(sVarPre & "_PARA_CC_9") & "(Mode) " & _
                    sRegPre & "DRM = (" & sRegPre & "DRM & ~(uword)0x000C) | (Mode << 2)"

          Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                    r.v(sVarPre & "_PARA_CC_10") & "(Mode) " & _
                    sRegPre & "DRM = (" & sRegPre & "DRM & ~(uword)0x0030) | (Mode << 4)"

          Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                    r.v(sVarPre & "_PARA_CC_11") & "(Mode) " & _
                    sRegPre & "DRM = (" & sRegPre & "DRM & ~(uword)0x00C0) | (Mode << 6)"

          Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                    r.v(sVarPre & "_PARA_CC_12") & "(Mode) " & _
                    sRegPre & "DRM = (" & sRegPre & "DRM & ~(uword)0x0300) | (Mode << 8)"

          Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                    r.v(sVarPre & "_PARA_CC_13") & "(Mode) " & _
                    sRegPre & "DRM = (" & sRegPre & "DRM & ~(uword)0x0C00) | (Mode << 10)"

          Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                    r.v(sVarPre & "_PARA_CC_14") & "(Mode) " & _
                    sRegPre & "DRM = (" & sRegPre & "DRM & ~(uword)0x3000) | (Mode << 12)"

          Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                    r.v(sVarPre & "_PARA_CC_15") & "(Mode) " & _
                    sRegPre & "DRM = (" & sRegPre & "DRM & ~(uword)0xC000) | (Mode << 14)"

      Else
          Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                    r.v(sVarPre & "_PARA_CC_24") & "(Mode) " & _
                    sRegPre & "DRM = (" & sRegPre & "DRM & ~(uword)0x0003) | (Mode)"

          Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                    r.v(sVarPre & "_PARA_CC_25") & "(Mode) " & _
                    sRegPre & "DRM = (" & sRegPre & "DRM & ~(uword)0x000C) | (Mode << 2)"

          Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                    r.v(sVarPre & "_PARA_CC_26") & "(Mode) " & _
                    sRegPre & "DRM = (" & sRegPre & "DRM & ~(uword)0x0030) | (Mode << 4)"

          Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                    r.v(sVarPre & "_PARA_CC_27") & "(Mode) " & _
                    sRegPre & "DRM = (" & sRegPre & "DRM & ~(uword)0x00C0) | (Mode << 6)"

          Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                    r.v(sVarPre & "_PARA_CC_28") & "(Mode) " & _
                    sRegPre & "DRM = (" & sRegPre & "DRM & ~(uword)0x0300) | (Mode << 8)"

          Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                    r.v(sVarPre & "_PARA_CC_29") & "(Mode) " & _
                    sRegPre & "DRM = (" & sRegPre & "DRM & ~(uword)0x0C00) | (Mode << 10)"

          Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                    r.v(sVarPre & "_PARA_CC_30") & "(Mode) " & _
                    sRegPre & "DRM = (" & sRegPre & "DRM & ~(uword)0x3000) | (Mode << 12)"

          Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                    r.v(sVarPre & "_PARA_CC_31") & "(Mode) " & _
                    sRegPre & "DRM = (" & sRegPre & "DRM & ~(uword)0xC000) | (Mode << 14)"

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

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_SetCCxOUT macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetCCxOUT"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & " " & _
                r.v(sVarPre & "_NAME_" & n) & "_##Channel()"

      If (vIndex = 1) Then
          For i = iCh_low To iCh_high
              Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & r.v(sVarPre & "_PARA_CC_" & i) & _
                        "() " & sRegPre & "OUT_CC" & i & "IO = 1"
          Next i
      Else
          For i = iCh_low To iCh_high
              Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & r.v(sVarPre & "_PARA_CC_" & i) & _
                        "() " & sRegPre & "OUT_CC" & i - 16 & "IO = 1"
          Next i
      End If

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

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_ResetCCxOUT macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ResetCCxOUT"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & " " & _
                r.v(sVarPre & "_NAME_" & n) & "_##Channel()"

      If (vIndex = 1) Then
          For i = iCh_low To iCh_high
              Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & r.v(sVarPre & "_PARA_CC_" & i) & _
                        "() " & sRegPre & "OUT_CC" & i & "IO = 0"
          Next i
      Else
          For i = iCh_low To iCh_high
              Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & r.v(sVarPre & "_PARA_CC_" & i) & _
                        "() " & sRegPre & "OUT_CC" & i - 16 & "IO = 0"
          Next i
      End If

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

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_GetCCxOUT macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetCCxOUT"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & " " & _
                r.v(sVarPre & "_NAME_" & n) & "_##Channel()"

      If (vIndex = 1) Then
          For i = iCh_low To iCh_high
              Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & r.v(sVarPre & "_PARA_CC_" & i) & _
                        "() " & sRegPre & "OUT_CC" & i & "IO"
          Next i
      Else
          For i = iCh_low To iCh_high
              Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & r.v(sVarPre & "_PARA_CC_" & i) & _
                        "() " & sRegPre & "OUT_CC" & i - 16 & "IO"
          Next i
      End If

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

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_SetPortDirectionOutput macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetPortDirectionOutput"
    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 & "IOC_PDS = 0"
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_SetPortDirectionOutput macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetPortDirectionInput"
    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 & "IOC_PDS = 1"
      Print #f, ""
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' Interrupt Vectors
    '------------------------------------------------------------------------------------------------
    Print #f, SECTION_INTERRUPT_VECTORS
    Print #f, ""  'DAvE interrupt vector definitions

    If (vIndex = 1) Then
        For iTmr = iTmr_x To iTmr_y
            n = "Tmr" & iTmr
            If (IsISRSelected(r, sVarPre, sRegPre & "T" & iTmr & "IC", n)) Then
                v = 32 + iTmr
                Print #f, "#define " & sVarPre & "_T" & iTmr & "INT    " & FormatTextOutput(v, "H2")
            End If
        Next iTmr
    Else
        For iTmr = iTmr_x To iTmr_y
            n = "Tmr" & iTmr
            If (IsISRSelected(r, sVarPre, sRegPre & "T" & iTmr & "IC", n)) Then
                v = 54 + iTmr
                Print #f, "#define " & sVarPre & "_T" & iTmr & "INT    " & FormatTextOutput(v, "H2")
            End If
        Next iTmr
    End If

    If (vIndex = 1) Then
        For iChannel = 0 To 15
            n = "CC" & iChannel
            If (IsISRSelected(r, sVarPre, sRegPre & "CC" & iChannel & "IC", n)) Then
                v = 16 + iChannel
                Print #f, "#define " & sVarPre & "_CC" & iChannel & "INT    " & FormatTextOutput(v, "H2")
            End If
        Next iChannel
    Else
        For iChannel = 16 To 28
            n = "CC" & iChannel
            If (IsISRSelected(r, sVarPre, sRegPre & "CC" & iChannel & "IC", n)) Then
                v = 32 + iChannel
                Print #f, "#define " & sVarPre & "_CC" & iChannel & "INT    " & FormatTextOutput(v, "H2")
            End If
        Next iChannel
        For iChannel = 29 To 31
            n = "CC" & iChannel
            If (IsISRSelected(r, sVarPre, sRegPre & "CC" & iChannel & "IC", n)) Then
                v = 39 + iChannel
                Print #f, "#define " & sVarPre & "_CC" & iChannel & "INT    " & FormatTextOutput(v, "H2")
            End If
        Next iChannel
    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
    
    CAPCOMxGenerateH = True
    Exit Function
    
CAPCOMxGenerateHError:
    Close f
    Exit Function
End Function

