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

Option Explicit
'*****************************************************************************
' Module:
'        CAN
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions for generating code for the
'        module 'CAN'.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        0.1  sheshadri     05.04.06     Initial version for XC886CLM
'*****************************************************************************


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

    Dim sVarPre As String
    sVarPre = "CAN" & vIndex

    ' write file CAN.h
    If (r.v(sVarPre & "_USE_Init")) Then
      If (Not CANGenerateH(r, vIndex)) Then Exit Function
      If (Not r.AddFile(r.s(sVarPre & "_HFILE"), "H")) Then Exit Function
    End If

    ' write file CAN.c
    If (r.v(sVarPre & "_USE_Init")) Then
      If (Not CANGenerateC(r, vIndex)) Then Exit Function
      If (Not r.AddFile(r.s(sVarPre & "_CFILE"), "C")) 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 CAN.c file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function CANGenerateC(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo CANGenerateCError

    CANGenerateC = False
    
    Dim i As Integer               ' used as index for loops
    Dim j As Integer               ' used as index for loops
    Dim m As Integer               ' used as index for loops
    Dim f As Variant               ' used for file operations
    Dim v As Variant               ' used as temp variable for register operations
    Dim d As String                ' used as temp variable for string operations
    Dim n As String                ' used as temp variable for function names
    Dim dc As DAvECollection       ' used as temp variable for Collections
    Dim uc As UserCode             ' used for UserCode objects
    Dim func As FunctionClass      ' used for FunctionClass objects
    Dim func2 As FunctionClass      ' used for FunctionClass objects
    Dim sVarPre As String          ' used as prefix for variable names
    Dim sRegPre As String          ' used as prefix for register names
    Dim str1 As String             ' used as temp variable for string operations
    Dim str2 As String             ' used as temp variable for string operations
    Dim sAlternatePinName(1 To 2) As String
    Dim Baudrate As baudratetype
    Dim vMask4A As Variant
    Dim vMask4B As Variant
    Dim vRxMask(0 To 7) As Variant
    Dim vTxMask(0 To 7) As Variant
    Dim s As String

    Dim sNCRx As String  ' Node x Control Register
    Dim sNIPRx As String ' Node x Interrupt Pointer Register
    Dim sNPCRx As String ' Node x Port Control Register
    Dim sNBTRx As String ' Node x Bit Timing Register
    Dim sNECNTx As String ' Node x Error Counter Register
    Dim sNFCRx As String ' Node x Frame Counter Register

    Dim iLowestMO(1 To 4) As Integer
    Dim iListNr As Integer
    Dim iListPos As Integer
    Dim iNextNr As Integer
    Dim iSRN As Integer

    Dim sPinName As String
    Dim sPin As String

      '----------------------------------------------------------------------------
      'Alternate pin Functions.............
      '----------------------------------------------------------------------------
    Dim bP0Selected As Boolean
    Dim lP0MaskALTSEL0 As Long
    Dim lP0MaskALTSEL1 As Long
    Dim lP0MaskDIR  As Long
        
    Dim bP1Selected As Boolean
    Dim lP1MaskALTSEL0 As Long
    Dim lP1MaskALTSEL1 As Long
    Dim lP1MaskDIR  As Long
    
    Dim bP3Selected As Boolean
    Dim lP3MaskALTSEL0 As Long
    Dim lP3MaskALTSEL1 As Long
    Dim lP3MaskDIR  As Long
    
    Dim bP4Selected As Boolean
    Dim lP4MaskALTSEL0 As Long
    Dim lP4MaskALTSEL1 As Long
    Dim lP4MaskDIR  As Long

    sVarPre = "CAN" & vIndex     ' prefix for all project variables from this module (specified in Var.dave
    sRegPre = sVarPre & "_"        ' prefix for the registers

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

        r.v("CAN_USE_WriteCANAdrValue") = 1  'sVarPre & "_vWriteCANAdrValue"
        r.v("CAN_NAME_WriteCANAdrValue") = "CAN_vWriteAMData"  'sVarPre & "_vWriteCANAdrValue"

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

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

    'open CAN.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, ""
            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'
'            If (r.v(sVarPre & "_USE_ReadFIFO") = 1 Or r.v(sVarPre & "_USE_FlushReadFIFO") = 1 Or r.v(sVarPre & "_USE_WriteFIFO") = 1) Then
'                Print #f, "static ubyte aubFIFOWritePtr[" & CAN_MAX_OBJECTS & "];"
'                Print #f, "static ubyte aubFIFOReadPtr[" & CAN_MAX_OBJECTS & "];"
'                Print #f, ""
'            End If
'**************************************************************************************
'Following code will put highest value of FIFO to size.
'**************************************************************************************
Dim CAN_FIFO_HIGH As Integer
Dim temp_i As Integer
Dim Cur_Max As Integer

CAN_FIFO_HIGH = 1     ' aubFIFOWritePtr[1] - for initially, suppose noting is configured.

For temp_i = 1 To (CAN_MAX_OBJECTS - 1)
'    If ((r.v("CAN_MOFCR" & temp_i & "_MMC") = 1) Or (r.v("CAN_MOFCR" & temp_i & "_MMC") = 2)) Then
    If (r.v("CAN_MOCTR" & temp_i & "_MSGVAL") = 1) Then
        Cur_Max = temp_i
        CAN_FIFO_HIGH = Cur_Max
    End If
Next temp_i

            If (r.v(sVarPre & "_USE_ReadFIFO") = 1 Or r.v(sVarPre & "_USE_FlushReadFIFO") = 1 Or r.v(sVarPre & "_USE_WriteFIFO") = 1) Then
                Print #f, "static ubyte aubFIFOWritePtr[" & CAN_FIFO_HIGH & "];"
                Print #f, "static ubyte aubFIFOReadPtr[" & CAN_FIFO_HIGH & "];"
                Print #f, ""
            End If
            Print #f, ""
            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
'*'            Call GenerateISRPrototype(r, sVarPre, f, func2)
            Print #f, ""  'DAvE declarations of internal local functions'
            Print #f, uc.GetUserCode("(" & sVarPre & "_General,9)", 0)
            Print #f, ""
            Print #f, ""
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_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, "{"
'      If (r.v(sVarPre & "_USE_ReadFIFO") = 1 Or r.v(sVarPre & "_USE_WriteFIFO") = 1) Then
'        Print #f, "ubyte i;"
'      End If
      Print #f, ""
      Print #f, uc.GetUserCode("(" & n & ",2)", 2)
      Print #f, ""
      '------------------------------------------------------------------------------------------------
      ' Configuration of the Module Clock:
      '------------------------------------------------------------------------------------------------
      Print #f, FormatSectionHeader(2, 1, "Configuration of the Module Clock:")
      d = "the CAN module clock = " & Format(Get_fFCLK(r), "00.00") & " MHz"
      Print #f, FormatCommentsForInit(1, d)
      If (r.v(SCU_PREFIX & "CMCON_FCCFG") = 1) Then
          d = "FCLK runs at 2 times the frequency of PCLK."
          Print #f, FormatCommentsForInit(1, d)
      End If
      Print #f, ""
      d = "CMCON - Clock Control Register is Configured in MAIN_vInit"
      Print #f, FormatCommentsForInit(1, d)

      Print #f, ""
      d = "wait until Panel has finished the initialisation"
      Print #f, FormatCommentsForInit(0, d)
      Print #f, FormatCodeStatemente(4, "CAN_vWriteCANAddress(CAN_PANCTR);", "", "", "Addressing CAN_PANCTR")
      Print #f, FormatCodeStatemente(4, "CAN_DATA1 = CAN_PANCTR_BUSY;", "", "", "")
      Print #f, FormatCodeStatemente(4, "while(CAN_DATA1 & CAN_PANCTR_BUSY){", "", "", "wait until Panel has finished the initialisation")
      Print #f, FormatCodeStatemente(6, "CAN_ADCON &= ~0x01;", "", "", "")
      Print #f, FormatCodeStatemente(6, "}", "", "", "")
      Print #f, ""

      '###################################################################################
      '            CAN Node configuration
      '###################################################################################
      For i = 0 To (CAN_MAX_NODES - 1)
        sNCRx = sRegPre & "NCR" & i
        sNIPRx = sRegPre & "NIPR" & i
        sNPCRx = sRegPre & "NPCR" & i
        sNBTRx = sRegPre & "NBTR" & i
        sNECNTx = sRegPre & "NECNT" & i
        sNFCRx = sRegPre & "NFCR" & i
        
        '------------------------------------------------------------------------------------------------
        ' Configuration of CAN Node x:
        '------------------------------------------------------------------------------------------------
        Print #f, FormatSectionHeader(2, 1, "Configuration of CAN Node " & i & ":")
        Print #f, ""
        '###################################################################################
        '            CAN Node x general configuration
        '###################################################################################
        d = "General Configuration of the Node " & i & ":"
        Print #f, FormatCommentsForSource(2, 1, d)

        d = "set INIT and CCE"
        Print #f, FormatCommentsForInit(1, d)
        If (r.v(sNCRx & "_CALM") = 1) Then
          d = "enable the CAN analyzer mode; monitoring CAN bus traffic without participating in the CAN bus protocol itself"
          Print #f, FormatCommentsForInit(1, d)
        End If
 
        If (r.v(sNCRx & "_TRIE") = 1) Then
          d = "enable interrupt generation when a message transfer is completed"
          Print #f, FormatCommentsForInit(1, d)
          d = "transfer OK interrupt node pointer: MultiCAN SRN " & r.v(sNIPRx & "_TRINP")
          Print #f, FormatCommentsForInit(1, d)
        End If
        If (r.v(sNCRx & "_ALIE") = 1) Then
          d = "enable interrupt generation upon a change of BOFF or EWARN or upon a set of LLE, LOE or INIT"
          Print #f, FormatCommentsForInit(1, d)
          d = "alert interrupt node pointer: MultiCAN SRN " & r.v(sNIPRx & "_ALINP")
          Print #f, FormatCommentsForInit(1, d)
        End If
        If (r.v(sNCRx & "_LECIE") = 1) Then
          d = "enable interrupt generation upon each update of the bit field LEC"
          Print #f, FormatCommentsForInit(1, d)
          d = "last error code interrupt node pointer: MultiCAN SRN " & r.v(sNIPRx & "_LECINP")
          Print #f, FormatCommentsForInit(1, d)
        End If
            
        ' statement for CAN_NCRx Control register
        v = 0
        v = v + r.v(sNCRx & "_CALM") * 2 ^ 7
        v = v + 2 ^ 6   ' CCE allows access to Bit Timing Register
        v = v + r.v(sNCRx & "_CANDIS") * 2 ^ 4
        v = v + r.v(sNCRx & "_ALIE") * 2 ^ 3
        v = v + r.v(sNCRx & "_LECIE") * 2 ^ 2
        v = v + r.v(sNCRx & "_TRIE") * 2 ^ 1
        v = v + 1       ' INIT starts the initialization
        Print #f, ""
        Print #f, "  //-----------" & sNCRx & " = " & FormatTextOutput(v, "H8") & "-------------------------------------"
        Print #f, ""
        Print #f, FormatCodeStatemente(4, "CAN_vWriteCANAddress(" & sNCRx & ");", "", "", "Addressing " & sNCRx)
        Print #f, FormatCodeStatemente(4, "CAN_DATA0 =", "", FormatTextOutput(v, "H2"), "load NODE " & i & " control register[7-0]")
        Print #f, FormatCodeStatemente(4, "CAN_vWriteEN(D0_VALID+ADR_INC);", "", "", "Data0 is Valid for transmission and Write is Enabled" & _
                          "CAN Address pointing to the CAN_NSR" & i & ".")
           
        If (r.v(sNCRx & "_INIT") = 0) Then  ' only if the CAN x module is used
        
        Print #f, ""
        Print #f, "  //  CAN Address pointing to the CAN_NIPR" & i
        Print #f, FormatCodeStatemente(4, "CAN_ADCON = ADR_INC;", "", "", "Auto Increment the current address(+1)")
        Print #f, ""

        v = r.sfr(sNIPRx)
        d = "load NODE " & i & " interrupt pointer register"
        Print #f, FormatCommentsForInit(1, d)
        Print #f, "  //-----------" & sNIPRx & " = " & FormatTextOutput(v, "H8") & "-------------------------------------"
        Print #f, ""
          ' statement for CAN_NIPRx register
        Print #f, FormatCodeStatemente(4, "CAN_DATA0 =", "", "0x" & Format8BitStatement(v, 3), "load NIPR" & i & "_LECINP, NIPR" & i & "_ALINP")
        Print #f, FormatCodeStatemente(4, "CAN_DATA1 =", "", "0x" & Format8BitStatement(v, 2), "load NIPR" & i & "_CFCINP, NIPR" & i & "_TRINP")
        Print #f, FormatCodeStatemente(4, "CAN_vWriteEN(D0_VALID+D1_VALID+ADR_INC);", "", "", "Data0 and Data1 are Valid for with" & _
                    " Auto Increment transmission and Write is Enabled CAN Address pointing to the CAN_NPCR" & i)
        Print #f, ""
          
        Print #f, FormatSectionHeader(2, 1, "Configuration of the used CAN Input Port Pins:")
        ' statement for CAN_NPCRx register (inc. RXSEL)
         If (i = 0) Then ' if NODE 0
                If ((r.v("CAN_USE_NODE0_RXD_PIN")) = 0) Then
                        d = "RXD Pin is not used"
                ElseIf ((r.v("CAN_USE_NODE0_RXD_PIN")) = 1) Then
                        d = "Pin P1.0 is used as RXDC0_0 input"
                ElseIf ((r.v("CAN_USE_NODE0_RXD_PIN")) = 2) Then
                        d = "Pin P3.4 is used as RXDC0_1 input"
                ElseIf ((r.v("CAN_USE_NODE0_RXD_PIN")) = 3) Then
                        d = "Pin P1.6 is used as RXDC0_2 input"
                ElseIf ((r.v("CAN_USE_NODE0_RXD_PIN")) = 4) Then
                        d = "Pin P4.0 is used as RXDC0_3 input"
                End If
         Else
                If ((r.v("CAN_USE_NODE1_RXD_PIN")) = 0) Then
                        d = "RXD Pin is not used"
                ElseIf ((r.v("CAN_USE_NODE1_RXD_PIN")) = 1) Then
                        d = "Pin P0.1 is used as RXDC1_0 input"
                ElseIf ((r.v("CAN_USE_NODE1_RXD_PIN")) = 2) Then
                        d = "Pin P3.2 is used as RXDC1_1 input"
                ElseIf ((r.v("CAN_USE_NODE1_RXD_PIN")) = 3) Then
                        d = "Pin P1.4 is used as RXDC1_3 input"
                End If
         End If
        Print #f, FormatCommentsForInit(1, d)
        
        Print #f, ""
        v = r.sfr(sNPCRx)
        Print #f, "  //-----------" & sNPCRx & " = " & FormatTextOutput(v, "H8") & "-------------------------------------"
        If (r.v(sNPCRx & "_LBM") = 1) Then
            d = "Loop-back mode is enabled"
        Else
            d = "Loop-back mode is disabled"
        End If
            Print #f, FormatCommentsForInit(1, d)
            Print #f, ""
          
            Print #f, FormatCodeStatemente(4, "CAN_DATA0 =", "", "0x" & Format8BitStatement(v, 3), "Receive Select NPCR" & i & "_RXSEL")
            Print #f, FormatCodeStatemente(4, "CAN_DATA1 =", "", "0x" & Format8BitStatement(v, 2), d)
            Print #f, FormatCodeStatemente(4, "CAN_vWriteEN(D0_VALID+D1_VALID+ADR_INC); ", "", "", " Data0 & 1 are Valid for" & _
                       " transmission and Write is Enabled with AutoIncrement CAN Address pointing to the CAN_NBTR" & i)
            Print #f, ""
            Print #f, ""
                  
          '###################################################################################
          '            CAN Node x Baudrate
          '###################################################################################
          Print #f, ""
          d = "Configuration of the Node " & i & " Baud Rate:"
          Print #f, FormatCommentsForSource(2, 1, d)

          Baudrate = ConvertBaudrate(r.d(sVarPre & "_BaudrateNode" & i))
          d = "required baud rate = " & Format(Baudrate.dValue, CAN_PRECISION) & " " & Baudrate.sUnit
          Print #f, FormatCommentsForInit(1, d)

          Baudrate = ConvertBaudrate(GetNodexClock(r, vIndex, i) * 1000000 / _
                     (2 ^ (1 - 1) * (r.v(sNBTRx & "_BRP") + 1) * _
                     (3 + r.d(sNBTRx & "_TSEG1") + r.d(sNBTRx & "_TSEG2"))))

          d = "real baud rate     = " & Format(Baudrate.dValue, CAN_PRECISION) & " " & Baudrate.sUnit
          Print #f, FormatCommentsForInit(1, d)
          d = "sample point       = " & Format((r.v(sNBTRx & "_TSEG1") + 1 + 1) / (r.v(sNBTRx & "_TSEG1") + 1 + r.v(sNBTRx & "_TSEG2") + 1 + 1) * 100, "0.00") & " %"
          Print #f, FormatCommentsForInit(1, d)
          d = "there are " & Format(r.v(sNBTRx & "_TSEG1") + 1) & " time quanta before sample point"
          Print #f, FormatCommentsForInit(1, d)
          d = "there are " & Format(r.v(sNBTRx & "_TSEG2") + 1) & " time quanta after sample point"
          Print #f, FormatCommentsForInit(1, d)
          d = "the (re)synchronization jump width is " & Format(r.v(sNBTRx & "_SJW") + 1) & " time quanta"
          Print #f, FormatCommentsForInit(1, d)
          If (r.v(sNBTRx & "_DIV8") = 1) Then
            d = "enable the module clock divider (by 8)"
            Print #f, FormatCommentsForInit(1, d)
          End If

          ' statement for CAN_NBTRx Bit Timing register
          v = 0
          v = v + r.v(sNBTRx & "_DIV8") * 2 ^ 15
          v = v + r.v(sNBTRx & "_TSEG2") * 2 ^ 12
          v = v + r.v(sNBTRx & "_TSEG1") * 2 ^ 8
          v = v + r.v(sNBTRx & "_SJW") * 2 ^ 6
          v = v + r.v(sNBTRx & "_BRP")
        Print #f, ""
        Print #f, "  //-----------" & sNBTRx & " = " & FormatTextOutput(v, "H8") & "-------------------------------------"
        Print #f, ""
        Print #f, FormatCodeStatemente(4, "CAN_DATA0 =", "", "0x" & Format8BitStatement(v, 3), "load NBTR" & i & "_SJW, BRP")
        Print #f, FormatCodeStatemente(4, "CAN_DATA1 =", "", "0x" & Format8BitStatement(v, 2), "load NBTR" & i & "_DIV8, TSEG2, TSEG1")
        Print #f, FormatCodeStatemente(4, "CAN_vWriteEN(D0_VALID+D1_VALID+ADR_INC); ", "", "", "Data0, 1 are valid for " & _
            "transmission and Write is Enabled with Auto Increment(+1) CAN Address pointing to the CAN_NECNT" & i)
          Print #f, ""
                
          
          '###################################################################################
          ' Error Counter
          
          d = "Configuration of the Node " & i & " Error Counter:"
          Print #f, FormatCommentsForSource(2, 1, d)

          d = "the error warning threshold value (warning level) is " & Format(r.v(sNECNTx & "_EWRNLVL"))
          Print #f, FormatCommentsForInit(1, d)

          ' statement for CAN_NECNTx register
        v = r.sfr(sNECNTx)
        Print #f, ""
        Print #f, "  //-----------" & sNECNTx & " = " & FormatTextOutput(v, "H8") & "-------------------------------------"
        Print #f, ""
        Print #f, FormatCodeStatemente(4, "CAN_DATA2 =", "", "0x" & Format8BitStatement(v, 1), "load NECNT" & i & "_EWRNLVL register")
        Print #f, FormatCodeStatemente(4, "CAN_vWriteEN(D2_VALID+ADR_INC);", "", "", "Data2 is Valid for transmission," & _
                   " Auto Increment current address(+1) and Write is Enabled CAN Address pointing to the CAN_NFCR" & i)
        Print #f, ""

          '###################################################################################
          '            CAN Node x Frame Counter
          '###################################################################################
          d = "Configuration of the Frame Counter:"
          Print #f, FormatCommentsForSource(2, 1, d)
          If (r.v(sNFCRx & "_CFMOD") = 0) Then
              d = "Frame Counter Mode: the counter is incremented upon the reception and transmission of frames"
              Print #f, FormatCommentsForInit(1, d)

              If (IsBitSet(r, sNFCRx & "_CFSEL", 2)) Then
                d = "the counter is incremented each time a frame was transmitted correctly"
                Print #f, FormatCommentsForInit(1, d)
              End If
              If (IsBitSet(r, sNFCRx & "_CFSEL", 1)) Then
                d = "the counter is incremented each time a frame was received correctly"
                Print #f, FormatCommentsForInit(1, d)
              End If
              If (IsBitSet(r, sNFCRx & "_CFSEL", 0)) Then
                d = "the counter is incremented each time a correct foreign frame was monitored on the bus"
                Print #f, FormatCommentsForInit(1, d)
              End If
          End If

          If (r.v(sNFCRx & "_CFMOD") = 1) Then
              d = "Time Stamp Mode: the frame counter is used to count bit times"
              Print #f, FormatCommentsForInit(1, d)
              d = "the frame counter is increment (internally) with the beginning of a new bit time. " & _
                  "The value is sampled during the SOF bit of a new frame. The samped value is visible in the CFC field."
              Print #f, FormatCommentsForInit(1, d)
          End If

          If (r.v(sNFCRx & "_CFMOD") = 2) Then
              d = "Bit Timing Mode: the frame counter is used for analysis of the bit timing"
              Print #f, FormatCommentsForInit(1, d)

              Select Case r.v(sNFCRx & "_CFSEL")
                  Case 0
                       d = "whenever a dominant edge is monitored on the receive input the time between this edge and the most recent dominant edge is stored in CFC"
                  Case 1
                       d = "whenever a recessive edge is monitored on the receive input the time between this edge and the most recent dominant edge is stored in CFC"
                  Case 2
                       d = "whenever a dominant edge is received as a result of a transmitted dominant edge the time between both edges is stored in CFC"
                  Case 3
                       d = "whenever a recessive edge is received as a result of a transmitted recessive edge the time between both edges is stored in CFC"
                  Case 4
                       d = "whenever a dominant edge that qualifies for synchronization is monitored on the received input the time between this edge and the most recent sample point is stored in CFC"
                  Case 5
                       d = "with each start of a new bit time the time between the start of the new bit time and the start of the previous bit time is stored in CFC [11:0]"
                  Case Else
                       d = ""
              End Select
              Print #f, FormatCommentsForInit(1, d)
          End If

          If (r.v(sNFCRx & "_CFCIE") = 1) Then
              d = "enable the frame counter overflow interrupt"
              Print #f, FormatCommentsForInit(1, d)
              d = "frame counter interrupt node pointer: MultiCAN SRN " & r.v(sNIPRx & "_CFCINP")
              Print #f, FormatCommentsForInit(1, d)
          End If
          d = "frame counter: " & FormatTextOutput(r.v(sNFCRx & "_CFC"), "H4")
          Print #f, FormatCommentsForInit(1, d)
          Print #f, ""
          ' statement for Frame Counter register
        Print #f, ""
          v = r.sfr(sNFCRx)
        Print #f, "  //-----------" & sNFCRx & " = " & FormatTextOutput(v, "H8") & "-------------------------------------"
        Print #f, ""
        Print #f, FormatCodeStatemente(4, "CAN_DATA0 =", "", "0x" & Format8BitStatement(v, 3), "load NFCR" & i & "_CFCL")
        Print #f, FormatCodeStatemente(4, "CAN_DATA1 =", "", "0x" & Format8BitStatement(v, 2), "load NFCR" & i & "_CFCH")
        Print #f, FormatCodeStatemente(4, "CAN_DATA2 =", "", "0x" & Format8BitStatement(v, 1), "load NFCR" & i & "_CFCOV, CFCIE, CFMOD, CFSEL")
        Print #f, FormatCodeStatemente(4, "CAN_vWriteEN(D0_VALID+D1_VALID+D2_VALID);", "", "", "Data0, 1, 2 are valid for " & _
                "transmission and Write is Enabled")
          Print #f, ""

        Else
          Print #f, ""
        End If  ' only if the CAN module is used
      Next i

        '###################################################################################
        '            CAN Node x Ports
        '###################################################################################
                                                                                                                                           
        lP0MaskALTSEL0 = 0
        lP0MaskALTSEL1 = 0
        lP0MaskDIR = 0
        bP0Selected = False
        
        lP1MaskALTSEL0 = 0
        lP1MaskALTSEL1 = 0
        lP1MaskDIR = 0
        bP1Selected = False
        
        lP3MaskALTSEL0 = 0
        lP3MaskALTSEL1 = 0
        lP3MaskDIR = 0
        bP3Selected = False
        
        lP4MaskALTSEL0 = 0
        lP4MaskALTSEL1 = 0
        lP4MaskDIR = 0
        bP4Selected = False
    
        Print #f, FormatSectionHeader(2, 1, "Configuration of the used CAN Output Port Pins:")
        ' statement for CAN_NPCRx register (inc. RXSEL)
    i = 0
        sNCRx = sRegPre & "NCR" & i
