'#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         18.05.01     final version for XC164CS
'
'        1.1   ahe         18.01.02     set SR register always
'
'              ahe         05.06.02     added USER_CODE_9
'
'              ahe         06.08.02     change the condition for generate ENCHE
'                                       and ENWHE
'        1.2   ste         18.12.02     final version for XC167CI
'
'*****************************************************************************


'*****************************************************************************
'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 = "CCU" & 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_12 As Integer
    Dim iTmr_13 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 sAlternatePinName(1 To 32) As String

    Dim sVarPre As String
    Dim sRegPre As String

    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"
    
    iCh_low = 0
    iCh_high = 3
    iTmr_12 = 12
    iTmr_13 = 13
    
    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)

    '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, ""
    
      '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      iTmr = iTmr_12
      '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      '------------------------------------------------------------------------------------------------
      ' Configuration of " & sVarPre & " Timer " & iTmr & ":
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of " & sVarPre & " Timer " & iTmr & ":")

      '-------------------------------------------------------------- TxyCON_TiM
      d = "prescaler factor is " & _
      2^(r.v(sRegPre & "TCTR0_T" & iTmr & "CLK"))
      Print #f, FormatCommentsForInit(1, d)

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

      If (r.v(sRegPre & "TCTR2_T" & iTmr & "SSC") = 1) Then
          d = "single shot mode is enabled"
      Else
          d = "single shot mode is disabled"
      End If
      Print #f, FormatCommentsForInit(1, d)

      If (r.v(sRegPre & "TCTR0_CTM") = 0) Then
          d = "timer " & iTmr & " works in edge aligned mode"
      Else
          d = "timer " & iTmr & " works in center aligned mode"
      End If
      Print #f, FormatCommentsForInit(1, d)

      If (r.v(sRegPre & "IEN_ENT12PM") = 0) Then
          d = "interrupt on period match is disabled"
      Else
          d = "interrupt on period match is enabled"
      End If
      Print #f, FormatCommentsForInit(1, d)

      If (r.v(sRegPre & "IEN_ENT12OM") = 0) Then
          d = "interrupt on one match is disabled"
      Else
          d = "interrupt on one match is enabled"
      End If
      Print #f, FormatCommentsForInit(1, d)

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

      '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      iTmr = iTmr_13
      '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      '------------------------------------------------------------------------------------------------
      ' Configuration of " & sVarPre & " Timer " & iTmr & ":
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of " & sVarPre & " Timer " & iTmr & ":")

      '-------------------------------------------------------------- TxyCON_TiM
      d = "prescaler factor is " & _
      2^(r.v(sRegPre & "TCTR0_T" & iTmr & "CLK"))
      Print #f, FormatCommentsForInit(1, d)

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

      
      If (r.v(sRegPre & "TCTR4_T" & iTmr & "RS") = 0) Then
          If (r.v(sRegPre & "TCTR2_T" & iTmr & "TEC") = 0) Then
              d = "trigger control is disabled"
          ElseIf (r.v(sRegPre & "TCTR2_T" & iTmr & "TEC") = 1) Then
              d = "T13 is started on a T12 compare event on channel 0"
          ElseIf (r.v(sRegPre & "TCTR2_T" & iTmr & "TEC") = 2) Then
              d = "T13 is started on a T12 compare event on channel 1"
          ElseIf (r.v(sRegPre & "TCTR2_T" & iTmr & "TEC") = 3) Then
              d = "T13 is started on a T12 compare event on channel 2"
          ElseIf (r.v(sRegPre & "TCTR2_T" & iTmr & "TEC") = 4) Then
              d = "T13 is started on any T12 compare event (ch. 0, 1, 2)"
          ElseIf (r.v(sRegPre & "TCTR2_T" & iTmr & "TEC") = 5) Then
              d = "T13 is started upon a period match of T12"
          ElseIf (r.v(sRegPre & "TCTR2_T" & iTmr & "TEC") = 6) Then
              d = "T13 is started upon a zero-match of T12"
          ElseIf (r.v(sRegPre & "TCTR2_T" & iTmr & "TEC") = 7) Then
              d = "T13 is started on any hall state change"
          End If
          If (r.v(sRegPre & "TCTR2_T" & iTmr & "TEC") > 0) Then
              If (r.v(sRegPre & "TCTR2_T" & iTmr & "TED") = 1) Then
                  d = d & ", while T12 is counting up"
              ElseIf (r.v(sRegPre & "TCTR2_T" & iTmr & "TED") = 2) Then
                  d = d & ", while T12 is counting down"
              ElseIf (r.v(sRegPre & "TCTR2_T" & iTmr & "TED") = 3) Then
                  d = d & ", independent on the count direction of T12"
              End If
          End If
          Print #f, FormatCommentsForInit(1, d)
      End If
      
      If (r.v(sRegPre & "TCTR2_T" & iTmr & "SSC") = 1) Then
          d = "single shot mode is enabled"
      Else
          d = "single shot mode is disabled"
      End If
      Print #f, FormatCommentsForInit(1, d)

      If (r.v(sRegPre & "IEN_ENT13PM") = 0) Then
          d = "interrupt on period match is disabled"
      Else
          d = "interrupt on period match is enabled"
      End If
      Print #f, FormatCommentsForInit(1, d)

      If (r.v(sRegPre & "IEN_ENT13CM") = 0) Then
          d = "interrupt on compare match is disabled"
      Else
          d = "interrupt on compare match is enabled"
      End If
      Print #f, FormatCommentsForInit(1, d)

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

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

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

      '------------------------------------------------------------------------------------------------
      ' Configuration of multi channel mode:
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of Multi Channel Mode:")

      If (r.v(sRegPre & "MODCTR_MCMEN") = 0) Then
          d = "multi channel mode is disabled"
      Else
          d = "multi channel mode is enabled"
      End If
      Print #f, FormatCommentsForInit(1, d)

      If (r.v(sRegPre & "MODCTR_MCMEN") = 1) Then
          Print #f, ""
          d = "switching selelction:"
          Print #f, FormatCommentsForInit(1, d)

          If (r.v(sRegPre & "MCMCTR_SWSEL") = 0) Then
              d = "no trigger request will be generated"
          ElseIf (r.v(sRegPre & "MCMCTR_SWSEL") = 1) Then
              d = "transfer on correct hall event"
          ElseIf (r.v(sRegPre & "MCMCTR_SWSEL") = 2) Then
              d = "transfer on T13 period match (while counting up)"
          ElseIf (r.v(sRegPre & "MCMCTR_SWSEL") = 3) Then
              d = "transfer on T12 one match (while counting down)"
          ElseIf (r.v(sRegPre & "MCMCTR_SWSEL") = 4) Then
              d = "transfer on T12 channel 1 compare match"
          ElseIf (r.v(sRegPre & "MCMCTR_SWSEL") = 5) Then
              d = "transfer on T12 period match"
          End If
          Print #f, FormatCommentsForInit(1, d)

          Print #f, ""
          d = "switching synchronization:"
          Print #f, FormatCommentsForInit(1, d)

          If (r.v(sRegPre & "MCMCTR_SWSYN") = 0) Then
              d = "direct transfer"
          ElseIf (r.v(sRegPre & "MCMCTR_SWSYN") = 1) Then
              d = "synchronisation on T13 period match"
          ElseIf (r.v(sRegPre & "MCMCTR_SWSYN") = 2) Then
              d = "synchronisation on T12 period match (while counting up)"
          ElseIf (r.v(sRegPre & "MCMCTR_SWSYN") = 3) Then
              d = "synchronisation on T12 one match (while counting down)"
          End If
          Print #f, FormatCommentsForInit(1, d)
          Print #f, ""
          
          If (r.v(sRegPre & "MCMCTR_SWSEL") = 1 OR r.v(sRegPre & "MCMCTR_SWSEL") = 5) Then
              If (r.v(sRegPre & "IEN_ENCHE") = 0) Then
                  d = "interrupt on correct hall event is disabled"
              Else
                  d = "interrupt on correct hall event is enabled"
              End If
              Print #f, FormatCommentsForInit(1, d)
    
              If (r.v(sRegPre & "IEN_ENWHE") = 0) Then
                  d = "interrupt on wrong hall event is disabled"
              Else
                  d = "interrupt on wrong hall event is enabled"
              End If
              Print #f, FormatCommentsForInit(1, d)
          End If
    
          If (r.v(sRegPre & "MCMCTR_SWSEL") = 1) Then
              If (r.v(sRegPre & "IEN_ENIDLE") = 0) Then
                  d = "the automatic entering of the idle state is disabled"
              Else
                  d = "the automatic entering of the idle state is enabled"
              End If
              Print #f, FormatCommentsForInit(1, d)
          End If
      End If

      Print #f, ""
      Print #f, ""

      For iChannel = 0 To 2
          '------------------------------------------------------------------------------------------------
          ' Configuration of " & sVarPre & " Channel " & iChannel & ":
          '------------------------------------------------------------------------------------------------
          Print #f, FormatSectionHeader(2, 1, "Configuration of " & sVarPre & " Channel " & iChannel & ":")

          If (r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = 0) Then
              d = "channel " & iChannel & " is disabled"
          ElseIf (r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = 1) Then
              d = "compare mode 1 is selected"
          ElseIf (r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = 2) Then
              d = "compare mode 2 is selected"
          ElseIf (r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = 3) Then
              d = "compare mode 3 is selected"
          ElseIf (r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = 4) Then
              d = "double register capture mode 1 is selected"
          ElseIf (r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = 5) Then
              d = "double register capture mode 2 is selected"
          ElseIf (r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = 6) Then
              d = "double register capture mode 3 is selected"
          ElseIf (r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = 7) Then
              d = "double register capture mode 4 is selected"
          ElseIf (r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = 8) Then
              d = "hall sensor mode is selected"
          ElseIf (r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = 9) Then
              d = "hysteresis like control mode with dead time generation is selected"
          ElseIf (r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = 10) Then
              d = "multi input capture mode 1 is selected"
          ElseIf (r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = 11) Then
              d = "multi input capture mode 2 is selected"
          ElseIf (r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = 12) Then
              d = "multi input capture mode 3 is selected"
          ElseIf (r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = 13) Then
              d = "multi input capture mode 4 is selected"
          ElseIf (r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = 14) Then
              d = "multi input capture mode 5 is selected"
          End If
          Print #f, FormatCommentsForInit(1, d)

          If (Not(r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = 0) And _
              GetPinFunction(r, sVarPre & "_CC6" & iChannel & "_O") = 2 And _
              Not(IsHallSensorModeEnable(r, vIndex))) Then

              If (r.v(sRegPre & "T12MODEN_CC6" & iChannel) = 0) Then
                  d = "T12 modulation for output CC6" & iChannel & " is disabled"
              Else
                  d = "T12 modulation for output CC6" & iChannel & " is enabled"
              End If
              Print #f, FormatCommentsForInit(1, d)
          End If
      
          If (GetPinFunction(r, sVarPre & "_CC6" & iChannel & "_O") = 2) Then
              If (r.v(sRegPre & "T13MODEN_CC6" & iChannel) = 0) Then
                  d = "T13 modulation for output CC6" & iChannel & " is disabled"
              Else
                  d = "T13 modulation for output CC6" & iChannel & " is enabled"
              End If
              Print #f, FormatCommentsForInit(1, d)
          End If

          If (GetPinFunction(r, sVarPre & "_CC6" & iChannel & "_O") = 2 And _
              r.v(sRegPre & "MODCTR_MCMEN") = 1) Then
              
              If (r.v(sRegPre & "MCMPS_CC6" & iChannel) = 0) Then
                  d = "the output CC6" & iChannel & " is set to the passive state"
              Else
                  d = "the output CC6" & iChannel & " can deliver the PWM generated by T12 or T13"
              End If
              Print #f, FormatCommentsForInit(1, d)
          End If
      
          If (GetPinFunction(r, sVarPre & "_CC6" & iChannel & "_O") = 2) Then
              If (r.v(sRegPre & "TRPEN_CC6" & iChannel) = 0) Then
                  d = "the trap functionality of the pin CC6" & iChannel & " is disabled"
              Else
                  d = "the trap functionality of the pin CC6" & iChannel & " is enabled"
              End If
              Print #f, FormatCommentsForInit(1, d)
          End If

          If (Not(r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = 0) And _
              GetPinFunction(r, sVarPre & "_CC6" & iChannel & "_O") = 2 And _
              Not(IsHallSensorModeEnable(r, vIndex))) Then

              If (r.v(sRegPre & "CMPSTAT_CC6" & iChannel & "PS") = 0) Then
                  d = "the compare output CC6" & iChannel & " drives passive level while CC6" & iChannel & "ST is '0'"
              Else
                  d = "the compare output CC6" & iChannel & " drives passive level while CC6" & iChannel & "ST is '1'"
              End If
              Print #f, FormatCommentsForInit(1, d)
          End If

          If (GetPinFunction(r, sVarPre & "_CC6" & iChannel & "_O") = 2) Then
              If (r.v(sRegPre & "PSL_CC6" & iChannel) = 0) Then
                  d = "the passive level of the output CC6" & iChannel & " is '0'"
              Else
                  d = "the passive level of the output CC6" & iChannel & " is '1'"
              End If
              Print #f, FormatCommentsForInit(1, d)
          End If

          '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          'COUTx
          '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          If (Not(r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = 0) And _
              GetPinFunction(r, sVarPre & "_COUT6" & iChannel & "_O") = 2 And _
              Not(IsHallSensorModeEnable(r, vIndex))) Then

              If (r.v(sRegPre & "T12MODEN_COUT6" & iChannel) = 0) Then
                  d = "T12 modulation for output COUT6" & iChannel & " is disabled"
              Else
                  d = "T12 modulation for output COUT6" & iChannel & " is enabled"
              End If
              Print #f, FormatCommentsForInit(1, d)
          End If
      
          If (GetPinFunction(r, sVarPre & "_COUT6" & iChannel & "_O") = 2) Then
              If (r.v(sRegPre & "T13MODEN_COUT6" & iChannel) = 0) Then
                  d = "T13 modulation for output COUT6" & iChannel & " is disabled"
              Else
                  d = "T13 modulation for output COUT6" & iChannel & " is enabled"
              End If
              Print #f, FormatCommentsForInit(1, d)
          End If

          If (GetPinFunction(r, sVarPre & "_COUT6" & iChannel & "_O") = 2 And _
              r.v(sRegPre & "MODCTR_MCMEN") = 1) Then
              
              If (r.v(sRegPre & "MCMPS_COUT6" & iChannel) = 0) Then
                  d = "the output COUT6" & iChannel & " is set to the passive state"
              Else
                  d = "the output COUT6" & iChannel & " can deliver the PWM generated by T12 or T13"
              End If
              Print #f, FormatCommentsForInit(1, d)
          End If
      
          If (GetPinFunction(r, sVarPre & "_COUT6" & iChannel & "_O") = 2) Then
              If (r.v(sRegPre & "TRPEN_COUT6" & iChannel) = 0) Then
                  d = "the trap functionality of the pin COUT6" & iChannel & " is disabled"
              Else
                  d = "the trap functionality of the pin COUT6" & iChannel & " is enabled"
              End If
              Print #f, FormatCommentsForInit(1, d)
          End If

          If (Not(r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = 0) And _
              GetPinFunction(r, sVarPre & "_COUT6" & iChannel & "_O") = 2 And _
              Not(IsHallSensorModeEnable(r, vIndex))) Then

              If (r.v(sRegPre & "CMPSTAT_COUT6" & iChannel & "PS") = 0) Then
                  d = "the compare output COUT6" & iChannel & " drives passive level while CC6" & iChannel & "ST is '0'"
              Else
                  d = "the compare output COUT6" & iChannel & " drives passive level while CC6" & iChannel & "ST is '1'"
              End If
              Print #f, FormatCommentsForInit(1, d)
          End If

          If (GetPinFunction(r, sVarPre & "_COUT6" & iChannel & "_O") = 2) Then
              If (r.v(sRegPre & "PSL_COUT6" & iChannel) = 0) Then
                  d = "the passive level of the output COUT6" & iChannel & " is '0'"
              Else
                  d = "the passive level of the output COUT6" & iChannel & " is '1'"
              End If
              Print #f, FormatCommentsForInit(1, d)
          End If

      
          If (Not(r.v(sRegPre & "T12MSEL_MSEL6" & iChannel) = 0)) Then
              If (r.v(sRegPre & "T12DTC_DTE" & iChannel) = 0) Then
                  d = "dead time generation is disabled"
              Else
                  d = "dead time generation is enabled"
              End If
              Print #f, FormatCommentsForInit(1, d)
              Print #f, ""
          End If

          If (r.v(sRegPre & "IEN_ENCC6" & iChannel & "R") = 0) Then
              d = "generation interrupt on flag ICC6" & iChannel & "R is disabled"
          Else
              d = "generation interrupt on flag ICC6" & iChannel & "R is enabled"
          End If
          Print #f, FormatCommentsForInit(1, d)

          If (r.v(sRegPre & "IEN_ENCC6" & iChannel & "F") = 0) Then
              d = "generation interrupt on flag ICC6" & iChannel & "F is disabled"
          Else
              d = "generation interrupt on flag ICC6" & iChannel & "F is enabled"
          End If
          Print #f, FormatCommentsForInit(1, d)
    
          Print #f, ""
        
          v = 0
          v = v + r.sfr(sRegPre & "CC6" & iChannel & "SR")
          Print #f, FormatCodeStatemente(2, sRegPre & "CC6" & iChannel & "SR", "=", _
                    FormatTextOutput(v, "H4"), _
                   "load " & sVarPre & " capture/compare shadow register for channel " & iChannel)
          Print #f, ""

      Next iChannel

      iChannel = 3
      '------------------------------------------------------------------------------------------------
      ' Configuration of " & sVarPre & " Channel " & iChannel & ":
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of " & sVarPre & " Channel " & iChannel & ":")

      If (GetPinFunction(r, sVarPre & "_COUT63_O") = 2) Then
          If (r.v(sRegPre & "MODCTR_ECT13O") = 0) Then
              d = "alternate output function COUT63 for the PWM signal generated by T13 is disabled"
          Else
              d = "alternate output function COUT63 for the PWM signal generated by T13 is enabled"
          End If
          Print #f, FormatCommentsForInit(1, d)
          
          If (r.v(sRegPre & "TRPCTR_TRPEN13") = 0) Then
              d = "the trap functionality of the pin COUT6" & iChannel & " is disabled"
          Else
              d = "the trap functionality of the pin COUT6" & iChannel & " is enabled"
          End If
          Print #f, FormatCommentsForInit(1, d)

          If (r.v(sRegPre & "CMPSTAT_COUT6" & iChannel & "PS") = 0) Then
              d = "the compare output COUT6" & iChannel & " drives passive level while CC6" & iChannel & "ST is '0'"
          Else
              d = "the compare output COUT6" & iChannel & " drives passive level while CC6" & iChannel & "ST is '1'"
          End If
          Print #f, FormatCommentsForInit(1, d)

          If (r.v(sRegPre & "PSLR_PSL6" & iChannel) = 0) Then
              d = "the passive level of the output COUT6" & iChannel & " is '0'"
          Else
              d = "the passive level of the output COUT6" & iChannel & " is '1'"
          End If
          Print #f, FormatCommentsForInit(1, d)
      End If

      If (r.v(sRegPre & "CMPSTAT_T13IM") = 0) Then
          d = "T13 output is not inverted"
      Else
          d = "T13 output is inverted for further modulation"
      End If
      Print #f, FormatCommentsForInit(1, d)
      
      Print #f, ""
      Print #f, ""
      
      v = 0
      v = v + r.sfr(sRegPre & "CC6" & iChannel & "SR")
      Print #f, FormatCodeStatemente(2, sRegPre & "CC6" & iChannel & "SR", "=", _
                FormatTextOutput(v, "H4"), _
               "load " & sVarPre & " capture/compare shadow register for channel " & iChannel)
      
      Print #f, ""
      Print #f, ""

      v = 0
      v = v + r.sfr(sRegPre & "T12DTC")
      Print #f, FormatCodeStatemente(2, sRegPre & "T12DTC", "=", _
                FormatTextOutput(v, "H4"), _
               "load " & sVarPre & " dead time control register for timer T12")

      Print #f, ""
      v = 0
      v = v + r.sfr(sRegPre & "T12MSEL")
      Print #f, FormatCodeStatemente(2, sRegPre & "T12MSEL", "=", _
                FormatTextOutput(v, "H4"), _
               "load " & sVarPre & " T12 campture/compare mode select register")

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

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

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

      Print #f, ""
      v = 0
      v = v + r.sfr(sRegPre & "MCMOUTS")
      Print #f, FormatCodeStatemente(2, sRegPre & "MCMOUTS", "=", _
                FormatTextOutput(v, "H4"), _
               "load " & sVarPre & " multi channel mode output control register")
      Print #f, ""
      v = 0
      v = v + r.sfr(sRegPre & "MCMCTR")
      Print #f, FormatCodeStatemente(2, sRegPre & "MCMCTR", "=", _
                FormatTextOutput(v, "H4"), _
               "load " & sVarPre & " multi channel mode control register")

      Print #f, ""

      d = "enable shadow transfer to T12 and T13"
      Print #f, FormatCommentsForInit(1, d)
      v = 0
      v = v + 2^6
      v = v + 2^14
      Print #f, FormatCodeStatemente(2, sRegPre & "TCTR4", "=", _
                FormatTextOutput(v, "H4"), _
               "load " & sVarPre & " timer control register 4")
      Print #f, ""

      
      '------------------------------------------------------------------------------------------------
      ' Configuration of " & sVarPre & " trap control:
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of " & sVarPre & " trap control:")

      If (r.v(sRegPre & "TRPCTR_TRPPEN") = 0) Then
          d = "a trap can only be generated by SW by setting the bit TRPF"
      Else
          d = "a trap can be generated by SW or by rising edge on #CTRAP"
      End If
      Print #f, FormatCommentsForInit(1, d)
      
      If (r.v(sRegPre & "TRPCTR_TRPM0") = 0 And r.v(sRegPre & "TRPCTR_TRPM1") = 0) Then
          d = "the trap state is left when a zero-match of T12 (while counting up) is detected (synchronization to T12)"
      ElseIf (r.v(sRegPre & "TRPCTR_TRPM0") = 1 And r.v(sRegPre & "TRPCTR_TRPM1") = 0) Then
          d = "the trap state is left when a zero-match of T13 is detected (synchronization to T13)"
      ElseIf (r.v(sRegPre & "TRPCTR_TRPM0") = 1 And r.v(sRegPre & "TRPCTR_TRPM1") = 1) Then
          d = "the trap state is left immediately without any synchronization to T12 or T13"
      End If
      Print #f, FormatCommentsForInit(1, d)
      
      If (r.v(sRegPre & "TRPCTR_TRPM2") = 0) Then
          d = "bit TRPF is automatically cleared by HW (according to TRPPEN, TRPM0 and TRPM1)"
      Else
          d = "the trap state can be left as soon as bit TRPF is reset by SW (according to TRPPEN, TRPM0 and TRPM1)"
      End If
      Print #f, FormatCommentsForInit(1, d)
      
      If (r.v(sRegPre & "IEN_ENTRPF") = 0) Then
          d = "trap interrupt is disabled"
      Else
          d = "trap interrupt is enabled"
      End If
      Print #f, FormatCommentsForInit(1, d)

      Print #f, ""
      Print #f, ""

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


      If (AllIsrDisabled(r, vIndex) = False) Then
          '------------------------------------------------------------------------------------------------
          ' Configuration of " & sVarPre & " interrupt control:
          '------------------------------------------------------------------------------------------------
          Print #f, FormatSectionHeader(2, 1, "Configuration of " & sVarPre & " interrupt control:")

          i = r.v(sRegPre & "INP_INPCC60")
          d = "for channel 0 interrupts is node I" & i & " selected"
          Print #f, FormatCommentsForInit(1, d)

          i = r.v(sRegPre & "INP_INPCC61")
          d = "for channel 1 interrupts is node I" & i & " selected"
          Print #f, FormatCommentsForInit(1, d)

          i = r.v(sRegPre & "INP_INPCC62")
          d = "for channel 2 interrupts is node I" & i & " selected"
          Print #f, FormatCommentsForInit(1, d)

          i = r.v(sRegPre & "INP_INPCHE")
          d = "for correct hall event interrupt is node I" & i & " selected"
          Print #f, FormatCommentsForInit(1, d)

          i = r.v(sRegPre & "INP_INPERR")
          d = "for error interrupts is node I" & i & " selected"
          Print #f, FormatCommentsForInit(1, d)

          i = r.v(sRegPre & "INP_INPT12")
          d = "for T12 interrupts is node I" & i & " selected"
          Print #f, FormatCommentsForInit(1, d)

          i = r.v(sRegPre & "INP_INPT13")
          d = "for T13 interrupts is node I" & i & " selected"
          Print #f, FormatCommentsForInit(1, d)

          Print #f, ""
          Print #f, ""

          v = 0
          v = v + r.sfr(sRegPre & "INP")
          Print #f, FormatCodeStatemente(2, sRegPre & "INP", "=", _
                    FormatTextOutput(v, "H4"), _
                   "load " & sVarPre & " capture/compare interrupt node pointer register")

          Print #f, ""
          v = 0
          v = v + r.sfr(sRegPre & "IEN")
          Print #f, FormatCodeStatemente(2, sRegPre & "IEN", "=", _
                    FormatTextOutput(v, "H4"), _
                   "load " & sVarPre & " capture/compare interrupt enable register")
        
          Print #f, ""
          Print #f, ""

      End If

      '------------------------------------------------------------------------------------------------
      ' Configuration of the used CAPCOMx Port Pins:
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of the used " & sVarPre & " Channel Port Pins:")
      v = 0
      
      sAlternatePinName(1) = sVarPre & "_CC60_I"
      sAlternatePinName(2) = sVarPre & "_CC60_O"
      sAlternatePinName(3) = sVarPre & "_CC61_I"
      sAlternatePinName(4) = sVarPre & "_CC61_O"
      sAlternatePinName(5) = sVarPre & "_CC62_I"
      sAlternatePinName(6) = sVarPre & "_CC62_O"
      sAlternatePinName(7) = sVarPre & "_COUT60_O"
      sAlternatePinName(8) = sVarPre & "_COUT61_O"
      sAlternatePinName(9) = sVarPre & "_COUT62_O"
      sAlternatePinName(10) = sVarPre & "_COUT63_O"
      sAlternatePinName(11) = sVarPre & "_CC6POS0_I"
      sAlternatePinName(12) = sVarPre & "_CC6POS1_I"
      sAlternatePinName(13) = sVarPre & "_CC6POS2_I"
      sAlternatePinName(14) = sVarPre & "_CTRAP_I"
      
      Call SetAlternatePinStatements(f, r, sAlternatePinName())
      
      Print #f, ""

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

      Call GenerateIR(f, r, sRegPre & "IC", "NodeI0")
      Call GenerateIR(f, r, sRegPre & "EIC", "NodeI1")
      Call GenerateIR(f, r, sRegPre & "T12IC", "NodeI2")
      Call GenerateIR(f, r, sRegPre & "T13IC", "NodeI3")
      Print #f, ""

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


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

    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_viNodeI0 function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "NodeI0"
    If (IsISRSelected(r, sVarPre, sRegPre & "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 & "_NodeI0_INT")
      Print #f, "{"
      Print #f, uc.GetUserCode("(" & n & ",2)",2) 
      Print #f, ""
      Call GenerateCodeForNodes(f, r, vIndex, 0, n, uc)
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_viNodeI1 function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "NodeI1"
    If (IsISRSelected(r, sVarPre, sRegPre & "EIC", n)) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
      Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, GenerateIsrDeclaration(r, r.v(sVarPre & "_NAME_" & n), _
                  sVarPre & "_NodeI1_INT")
      Print #f, "{"
      Print #f, uc.GetUserCode("(" & n & ",2)",2) 
      Print #f, ""
      Call GenerateCodeForNodes(f, r, vIndex, 1, n, uc)
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_viNodeI2 function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "NodeI2"
    If (IsISRSelected(r, sVarPre, sRegPre & "T12IC", 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 & "_NodeI2_INT")
      Print #f, "{"
      Print #f, uc.GetUserCode("(" & n & ",2)",2) 
      Print #f, ""
      Call GenerateCodeForNodes(f, r, vIndex, 2, n, uc)
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_viNodeI3 function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "NodeI3"
    If (IsISRSelected(r, sVarPre, sRegPre & "T13IC", 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 & "_NodeI3_INT")
      Print #f, "{"
      Print #f, uc.GetUserCode("(" & n & ",2)",2) 
      Print #f, ""
      Call GenerateCodeForNodes(f, r, vIndex, 3, n, uc)
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
      Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' Other function
    '------------------------------------------------------------------------------------------------

    Print #f, ""
    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,10)",0)
    Print #f, ""
    
    'close file
    Close f
    
    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_12 As Integer
    Dim iTmr_13 As Integer
    Dim iTmr As Integer
    Dim iCh_low As Integer
    Dim iCh_high As Integer
    Dim iChannel As Integer
    Dim sRegPre As String
    
    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"
    
    iCh_low = 0
    iCh_high = 3
    iTmr_12 = 12
    iTmr_13 = 13
   
    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)
    
    '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_12
    Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "" & iTmr_12)

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

    For i = iCh_low To iCh_high
        n = "CHANNEL_" & i
        Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "" & i)
    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_12) & "() " & _
                sRegPre & "TCTR4 |= 0x0002"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_" & iTmr_13) & "() " & _
                sRegPre & "TCTR4 |= 0x0200"
      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_12) & "() " & _
                sRegPre & "TCTR4 |= 0x0001"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_" & iTmr_13) & "() " & _
                sRegPre & "TCTR4 |= 0x0100"
      Print #f, ""                                                                                    
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_ResetTmr macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ResetTmr"
    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_12) & "() " & _
                sRegPre & "TCTR4 |= 0x0004"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_" & iTmr_13) & "() " & _
                sRegPre & "TCTR4 |= 0x0400"
      Print #f, ""                                                                                    
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_SetTmrPeriod macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetTmrPeriod"
    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_12) & "(Value) " & _
                sRegPre & "T12PR = Value"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_" & iTmr_13) & "(Value) " & _
                sRegPre & "T13PR = Value"
      Print #f, ""                                                                                    
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_GetTmrCounter macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetTmrCounter"
    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_12) & "() " & _
                sRegPre & "T12"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_" & iTmr_13) & "() " & _
                sRegPre & "T13"
      Print #f, ""                                                                                    
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_SetTmrCounter macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetTmrCounter"
    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_12) & "(Value) " & _
                sRegPre & "T12 = Value"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_" & iTmr_13) & "(Value) " & _
                sRegPre & "T13 = Value"
      Print #f, ""                                                                                    
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_SetDeadTimePeriod macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetDeadTimePeriod"
    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 & "T12DTC = (" & sRegPre & "T12DTC & ~(uword)0x003F) | ubValue
      Print #f, ""                                                                                    
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_EnableShadowTransfer macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "EnableShadowTransfer"
    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_12) & "() " & _
                sRegPre & "TCTR4 |= 0x0040"
      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_TIMER_" & iTmr_13) & "() " & _
                sRegPre & "TCTR4 |= 0x4000"
      Print #f, ""                                                                                    
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CCx_LoadChannelShadowRegister macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "LoadChannelShadowRegister"
    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_CHANNEL_" & i) & "(Value) " & _
                    sRegPre & "CC6" & i & "SR = Value"
      Next i

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

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' GetChannelShadowRegister macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetChannelShadowRegister"
    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_CHANNEL_" & i) & "() " & _
                    sRegPre & "CC6" & i & "SR"
      Next i

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

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' GetChannelRegister macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetChannelRegister"
    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_CHANNEL_" & i) & "() " & _
                    sRegPre & "CC6" & i & "R"
      Next i

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

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' SetStatusBit macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetStatusBit"
    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) & "_##ChName()"

      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_CHANNEL_0") & "() " & _
                sRegPre & "CMPMODIF = 0x0001"

      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_CHANNEL_1") & "() " & _
                sRegPre & "CMPMODIF = 0x0002"

      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_CHANNEL_2") & "() " & _
                sRegPre & "CMPMODIF = 0x0004"

      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_CHANNEL_3") & "() " & _
                sRegPre & "CMPMODIF = 0x0040"

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

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ResetStatusBit macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ResetStatusBit"
    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) & "_##ChName()"

      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_CHANNEL_0") & "() " & _
                sRegPre & "CMPMODIF = 0x0100"

      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_CHANNEL_1") & "() " & _
                sRegPre & "CMPMODIF = 0x0200"

      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_CHANNEL_2") & "() " & _
                sRegPre & "CMPMODIF = 0x0400"

      Print #f, "#define " & r.v(sVarPre & "_NAME_" & n) & "_" & _
                r.v(sVarPre & "_PARA_CHANNEL_3") & "() " & _
                sRegPre & "CMPMODIF = 0x4000"

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


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

    n = "NodeI0"
    If (IsISRSelected(r, sVarPre, sRegPre & "IC", n)) Then
        v = 80
        Print #f, "#define " & sVarPre & "_NodeI0_INT    " & FormatTextOutput(v, "H2")
    End If

    n = "NodeI1"
    If (IsISRSelected(r, sVarPre, sRegPre & "EIC", n)) Then
        v = 79
        Print #f, "#define " & sVarPre & "_NodeI1_INT    " & FormatTextOutput(v, "H2")
    End If
    
    n = "NodeI2"
    If (IsISRSelected(r, sVarPre, sRegPre & "T12IC", n)) Then
        v = 77
        Print #f, "#define " & sVarPre & "_NodeI2_INT    " & FormatTextOutput(v, "H2")
    End If
    
    n = "NodeI3"
    If (IsISRSelected(r, sVarPre, sRegPre & "T13IC", n)) Then
        v = 78
        Print #f, "#define " & sVarPre & "_NodeI3_INT    " & FormatTextOutput(v, "H2")
    End If
    
    Print #f, ""
    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,9)",0)
    Print #f, ""
    Print #f, ""
    Print #f, "#endif  // ifndef _" & StripExtension(r.v(sVarPre & "_HFILE")) & "_H_"

    'close file
    Close f
    
    CAPCOMxGenerateH = True
    Exit Function
    
CAPCOMxGenerateHError:
    Close f
    Exit Function
End Function




'*****************************************************************************
'Description:
'        This function generates code for ISR NodeIx.
'-----------------------------------------------------------------------------
'Parameters:
'        f
'        r                reference to the Registers object
'        vIndex           a parameter
'        iNode            number of node
'        n                ISR-name
'        uc               user code variable
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Sub GenerateCodeForNodes(f As Variant, r As ProjectData, vIndex As Variant, iNode As Integer, n As String, uc As UserCode)
    Dim sVarPre As String
    Dim sRegPre As String
    Dim s       As String
    
    sVarPre = "CCU" & vIndex
    sRegPre = sVarPre & "_"

    If (r.v(sRegPre & "INP_INPCC60") = iNode) Then
        s = "if " & sRegPre & "IS_ICC60R"
        If (r.v(sRegPre & "IEN_ENCC60R") = 1) Then
            Print #f, "  if(" & sRegPre & "IS & 0x0001)   // " & s
            Print #f, "  {
            s = "    // capture, compare match rising edge detection an channel 0" & vbcrlf
            Print #f, "" & s
            Print #f, uc.GetUserCode("(" & n & ",10)",4)
            Print #f, ""
            s = "clear flag " & sRegPre & "IS_ICC60R"
            Print #f, "    " & sRegPre & "ISR = 0x0001;  // " & s
            Print #f, "  }
            Print #f, ""
        End If
        s = "if " & sRegPre & "IS_ICC60F"
        If (r.v(sRegPre & "IEN_ENCC60F") = 1) Then
            Print #f, "  if(" & sRegPre & "IS & 0x0002)  // " & s 
            Print #f, "  {
            s = "    // capture, compare match faling edge detection an channel 0" & vbcrlf
            Print #f, "" & s
            Print #f, uc.GetUserCode("(" & n & ",11)",4)
            Print #f, ""
            s = "clear flag " & sRegPre & "IS_ICC60F"
            Print #f, "    " & sRegPre & "ISR = 0x0002;  // " & s
            Print #f, "  }
            Print #f, ""
        End If
    End If

    If (r.v(sRegPre & "INP_INPCC61") = iNode) Then
        s = "if " & sRegPre & "IS_ICC61R"
        If (r.v(sRegPre & "IEN_ENCC61R") = 1) Then
            Print #f, "  if(" & sRegPre & "IS & 0x0004)  // " & s
            Print #f, "  {
            s = "    // capture, compare match rising edge detection an channel 1" & vbcrlf
            Print #f, "" & s
            Print #f, uc.GetUserCode("(" & n & ",12)",4)
            Print #f, ""
            s = "clear flag " & sRegPre & "IS_ICC61R"
            Print #f, "    " & sRegPre & "ISR = 0x0004;  // " & s
            Print #f, "  }
            Print #f, ""
        End If
        s = "if " & sRegPre & "IS_ICC61F"
        If (r.v(sRegPre & "IEN_ENCC61F") = 1) Then
            Print #f, "  if(" & sRegPre & "IS & 0x0008)  // " & s
            Print #f, "  {
            s = "    // capture, compare match faling edge detection an channel 1" & vbcrlf
            Print #f, "" & s
            Print #f, uc.GetUserCode("(" & n & ",13)",4)
            Print #f, ""
            s = "clear flag " & sRegPre & "IS_ICC61F"
            Print #f, "    " & sRegPre & "ISR = 0x0008;  // " & s
            Print #f, "  }
            Print #f, ""
        End If
    End If

    If (r.v(sRegPre & "INP_INPCC62") = iNode) Then
        s = "if " & sRegPre & "IS_ICC62R"
        If (r.v(sRegPre & "IEN_ENCC62R") = 1) Then
            Print #f, "  if(" & sRegPre & "IS & 0x0010)  // " & s
            Print #f, "  {
            s = "    // capture, compare match rising edge detection an channel 2" & vbcrlf
            Print #f, "" & s
            Print #f, uc.GetUserCode("(" & n & ",14)",4)
            Print #f, ""
            s = "clear flag " & sRegPre & "IS_ICC62R"
            Print #f, "    " & sRegPre & "ISR = 0x0010;  // " & s
            Print #f, "  }
            Print #f, ""
        End If
        s = "if " & sRegPre & "IS_ICC62F"
        If (r.v(sRegPre & "IEN_ENCC62F") = 1) Then
            Print #f, "  if(" & sRegPre & "IS & 0x0020)  // " & s
            Print #f, "  {
            s = "    // capture, compare match faling edge detection an channel 2" & vbcrlf
            Print #f, "" & s
            Print #f, uc.GetUserCode("(" & n & ",15)",4)
            Print #f, ""
            s = "clear flag " & sRegPre & "IS_ICC62F"
            Print #f, "    " & sRegPre & "ISR = 0x0020;  // " & s
            Print #f, "  }
            Print #f, ""
        End If
    End If

    If (r.v(sRegPre & "INP_INPT12") = iNode) Then
        s = "if " & sRegPre & "IS_T12OM"
        If (r.v(sRegPre & "IEN_ENT12OM") = 1) Then
            Print #f, "  if(" & sRegPre & "IS & 0x0040)  // " & s
            Print #f, "  {
            s = "    // timer T12 one match detection" & vbcrlf
            Print #f, "" & s
            Print #f, uc.GetUserCode("(" & n & ",20)",4)
            Print #f, ""
            s = "clear flag " & sRegPre & "IS_T12OM"
            Print #f, "    " & sRegPre & "ISR = 0x0040;  // " & s
            Print #f, "  }
            Print #f, ""
        End If
        s = "if " & sRegPre & "IS_T12PM"
        If (r.v(sRegPre & "IEN_ENT12PM") = 1) Then
            Print #f, "  if(" & sRegPre & "IS & 0x0080)  // " & s
            Print #f, "  {
            s = "    // timer T12 period match detection" & vbcrlf
            Print #f, "" & s
            Print #f, uc.GetUserCode("(" & n & ",19)",4)
            Print #f, ""
            s = "clear flag " & sRegPre & "IS_T12PM"
            Print #f, "    " & sRegPre & "ISR = 0x0080;  // " & s
            Print #f, "  }
            Print #f, ""
        End If
    End If

    If (r.v(sRegPre & "INP_INPT13") = iNode) Then
        s = "if " & sRegPre & "IS_T13CM"
        If (r.v(sRegPre & "IEN_ENT13CM") = 1) Then
            Print #f, "  if(" & sRegPre & "IS & 0x0100)  // " & s
            Print #f, "  {
            s = "    // timer T13 compare match detection" & vbcrlf
            Print #f, "" & s
            Print #f, uc.GetUserCode("(" & n & ",22)",4)
            Print #f, ""
            s = "clear flag " & sRegPre & "IS_T13CM"
            Print #f, "    " & sRegPre & "ISR = 0x0100;  // " & s
            Print #f, "  }
            Print #f, ""
        End If
        s = "if " & sRegPre & "IS_T13PM"
        If (r.v(sRegPre & "IEN_ENT13PM") = 1) Then
            Print #f, "  if(" & sRegPre & "IS & 0x0200)  // " & s
            Print #f, "  {
            s = "    // timer T13 period match detection" & vbcrlf
            Print #f, "" & s
            Print #f, uc.GetUserCode("(" & n & ",21)",4)
            Print #f, ""
            s = "clear flag " & sRegPre & "IS_T13PM"
            Print #f, "    " & sRegPre & "ISR = 0x0200;  // " & s
            Print #f, "  }
            Print #f, ""
        End If
    End If

    If (r.v(sRegPre & "INP_INPERR") = iNode) Then
        s = "if " & sRegPre & "IS_TRPF"
        If (r.v(sRegPre & "IEN_ENTRPF") = 1) Then
            Print #f, "  if(" & sRegPre & "IS & 0x0400)  // " & s
            Print #f, "  {
            s = "    // trap detection" & vbcrlf
            Print #f, "" & s
            Print #f, uc.GetUserCode("(" & n & ",17)",4)
            Print #f, ""
            s = "clear flag " & sRegPre & "IS_TRPF"
            Print #f, "    " & sRegPre & "ISR = 0x0400;  // " & s
            Print #f, "  }
            Print #f, ""
        End If
        s = "if " & sRegPre & "IS_WHE"
        If (r.v(sRegPre & "IEN_ENWHE") = 1) Then
            Print #f, "  if(" & sRegPre & "IS & 0x2000)  // " & s
            Print #f, "  {
            s = "    // wrong hall event detection" & vbcrlf
            Print #f, "" & s
            Print #f, uc.GetUserCode("(" & n & ",18)",4)
            Print #f, ""
            s = "clear flag " & sRegPre & "IS_WHE"
            Print #f, "    " & sRegPre & "ISR = 0x2000;  // " & s
            Print #f, "  }
            Print #f, ""
        End If
        s = "if " & sRegPre & "IS_IDLE"
        If (r.v(sRegPre & "IEN_ENIDLE") = 1) Then
            Print #f, "  if(" & sRegPre & "IS & 0x4000)  // " & s
            Print #f, "  {
            s = "    // idle state detection" & vbcrlf
            Print #f, "" & s
            Print #f, uc.GetUserCode("(" & n & ",18)",4)
            Print #f, ""
            s = "clear flag " & sRegPre & "IS_IDLE"
            Print #f, "    " & sRegPre & "ISR = 0x4000;  // " & s
            Print #f, "  }
            Print #f, ""
        End If
    End If

    If (r.v(sRegPre & "INP_INPCHE") = iNode) Then
        s = "if " & sRegPre & "IS_CHE"
        If (r.v(sRegPre & "IEN_ENCHE") = 1) Then
            Print #f, "  if(" & sRegPre & "IS  & 0x1000) // " & s
            Print #f, "  {
            s = "    // correct hall event detection" & vbcrlf
            Print #f, "" & s
            Print #f, uc.GetUserCode("(" & n & ",16)",4)
            Print #f, ""
            s = "clear flag " & sRegPre & "IS_CHE"
            Print #f, "    " & sRegPre & "ISR = 0x1000;  // " & s
            Print #f, "  }
            Print #f, ""
        End If
    End If


End Sub