'    If (i = 0) Then
        If ((r.v(sNCRx & "_INIT") = 0) And (Not r.v("CAN_USE_NODE" & i & "_TXD_PIN") = 0)) Then
            If (r.v(sVarPre & "_USE_NODE0_TXD_PIN") = 1) Then
                sPinName = "TXDC0_0"
                sPin = "P1.1"
                lP1MaskALTSEL0 = lP1MaskALTSEL0 + 2 ^ 1
                lP1MaskALTSEL1 = lP1MaskALTSEL1 + 2 ^ 1
                lP1MaskDIR = lP1MaskDIR + 2 ^ 1
                bP1Selected = True
            End If
               
            If (r.v(sVarPre & "_USE_NODE0_TXD_PIN") = 2) Then
                sPinName = "TXDC0_1"
                sPin = "P3.5"
                lP3MaskALTSEL0 = lP3MaskALTSEL0 + 2 ^ 5
                lP3MaskALTSEL1 = lP3MaskALTSEL1 + 2 ^ 5
                lP3MaskDIR = lP3MaskDIR + 2 ^ 5
                bP3Selected = True
            End If
            If (r.v(sVarPre & "_USE_NODE0_TXD_PIN") = 3) Then
                sPinName = "TXDC0_2"
                sPin = "P1.7"
                lP1MaskALTSEL0 = lP1MaskALTSEL0 + 2 ^ 7
                lP1MaskALTSEL1 = lP1MaskALTSEL1 + 2 ^ 7
                lP1MaskDIR = lP1MaskDIR + 2 ^ 7
                bP1Selected = True
            End If
            
            If (r.v(sVarPre & "_USE_NODE0_TXD_PIN") = 4) Then
                sPinName = "TXDC0_3"
                sPin = "P4.1"
                lP4MaskALTSEL0 = lP4MaskALTSEL0 + 2 ^ 1
                lP4MaskALTSEL1 = lP4MaskALTSEL1 + 2 ^ 1
                lP4MaskDIR = lP4MaskDIR + 2 ^ 1
                bP4Selected = True
            End If
            
            Print #f, FormatComments(2, 1, "", "", "Pin " & sPin & " is used as " & sPinName & " Output "); ""
            
        Else
                d = "NODE0 TXD Pin's are not used"
                Print #f, FormatCommentsForInit(1, d)
        End If
'    Else
    i = 1
        sNCRx = sRegPre & "NCR" & i
    'for NODE 1 is selected
        If ((r.v(sNCRx & "_INIT") = 0) And (Not r.v("CAN_USE_NODE" & i & "_TXD_PIN") = 0)) Then
            If (r.v(sVarPre & "_USE_NODE1_TXD_PIN") = 1) Then
                sPinName = "TXDC1_0"
                sPin = "P0.2"
                lP0MaskALTSEL0 = lP0MaskALTSEL0 + 2 ^ 2
                lP0MaskALTSEL1 = lP0MaskALTSEL1 + 2 ^ 2
                lP0MaskDIR = lP0MaskDIR + 2 ^ 2
                bP0Selected = True
            End If
               
            If (r.v(sVarPre & "_USE_NODE1_TXD_PIN") = 2) Then
                sPinName = "TXDC1_1"
                sPin = "P3.3"
                lP3MaskALTSEL0 = lP3MaskALTSEL0 + 2 ^ 3
                lP3MaskALTSEL1 = lP3MaskALTSEL1 + 2 ^ 3
                lP3MaskDIR = lP3MaskDIR + 2 ^ 3
                bP3Selected = True
            End If
            If (r.v(sVarPre & "_USE_NODE1_TXD_PIN") = 3) Then
                sPinName = "TXDC1_3"
                sPin = "P1.3"
                lP1MaskALTSEL0 = lP1MaskALTSEL0 + 2 ^ 3
                lP1MaskALTSEL1 = lP1MaskALTSEL1 + 2 ^ 3
                lP1MaskDIR = lP1MaskDIR + 2 ^ 3
                bP1Selected = True
            End If
        
            Print #f, FormatComments(2, 1, "", "", "Pin " & sPin & " is used as " & sPinName & " Output "); ""
        Else
                d = "NODE1 TXD Pin's are not used"
                Print #f, FormatCommentsForInit(1, d)
        End If
'    End If
            If bP0Selected Then
                Print #f, ""
                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & PORT_PAGE_NAME & "2, noSST);", "", "", "switch to page 2")
                Print #f, "      P0_ALTSEL0      |= " & FormatTextOutput(lP0MaskALTSEL0, "H2") & ";    //  set AltSel0 "
                Print #f, "      P0_ALTSEL1      |= " & FormatTextOutput(lP0MaskALTSEL1, "H2") & ";    //  set AltSel1"
                Print #f, ""
                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & PORT_PAGE_NAME & "0, noSST);", "", "", "switch to page 0")
                Print #f, "      P0_DIR          |= " & FormatTextOutput(lP0MaskDIR, "H2") & ";        //  set Direction as Output"
                Print #f, ""
            End If
            
            If bP1Selected Then
                Print #f, ""
                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & PORT_PAGE_NAME & "2, noSST);", "", "", "switch to page 2")
                Print #f, "      P1_ALTSEL0      |= " & FormatTextOutput(lP1MaskALTSEL0, "H2") & ";    //  set AltSel0 "
                Print #f, "      P1_ALTSEL1      |= " & FormatTextOutput(lP1MaskALTSEL1, "H2") & ";    //  set AltSel1"
                Print #f, ""
                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & PORT_PAGE_NAME & "0, noSST);", "", "", "switch to page 0")
                Print #f, "      P1_DIR          |= " & FormatTextOutput(lP1MaskDIR, "H2") & ";        //  set Direction as Output"
                Print #f, ""
            End If
            
            If bP3Selected Then
                Print #f, ""
                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & PORT_PAGE_NAME & "2, noSST);", "", "", "switch to page 2")
                Print #f, "      P3_ALTSEL0      |= " & FormatTextOutput(lP3MaskALTSEL0, "H2") & ";    //  set AltSel0 "
                Print #f, "      P3_ALTSEL1      |= " & FormatTextOutput(lP3MaskALTSEL1, "H2") & ";    //  set AltSel1"
                Print #f, ""
                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & PORT_PAGE_NAME & "0, noSST);", "", "", "switch to page 0")
                Print #f, "      P3_DIR          |= " & FormatTextOutput(lP3MaskDIR, "H2") & ";        //  set Direction as Output"
                Print #f, ""
            End If
            
            If bP4Selected Then
                Print #f, ""
                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & PORT_PAGE_NAME & "2, noSST);", "", "", "switch to page 2")
                Print #f, "      P4_ALTSEL0      |= " & FormatTextOutput(lP4MaskALTSEL0, "H2") & ";    //  set AltSel0 "
                Print #f, "      P4_ALTSEL1      |= " & FormatTextOutput(lP4MaskALTSEL1, "H2") & ";    //  set AltSel1"
                Print #f, ""
                Print #f, FormatCodeStatemente(4, "SFR_PAGE(" & PORT_PAGE_NAME & "0, noSST);", "", "", "switch to page 0")
                Print #f, "      P4_DIR          |= " & FormatTextOutput(lP4MaskDIR, "H2") & ";        //  set Direction as Output"
                Print #f, ""
            End If
                Print #f, ""

'    '###################################################################################
'    '            Initialization of the FIFO
'    '###################################################################################
'      If (r.v(sVarPre & "_USE_ReadFIFO") = 1 Or r.v(sVarPre & "_USE_WriteFIFO") = 1) Then
'        Print #f, FormatSectionHeader(2, 1, "Initialization of the FIFO Pointer:")
'        Print #f, ""
'        Print #f, "    for (i = 0; i < " & (CAN_MAX_OBJECTS - 1) & "; i++)"
'        Print #f, "    {"
'        If (r.v(sVarPre & "_USE_WriteFIFO") = 1) Then
'              Print #f, FormatCodeStatemente(8, "aubFIFOWritePtr[i] = 0;", "", "", "")
'        End If
'        If (r.v(sVarPre & "_USE_ReadFIFO") = 1) Then
'              Print #f, FormatCodeStatemente(8, "aubFIFOReadPtr[i] = 0;", "", "", "")
'        End If
'        Print #f, "    }"
'        Print #f, ""
'      End If

      '###################################################################################
      '            Configuration of the CAN Message Object List Structure
      '###################################################################################
      Print #f, FormatSectionHeader(2, 1, "Configuration of the " & sVarPre & " Message Object List Structure:")

      iLowestMO(1) = 255
      iLowestMO(2) = 255
      iLowestMO(3) = 255
      iLowestMO(4) = 255
    Dim ListSelected As Boolean
    ListSelected = False
      ' search the MO with lowest index in each list
      For i = 0 To (CAN_MAX_OBJECTS - 1)
        iListNr = r.v(sRegPre & "MOCTR" & i & "_LIST")
        iListPos = r.v(sVarPre & "_MO" & i & "_LISTPOS")
        If (iListNr > 0) Then
           ListSelected = True
           If (iLowestMO(iListNr) = 255) Then ' for the first found MO
             iLowestMO(iListNr) = i
           Else
             If (iListPos < r.v(sVarPre & "_MO" & iLowestMO(iListNr) & "_LISTPOS")) Then
               iLowestMO(iListNr) = i
             End If
           End If
        End If
      Next i

        Print #f, ""
     If (ListSelected = True) Then
        Print #f, FormatCodeStatemente(4, "CAN_vWriteCANAddress(CAN_PANCTR);", "", "", "Addressing CAN_PANCTR")
        Print #f, ""
     End If
     
      For i = 1 To CAN_MAX_NODES ' for each list
        If (iLowestMO(i) <> 255) Then
          d = "Allocate MOs for list " & i & ":"
          Print #f, FormatCommentsForSource(2, 1, d)
          ' search all MOs of the list i
          iNextNr = iLowestMO(i)
          Do
             If (iNextNr <> GetPNEXT(r, vIndex, iNextNr)) Then
               v = 0
               v = v + i * 2 ^ 24       ' List
               v = v + iNextNr * 2 ^ 16 ' MO number
               v = v + 2                ' Static Allocate (2)
               Print #f, "    CAN_vSetListCommand(" & FormatTextOutput(v, "H8") & ");  // MO" & iNextNr & " for list " & i
               iNextNr = GetPNEXT(r, vIndex, iNextNr)
             End If
          Loop Until (iNextNr = GetPNEXT(r, vIndex, iNextNr))
          v = 0
          v = v + i * 2 ^ 24       ' List
          v = v + iNextNr * 2 ^ 16 ' MO number
          v = v + 2                ' Static Allocate (2)
          Print #f, "    CAN_vSetListCommand(" & FormatTextOutput(v, "H8") & ");  // MO" & iNextNr & " for list " & i
          Print #f, ""
        End If
      Next i

      '###################################################################################
      '            Configuration of the CAN Message Objects 0 - (CAN_MAX_OBJECTS - 1)
      '###################################################################################
      Print #f, FormatSectionHeader(2, 1, "Configuration of the " & sVarPre & " Message Objects 0 - " & (CAN_MAX_OBJECTS - 1) & ":")
      For i = 0 To (CAN_MAX_OBJECTS - 1)
        Call GenerateObjx(r, vIndex, f, i)
      Next i
      Print #f, ""
      
                                                                                                                                           
                                                                                                                                           
      '###################################################################################
      '            Enable Interrupt's
      '###################################################################################
'    Dim isrSelected As Boolean
'    isrSelected = False
    
    Print #f, FormatSectionHeader(2, 1, "Configuration of the Interrupts:")
    For i = 0 To (CAN_MAX_SRNS - 1)
        If (r.v(sVarPre & "_USE_SRN" & i) = 1) Then
              d = "CAN interrupt node " & i & " is enabled"
        Else
              d = "CAN interrupt node " & i & " is disabled"
        End If
          Print #f, FormatCommentsForInit(1, d)
    Next i
    Print #f, ""
    If (r.v(sVarPre & "_USE_SRN0") = 1) Then
        'Print #f, FormatCodeStatemente(2, "ET2", "=", "1", "MultiCAN Node 0 enabled")
         Print #f, FormatComments(2, 1, "", "", "MultiCAN Node 0 Interrupt enable bit is set in SHINT_vInit() function")
    End If
    'If ((r.v(sVarPre & "_USE_SRN0") = 1) And (r.v("BRG_Interrupt_Select") <> 1) And (r.v(CORE_PREFIX & "IEN0_ET2") <> 1)) Then
    'Added by jag on 6/4/6 in order to generate if we deselect init function of T2 or UART.
'    If ((r.v(sVarPre & "_USE_SRN0") = 1) And ((r.v("BRG_Interrupt_Select") <> 1) Or (r.v("UART_USE_Init") = 0)) And ((r.v(CORE_PREFIX & "IEN0_ET2") <> 1) Or (r.v("T2_USE_Init") = 0))) Then
'       Print #f, FormatCodeStatemente(2, "ET2", "=", "1", "MultiCAN Node 0 enabled")
'    End If
    Print #f, ""
    If (r.v(sVarPre & "_USE_SRN1") = 1) Or (r.v(sVarPre & "_USE_SRN2") = 1) Or (r.v(sVarPre & "_USE_SRN3") = 1) Or _
       (r.v(sVarPre & "_USE_SRN4") = 1) Or (r.v(sVarPre & "_USE_SRN5") = 1) Or (r.v(sVarPre & "_USE_SRN6") = 1) Or _
       (r.v(sVarPre & "_USE_SRN7") = 1) Then
      v = 0
     'Added by jag on 6/4/6 in order to generate if we deselect init function of respective shared int modules.
       If ((r.v(sVarPre & "_USE_SRN1") = 1) Or (r.v(sVarPre & "_USE_SRN2") = 1)) Then
           v = v + 2 ^ 0
       End If
       If (r.v(sVarPre & "_USE_SRN3") = 1) Then
           v = v + 2 ^ 3
       End If
       If (r.v(sVarPre & "_USE_SRN4") = 1) Then
           v = v + 2 ^ 4
       End If
       If (r.v(sVarPre & "_USE_SRN5") = 1) Then
           v = v + 2 ^ 5
       End If
       If (r.v(sVarPre & "_USE_SRN6") = 1) Then
           v = v + 2 ^ 6
       End If
       If (r.v(sVarPre & "_USE_SRN7") = 1) Then
           v = v + 2 ^ 7
       End If
    End If
'     If (((r.v(sVarPre & "_USE_SRN1") = 1) Or (r.v(sVarPre & "_USE_SRN2") = 1)) And ((r.v("ADC_USE_Isr") <> 1) Or (r.v("ADC_USE_Init") = 0))) Or ((r.v(sVarPre & "_USE_SRN3") = 1) And ((r.v("IR_USE_IsrExt3") <> 1) Or (r.v("IR_USE_Init") = 0))) Or _
'       ((r.v(sVarPre & "_USE_SRN4") = 1) And ((r.v("CCU6_USE_NodeI0") <> 1) Or (r.v("CCU6_USE_Init") = 0))) Or ((r.v(sVarPre & "_USE_SRN5") = 1) And ((r.v("CCU6_USE_NodeI1") <> 1) Or (r.v("CCU6_USE_Init") = 0))) Or ((r.v(sVarPre & "_USE_SRN6") = 1) And ((r.v("CCU6_USE_NodeI2") <> 1) Or (r.v("CCU6_USE_Init") = 0))) Or _
'       ((r.v(sVarPre & "_USE_SRN7") = 1) And ((r.v("CCU6_USE_NodeI3") <> 1) Or (r.v("CCU6_USE_Init") = 0))) Then
     If (r.v(sVarPre & "_USE_SRN1") = 1) Or (r.v(sVarPre & "_USE_SRN2") = 1) Or (r.v(sVarPre & "_USE_SRN3") = 1) Or _
       (r.v(sVarPre & "_USE_SRN4") = 1) Or (r.v(sVarPre & "_USE_SRN5") = 1) Or (r.v(sVarPre & "_USE_SRN6") = 1) Or _
       (r.v(sVarPre & "_USE_SRN7") = 1) Then
'               Print #f, FormatCodeStatemente(2, CORE_PREFIX & "IEN1", "|=", _
'                FormatTextOutput(v, "H2"), _
'               "load interrupt enable register(MultiCAN Nodex)")
                  Print #f, FormatComments(2, 1, "", "", "- MultiCAN Nodex Interrupt enable bit is set in SHINT_vInit() function")
       Print #f, ""
    End If
    
                                                                                                                                
      '###################################################################################
      '            Start the Nodes
      '###################################################################################
      Print #f, FormatSectionHeader(2, 0, "Start the " & sVarPre & " Nodes:")
      ' statement for CAN_NCRx Control register
      For i = 0 To (CAN_MAX_NODES - 1)
        Print #f, ""
        sNCRx = sRegPre & "NCR" & i
        v = 0
        v = v + 2 ^ 6   ' CCE allows access to Bit Timing Register
        v = v + 1       ' INIT starts the initialization
        If (r.v(sNCRx & "_INIT") = 0) Then  ' only if the node x is used
        d = "-----------" & sNCRx & "--------------------------------------------------------"
            Print #f, FormatCommentsForInit(1, d)
            Print #f, ""
            Print #f, FormatCodeStatemente(4, "CAN_vWriteCANAddress(" & sNCRx & ");", "", "", "Addressing CAN_NCR" & i)
            Print #f, FormatCodeStatemente(4, "CAN_vReadEN();", "", "", "Read Mode is Enabled")
            Print #f, FormatCodeStatemente(4, "CAN_DATA0", "&=", "~" & FormatTextOutput(v, "H2"), "reset INIT and CCE")
            Print #f, FormatCodeStatemente(4, "CAN_vWriteEN(D0_VALID);", "", "", "Data0 is Valid for transmission and Write is Enabled")
        End If
      Next i
            
      Print #f, ""
      Print #f, ""
      Print #f, uc.GetUserCode("(" & n & ",3)", 2)
      Print #f, ""
      Print #f, "} " & END_OF_FUNCTION & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' void CAN_vWriteAMData(ulong) function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "WriteAMData"
    If (r.v(sVarPre & "_USE_WriteCANAdrValue") = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_WriteCANAdrValue")
      Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_WriteCANAdrValue")
      Print #f, "{"
      Print #f, "  un_32bit ulData;"
      Print #f, "  ulData.ulVal = ulValue;"
      Print #f, ""
      If (r.v("MAIN_COMPILER") = "Keil") Then
        Print #f, "    CAN_DATA0 = ulData.ubDB[3]; // load CAN Data Register 0"
        Print #f, "    CAN_DATA1 = ulData.ubDB[2]; // load CAN Data Register 1"
        Print #f, "    CAN_DATA2 = ulData.ubDB[1]; // load CAN Data Register 2"
        Print #f, "    CAN_DATA3 = ulData.ubDB[0]; // load CAN Data Register 3"
      ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
        Print #f, "    CAN_DATA0 = ulData.ubDB[0]; // load CAN Data Register 0"
        Print #f, "    CAN_DATA1 = ulData.ubDB[1]; // load CAN Data Register 1"
        Print #f, "    CAN_DATA2 = ulData.ubDB[2]; // load CAN Data Register 2"
        Print #f, "    CAN_DATA3 = ulData.ubDB[3]; // load CAN Data Register 3"
      End If
      Print #f, "        CAN_vWriteEN(ALL_DATA_VALID); // Writemode is Enabled"
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_WriteCANAdrValue")
      Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ulong CAN_ulGetCANData(void) function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetCANData"
    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, "  un_32bit ulData;"
      Print #f, ""
      Print #f, "    CAN_vReadEN();  // Readmode is Enabled"
      If (r.v("MAIN_COMPILER") = "Keil") Then
        Print #f, "    ulData.ubDB[3] = CAN_DATA0; // Read CAN Data Register 0"
        Print #f, "    ulData.ubDB[2] = CAN_DATA1; // Read CAN Data Register 1"
        Print #f, "    ulData.ubDB[1] = CAN_DATA2; // Read CAN Data Register 2"
        Print #f, "    ulData.ubDB[0] = CAN_DATA3; // Read CAN Data Register 3"
      ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
        Print #f, "    ulData.ubDB[0] = CAN_DATA0; // Read CAN Data Register 0"
        Print #f, "    ulData.ubDB[1] = CAN_DATA1; // Read CAN Data Register 1"
        Print #f, "    ulData.ubDB[2] = CAN_DATA2; // Read CAN Data Register 2"
        Print #f, "    ulData.ubDB[3] = CAN_DATA3; // Read CAN Data Register 3"
      End If
      Print #f, "return (ulData.ulVal);"
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' void CAN_vSetListCommand(ulong) function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetListCommand"
    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, "    CAN_vWriteAMData(ulVal);  // - load Panel Control Register"
      Print #f, FormatCodeStatemente(4, "CAN_DATA1 = CAN_PANCTR_BUSY;", "", "", "")
      Print #f, FormatCodeStatemente(4, "while(CAN_DATA1 & CAN_PANCTR_BUSY){", "", "", "wait until Panel has finished the initialisation")
      Print #f, FormatCodeStatemente(6, "CAN_ADCON &= ~0x01;", "", "", "")
      Print #f, FormatCodeStatemente(6, "}", "", "", "")
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
    End If
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_vGetMsgObj function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetMsgObj"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)
      Print #f, "{"
      Print #f, "ubyte ubTemp;"
      Print #f, ""
      Call GenerateAMprotection_FunBeg(r, vIndex, f, 0)
      Print #f, ""
      Print #f, "    //----- get DLC -------------------------------------------------------"
      Print #f, ""
      Print #f, "    CAN_vWriteCANAddress(CAN_MOFCR(ubObjNr));"
      Print #f, "    CAN_vReadEN();   // Readmode is Enabled"
      Print #f, ""
      Print #f, "    pstObj->ubMOdlc  =  CAN_DATA3;//MOFCRn[31-24] DLC"
      Print #f, ""
      Print #f, "    //----- get MODATAL ---------------------------------------------------"
      Print #f, "    CAN_vWriteCANAddress(CAN_MODATAL(ubObjNr));"
'      Print #f, "    pstObj->ulDATAL.ulVal = CAN_ulGetCANData();"
      Print #f, ""
      Print #f, "    CAN_vReadEN();  // Readmode is Enabled"
     If (r.v("MAIN_COMPILER") = "Keil") Then
      Print #f, "    pstObj->ulDATAL.ubDB[3] = CAN_DATA0; // Read CAN Data Register 0"
      Print #f, "    pstObj->ulDATAL.ubDB[2] = CAN_DATA1; // Read CAN Data Register 1"
      Print #f, "    pstObj->ulDATAL.ubDB[1] = CAN_DATA2; // Read CAN Data Register 2"
      Print #f, "    pstObj->ulDATAL.ubDB[0] = CAN_DATA3; // Read CAN Data Register 3"
     ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
      Print #f, "    pstObj->ulDATAL.ubDB[0] = CAN_DATA0; // Read CAN Data Register 0"
      Print #f, "    pstObj->ulDATAL.ubDB[1] = CAN_DATA1; // Read CAN Data Register 1"
      Print #f, "    pstObj->ulDATAL.ubDB[2] = CAN_DATA2; // Read CAN Data Register 2"
      Print #f, "    pstObj->ulDATAL.ubDB[3] = CAN_DATA3; // Read CAN Data Register 3"
     End If
      Print #f, ""
      Print #f, "    //  CAN Address pointing to the CAN_MODATAH"
      Print #f, "    CAN_ADCON = ADR_INC;"
      Print #f, ""
      Print #f, "    //----- get MODATAH ---------------------------------------------------"
'      Print #f, "    pstObj->ulDATAH.ulVal = CAN_ulGetCANData();"
      Print #f, ""
      Print #f, "    CAN_vReadEN();  // Readmode is Enabled"
     If (r.v("MAIN_COMPILER") = "Keil") Then
      Print #f, "    pstObj->ulDATAH.ubDB[3] = CAN_DATA0; // Read CAN Data Register 0"
      Print #f, "    pstObj->ulDATAH.ubDB[2] = CAN_DATA1; // Read CAN Data Register 1"
      Print #f, "    pstObj->ulDATAH.ubDB[1] = CAN_DATA2; // Read CAN Data Register 2"
      Print #f, "    pstObj->ulDATAH.ubDB[0] = CAN_DATA3; // Read CAN Data Register 3"
     ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
      Print #f, "    pstObj->ulDATAH.ubDB[0] = CAN_DATA0; // Read CAN Data Register 0"
      Print #f, "    pstObj->ulDATAH.ubDB[1] = CAN_DATA1; // Read CAN Data Register 1"
      Print #f, "    pstObj->ulDATAH.ubDB[2] = CAN_DATA2; // Read CAN Data Register 2"
      Print #f, "    pstObj->ulDATAH.ubDB[3] = CAN_DATA3; // Read CAN Data Register 3"
     End If
      Print #f, ""
      Print #f, "    //----- get DIR -------------------------------------------------------"
      Print #f, "    CAN_vWriteCANAddress(CAN_MOCTR(ubObjNr));"
      Print #f, "    CAN_vReadEN();   // Readmode is Enabled"
      Print #f, ""
      Print #f, "    if(CAN_DATA1 & 0x08)   // if transmit object (MOSTAT_DIR)"
      Print #f, "    {"
      Print #f, "        pstObj->ubMOcfg  = (pstObj->ubMOcfg | 0x08);  // set DIR"
      Print #f, "    }"
      Print #f, ""
      Print #f, "    //----- get XID - Extended Identifier ---------------------------------"
      Print #f, ""
      Print #f, "    //  CAN Address pointing to the CAN_MOARn"
      Print #f, "    CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
      Print #f, "    CAN_vReadEN();   // ReadMode is Enabled"
      Print #f, ""
      Print #f, "    if(CAN_DATA3 & 0x20)   // if extended identifier"
      Print #f, "    {"
      Print #f, ""
      Print #f, "        //----- get XID - Extended Identifier (MOARn) ------------------------"
    If (r.v("MAIN_COMPILER") = "Keil") Then
      Print #f, "        pstObj->ulID.ubDB[3] = CAN_DATA0;   // MOARn[7-0]"
      Print #f, "        pstObj->ulID.ubDB[2] = CAN_DATA1;   // MOARn[15-8]"
      Print #f, "        pstObj->ulID.ubDB[1] = CAN_DATA2;   // MOARn[23-16]"
      Print #f, "        pstObj->ulID.ubDB[0] = (CAN_DATA3 & 0x1f);  // MOARn[31-24]"
    ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
      Print #f, "        pstObj->ulID.ubDB[0] = CAN_DATA0;   // MOARn[7-0]"
      Print #f, "        pstObj->ulID.ubDB[1] = CAN_DATA1;   // MOARn[15-8]"
      Print #f, "        pstObj->ulID.ubDB[2] = CAN_DATA2;   // MOARn[23-16]"
      Print #f, "        pstObj->ulID.ubDB[3] = (CAN_DATA3 & 0x1f);  // MOARn[31-24]"
    End If
      Print #f, ""
      Print #f, "        //  CAN Address pointing to the CAN_MODATAH"
      Print #f, "        CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
      Print #f, ""
      Print #f, "        //  CAN Address pointing to the CAN_MODATAL"
      Print #f, "        CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
      Print #f, ""
      Print #f, "        //  CAN Address pointing to the CAN_MOAMR"
      Print #f, "        CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
      Print #f, ""
      Print #f, "        CAN_vReadEN();   // ReadMode is Enabled"
      Print #f, ""
      Print #f, "        //----- get XID - Extended Acceptance Mask (MOAMRn) -----------------"
    If (r.v("MAIN_COMPILER") = "Keil") Then
      Print #f, "        pstObj->ulMask.ubDB[3] = CAN_DATA0;   // MOAMRn[7-0]"
      Print #f, "        pstObj->ulMask.ubDB[2] = CAN_DATA1;   // MOAMRn[15-8]"
      Print #f, "        pstObj->ulMask.ubDB[1] = CAN_DATA2;   // MOAMRn[23-16]"
      Print #f, "        pstObj->ulMask.ubDB[0] = (CAN_DATA3 & 0x1f);      // MOAMRn[31-24]"
    ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
      Print #f, "        pstObj->ulMask.ubDB[0] = CAN_DATA0;   // MOAMRn[7-0]"
      Print #f, "        pstObj->ulMask.ubDB[1] = CAN_DATA1;   // MOAMRn[15-8]"
      Print #f, "        pstObj->ulMask.ubDB[2] = CAN_DATA2;   // MOAMRn[23-16]"
      Print #f, "        pstObj->ulMask.ubDB[3] = (CAN_DATA3 & 0x1f);      // MOAMRn[31-24]"
    End If
      Print #f, ""
      Print #f, "        pstObj->ubMOcfg  = (pstObj->ubMOcfg | 0x04);      // set IDE 29-bit"
      Print #f, "    }"
      Print #f, "    else                                                  // standard identifier"
      Print #f, "    {"
      Print #f, "        //----- get - ID - Standard Identifier (MOARn) ---------------------"
    If (r.v("MAIN_COMPILER") = "Keil") Then
      Print #f, "        pstObj->ulID.ubDB[3] = (CAN_DATA2 & 0xFC) >> 2;   // MOARn[23-16]"
      Print #f, "        pstObj->ulID.ubDB[2] = (CAN_DATA3 & 0x1f);        // MOARn[31-24]"
      Print #f, "        pstObj->ulID.ubDB[1] = 0x00;    // MOARn[15-8]"
      Print #f, "        pstObj->ulID.ubDB[0] = 0x00;    // MOARn[15-8]"
      Print #f, ""
      Print #f, "        ubTemp = 0;"
      Print #f, "        ubTemp = ((pstObj->ulID.ubDB[2]) & 0x03) << 6;"
      Print #f, "        pstObj->ulID.ubDB[2] = (pstObj->ulID.ubDB[2] >> 2);"
      Print #f, "        pstObj->ulID.ubDB[3] = (pstObj->ulID.ubDB[3] + ubTemp);"
    ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
      Print #f, "        pstObj->ulID.ubDB[0] = (CAN_DATA2 & 0xFC) >> 2;   // MOARn[23-16]"
      Print #f, "        pstObj->ulID.ubDB[1] = (CAN_DATA3 & 0x1f);        // MOARn[31-24]"
      Print #f, "        pstObj->ulID.ubDB[2] = 0x00;    // MOARn[15-8]"
      Print #f, "        pstObj->ulID.ubDB[3] = 0x00;    // MOARn[15-8]"
      Print #f, ""
      Print #f, "        ubTemp = 0;"
      Print #f, "        ubTemp = ((pstObj->ulID.ubDB[1]) & 0x03) << 6;"
      Print #f, "        pstObj->ulID.ubDB[1] = (pstObj->ulID.ubDB[1] >> 2);"
      Print #f, "        pstObj->ulID.ubDB[0] = (pstObj->ulID.ubDB[0] + ubTemp);"
    End If
      Print #f, ""
      Print #f, "        //  CAN Address pointing to the CAN_MODATAH"
      Print #f, "        CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
      Print #f, ""
      Print #f, "        //  CAN Address pointing to the CAN_MODATAL"
      Print #f, "        CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
      Print #f, ""
      Print #f, "        //  CAN Address pointing to the CAN_MOAMR"
      Print #f, "        CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
      Print #f, ""
      Print #f, "        CAN_vReadEN();   // ReadMode is Enabled"
      Print #f, ""
      Print #f, "        //----- get - ID - Acceptance Mask (MOAMRn) -----------------------"
      Print #f, ""
    If (r.v("MAIN_COMPILER") = "Keil") Then
      Print #f, "        pstObj->ulMask.ubDB[3] = (CAN_DATA2 & 0xFC) >> 2;   // MOAMRn[23-16]"
      Print #f, "        pstObj->ulMask.ubDB[2] = (CAN_DATA3 & 0x1f);       // MOAMRn[31-24]"
      Print #f, "        pstObj->ulMask.ubDB[1] = 0x00;     // MOAMRn[15-8]"
      Print #f, "        pstObj->ulMask.ubDB[0] = 0x00;    // MOAMRn[15-8]"
      Print #f, ""
      Print #f, "        ubTemp = 0;"
      Print #f, "        ubTemp = ((pstObj->ulMask.ubDB[2]) & 0x03) << 6;"
      Print #f, "        pstObj->ulMask.ubDB[2] = (pstObj->ulMask.ubDB[2] >> 2);"
      Print #f, "        pstObj->ulMask.ubDB[3] = (pstObj->ulMask.ubDB[3] + ubTemp);"
    ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
      Print #f, "        pstObj->ulMask.ubDB[0] = (CAN_DATA2 & 0xFC) >> 2;   // MOARn[23-16]"
      Print #f, "        pstObj->ulMask.ubDB[1] = (CAN_DATA3 & 0x1f);        // MOARn[31-24]"
      Print #f, "        pstObj->ulMask.ubDB[2] = 0x00;    // MOARn[15-8]"
      Print #f, "        pstObj->ulMask.ubDB[3] = 0x00;    // MOARn[15-8]"
      Print #f, ""
      Print #f, "        ubTemp = 0;"
      Print #f, "        ubTemp = ((pstObj->ulMask.ubDB[1]) & 0x03) << 6;"
      Print #f, "        pstObj->ulMask.ubDB[1] = (pstObj->ulMask.ubDB[1] >> 2);"
      Print #f, "        pstObj->ulMask.ubDB[0] = (pstObj->ulMask.ubDB[0] + ubTemp);"
    End If
      Print #f, "    }"
      Print #f, ""
      Print #f, "    //----- get CFCVAL - CAN Frame Counter Value (MOIPRn) -----------------"
      Print #f, ""
      Print #f, "    CAN_vWriteCANAddress(CAN_MOIPR(ubObjNr));"
      Print #f, "    CAN_vReadEN();   // Readmode is Enabled"
      Print #f, ""
    If (r.v("MAIN_COMPILER") = "Keil") Then
      Print #f, "    pstObj->uwCounter.ubDB[1] = CAN_DATA2;  // MOIPRn[16-23]"
      Print #f, "    pstObj->uwCounter.ubDB[0] = CAN_DATA3;  // MOIPRn[31-24]"
    ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
      Print #f, "    pstObj->uwCounter.ubDB[0] = CAN_DATA2;  // MOIPRn[16-23]"
      Print #f, "    pstObj->uwCounter.ubDB[1] = CAN_DATA3;  // MOIPRn[31-24]"
    End If
      Print #f, ""
      Call GenerateAMprotection_FunEnd(r, vIndex, f, 0)
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_ubRequestMsgObj function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "RequestMsgObj"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
        Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
        Print #f, ""
        Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)
        Print #f, "{"
        Print #f, "ubyte ubReturn = 0;"
        Print #f, ""
        Call GenerateAMprotection_FunBeg(r, vIndex, f, 0)
        Print #f, ""
        Print #f, "    CAN_vWriteCANAddress(CAN_MOCTR(ubObjNr));"
        Print #f, "    CAN_vReadEN();   // Readmode is Enabled"
        Print #f, ""
        Print #f, "    if((CAN_DATA1 & 0x01) == 0)         // if reset TXRQ"
        Print #f, "    {"
        Print #f, "        CAN_vWriteCANData(0x00000020);  // reset MSGVAL"
        Print #f, "        ubReturn = 1;"
        Print #f, "    }"
        Call GenerateAMprotection_FunEnd(r, vIndex, f, 0)
        Print #f, ""
        Print #f, "return(ubReturn);"
        Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
        Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_bNewData function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "NewData"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
        Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
        Print #f, ""
        Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)
        Print #f, "{"
        Print #f, "ubyte ubReturn = 0;"
        Print #f, ""
        Call GenerateAMprotection_FunBeg(r, vIndex, f, 0)
        Print #f, ""
        Print #f, "    CAN_vWriteCANAddress(CAN_MOCTR(ubObjNr));"
        Print #f, "    CAN_vReadEN();   // Readmode is Enabled"
        Print #f, "    if(CAN_DATA0 & 0x08)   // if NEWDAT"
        Print #f, "    {"
        Print #f, "        ubReturn = 1;"
        Print #f, "    }"
        Call GenerateAMprotection_FunEnd(r, vIndex, f, 0)
        Print #f, ""
        Print #f, "return(ubReturn);"
        Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
        Print #f, ""
    End If
       
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_vTransmit function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "Transmit"
    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, "{"
      Call GenerateAMprotection_FunBeg(r, vIndex, f, 0)
      Print #f, ""
      Print #f, "    CAN_vWriteCANAddress(CAN_MOCTR(ubObjNr));  //Addressing CAN_MOCTR register"
      Print #f, "    CAN_vWriteCANData(0x07200000);      // set TXRQ,TXEN0,TXEN1,MSGVAL"
      Print #f, ""
      Call GenerateAMprotection_FunEnd(r, vIndex, f, 0)
      Print #f, ""
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_vConfigMsgObj function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ConfigMsgObj"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)
      Print #f, "{"
      Print #f, "ubyte ubTemp;"
      Print #f, ""
      Call GenerateAMprotection_FunBeg(r, vIndex, f, 0)
      Print #f, ""
      Print #f, "    CAN_vWriteCANAddress(CAN_MOCTR(ubObjNr));  // Addressing CAN_MOCTRn register"
      Print #f, "    CAN_vWriteCANData(0x00000020);      // reset MSGVAL"
      Print #f, ""
      Print #f, "    //----- CAN Identifier ----------------------------------------------"
      Print #f, ""
      Print #f, "    CAN_vWriteCANAddress(CAN_MOAR(ubObjNr));"
      Print #f, ""
      Print #f, "    if(pstObj->ubMOcfg & 0x04)                 // extended identifier"
      Print #f, "    {"
      Print #f, "        CAN_vReadEN();  // Read mode is enabled"
      Print #f, ""
      Print #f, "        CAN_DATA3  &= ~0x1F;    //MOARn[31-24]"
      Print #f, ""
      Print #f, "        //----- CAN XID Extended identifier -----------------------------"
      Print #f, ""
    If (r.v("MAIN_COMPILER") = "Keil") Then
      Print #f, "        CAN_DATA0 = pstObj->ulID.ubDB[3];   //MOARn[7-0]"
      Print #f, "        CAN_DATA1 = pstObj->ulID.ubDB[2];   //MOARn[15-8]"
      Print #f, "        CAN_DATA2 = pstObj->ulID.ubDB[1];   //MOARn[16-23]"
      Print #f, "        CAN_DATA3 |= ( 0x20 | ((pstObj->ulID.ubDB[0]) & 0x1F)); //MOARn[31-24]"
    ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
      Print #f, "        CAN_DATA0 = pstObj->ulID.ubDB[0];   //MOARn[7-0]"
      Print #f, "        CAN_DATA1 = pstObj->ulID.ubDB[1];   //MOARn[15-8]"
      Print #f, "        CAN_DATA2 = pstObj->ulID.ubDB[2];   //MOARn[16-23]"
      Print #f, "        CAN_DATA3 |= ( 0x20 | ((pstObj->ulID.ubDB[3]) & 0x1F)); //MOARn[31-24]"
    End If
      Print #f, ""
      Print #f, "        CAN_vWriteEN(ALL_DATA_VALID); // Write mode is enabled"
      Print #f, ""
      Print #f, "        //  CAN Address pointing to the CAN_MODATAHn"
      Print #f, "        CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
      Print #f, ""
      Print #f, "        //  CAN Address pointing to the CAN_MODATALn"
      Print #f, "        CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
      Print #f, ""
      Print #f, "        //  CAN Address pointing to the CAN_MOAMRn"
      Print #f, "        CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
      Print #f, ""
      Print #f, "        CAN_vReadEN();  // Read mode is enabled"
      Print #f, ""
      Print #f, "        CAN_DATA3  &= ~0x1F;    //MOAMRn[31-24]"
      Print #f, ""
      Print #f, "        //----- CAN Mask ------------------------------------------------"
      Print #f, ""
    If (r.v("MAIN_COMPILER") = "Keil") Then
      Print #f, "        CAN_DATA0 = pstObj->ulMask.ubDB[3]; //MOAMRn[7-0]"
      Print #f, "        CAN_DATA1 = pstObj->ulMask.ubDB[2]; //MOAMRn[15-8]"
      Print #f, "        CAN_DATA2 = pstObj->ulMask.ubDB[1]; //MOAMRn[23-16]"
      Print #f, "        CAN_DATA3 |= (pstObj->ulMask.ubDB[0] & 0x1F);   //MOAMRn[31-24]"
    ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
      Print #f, "        CAN_DATA0 = pstObj->ulMask.ubDB[0]; //MOAMRn[7-0]"
      Print #f, "        CAN_DATA1 = pstObj->ulMask.ubDB[1]; //MOAMRn[15-8]"
      Print #f, "        CAN_DATA2 = pstObj->ulMask.ubDB[2]; //MOAMRn[23-16]"
      Print #f, "        CAN_DATA3 |= (pstObj->ulMask.ubDB[3] & 0x1F);   //MOAMRn[31-24]"
    End If
      Print #f, ""
      Print #f, "        CAN_vWriteEN(ALL_DATA_VALID); // Write mode is enabled"
      Print #f, "    }"
      Print #f, "    else                                         // standard identifier"
      Print #f, "    {"
      Print #f, "        CAN_vReadEN();  // Read mode is enabled"
      Print #f, ""
      Print #f, "        CAN_DATA3  &= ~0x3F;    //MOARn[31-24]"
      Print #f, ""
      Print #f, "        //----- CAN Standard identifier ---------------------------------"
      Print #f, "        ubTemp = 0;"
    If (r.v("MAIN_COMPILER") = "Keil") Then
      Print #f, "        ubTemp = ((pstObj->ulID.ubDB[3] & 0xC0) >> 6);"
      Print #f, ""
      Print #f, "        CAN_DATA0 = 0x00;                   //MOARn[7-0]"
      Print #f, "        CAN_DATA1 = 0x00;                   //MOARn[15-8]"
      Print #f, "        CAN_DATA2 = (pstObj->ulID.ubDB[3] << 2);    //MOARn[16-23]"
      Print #f, "        CAN_DATA3 |= (((pstObj->ulID.ubDB[2] & 0x07) << 2) + ubTemp);   //MOARn[31-24]"
    ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
      Print #f, "        ubTemp = ((pstObj->ulID.ubDB[0] & 0xC0) >> 6);"
      Print #f, ""
      Print #f, "        CAN_DATA0 = 0x00;                   //MOARn[7-0]"
      Print #f, "        CAN_DATA1 = 0x00;                   //MOARn[15-8]"
      Print #f, "        CAN_DATA2 = (pstObj->ulID.ubDB[0] << 2);    //MOARn[16-23]"
      Print #f, "        CAN_DATA3 |= (((pstObj->ulID.ubDB[1] & 0x07) << 2) + ubTemp);   //MOARn[31-24]"
    End If
      Print #f, ""
      Print #f, "        CAN_vWriteEN(ALL_DATA_VALID); // Write mode is enabled"
      Print #f, ""
      Print #f, "        //  CAN Address pointing to the CAN_MODATAHn"
      Print #f, "        CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
      Print #f, ""
      Print #f, "        //  CAN Address pointing to the CAN_MODATALn"
      Print #f, "        CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
      Print #f, ""
      Print #f, "        //  CAN Address pointing to the CAN_MOAMRn"
      Print #f, "        CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
      Print #f, ""
      Print #f, "        CAN_vReadEN();  // Read mode is enabled"
      Print #f, ""
      Print #f, "        CAN_DATA3  &= ~0x1F;    //MOARn[31-24]"
      Print #f, ""
      Print #f, "        //----- CAN Mask --------------------------------------------------"
      Print #f, ""
      Print #f, "        ubTemp = 0;"
    If (r.v("MAIN_COMPILER") = "Keil") Then
      Print #f, "        ubTemp = ((pstObj->ulMask.ubDB[3] & 0xC0) >> 6);"
      Print #f, ""
      Print #f, "        CAN_DATA0 = 0x00;                   //MOAMRn[7-0]"
      Print #f, "        CAN_DATA1 = 0x00;                   //MOAMRn[15-8]"
      Print #f, "        CAN_DATA2 = (pstObj->ulMask.ubDB[3] << 2);    //MOAMRn[16-23]"
      Print #f, "        CAN_DATA3 |= (((pstObj->ulMask.ubDB[2] & 0x07) << 2) + ubTemp);   //MOAMRn[31-24]"
    ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
      Print #f, "        ubTemp = ((pstObj->ulMask.ubDB[0] & 0xC0) >> 6);"
      Print #f, ""
      Print #f, "        CAN_DATA0 = 0x00;                   //MOAMRn[7-0]"
      Print #f, "        CAN_DATA1 = 0x00;                   //MOAMRn[15-8]"
      Print #f, "        CAN_DATA2 = (pstObj->ulMask.ubDB[0] << 2);    //MOAMRn[16-23]"
      Print #f, "        CAN_DATA3 |= (((pstObj->ulMask.ubDB[1] & 0x07) << 2) + ubTemp);   //MOAMRn[31-24]"
    End If
      Print #f, ""
      Print #f, "        CAN_vWriteEN(ALL_DATA_VALID); // Write mode is enabled"
      Print #f, "    }"
      Print #f, ""
      Print #f, "    //----- CAN Frame Counter Value -----------------------------------"
      Print #f, "    CAN_vWriteCANAddress(CAN_MOIPR(ubObjNr));"
      Print #f, ""
    If (r.v("MAIN_COMPILER") = "Keil") Then
      Print #f, "    CAN_DATA2 = pstObj->uwCounter.ubDB[1];"
      Print #f, "    CAN_DATA3 = pstObj->uwCounter.ubDB[0];"
    ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
      Print #f, "    CAN_DATA2 = pstObj->uwCounter.ubDB[0];"
      Print #f, "    CAN_DATA3 = pstObj->uwCounter.ubDB[1];"
    End If
      Print #f, "    CAN_vWriteEN(D3_VALID + D2_VALID);   // Data3 and Data2 are Valid"
      Print #f, "                                         // for transmission and Write is Enabled"
      Print #f, ""
      Print #f, "    //  CAN Address pointing to the CAN_MOFGPRn"
      Print #f, "    CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
      Print #f, ""
      Print #f, "    //  CAN Address pointing to the CAN_MOFCRn"
      Print #f, "    CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
      Print #f, ""
      Print #f, "    //----- CAN DLC -----------------------------------------------------"
      Print #f, "    CAN_DATA3 = pstObj->ubMOdlc;"
      Print #f, "    CAN_vWriteEN(D3_VALID);   // Data3 is Valid for transmission"
      Print #f, "                              // and Write is Enabled"
      Print #f, ""
      Print #f, "    CAN_vWriteCANAddress(CAN_MOCTR(ubObjNr));"
      Print #f, "    CAN_vReadEN();   // Readmode is Enabled"
      Print #f, ""
      Print #f, "    if(CAN_DATA1 & 0x08)  // if transmit direction"
      Print #f, "    {"
      Print #f, "      //  CAN Address pointing to the CAN_MOARn"
      Print #f, "      CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
      Print #f, ""
      Print #f, "      //  CAN Address pointing to the CAN_MODATAHn"
      Print #f, "      CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
      Print #f, ""
      Print #f, "      CAN_vWriteCANData(pstObj->ulDATAH.ulVal);"
      Print #f, ""
      Print #f, "      //  CAN Address pointing to the CAN_MODATALn"
      Print #f, "      CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
      Print #f, ""
      Print #f, "      CAN_vWriteCANData(pstObj->ulDATAL.ulVal);"
      Print #f, ""
      Print #f, "      //  CAN Address pointing to the CAN_MODATAHn"
      Print #f, "      CAN_ADCON = ADR_INC;   // Auto Increment the current address(+1)"
      Print #f, ""
      Print #f, "      //  CAN Address pointing to the CAN_MOARn"
      Print #f, "      CAN_ADCON = ADR_INC;   // Auto Increment the current address(+1)"
      Print #f, ""
      Print #f, "      //  CAN Address pointing to the CAN_MOCTRn"
      Print #f, "      CAN_ADCON = ADR_INC;   // Auto Increment the current address(+1)"
      Print #f, ""
      Print #f, "      CAN_vWriteCANData(0x06280040);  // set NEWDAT, reset RTSEL,"
      Print #f, "    }"
      Print #f, "    else     // if receive direction"
      Print #f, "    {"
      Print #f, "      CAN_vWriteCANData(0x00200040);  // reset RTSEL, set MSGVAL"
      Print #f, "    }"
      Call GenerateAMprotection_FunEnd(r, vIndex, f, 0)
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
    End If
  
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_vLoadData function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "LoadData"
    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, "{"
        Call GenerateAMprotection_FunBeg(r, vIndex, f, 0)
        Print #f, ""
        Print #f, "    CAN_vWriteCANAddress(CAN_MOCTR(ubObjNr));  // Addressing CAN_MOCTRn register"
        Print #f, "    CAN_vWriteCANData(0x00080000);      // Set NEWDATA"
        Print #f, ""
        Print #f, "    //  CAN Address pointing to the CAN_MOARn"
        Print #f, "    CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
        Print #f, ""
        Print #f, "    //  CAN Address pointing to the CAN_MODATAHn"
        Print #f, "    CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
        Print #f, ""
        Print #f, "    //  CAN Address pointing to the CAN_MODATALn"
        Print #f, "    CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
        Print #f, ""
        Print #f, "    CAN_vWriteCANData(*ulpubData); // Loading CAN_MODATALn"
        Print #f, ""
        Print #f, "     // CAN Address pointing to the CAN_MODATAH"
        Print #f, "    CAN_ADCON = ADR_INC;   // Auto Increment the current address(+1)"
        Print #f, ""
        Print #f, "    ulpubData++;"
        Print #f, ""
        Print #f, "    CAN_vWriteCANData(*ulpubData); // Loading CAN_MODATAHn"
        Print #f, ""
        Print #f, "    //  CAN Address pointing to the CAN_MOARn"
        Print #f, "    CAN_ADCON = ADR_INC;   // Auto Increment the current address(+1)"
        Print #f, ""
        Print #f, "    //  CAN Address pointing to the CAN_MOCTRn"
        Print #f, "    CAN_ADCON = ADR_INC;   // Auto Increment the current address(+1)"
        Print #f, ""
        Print #f, "    CAN_vWriteCANData(0x00200040);  // reset RTSEL, set MSGVAL"
        Print #f, ""
        Call GenerateAMprotection_FunEnd(r, vIndex, f, 0)
        Print #f, ""
        Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
        Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_bDelMsgObj function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "DelMsgObj"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)
      Print #f, "{"
      Print #f, "ubyte ubReturn = 0;"
      Print #f, ""
      Call GenerateAMprotection_FunBeg(r, vIndex, f, 0)
      Print #f, "    CAN_vWriteCANAddress(CAN_MOCTR(ubObjNr));"
      Print #f, "    CAN_vReadEN();   // Readmode is Enabled"
      Print #f, "    if(!((CAN_DATA1 & 0x01) | (CAN_DATA0 & 0x08)))  //if set TXRQ or NEWDAT"
      Print #f, "    {"
      Print #f, "        CAN_vWriteCANData(0x00000020);  // reset MSGLST"
      Print #f, "        ubReturn = 1;"
      Print #f, "    }"
      Call GenerateAMprotection_FunEnd(r, vIndex, f, 0)
      Print #f, ""
      Print #f, "return(ubReturn);"
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
    End If
   
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_bMsgLost function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "MsgLost"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)
      Print #f, "{"
      Print #f, "ubyte ubReturn = 0;"
      Print #f, ""
      Call GenerateAMprotection_FunBeg(r, vIndex, f, 0)
      Print #f, ""
      Print #f, "    CAN_vWriteCANAddress(CAN_MOCTR(ubObjNr));"
      Print #f, "    CAN_vReadEN();   // Readmode is Enabled"
      Print #f, ""
      Print #f, "    if(CAN_DATA0 & 0x10) //if set MSGLST"
      Print #f, "    {"
      Print #f, "      CAN_vWriteCANData(0x00000010);  // reset MSGLST"
      Print #f, "      ubReturn = 1;"
      Print #f, "    }"
      Call GenerateAMprotection_FunEnd(r, vIndex, f, 0)
      Print #f, ""
      Print #f, "return(ubReturn);"
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
    End If
   
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_vReleaseObj function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ReleaseObj"
    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, ""
      Call GenerateAMprotection_FunBeg(r, vIndex, f, 0)
      Print #f, "    CAN_vWriteCANAddress(CAN_MOCTR(ubObjNr));  // Addressing CAN_MOCTRn register"
      Print #f, "    CAN_vWriteCANData(0x00000008);      // reset NEWDAT"
      Print #f, ""
      Call GenerateAMprotection_FunEnd(r, vIndex, f, 0)
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_vSetMSGVAL function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetMSGVAL"
    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, ""
      Call GenerateAMprotection_FunBeg(r, vIndex, f, 0)
      Print #f, "     CAN_vWriteCANAddress(CAN_MOCTR(ubObjNr));  // Addressing CAN_MOCTRn register"
      Print #f, "     CAN_vWriteCANData(0x00200000);      // set MSGVAL"
      Print #f, ""
      Call GenerateAMprotection_FunEnd(r, vIndex, f, 0)
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_ubWriteFIFO function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "WriteFIFO"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)
      Print #f, "{"
      Print #f, "ubyte j;"
      Print #f, "ubyte ubTemp;"
      Print #f, "ubyte ubReturn = 2;"
      Print #f, ""
      Call GenerateAMprotection_FunBeg(r, vIndex, f, 0)
      Print #f, ""
      Print #f, "    CAN_vWriteCANAddress(CAN_MOFCR(ubObjNr));"
      Print #f, "    CAN_vReadEN();   // Readmode is Enabled"
      Print #f, ""
      Print #f, "    if(CAN_DATA0 & 0x02) // if transmit FIFO base object"
      Print #f, "     {"
      Print #f, "        j = aubFIFOWritePtr[ubObjNr];"
      Print #f, ""
      Print #f, "        ubReturn = 0;"
      Print #f, ""
      Print #f, "        CAN_vWriteCANAddress(CAN_MOCTR(j));"
      Print #f, "        CAN_vReadEN();   // Readmode is Enabled"
      Print #f, ""
      Print #f, "        if((CAN_DATA1 & 0x01)== 0x00)  // if reset TXRQ"
      Print #f, "        {"
      Print #f, "            if(j == CAN_DATA3)  // last MO in a list"
      Print #f, "            {"
      Print #f, "               // WritePtr = BOT of the base object"
      Print #f, "               CAN_vWriteCANAddress(CAN_MOFGPR(ubObjNr));"
      Print #f, "               CAN_vReadEN();   // Readmode is Enabled"
      Print #f, "               aubFIFOWritePtr[ubObjNr] = CAN_DATA0;"
      Print #f, "            }"
      Print #f, "            else"
      Print #f, "            {"
      Print #f, "               // WritePtr = PNEXT of the current selected slave"
      Print #f, "               aubFIFOWritePtr[ubObjNr] = CAN_DATA3;"
      Print #f, "            }"
      Print #f, ""
      Print #f, "            CAN_vWriteCANAddress(CAN_MOCTR(j));  // Addressing CAN_MOCTRn register"
      Print #f, "            CAN_vWriteCANData(0x00000008);      // reset NEWDAT"
      Print #f, ""
      Print #f, "            CAN_vWriteCANAddress(CAN_MOAR(j));"
      Print #f, "            CAN_vReadEN();   // Readmode is Enabled"
      Print #f, ""
      Print #f, "            if(CAN_DATA3 & 0x20)    // extended identifier"
      Print #f, "            {"
      Print #f, "                //----- Extended ID ---------------------------------------------"
      Print #f, "                CAN_DATA3 &= ~0x3F; //MOARn[31-24]"
      Print #f, ""
    If (r.v("MAIN_COMPILER") = "Keil") Then
      Print #f, "                CAN_DATA3 |= (0x20 | pstObj->ulID.ubDB[0]); //MOARn[31-24]"
      Print #f, "                CAN_DATA2 = pstObj->ulID.ubDB[1];   //MOARn[16-23]"
      Print #f, "                CAN_DATA1 = pstObj->ulID.ubDB[2];   //MOARn[15-8]"
      Print #f, "                CAN_DATA0 = pstObj->ulID.ubDB[3];   //MOARn[7-0]"
    ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
      Print #f, "                CAN_DATA3 |= (0x20 | pstObj->ulID.ubDB[3]); //MOARn[31-24]"
      Print #f, "                CAN_DATA2 = pstObj->ulID.ubDB[2];   //MOARn[16-23]"
      Print #f, "                CAN_DATA1 = pstObj->ulID.ubDB[1];   //MOARn[15-8]"
      Print #f, "                CAN_DATA0 = pstObj->ulID.ubDB[0];   //MOARn[7-0]"
    End If
      Print #f, "           }"
      Print #f, "           else            // if standard identifier"
      Print #f, "           {"
      Print #f, "                //----- Standard ID --------------------------------------------"
    If (r.v("MAIN_COMPILER") = "Keil") Then
      Print #f, "                CAN_DATA3 &= ~0x3F; //MOARn[31-24]"
      Print #f, "                ubTemp = 0;"
      Print #f, "                ubTemp = ((pstObj->ulID.ubDB[3] & 0xC0) >> 6);"
      Print #f, ""
      Print #f, "                CAN_DATA0 = 0x00;                   //MOARn[7-0]"
      Print #f, "                CAN_DATA1 = 0x00;                   //MOARn[15-8]"
      Print #f, "                CAN_DATA2 = (pstObj->ulID.ubDB[3] << 2);    //MOARn[16-23]"
      Print #f, "                CAN_DATA3 |= (((pstObj->ulID.ubDB[2] & 0x07) << 2) + ubTemp);   //MOARn[31-24]"
    ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
      Print #f, "                CAN_DATA3 &= ~0x3F; //MOARn[31-24]"
      Print #f, "                ubTemp = 0;"
      Print #f, "                ubTemp = ((pstObj->ulID.ubDB[0] & 0xC0) >> 6);"
      Print #f, ""
      Print #f, "                CAN_DATA0 = 0x00;                   //MOARn[7-0]"
      Print #f, "                CAN_DATA1 = 0x00;                   //MOARn[15-8]"
      Print #f, "                CAN_DATA2 = (pstObj->ulID.ubDB[0] << 2);    //MOARn[16-23]"
      Print #f, "                CAN_DATA3 |= (((pstObj->ulID.ubDB[1] & 0x07) << 2) + ubTemp);   //MOARn[31-24]"
    End If
      Print #f, "            }"
      Print #f, "            CAN_vWriteEN(ALL_DATA_VALID); // Write mode is enabled"
      Print #f, ""
      Print #f, "            //------- DLC ------------------------------------------------------"
      Print #f, "            CAN_vWriteCANAddress(CAN_MOFCR(j));"
      Print #f, "            CAN_vReadEN();   // Readmode is Enabled"
      Print #f, "            CAN_DATA3 = pstObj->ubMOdlc;"
      Print #f, "            CAN_vWriteEN(ALL_DATA_VALID); // Write mode is enabled"
      Print #f, ""
      Print #f, "            //------- MODATALn -------------------------------------------------"
      Print #f, ""
      Print #f, "            CAN_vWriteCANAddress(CAN_MODATAL(j));  // Addressing CAN_MODATALn register"
      Print #f, ""
      Print #f, "            CAN_vWriteCANData(pstObj->ulDATAL.ulVal);      // load data"
      Print #f, ""
      Print #f, "            //  CAN Address pointing to the CAN_MODATAH"
      Print #f, "            CAN_ADCON = ADR_INC;   // Auto Increment the current address(+1)"
      Print #f, ""
      Print #f, "            //-------- MODATAHn -----------------------------------------------"
      Print #f, "            CAN_vWriteCANData(pstObj->ulDATAH.ulVal);"
      Print #f, ""
      Print #f, "            CAN_vWriteCANAddress(CAN_MOCTR(j));  // Addressing CAN_MOCTRn register"
      Print #f, ""
      Print #f, "            CAN_vWriteCANData(0x01280000);      // set TXRQ, NEWDAT, MSGVAL"
      Print #f, "            ubReturn = 1;"
      Print #f, "        }"
      Print #f, "    }"
      Call GenerateAMprotection_FunEnd(r, vIndex, f, 0)
      Print #f, ""
      Print #f, "return(ubReturn);"
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_ubReadFIFO function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ReadFIFO"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
      Print #f, ""
      Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)
      Print #f, "{"
      Print #f, "ubyte j;"
      Print #f, "ubyte ubTemp;"
      Print #f, "ubyte ubReturn = 2;"
      Print #f, ""
      Call GenerateAMprotection_FunBeg(r, vIndex, f, 0)
      Print #f, ""
      Print #f, "    CAN_vWriteCANAddress(CAN_MOFCR(ubObjNr));"
      Print #f, "    CAN_vReadEN();   // Readmode is Enabled"
      Print #f, "    if(CAN_DATA0 & 0x01) // if receive FIFO base object"
      Print #f, "     {"
      Print #f, "        j = aubFIFOReadPtr[ubObjNr];"
      Print #f, "        ubReturn = 0;"
      Print #f, ""
      Print #f, "        CAN_vWriteCANAddress(CAN_MOCTR(j));"
      Print #f, "        CAN_vReadEN();   // Readmode is Enabled"
      Print #f, "        if(CAN_DATA0 & 0x08)  // if NEWDAT"
      Print #f, "        {"
      Print #f, "            CAN_vWriteCANData(0x00000008); // clear NEWDAT"
      Print #f, ""
      Print #f, "            CAN_vReadEN();   // Readmode is Enabled"
      Print #f, "            if(j == CAN_DATA3) // last MO in a list"
      Print #f, "            {"
      Print #f, "              // ReadPtr = BOT of the base object"
      Print #f, "              CAN_vWriteCANAddress(CAN_MOFGPR(ubObjNr));"
      Print #f, "              CAN_vReadEN();   // Readmode is Enabled"
      Print #f, ""
      Print #f, "              aubFIFOReadPtr[ubObjNr] = CAN_DATA0;"
      Print #f, "            }"
      Print #f, "            else"
      Print #f, "            {"
      Print #f, "              // ReadPtr = PNEXT of the current selected slave"
      Print #f, "              aubFIFOReadPtr[ubObjNr] = CAN_DATA3;"
      Print #f, "            }"
      Print #f, "        // check if the previous message was lost"
      Print #f, "        CAN_vWriteCANAddress(CAN_MOCTR(j));"
      Print #f, "        CAN_vReadEN();   // Readmode is Enabled"
      Print #f, ""
      Print #f, "        if(CAN_DATA0 & 0x10)  // if set MSGLST"
      Print #f, "        {"
      Print #f, "            CAN_vWriteCANData(0x00000010);      // reset MSGLST"
      Print #f, "            return(3);"
      Print #f, "        }"
      Print #f, ""
      Print #f, "        //--------DLC ------------------------------------------------"
      Print #f, "        CAN_vWriteCANAddress(CAN_MOFCR(j));"
      Print #f, "        CAN_vReadEN();   // Readmode is Enabled"
      Print #f, "        pstObj->ubMOdlc  = CAN_DATA3;"
      Print #f, ""
      Print #f, "        //------ MODATALn ---------------------------------------------"
      Print #f, "        CAN_vWriteCANAddress(CAN_MODATAL(j));"
'      Print #f, "        pstObj->ulDATAL.ulVal = CAN_ulGetCANData();"
      Print #f, ""
      Print #f, "        CAN_vReadEN();  // Readmode is Enabled"
    If (r.v("MAIN_COMPILER") = "Keil") Then
      Print #f, "        pstObj->ulDATAL.ubDB[3] = CAN_DATA0; // Read CAN Data Register 0"
      Print #f, "        pstObj->ulDATAL.ubDB[2] = CAN_DATA1; // Read CAN Data Register 1"
      Print #f, "        pstObj->ulDATAL.ubDB[1] = CAN_DATA2; // Read CAN Data Register 2"
      Print #f, "        pstObj->ulDATAL.ubDB[0] = CAN_DATA3; // Read CAN Data Register 3"
    ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
      Print #f, "        pstObj->ulDATAL.ubDB[0] = CAN_DATA0; // Read CAN Data Register 0"
      Print #f, "        pstObj->ulDATAL.ubDB[1] = CAN_DATA1; // Read CAN Data Register 1"
      Print #f, "        pstObj->ulDATAL.ubDB[2] = CAN_DATA2; // Read CAN Data Register 2"
      Print #f, "        pstObj->ulDATAL.ubDB[3] = CAN_DATA3; // Read CAN Data Register 3"
    End If
      Print #f, ""
      Print #f, "        //  CAN Address pointing to the CAN_MODATAH"
      Print #f, "        CAN_ADCON = ADR_INC;   // Auto Increment the current address(+1)"
      Print #f, ""
      Print #f, "        //------ MODATAHn ---------------------------------------------"
'      Print #f, "        pstObj->ulDATAH.ulVal = CAN_ulGetCANData();"
      Print #f, ""
      Print #f, "        CAN_vReadEN();  // Readmode is Enabled"
    If (r.v("MAIN_COMPILER") = "Keil") Then
      Print #f, "        pstObj->ulDATAH.ubDB[3] = CAN_DATA0; // Read CAN Data Register 0"
      Print #f, "        pstObj->ulDATAH.ubDB[2] = CAN_DATA1; // Read CAN Data Register 1"
      Print #f, "        pstObj->ulDATAH.ubDB[1] = CAN_DATA2; // Read CAN Data Register 2"
      Print #f, "        pstObj->ulDATAH.ubDB[0] = CAN_DATA3; // Read CAN Data Register 3"
    ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
      Print #f, "        pstObj->ulDATAH.ubDB[0] = CAN_DATA0; // Read CAN Data Register 0"
      Print #f, "        pstObj->ulDATAH.ubDB[1] = CAN_DATA1; // Read CAN Data Register 1"
      Print #f, "        pstObj->ulDATAH.ubDB[2] = CAN_DATA2; // Read CAN Data Register 2"
      Print #f, "        pstObj->ulDATAH.ubDB[3] = CAN_DATA3; // Read CAN Data Register 3"
    End If
      Print #f, ""
      Print #f, "        CAN_vWriteCANAddress(CAN_MOCTR(j));"
      Print #f, "        CAN_vReadEN();   // Readmode is Enabled"
      Print #f, "        if(CAN_DATA1 & 0x08)     // transmit object"
      Print #f, "        {"
      Print #f, "            pstObj->ubMOcfg  = pstObj->ubMOcfg | 0x08;"
      Print #f, "        }"
      Print #f, ""
      Print #f, "        //  CAN Address pointing to the CAN_MOARn"
      Print #f, "        CAN_ADCON = ADR_DEC;   // Auto Decrement the current address(-1)"
      Print #f, ""
      Print #f, "        CAN_vReadEN();   // Readmode is Enabled"
      Print #f, "        if(CAN_DATA3 & 0x20)       // extended identifier"
      Print #f, "        {"
    If (r.v("MAIN_COMPILER") = "Keil") Then
      Print #f, "            pstObj->ulID.ubDB[0] = (CAN_DATA3 & 0x1F);"
      Print #f, "            pstObj->ulID.ubDB[1] = CAN_DATA2;"
      Print #f, "            pstObj->ulID.ubDB[2] = CAN_DATA1;"
      Print #f, "            pstObj->ulID.ubDB[3] = CAN_DATA0;"
    ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
      Print #f, "            pstObj->ulID.ubDB[3] = (CAN_DATA3 & 0x1F);"
      Print #f, "            pstObj->ulID.ubDB[2] = CAN_DATA2;"
      Print #f, "            pstObj->ulID.ubDB[1] = CAN_DATA1;"
      Print #f, "            pstObj->ulID.ubDB[0] = CAN_DATA0;"
    End If
      Print #f, "            pstObj->ubMOcfg  = pstObj->ubMOcfg | 0x04;"
      Print #f, "        }"
      Print #f, "        else             // standard identifier"
      Print #f, "        {"
    If (r.v("MAIN_COMPILER") = "Keil") Then
      Print #f, "            pstObj->ulID.ubDB[3] = (CAN_DATA2 & 0xFC) >> 2;   // MOAMRn[23-16]"
      Print #f, "            pstObj->ulID.ubDB[2] = (CAN_DATA3 & 0x1f);    // MOAMRn[31-24]"
      Print #f, "            pstObj->ulID.ubDB[1] = 0x00;    // MOAMRn[15-8]"
      Print #f, "            pstObj->ulID.ubDB[0] = 0x00;    // MOAMRn[7-0]"
      Print #f, ""
      Print #f, "            ubTemp = 0;"
      Print #f, "            ubTemp = ((pstObj->ulID.ubDB[2]) & 0x03) << 6;"
      Print #f, "            pstObj->ulID.ubDB[2] = (pstObj->ulID.ubDB[2] >> 2);"
      Print #f, "            pstObj->ulID.ubDB[3] = (pstObj->ulID.ubDB[3] + ubTemp);"
    ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
      Print #f, "            pstObj->ulID.ubDB[0] = (CAN_DATA2 & 0xFC) >> 2;   // MOAMRn[23-16]"
      Print #f, "            pstObj->ulID.ubDB[1] = (CAN_DATA3 & 0x1f);    // MOAMRn[31-24]"
      Print #f, "            pstObj->ulID.ubDB[2] = 0x00;    // MOAMRn[15-8]"
      Print #f, "            pstObj->ulID.ubDB[3] = 0x00;    // MOAMRn[7-0]"
      Print #f, ""
      Print #f, "            ubTemp = 0;"
      Print #f, "            ubTemp = ((pstObj->ulID.ubDB[1]) & 0x03) << 6;"
      Print #f, "            pstObj->ulID.ubDB[1] = (pstObj->ulID.ubDB[1] >> 2);"
      Print #f, "            pstObj->ulID.ubDB[0] = (pstObj->ulID.ubDB[0] + ubTemp);"
    End If
      Print #f, "        }"
      Print #f, "        //------ CAN Frame Counter --------------------------------"
      Print #f, "        CAN_vWriteCANAddress(CAN_MOIPR(j));"
      Print #f, "        CAN_vReadEN();   // Readmode is Enabled"
      Print #f, ""
    If (r.v("MAIN_COMPILER") = "Keil") Then
      Print #f, "        pstObj->uwCounter.ubDB[1] = CAN_DATA2;"
      Print #f, "        pstObj->uwCounter.ubDB[0] = CAN_DATA3;"
    ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
      Print #f, "        pstObj->uwCounter.ubDB[0] = CAN_DATA2;"
      Print #f, "        pstObj->uwCounter.ubDB[1] = CAN_DATA3;"
    End If
      Print #f, ""
      Print #f, "        // check if the message was corrupted"
      Print #f, "        CAN_vWriteCANAddress(CAN_MOCTR(j));"
      Print #f, "        CAN_vReadEN();   // Readmode is Enabled"
      Print #f, "        if(CAN_DATA0 & 0x08)    // if NEWDAT"
      Print #f, "        {"
      Print #f, "            CAN_vWriteCANData(0x00000008); // clear NEWDAT"
      Print #f, "            return(4);"
      Print #f, "        }"
      Print #f, "      ubReturn = 1;"
      Print #f, "    }"
      Print #f, "  }"
      Call GenerateAMprotection_FunEnd(r, vIndex, f, 0)
      Print #f, ""
      Print #f, "return(ubReturn);"
      Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_ubGetTxErrorCounter function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------

    n = "GetTxErrorCounter"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
            Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
            Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
            Print #f, ""
            Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)
            Print #f, "{"
            Print #f, "ubyte ubReturn = 0;"
            Print #f, ""
            Call GenerateAMprotection_FunBeg(r, vIndex, f, 0)
            Print #f, ""
            Print #f, "    CAN_vWriteCANAddress(CAN_NECNT(ubNodeNr));"
            Print #f, "    CAN_vReadEN(); // Read mode is enabled"
            Print #f, "    ubReturn = CAN_DATA1;"
            Print #f, ""
            Call GenerateAMprotection_FunEnd(r, vIndex, f, 0)
            Print #f, ""
            Print #f, "return(ubReturn);"
            Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_ubGetRxErrorCounter function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetRxErrorCounter"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
            Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
            Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
            Print #f, ""
            Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)
            Print #f, "{"
            Print #f, "ubyte ubReturn = 0;"
            Print #f, ""
            Call GenerateAMprotection_FunBeg(r, vIndex, f, 0)
            Print #f, ""
            Print #f, "    CAN_vWriteCANAddress(CAN_NECNT(ubNodeNr));"
            Print #f, "    CAN_vReadEN(); //Read mode is enabled"
            Print #f, "    ubReturn = CAN_DATA0;"
            Print #f, ""
            Call GenerateAMprotection_FunEnd(r, vIndex, f, 0)
            Print #f, ""
            Print #f, "return(ubReturn);"
            Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_ubGetErrorTransferDirection function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetErrorTransferDirection"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
            Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
            Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
            Print #f, ""
            Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)
            Print #f, "{"
            Print #f, "ubyte ubReturn = 0;"
            Print #f, ""
            Call GenerateAMprotection_FunBeg(r, vIndex, f, 0)
            Print #f, ""
            Print #f, "    CAN_vWriteCANAddress(CAN_NECNT(ubNodeNr));"
            Print #f, "    CAN_vReadEN();  //Readmode is Enabled"
            Print #f, "    ubReturn = (CAN_DATA3 & 0x01);"
            Print #f, ""
            Call GenerateAMprotection_FunEnd(r, vIndex, f, 0)
            Print #f, ""
            Print #f, "return(ubReturn);"
            Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_ubGetErrorIncrement function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetErrorIncrement"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
            Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
            Print #f, uc.GetUserCode("(" & n & ",1)", 0)     'USER CODE for application specific descriptions'
            Print #f, ""
            Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)
            Print #f, "{"
            Print #f, "ubyte ubReturn = 0;"
            Print #f, ""
            Call GenerateAMprotection_FunBeg(r, vIndex, f, 0)
            Print #f, ""
            Print #f, "    CAN_vWriteCANAddress(CAN_NECNT(ubNodeNr));"
            Print #f, "    CAN_vReadEN();  //Readmode is enabled"
            Print #f, "    ubReturn = (CAN_DATA3 & 0x02);"
            Print #f, ""
            Call GenerateAMprotection_FunEnd(r, vIndex, f, 0)
            Print #f, ""
            Print #f, "return(ubReturn);"
            Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
            Print #f, ""
    End If
    
    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,10)", 0)
    Print #f, ""
    
    'close file
    Close f
    
    CANGenerateC = True
    Exit Function
    
CANGenerateCError:
    Close f
'    Errorbox("Code generation for " & sVarPre & " failed", Error$)
    Exit Function
End Function


'*****************************************************************************
'Description:
'        Generates the code for the CAN.h file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function CANGenerateH(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo CANGenerateHError

    CANGenerateH = False
    
    Dim i As Integer               ' used as index for loops
    Dim f As Variant               ' used for file operations
    Dim v As Variant               ' used as temp variable for register operations
    Dim d As String                ' used as temp variable for string operations
    Dim n As String                ' used as temp variable for function names
    Dim dc As DAvECollection       ' used as temp variable for Collections
    Dim uc As UserCode             ' used for UserCode objects
    Dim func As FunctionClass      ' used for FunctionClass objects
    Dim sVarPre As String          ' used as prefix for variable names
    Dim sRegPre As String          ' used as prefix for register names
    Dim l As Long                  ' used as index for loops

    sVarPre = "CAN" & vIndex     ' prefix for all project variables from this module (specified in Var.dave
    sRegPre = sVarPre & "_"        ' prefix for the registers

   
    Set dc = ObjectCollection("MODULENAMES")
    Set uc = New UserCode
    Set func = New FunctionClass
    
        r.v("CAN_USE_SRN15") = 1
        r.v("CAN_NAME_SRN15") = "CAN_pushAMRegs/_popAMRegs"

        r.v("CAN_USE_WriteCANAdrValue") = 1  'sVarPre & "_vWriteCANAdrValue"
        r.v("CAN_NAME_WriteCANAdrValue") = "CAN_vWriteAMData"  'sVarPre & "_vWriteCANAdrValue"

    'refresh the decriptions of the functions and macros
    Call RefreshFunctionDescriptions(func, r)
    
    'read usercode
    uc.ReadFile ((StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v(sVarPre & "_HFILE"))
    
    'open CAN.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'
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,4)", 0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_TYPEDEFS
    
    '------------------------------------------------------------------------------------------------
    'CAN SFR's Address
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    Dim szSfrAddress As String
    Dim szSfrName As String
    Dim lSfrIndex As Long
    
    '------------------------------------------------------------------------------------------------
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_SFR_DECLARATION
    Print #f, ""
    Print #f, ""
    
    For lSfrIndex = 1 To r.Regs.Sfrcount
    
      r.Regs.SfrIndex = lSfrIndex
      szSfrName = r.Regs.SfrName
      szSfrAddress = r.Regs.SfrAdress
            
      If (Left(szSfrName, 3) = "CAN") Then
        If ((Left(szSfrName, 5) = "CAN_A") Or (Left(szSfrName, 5) = "CAN_D")) Then
        Else
            If (InStr(r.v("MAIN_Compiler"), "Keil")) Then
              Print #f, "#define " & szSfrName & Space(8) & szSfrAddress; " "
            End If
            If (InStr(r.v("MAIN_Compiler"), "SDCC")) Then
              Print #f, "#define " & szSfrName & Space(8) & szSfrAddress; " "
            End If
        End If
      End If
    Next lSfrIndex
    
    Print #f, " "
    Print #f, " "
    Print #f, " //------------------------------------------------------------"
    Print #f, " //-- CAN Message Object x ------------------------------------"
    Print #f, " //------------------------------------------------------------"
    Print #f, " //------------------------------------------------------------"
    Print #f, " // Note: DAvE Ignores Lower Address Lines A[1:0]"
    Print #f, " //"
    Print #f, " // MO = Message Ojbect; n = 31-0"
    Print #f, " // MO base = 400H + n * 20H"
    Print #f, " //"
    Print #f, " //  Message Object 0 Address Map..."
    Print #f, " //   __________"
    Print #f, " //  |MOCTRn    |  -> 0x0407"
    Print #f, " //  |----------|"
    Print #f, " //  |MOARn     |  -> 0x0406"
    Print #f, " //  |----------|"
    Print #f, " //  |MODATAHn  |  -> 0x0405"
    Print #f, " //  |----------|"
    Print #f, " //  |MODATALn  |  -> 0x0404"
    Print #f, " //  |----------|"
    Print #f, " //  |MOAMRn    |  -> 0x0403"
    Print #f, " //  |----------|"
    Print #f, " //  |MOOIPRn   |  -> 0x0402"
    Print #f, " //  |----------|"
    Print #f, " //  |MOFGPRn   |  -> 0x0401"
    Print #f, " //  |----------|"
    Print #f, " //  |MOFCRn    |  -> 0x0400"
    Print #f, " //  |__________|"
    Print #f, " //"
    Print #f, " //------------------------------------------------------------"
    
    Print #f, " //------------------------------------------------------------"
    Print #f, " //-- CAN Message Object x ------------------------------------"
    Print #f, " //------------------------------------------------------------"
    Print #f, " #define MO_BASE 0x0400"
    Print #f, " "
    Print #f, " //------------------------------------------------------------"
    Print #f, " //-- CAN Message Object 0-31 ------------------------------------"
    Print #f, " //------------------------------------------------------------"
    Print #f, " "
    Print #f, " // Message Object Nr Function Control Register"
    Print #f, " #define CAN_MOFCR(Nr) (MO_BASE + (Nr * 8))"
    Print #f, " "
    Print #f, " // Message Object Nr FIFO/Gateway Pointer Register"
    Print #f, " #define CAN_MOFGPR(Nr) (MO_BASE + 01 + (Nr * 8))"
    Print #f, " "
    Print #f, " // Message Object Nr Interrupt Pointer Register"
    Print #f, " #define CAN_MOIPR(Nr) (MO_BASE + 02 + (Nr * 8))"
    Print #f, " "
    Print #f, " // Message Object Nr Acceptance Mask Register"
    Print #f, " #define CAN_MOAMR(Nr) (MO_BASE + 03 + (Nr * 8))"
    Print #f, " "
    Print #f, " // Message Object Nr Data Register Low"
    Print #f, " #define CAN_MODATAL(Nr) (MO_BASE + 04 + (Nr * 8))"
    Print #f, " "
    Print #f, " // Message Object Nr Data Register High"
    Print #f, " #define CAN_MODATAH(Nr) (MO_BASE + 05 + (Nr * 8))"
    Print #f, " "
    Print #f, " // Message Object Nr Arbitration Register"
    Print #f, " #define CAN_MOAR(Nr) (MO_BASE + 06 + (Nr * 8))"
    Print #f, " "
    Print #f, " // Message Object Nr Control Register"
    Print #f, " #define CAN_MOCTR(Nr) (MO_BASE + 07 + (Nr * 8))"
    Print #f, ""
    Print #f, " //------------------------------------------------------------"
    Print #f, " //-- CAN Node Registers 0-1 ----------------------------------"
    Print #f, " //------------------------------------------------------------"
    Print #f, " "
    Print #f, " // Node x Error Counter Register"
    Print #f, " #define CAN_NECNT(Nr) (CAN_NECNT0 + (Nr * 0x40))"
    Print #f, ""
    Print #f, " //****************************************************************************"
    Print #f, " // @Defines"
    Print #f, " //****************************************************************************"
    Print #f, ""
    Print #f, " // Panel Busy Flag"
    Print #f, "#define CAN_PANCTR_BUSY          0x01"
    Print #f, ""
    Print #f, "// Message Object Status Register"
    Print #f, "#define MOSTAT_RXPND     0x01"
    Print #f, "#define MOSTAT_TXPND     0x02"
    Print #f, "#define MOSTAT_NEWDAT    0x08"
    Print #f, "#define MOSTAT_MSGLST    0x10"
    Print #f, "#define MOSTAT_RST_MNR   0x19"
    Print #f, "                  "
    Print #f, " // MultiCAN Access Mediator Registers"
    Print #f, ""
    Print #f, " // ADCON Read is enabled"
    Print #f, "#define ADCON_R 0x00"
    Print #f, ""
    Print #f, " // ADCON Write is enabled"
    Print #f, "#define ADCON_W 0x01"
    Print #f, ""
    Print #f, " // Data Transmission Busy"
    Print #f, "#define CAN_ADCON_BSY 0x02"
    Print #f, ""
    Print #f, " // Auto increment the current address(+1)"
    Print #f, "#define ADR_INC 0x04"
    Print #f, ""
    Print #f, " // Auto increment the current address(+8)"
    Print #f, "#define ADR_INC8 0x0C"
    Print #f, ""
    Print #f, " // Auto decrement the current address(-1)"
    Print #f, "#define ADR_DEC 0x08"
    Print #f, ""
    Print #f, " // CAN Data 0 Valid"
    Print #f, "#define D0_VALID 0x10"
    Print #f, ""
    Print #f, " // CAN Data 1 Valid"
    Print #f, "#define D1_VALID 0x20"
    Print #f, ""
    Print #f, " // CAN Data 2 Valid"
    Print #f, "#define D2_VALID 0x40"
    Print #f, ""
    Print #f, " // CAN Data 3 Valid"
    Print #f, "#define D3_VALID 0x80"
    Print #f, ""
    Print #f, " // CAN Data 0-3 Valid"
    Print #f, "#define ALL_DATA_VALID 0xF0"
    Print #f, ""
    Print #f, ""
    Print #f, " ///  -------------------------------------------------------------------------"
    Print #f, " ///  @Definition of a union for the CAN data structure"
    Print #f, " ///  -------------------------------------------------------------------------"
    Print #f, ""
    Print #f, " // The following data type used for converting 32-bit data to Four 8-bit data types"
    Print #f, " // and vica-versa. This is used for accessing MultiCAN Kernel to Access Mediator"
    Print #f, " // Registers. The data type has the following fields:"
    Print #f, " //"
    Print #f, " //       31       24  23       16  15       8  7         0"
    Print #f, " //      |------------------------------------------------|"
   If (r.v("MAIN_COMPILER") = "Keil") Then
    Print #f, " //      |      DB0   |     DB1    |    DB2   |    DB3    |"
   End If
   If (r.v("MAIN_COMPILER") = "SDCC") Then
    Print #f, " //      |      DB3   |     DB2    |    DB1   |    DB0    |"
   End If
    Print #f, " //      |------------------------------------------------|"
    Print #f, " //      |                                                |"
    Print #f, " //      | <------------------ ulVal -------------------> |"
    Print #f, " //"
    Print #f, ""
    Print #f, "typedef union ulValue {"
    Print #f, "    ulong ulVal;        // 32-bit Data Type"
    Print #f, "    ubyte ubDB[4];      // Four 8-bit Data Type"
    Print #f, "} un_32bit;"
'    Print #f, "  un_32bit unPocket;"
    Print #f, ""
    Print #f, ""
    Print #f, " // The following data type used for converting 16-bit data to Two 8-bit data types"
    Print #f, " // and vica-versa. The data type has the following fields:"
    Print #f, " //"
    Print #f, " //       15       8 7          0"
    Print #f, " //      |----------------------|"
   If (r.v("MAIN_COMPILER") = "Keil") Then
    Print #f, " //      |    DB0   |    DB1    |"
   End If
   If (r.v("MAIN_COMPILER") = "SDCC") Then
    Print #f, " //      |    DB1   |    DB0    |"
   End If
    Print #f, " //      |----------------------|"
    Print #f, " //      |                      |"
    Print #f, " //      | <----- uwVal ------> |"
    Print #f, " //"
    Print #f, ""
    Print #f, "typedef union uwValue {"
    Print #f, "    uword uwVal;        // 16-bit Data Type"
    Print #f, "    ubyte ubDB[2];      // Two 8-bit Data Type"
    Print #f, "} un_16bit;"
    Print #f, ""
    Print #f, " ///  -------------------------------------------------------------------------"
    Print #f, " ///  @Definition of a structure for the CAN data"
    Print #f, " ///  -------------------------------------------------------------------------"
    Print #f, ""
    Print #f, " // The following data type serves as a software message object. Each access to"
    Print #f, " // a hardware message object has to be made by forward a pointer to a software"
    Print #f, " // message object (MCAN_SWObj). The data type has the following fields:"
    Print #f, " //"
    Print #f, " // ubMOdlc:"
    Print #f, " // this byte has the same structure as the message configuration register of a"
    Print #f, " // MOFCRn message object. It contains the ' Data Lenght Code (DLC)'"
    Print #f, " //"
    Print #f, " //         7     6     5      4    3     2     1     0"
    Print #f, " //      |------------------------------------------------|"
    Print #f, " //      |           0            |         DLC           |"
    Print #f, " //      |------------------------------------------------|"
    Print #f, " //"
    Print #f, " // ubMOcfg:"
    Print #f, " // this byte contains the 'Extended Identifier (IDE)', the 'Message Direction (DIR)',"
    Print #f, " // the 'Node Select (NODE)' And the 'Remote Monitoring Mode (RMM)'"
    Print #f, " //"
    Print #f, " //"
    Print #f, " //         7     6     5      4    3     2     1     0"
    Print #f, " //      |------------------------------------------------|"
    Print #f, " //      |                       | DIR | XTD | NODE | RMM |"
    Print #f, " //      |------------------------------------------------|"
    Print #f, " //"
    Print #f, " // ulID:"
    Print #f, " // this field is four bytes long and contains either the 11-bit identifier"
    Print #f, " // or the 29-bit identifier"
    Print #f, " //"
    Print #f, " // ulMask:"
    Print #f, " // this field is four bytes long and contains either the 11-bit mask"
    Print #f, " // or the 29-bit mask"
    Print #f, " //"
    Print #f, " // ulDATAL:"
    Print #f, " // contains the lowest 4 bytes of message object"
    Print #f, " //"
    Print #f, " // ulDATAH:"
    Print #f, " // contains the upper 4 bytes of message object"
    Print #f, " //"
    Print #f, " // uwCounter:"
    Print #f, " // this field is two bytes long and contains the counter value"
    Print #f, " //"
    Print #f, ""
    Print #f, "typedef struct"
    Print #f, "{"
    Print #f, "     ubyte ubMOdlc;       // message object function control register[31-24]"
    Print #f, "     ubyte ubMOcfg;       // message object configuration"
    Print #f, "     un_32bit ulID;       // standard (11-bit)/extended (29-bit) identifier"
    Print #f, "     un_32bit ulMask;     // standard (11-bit)/extended (29-bit) mask"
    Print #f, "     un_32bit ulDATAL;    // 32-bit data Lower bytes"
    Print #f, "     un_32bit ulDATAH;    // 32-bit data Higher bytes"
    Print #f, "     un_16bit uwCounter;  // frame counter(MOIPRn[31-16]"
    Print #f, "}stCAN_SWObj;"
    Print #f, ""
    Print #f, ""
    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'
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_vReadEN macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
'Print #f, "#define CAN_vReadEN() { CAN_ADCON &= ~0x01; while(CAN_ADCON & CAN_ADCON_BSY);}"
    n = "ReadEN"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, "#define CAN_vReadEN() CAN_ADCON = 0x00; while(CAN_ADCON & CAN_ADCON_BSY)"
      Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_vWriteEN macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
'Print #f, "#define CAN_vWriteEN(ubDCtrl) { CAN_ADCON = ((ubDCtrl) | ADCON_W); while(CAN_ADCON & CAN_ADCON_BSY);}"
    n = "WriteEN"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      Print #f, "#define CAN_vWriteEN(ubDCtrl) CAN_ADCON = ((ubDCtrl) | ADCON_W); while(CAN_ADCON & CAN_ADCON_BSY)"
      Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_vWriteEN macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "WriteCANAddress"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      If (r.v("MAIN_COMPILER") = "Keil") Then
          Print #f, "#define CAN_vWriteCANAddress(uwAdr) CAN_ADLH = uwAdr"
      Else
          Print #f, "#define CAN_vWriteCANAddress(uwAdr) CAN_ADL = (uwAdr & 0x00FF); \"
                  Print #f, "            CAN_ADH = ((uwAdr & 0xFF00) >> 8)"
      End If
      Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' void CAN_vWriteCANData(ulong) MACRO
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "WriteCANData"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
      Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)
      If (r.v("MAIN_COMPILER") = "Keil") Then
          Print #f, "#define CAN_vWriteCANData(ulValue) CAN_DATA01 = (ulValue & 0xFFFF); \"
          Print #f, "   CAN_DATA23 = (ulValue >> 16) & 0xFFFF; \"
          Print #f, "   CAN_vWriteEN(ALL_DATA_VALID)"
      ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
                  Print #f, "#define CAN_vWriteCANData(ulValue) CAN_DATA0 = (ulValue & 0x000000FF); \"
          Print #f, "   CAN_DATA1 = ((ulValue & 0x0000FF00) >> 8); \"
                  Print #f, "   CAN_DATA2 = ((ulValue & 0x00FF0000) >> 16); \"
          Print #f, "   CAN_DATA3 = ((ulValue & 0xFF000000) >> 24); \"
          Print #f, "   CAN_vWriteEN(ALL_DATA_VALID)"
      End If
          Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' CAN_pushAMRegs() MACRO
    ' CAN_popAMRegs() MACRO
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
      n = "push/pop" ''**''CAN_USE_SRN15
      If (r.v(sVarPre & "_USE_SRN15") = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_SRN15")
    If (r.v("MAIN_COMPILER") = "Keil") Then
        Print #f, "#define CAN_pushAMRegs() while(CAN_ADCON & CAN_ADCON_BSY); \"
        Print #f, "        _push_(CAN_ADL);   _push_(CAN_ADH); \"
        Print #f, "        _push_(CAN_DATA0); _push_(CAN_DATA1); \"
        Print #f, "        _push_(CAN_DATA2); _push_(CAN_DATA3)"
        Print #f, ""
        Print #f, "#define CAN_popAMRegs() _pop_(CAN_DATA3); _pop_(CAN_DATA2); \"
        Print #f, "        _pop_(CAN_DATA1); _pop_(CAN_DATA0); \"
        Print #f, "        _pop_(CAN_ADH);   _pop_(CAN_ADL)"
    ElseIf (r.v("MAIN_COMPILER") = "SDCC") Then
        Print #f, "#define CAN_pushAMRegs() while(CAN_ADCON & CAN_ADCON_BSY); \"
        Print #f, "        _asm \"
        Print #f, "        push SDCC_CAN_ADL \"
        Print #f, "        push SDCC_CAN_ADH \"
        Print #f, "        push SDCC_CAN_DATA0 \"
        Print #f, "        push SDCC_CAN_DATA1 \"
        Print #f, "        push SDCC_CAN_DATA2 \"
        Print #f, "        push SDCC_CAN_DATA3 \"
        Print #f, "        _endasm"
        Print #f, ""
        Print #f, "#define CAN_popAMRegs() _asm\"
        Print #f, "        pop SDCC_CAN_DATA3 \"
        Print #f, "        pop SDCC_CAN_DATA2 \"
        Print #f, "        pop SDCC_CAN_DATA1 \"
        Print #f, "        pop SDCC_CAN_DATA0 \"
        Print #f, "        pop SDCC_CAN_ADH \"
        Print #f, "        pop SDCC_CAN_ADL \"
        Print #f, "        _endasm"
    End If
        Print #f, ""
   End If

    '------------------------------------------------------------------------------------------------
    ' Interrupt Vectors
    '------------------------------------------------------------------------------------------------
            Print #f, SECTION_INTERRUPT_VECTORS
            Print #f, ""
            Print #f, FormatComments(0, 0, "", "", "Shared interrupt vector definitions are defined in SHARED_INT.H")
            Print #f, ""  'DAvE interrupt vector definitions
            
'    n = "SRN0"
'    If (r.v(sVarPre & "_USE_" & n) = 1) Then
'        Print #f, "#define " & sVarPre & "_XINTR5 " & IntNumber(&H2B)
'    End If
'
'    n = "SRN1"
'    If ((r.v(sVarPre & "_USE_" & n) = 1) Or (r.v(sVarPre & "_USE_SRN2") = 1)) Then
'        Print #f, "#define " & sVarPre & "_XINTR6 " & IntNumber(&H33)
'    End If
'
'    n = "SRN3"
'    If (r.v(sVarPre & "_USE_" & n) = 1) Then
'        Print #f, "#define " & sVarPre & "_XINTR9 " & IntNumber(&H4B)
'    End If
'
'    n = "SRN4"
'    If (r.v(sVarPre & "_USE_" & n) = 1) Then
'        Print #f, "#define " & sVarPre & "_XINTR10 " & IntNumber(&H53)
'    End If
'
'    n = "SRN5"
'    If (r.v(sVarPre & "_USE_" & n) = 1) Then
'        Print #f, "#define " & sVarPre & "_XINTR11 " & IntNumber(&H5B)
'    End If
'
'    n = "SRN6"
'    If (r.v(sVarPre & "_USE_" & n) = 1) Then
'        Print #f, "#define " & sVarPre & "_XINTR12 " & IntNumber(&H63)
'    End If
'
'    n = "SRN7"
'    If (r.v(sVarPre & "_USE_" & n) = 1) Then
'        Print #f, "#define " & sVarPre & "_XINTR13 " & IntNumber(&H6B)
'    End If

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


'*****************************************************************************
'Description:
'        This functions generates the code and statements for the CAN
'        message object x.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'        f                file handle
'        iObj             number of message object (0 - 31)
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Sub GenerateObjx(r As ProjectData, vIndex As Variant, f As Variant, iObj As Integer)
    On Error GoTo GenerateObjxError

    Dim i As Integer               ' used as index for loops
    Dim v As Variant               ' used as temp variable for register operations
    Dim d As String                ' used as temp variable for string operations
    Dim sVarPre As String          ' used as prefix for variable names
    Dim sRegPre As String          ' used as prefix for register names
    Dim iBase As Integer

    sVarPre = "CAN" & vIndex       ' prefix for all project variables from this module (specified in Var.dave
    sRegPre = sVarPre & "_"        ' prefix for the registers

    '------------------------------------------------------------------------------------------------
    ' Configuration of Message Object x:
    '------------------------------------------------------------------------------------------------
    Print #f, FormatSectionHeader(2, 1, "Configuration of Message Object " & iObj & ":")
    iBase = IsObjFIFOSlave(r, vIndex, iObj)
              
    If (r.v(sRegPre & "MOCTR" & iObj & "_MSGVAL") = 0) Then
      d = "message object " & iObj & " is not valid"
      Print #f, FormatCommentsForInit(1, d)
    Else
      d = "message object " & iObj & " is valid"
      Print #f, FormatCommentsForInit(1, d)

      If (r.v(sRegPre & "MOCTR" & iObj & "_DIR") = 1) Then
          d = "message object is used as transmit object"
      Else
          d = "message object is used as receive object"
      End If
      Print #f, FormatCommentsForInit(1, d)

      ' the allocation of a MO to a list is made ba the list controller
      If (r.v(sRegPre & "MOCTR" & iObj & "_LIST") = 0) Then
          d = "this message object is not assigned to a list"
      Else
          d = "this message object is assigned to list " & r.v(sRegPre & "MOCTR" & iObj & "_LIST") & " (node " & r.v(sRegPre & "MOCTR" & iObj & "_LIST") - 1 & ")"
      End If
      Print #f, FormatCommentsForInit(1, d)

      ' for transmit frame
      v = 0
      ' all bits of the MOCTR are set via CTRL bit field
      v = v + r.v(sRegPre & "MOCTR" & iObj & "_DIR") * 2 ^ 27
      v = v + r.v(sRegPre & "MOCTR" & iObj & "_MSGVAL") * 2 ^ 21
      v = v + 1 * 2 ^ 23  ' RXEN; only if RXEN is set then the MO is taken into the receive acceptance filtering
      If (r.v(sRegPre & "MOCTR" & iObj & "_DIR") = 1) Then  ' TX-Object
        If (iBase = -1) Then  ' if no FIFO Slave
            If ((r.v(sRegPre & "MOFGPR" & iObj & "_CUR") = iObj) And (r.v(sRegPre & "MOFCR" & iObj & "_MMC") = 2)) Then
                v = v + 1 * 2 ^ 26 ' TXEN1
            ElseIf (Not (r.v(sRegPre & "MOFCR" & iObj & "_MMC") = 2)) Then
                v = v + 1 * 2 ^ 26 ' TXEN1
            End If
        Else    ' if FIFO Slave
          If (r.v(sRegPre & "MOFGPR" & iBase & "_CUR") = iObj) Then ' if CUR from FIFO Base = iObj
            v = v + 1 * 2 ^ 26 ' TXEN1
          End If
        End If
          v = v + 1 * 2 ^ 25 ' TXEN0
          v = v + 1 * 2 ^ 19 ' NEWDAT
      Else  ' RX-Object
      End If
     
      Print #f, ""
      Print #f, "  //--------------MOCTR" & iObj & " = " & FormatTextOutput(v, "H8") & "---------------------------------------"
      Print #f, ""
      Print #f, FormatCodeStatemente(4, "CAN_vWriteCANAddress(CAN_MOCTR" & iObj & ");", "", "", "Addressing MO" & iObj & " control register")
      Print #f, ""
      Print #f, FormatCodeStatemente(4, "CAN_vWriteAMData(" & FormatTextOutput(v, "H8") & ");", "", "", "load MO" & iObj & " control register")
      Print #f, ""
      Print #f, "  //  CAN Address pointing to the CAN_MOAR" & iObj
      Print #f, FormatCodeStatemente(4, "CAN_ADCON = ADR_DEC;", "", "", "Auto Decrement the current address(-1)")
      Print #f, ""
        
        ' MOAR Register
        ' PRI
        Select Case (r.v(sRegPre & "MOAR" & iObj & "_PRI"))
            Case 0
               d = "priority class 0; time triggered CAN"
            Case 1
               d = "priority class 1; transmit acceptance filtering is based on the list order"
            Case 2
               d = "priority class 2; transmit acceptance filtering is based on the CAN identifier"
            Case 3
               d = "priority class 3; transmit acceptance filtering is based on the list order (like class 1)"
        End Select
        Print #f, FormatCommentsForInit(1, d)
        ' IDE
        If (r.v(sRegPre & "MOAR" & iObj & "_IDE") = 1) Then
            d = "extended 29-bit identifier"
        Else
            d = "standard 11-bit identifier"
        End If
        Print #f, FormatCommentsForInit(1, d)
        ' ID
        v = 0
        If (r.v(sRegPre & "MOAR" & iObj & "_IDE") = 1) Then ' 29-bit Identifier
            v = v + r.v(sRegPre & "MOAR" & iObj & "_ID")
            d = "identifier 29-bit:      " & FormatTextOutput(v, "H8")
        Else          ' 11-bit Identifier
            v = v + r.v(sRegPre & "MOAR" & iObj & "_ID")
            v = v \ 2 ^ 18
            d = "identifier 11-bit:      " & FormatTextOutput(v, "H3")
        End If
        Print #f, FormatCommentsForInit(1, d)

        v = 0
        v = r.sfr(sRegPre & "MOAR" & iObj)
        Print #f, ""
        Print #f, "  //--------------MOAR" & iObj & " = " & FormatTextOutput(v, "H8") & "---------------------------------------"
        Print #f, ""
        Print #f, FormatCodeStatemente(4, "CAN_vWriteAMData(" & FormatTextOutput(v, "H8") & ");", "", "", "load MO" & iObj & " arbitration register")
        Print #f, ""

        ' MODATAL Register
        ' MODATAH Register
        Print #f, "  //  CAN Address pointing to the CAN_MODATAH" & iObj
        Print #f, FormatCodeStatemente(4, "CAN_ADCON = ADR_DEC;", "", "", "Auto Decrement the current address(-1)")
        Print #f, ""
        
        v = 0
        v = r.sfr(sRegPre & "MODATAH" & iObj)
        Print #f, ""
        Print #f, "  //--------------MODATAH" & iObj & " = " & FormatTextOutput(v, "H8") & "---------------------------------------"
        Print #f, ""
        Print #f, FormatCodeStatemente(4, "CAN_vWriteAMData(" & FormatTextOutput(v, "H8") & ");", "", "", "load MO" & iObj & " data register high")
        Print #f, ""
        
        Print #f, "  //  CAN Address pointing to the CAN_MODATAL" & iObj
        Print #f, FormatCodeStatemente(4, "CAN_ADCON = ADR_DEC;", "", "", "Auto Decrement the current address(-1)")
        Print #f, ""
        
        v = 0
        v = r.sfr(sRegPre & "MODATAL" & iObj)
        Print #f, "  //--------------MODATAL" & iObj & " = " & FormatTextOutput(v, "H8") & "---------------------------------------"
        Print #f, ""
        Print #f, FormatCodeStatemente(4, "CAN_vWriteAMData(" & FormatTextOutput(v, "H8") & ");", "", "", "load MO" & iObj & " data register low")
        Print #f, ""

        Print #f, "  //  CAN Address pointing to the CAN_MOAMR" & iObj
        Print #f, FormatCodeStatemente(4, "CAN_ADCON = ADR_DEC;", "", "", "Auto Decrement the current address(-1)")
        Print #f, ""
        ' MOAMR Register Config
        If (r.v(sRegPre & "MOAMR" & iObj & "_MIDE") = 0) Then
            d = "accept reception of standard and extended frames"
        Else
            d = "only accept receive frames with matching IDE bit"
        End If
        Print #f, FormatCommentsForInit(1, d)
        ' AM
        If (r.v(sRegPre & "MOAR" & iObj & "_IDE") = 1 Or r.v(sRegPre & "MOAMR" & iObj & "_MIDE") = 0) Then ' 29-bit Identifier
            v = 0
            v = v + r.v(sRegPre & "MOAMR" & iObj & "_AM")
            d = "acceptance mask 29-bit: " & FormatTextOutput(v, "H8")
            Print #f, FormatCommentsForInit(1, d)
        End If
        If (r.v(sRegPre & "MOAR" & iObj & "_IDE") = 0 Or r.v(sRegPre & "MOAMR" & iObj & "_MIDE") = 0) Then ' 11-bit Identifier
            v = 0
            v = v + r.v(sRegPre & "MOAMR" & iObj & "_AM")
            v = v \ 2 ^ 18
            d = "acceptance mask 11-bit: " & FormatTextOutput(v, "H3")
            Print #f, FormatCommentsForInit(1, d)
        End If
        
        v = 0
        v = r.sfr(sRegPre & "MOAMR" & iObj)
        Print #f, ""
        Print #f, "  //--------------CAN_MOAMR" & iObj & " = " & FormatTextOutput(v, "H8") & "------------------------------------"

        Print #f, ""
        Print #f, FormatCodeStatemente(4, "CAN_vWriteAMData(" & FormatTextOutput(v, "H8") & ");", "", "", "load MO" & iObj & " acceptance mask register")
        Print #f, ""
    
        Print #f, "  //  CAN Address pointing to the CAN_MOIPR" & iObj
        Print #f, FormatCodeStatemente(4, "CAN_ADCON = ADR_DEC;", "", "", "Auto Decrement the current address(-1)")
        Print #f, ""

        ' MOIPR Register
        ' CFCVAL
        ' MPN
        
        d = "use message pending register " & iObj \ 32 & " bit position " & iObj Mod 32
        Print #f, FormatCommentsForInit(1, d)
        ' TXINP
        If (r.v(sRegPre & "MOFCR" & iObj & "_TXIE") = 1) Then
            d = "transmit interrupt node pointer: MultiCAN SRN " & r.v(sRegPre & "MOIPR" & iObj & "_TXINP")
            Print #f, FormatCommentsForInit(1, d)
        End If
        ' RXINP
        If (r.v(sRegPre & "MOFCR" & iObj & "_RXIE") = 1) Then
            d = "receive interrupt node pointer: MultiCAN SRN " & r.v(sRegPre & "MOIPR" & iObj & "_RXINP")
            Print #f, FormatCommentsForInit(1, d)
        End If
        v = 0
        v = r.sfr(sRegPre & "MOIPR" & iObj)
        Print #f, ""
        Print #f, "  //--------------MOIPR" & iObj & " = " & FormatTextOutput(v, "H8") & "---------------------------------------"
        Print #f, ""
        Print #f, FormatCodeStatemente(4, "CAN_vWriteAMData(" & FormatTextOutput(v, "H8") & ");", "", "", "load MO" & iObj & " interrupt pointer register")
        Print #f, ""
        
        Print #f, "  //  CAN Address pointing to the CAN_MOFGPR" & iObj
        Print #f, FormatCodeStatemente(4, "CAN_ADCON = ADR_DEC;", "", "", "Auto Decrement the current address(-1)")
        Print #f, ""
    
        ' MOFGPR Register
        ' SEL
        ' CUR
        ' TOP
        ' BOT
        ' DAvE initialize the SEl, CUR, TOP and BOT pointer with 0xff to mark them as
        ' unused but the reset value of the pointer is 0x00
    If (r.v(sRegPre & "MOCTR" & iObj & "_MSGVAL") = 1) Then  ' only if Object is valid, configure the Object
        Print #f, ""
        v = 0
        If (r.v(sRegPre & "MOFGPR" & iObj & "_BOT") <> &HFF) Then
          v = v + r.v(sRegPre & "MOFGPR" & iObj & "_BOT")
          d = "button pointer : MO" & r.v(sRegPre & "MOFGPR" & iObj & "_BOT")
          Print #f, FormatCommentsForInit(1, d)
        End If
        If (r.v(sRegPre & "MOFGPR" & iObj & "_TOP") <> &HFF) Then
          v = v + r.v(sRegPre & "MOFGPR" & iObj & "_TOP") * 2 ^ 8
          d = "top pointer : MO" & r.v(sRegPre & "MOFGPR" & iObj & "_TOP")
          Print #f, FormatCommentsForInit(1, d)
        End If
        If (r.v(sRegPre & "MOFGPR" & iObj & "_CUR") <> &HFF) Then
          v = v + r.v(sRegPre & "MOFGPR" & iObj & "_CUR") * 2 ^ 16
          d = "current select pointer : MO" & r.v(sRegPre & "MOFGPR" & iObj & "_CUR")
          Print #f, FormatCommentsForInit(1, d)
        End If
        If (r.v(sRegPre & "MOFGPR" & iObj & "_SEL") <> &HFF) Then
          v = v + r.v(sRegPre & "MOFGPR" & iObj & "_SEL") * 2 ^ 24
          d = "object select pointer : MO" & r.v(sRegPre & "MOFGPR" & iObj & "_SEL")
          Print #f, FormatCommentsForInit(1, d)
        End If

        Print #f, "  //--------------MOFGPR" & iObj & " = " & FormatTextOutput(v, "H8") & "---------------------------------------"
        
        Print #f, ""
        Print #f, FormatCodeStatemente(4, "CAN_vWriteAMData(" & FormatTextOutput(v, "H8") & ");", "", "", "load MO" & iObj & " FIFO/gateway pointer register")
        Print #f, ""
        
'---------------------------------------
' code for initialize FIFO obj's to array
        If (r.v(sRegPre & "MOFGPR" & iObj & "_BOT") <> &HFF) Then
            Print #f, "  //  initialization of aubFIFOPtr[" & iObj & "]"
            v = 0
            v = v + r.v(sRegPre & "MOFGPR" & iObj & "_BOT")
            If (r.v(sVarPre & "_USE_WriteFIFO") = 1) Then
                  Print #f, FormatCodeStatemente(4, "aubFIFOWritePtr[" & iObj & "] = " & FormatTextOutput(v, "H2") & ";", "", "", "initialize FIFOWritePtr")
            End If
            If (r.v(sVarPre & "_USE_ReadFIFO") = 1) Then
                  Print #f, FormatCodeStatemente(4, "aubFIFOReadPtr[" & iObj & "] = " & FormatTextOutput(v, "H2") & ";", "", "", "initialize FIFOReadPtr")
            End If
            Print #f, ""
        End If
'---------------------------------------

        Print #f, "  //  CAN Address pointing to the CAN_MOFCR" & iObj
        Print #f, FormatCodeStatemente(4, "CAN_ADCON = ADR_DEC;", "", "", "Auto Decrement the current address(-1)")
        Print #f, ""
                
        ' MOFCR Register
        ' MMC
        Select Case (r.v(sRegPre & "MOFCR" & iObj & "_MMC"))
            Case 0
                If (iBase >= 0) Then
                  ' receive FIFO slave
                  If (r.v(sRegPre & "MOFCR" & iBase & "_MMC") = 1) Then
                    d = "this object is a RECEIVE FIFO SLAVE OBJECT " & _
                        "connected to the base object " & iBase
                  Else
                    ' gateway destination
                    If (r.v(sRegPre & "MOFCR" & iBase & "_MMC") = 4) Then
                      d = "this object is a GATEWAY DESTINATION OBJECT " & _
                          "connected to the gateway source object " & iBase
                    End If
                  End If
                Else
                  ' standard object
                  d = "this object is a STANDARD MESSAGE OBJECT"
                End If
            Case 1
                d = "this object is a RECEIVE FIFO BASE OBJECT"
            Case 2
                d = "this object is a TRANSMIT FIFO BASE OBJECT"
            Case 3
                d = "this object is a TRANSMIT FIFO SLAVE OBJECT " & _
                    "connected to the base object " & iBase
            Case 4
                d = "this object is a GATEWAY SOURCE OBJECT"
        End Select
        Print #f, FormatCommentsForInit(1, d)
        ' DLC
        d = "" & r.v(sRegPre & "MOFCR" & iObj & "_DLC") & " valid data bytes"
        Print #f, FormatCommentsForInit(1, d)
        ' STT
        If (r.v(sRegPre & "MOFCR" & iObj & "_STT") = 1) Then
            d = "enable single transmission trial"
            Print #f, FormatCommentsForInit(1, d)
        End If
        ' SDT
        If (r.v(sRegPre & "MOFCR" & iObj & "_SDT") = 1) Then
            d = "disable the object automatically after a successful data transfer"
            Print #f, FormatCommentsForInit(1, d)
        End If
        ' RMM
        If (r.v(sRegPre & "MOFCR" & iObj & "_RMM") = 1) Then
            d = "the identifier, the IDE and the DLC code of a remote frame with matching identifier is copied to this transmit object"
            Print #f, FormatCommentsForInit(1, d)
        End If
        ' FRREN
        If (r.v(sRegPre & "MOFCR" & iObj & "_FRREN") = 1) Then
            d = "a matching remote frame on the destination CAN bus generate a remote frame on the source CAN bus"
            Print #f, FormatCommentsForInit(1, d)
        End If
        ' OVIE
        If (r.v(sRegPre & "MOFCR" & iObj & "_OVIE") = 1) Then
          d = "enable FIFO overflow interrupt"
          Print #f, FormatCommentsForInit(1, d)
        End If
        ' RXIE
        If (r.v(sRegPre & "MOFCR" & iObj & "_RXIE") = 1) Then
          d = "enable receive interrupt; bit RXPND is set after successful reception of a frame"
          Print #f, FormatCommentsForInit(1, d)
        End If
        ' TXIE
        If (r.v(sRegPre & "MOFCR" & iObj & "_TXIE") = 1) Then
          d = "enable transmit interrupt; bit TXPND is set after successful transmission of a frame"
          Print #f, FormatCommentsForInit(1, d)
        End If
        If (r.v(sRegPre & "MOFCR" & iObj & "_MMC") = 4) Then  ' only if gateway source
          ' DATC
          If (r.v(sRegPre & "MOFCR" & iObj & "_DATC") = 1) Then
              d = "the data fields of the gateway source object are copied to the gateway destination"
          Else
              d = "the data fields of the gateway source object are not copied to the gateway destination"
          End If
          Print #f, FormatCommentsForInit(1, d)
          ' DLCC
          If (r.v(sRegPre & "MOFCR" & iObj & "_DLCC") = 1) Then
              d = "the received source DLC information is copied to the gateway destination object"
          Else
              d = "the received source DLC information is not copied to the gateway destination object"
          End If
          Print #f, FormatCommentsForInit(1, d)
          ' IDC
          If (r.v(sRegPre & "MOFCR" & iObj & "_IDC") = 1) Then
              d = "the identifier of the gateway source object is copied to the gateway destination"
          Else
              d = "the identifier of the gateway source object is not copied to the gateway destination"
          End If
          Print #f, FormatCommentsForInit(1, d)
          ' GDFS
          If (r.v(sRegPre & "MOFCR" & iObj & "_GDFS") = 1) Then
              d = "a data frame received by the source object is automatically sent by the destination object"
          Else
              d = "a data frame received by the source object is not automatically sent by the destination object"
          End If
          Print #f, FormatCommentsForInit(1, d)
        End If
    End If
        v = 0
        v = r.sfr(sRegPre & "MOFCR" & iObj)
        Print #f, ""
        Print #f, "  //--------------MOFCR" & iObj & " = " & FormatTextOutput(v, "H8") & "---------------------------------------"
        Print #f, ""
        Print #f, FormatCodeStatemente(4, "CAN_vWriteAMData(" & FormatTextOutput(v, "H8") & ");", "", "", "load MO" & iObj & "  function control register")
        Print #f, ""
           
    End If  ' if message object valid
    Exit Sub
    
GenerateObjxError:
'    Call ErrMsg("Can not read values for Message Object " & iObj, Error$)
    Exit Sub
End Sub
