Attribute VB_Name = "Module1"
'#Uses "..\include\Const.inc"
'#Uses "..\include\Global.inc"
'#Uses "..\include\Format.inc"
'#Uses "..\classes\FunctionClass.cls
'#Uses "Include.inc"
Option Explicit
'*****************************************************************************
' Module:
'        UART1
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions for generating code for the
'        module 'UART'.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        0.1  Sunil          26-10-04    initial version
'        0.2  Sunil          19-11-04    alternate pin functions part modified.
'                                        port dir register names corrected.
'        0.3  Sunil          29-11-04    - The code which sets the TI bit when
'                                          UART interrupt is not selected is removed.
'                                        - "Mode 3" string was missing in the comments and added.
'        0.4  Sunil           29-12-04   - Bug fixing code added (for Step-A).
'                                        - module prefix added.
'
'
'*****************************************************************************


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

    ' write files UART.h
    If (r.v("UART1_USE_Init")) Then
        If (Not UARTGenerateH(r, vIndex)) Then Exit Function
        If (Not r.AddFile(r.s("UART1_HFILE"), "H")) Then Exit Function
    End If

    ' write file UART.c
    If (r.v("UART1_USE_Init")) Then
        If (Not UARTGenerateC(r, vIndex)) Then Exit Function
        If (Not r.AddFile(r.s("UART1_CFILE"), "C")) Then Exit Function
    End If
        
    StartGenerateCode = True
    Exit Function
    
StartGenerateCodeError:
    Errorbox "Code generation for UART1 failed", Error$
    Exit Function
End Function

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

    Dim i As Integer
    Dim f As Variant
    Dim v As Variant
    Dim d As String
    Dim Baudrate As BaudrateType
    Dim dc As DAvECollection
    Dim uc As UserCode
    Dim func As FunctionClass
    Dim bUARTConfigured As Boolean

    UARTGenerateC = False

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

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

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

    'open UART.c
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v("UART1_CFILE") For Output As f

    '------------------------------------------------------------------------------------------------
    ' module header
    '------------------------------------------------------------------------------------------------
    d = FormatModuleDescription(GetModuleHeaderC("UART1"))
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), dc("UART1").ModuleDescription, r.v("UART1_CFILE"), r.v("MAIN_CPU_TYPE"), r.v("MAIN_COMPILER"), d)

            Print #f, uc.GetUserCode("(UART1_General,1)", 0)   'USER CODE for application specific descriptions'
            Print #f, ""
            Print #f, Rtf.GenerateNote((r.v("UART1_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("(UART1_General,2)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_MACROS
            Print #f, ""  'DAvE local macro definitions'
            Print #f, uc.GetUserCode("(UART1_General,3)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_DEFINES
            Print #f, ""  'DAvE local #define definitions'
            Print #f, uc.GetUserCode("(UART1_General,4)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_TYPEDEFS
            Print #f, ""  'DAvE type defintions'
            Print #f, uc.GetUserCode("(UART1_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("(UART1_General,6)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_GLOBAL_VARIABLES
            Print #f, ""  'DAvE definitions of global variables'
            Print #f, uc.GetUserCode("(UART1_General,7)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_EXTERNAL_PROTOTYPES
            Print #f, ""  'DAvE external declarations of imported functions'
            Print #f, uc.GetUserCode("(UART1_General,8)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_LOCAL_PROTOTYPES
            Print #f, ""  'DAvE declarations of internal local functions'
            Print #f, uc.GetUserCode("(UART1_General,9)", 0)
            Print #f, ""
            Print #f, ""
    '------------------------------------------------------------------------------------------------
    ' UART_Init function
    '------------------------------------------------------------------------------------------------
    If (r.v("UART1_USE_Init") = 1) Then
            Print #f, FormatFunctionHeader(r, func, "UART1_NAME_Init")
            Print #f, uc.GetUserCode("(UART1_Init,1)", 0)     'USER CODE for application specific descriptions'
            Print #f, ""
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "UART1_NAME_Init")
            Print #f, "{"
            Print #f, uc.GetUserCode("(UART1_Init,2)", 2)
            Print #f, ""
            
            bUARTConfigured = Not (r.v("UART1_TxPin_P3.1_Used") = 0 And r.v("UART1_TxPin_P0.4_Used") = 0 And r.v(UART1_PREFIX & "SCON_REN_1") = 0)
            
If bUARTConfigured Then
            Print #f, FormatSectionHeader(2, 1, "UART1 settings")
        If r.v("UART1_TxPin_P0.4_Used") = 1 Then
            Print #f, FormatComments(2, 1, "", "", "Pin TXD1_0 (P0.4) is selected for transmission")
        Else
            If r.v("UART1_TxPin_P3.1_Used") = 1 Then
                Print #f, FormatComments(2, 1, "", "", "Pin TXD1_1 (P3.1) is selected for transmission")
            Else
                Print #f, FormatComments(2, 1, "", "", "No pins are selected for transmission")
            End If
        End If
                  
        If r.v("UART1_RxPin_P0.5_Used") = 1 Then
            Print #f, FormatComments(2, 1, "", "", "Pin RXD1_0 (P0.5) is selected for reception")
        Else
        
            If r.v("UART1_RxPin_P3.2_Used") = 1 Then
                Print #f, FormatComments(2, 1, "", "", "Pin RXD1_1 (P3.2) is selected for reception")
            Else
                Print #f, FormatComments(2, 1, "", "", "No pins are selected for reception")
            End If
        End If
        
        If (r.v(UART1_PREFIX & "SCON_REN_1") = 1) Then
            Print #f, FormatComments(2, 1, "", "", "Receiver enabled")
        Else
            Print #f, FormatComments(2, 1, "", "", "Receiver disabled")
        End If
        
        If (r.v("UART1_Interrupt_Select") = 1) Then
          Print #f, FormatComments(2, 1, "", "", "UART1 interrupt is enabled")
        Else
          Print #f, FormatComments(2, 1, "", "", "Ignore UART1 interrupt")
        End If
        
        
        If (r.v("BRG1_Interrupt_Select") = 1) Then
          Print #f, FormatComments(2, 1, "", "", "Normal divider overflow interrupt is enabled")
        Else
          Print #f, FormatComments(2, 1, "", "", "Ignore Normal divider overflow interrupt")
        End If
        



      Select Case GetUartMode(r)
        Case 0
            Print #f, FormatComments(2, 1, "", "", "Mode 0: 8-bit shift register, fixed baud rate")
            Print #f, FormatComments(2, 1, "", "", "Baud rate = " & Format(UartBaudrateMode0(r), "0.00") & " kBaud")
            If r.v("UART1_RXDO1_0_Pin_P0.3_Used") = 1 Then
                Print #f, FormatComments(2, 1, "", "", "Pin P0.3 (RXDO1_0) is selected for RXDO1")
            ElseIf r.v("UART1_RXDO1_1_Pin_P3.0_Used") = 1 Then
                Print #f, FormatComments(2, 1, "", "", "Pin P3.0 (RXDO1_1) is selected for RXDO1")
            End If
            
        Case 1
            Print #f, FormatComments(2, 1, "", "", "Mode 1: 8-bit data, 1 start bit, 1 stop bit, variable baud rate")
          If (r.v(UART1_PREFIX & "SCON_SM2_1") = 1) Then
            Print #f, FormatComments(2, 1, "", "", "Receiver interrupt flag RI_1 will only be activated if a valid stop bit was received")
          End If
          If r.v(BRG_PREFIX & "FDCON_BGS") = 0 Then
                Print #f, FormatComments(2, 1, "", "", "BRG is selected for baudrate generation")
            Else
'                Print #f, FormatComments(2, 1, "", "", "Timer1 is selected for baudrate generation")
'                Print #f, FormatComments(2, 1, "", "", "Baud rate = " & Format(GetTimer1Baudrate(r), "0.00") & " baud");
'                Print #f, ""      Since Timer1 is not used in UART1
            End If
          
        Case 2
            Print #f, FormatComments(2, 1, "", "", "Mode 2: 9-bit data, 1 start bit, 1 stop bit, fixed baud rate")
            Print #f, FormatComments(2, 1, "", "", "Baud rate = " & Format(UartBaudrateMode2(r), "0.00") & " kBaud")
            
        Case 3
            Print #f, FormatComments(2, 1, "", "", "Mode 3: 9-bit data, 1 start bit, 1 stop bit, variable baud rate")
            If r.v(BRG_PREFIX & "FDCON_BGS") = 0 Then
                Print #f, FormatComments(2, 1, "", "", "BRG is selected for baudrate generation")
            Else
'                Print #f, FormatComments(2, 1, "", "", "Timer1 is selected for baudrate generation")
'                Print #f, FormatComments(2, 1, "", "", "Baud rate = " & Format(GetTimer1Baudrate(r), "0.00") & " baud");
'                Print #f, ""    Since Timer1 is not used in UART1
            End If
            
      End Select

        If (IsMultiComMode(r)) Then
            Print #f, FormatComments(2, 1, "", "", "Multiprocessor communication enabled")
        End If

      '-------------------------------------------------
      ' Initialization code
      '-------------------------------------------------
      If (r.v("UART1_TxPin_P0.4_Used") = 1 Or r.v("UART1_TxPin_P3.1_Used") = 1 Or r.v("UART1_RxPin_P0.5_Used") = 1 Or r.v("UART1_RxPin_P3.2_Used") = 1) Then
          Print #f, ""
                   
          If GetUartMode(r) = 0 And r.v("UART1_RXDO1_0_Pin_P0.3_Used") = 1 And r.v("UART1_TxPin_P0.4_Used") = 1 Then
                Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & PORT_PAGE_NAME & "2, noSST);", "", "", "switch to page 2 without saving")
                Print #f, FormatCodeStatemente(2, "P0_ALTSEL0", "|=", "(ubyte)0x18", "configure alternate function register 0")
                Print #f, FormatCodeStatemente(2, "P0_ALTSEL1", "|=", "(ubyte)0x18", "configure alternate function register 1")
                Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & PORT_PAGE_NAME & "0, noSST);", "", "", "switch to page 0 without saving")
                Print #f, FormatCodeStatemente(2, "P0_DIR", "|=", "(ubyte)0x18", "set output direction")
          ElseIf r.v("UART1_TxPin_P0.4_Used") = 1 Then
                Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & PORT_PAGE_NAME & "2, noSST);", "", "", "switch to page 2 without saving")
                Print #f, FormatCodeStatemente(2, "P0_ALTSEL0", "|=", "(ubyte)0x10", "configure alternate function register 0")
                Print #f, FormatCodeStatemente(2, "P0_ALTSEL1", "|=", "(ubyte)0x10", "configure alternate function register 1")
                Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & PORT_PAGE_NAME & "0, noSST);", "", "", "switch to page 0 without saving")
                Print #f, FormatCodeStatemente(2, "P0_DIR", "|=", "(ubyte)0x10", "set output direction")
          End If
        
        
          If GetUartMode(r) = 0 And r.v("UART1_RXDO1_1_Pin_P3.0_Used") = 1 And r.v("UART1_TxPin_P3.1_Used") = 1 Then
                Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & PORT_PAGE_NAME & "2, noSST);", "", "", "switch to page 2 without saving")
                Print #f, FormatCodeStatemente(2, "P3_ALTSEL0", "|=", "(ubyte)0x03", "configure alternate function register 0")
                Print #f, FormatCodeStatemente(2, "P3_ALTSEL1", "|=", "(ubyte)0x03", "configure alternate function register 1")
                Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & PORT_PAGE_NAME & "0, noSST);", "", "", "switch to page 0 without saving")
                Print #f, FormatCodeStatemente(2, "P3_DIR", "|=", "(ubyte)0x03", "set output direction")
          ElseIf r.v("UART1_TxPin_P3.1_Used") = 1 Then
                Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & PORT_PAGE_NAME & "2, noSST);", "", "", "switch to page 2 without saving")
                Print #f, FormatCodeStatemente(2, "P3_ALTSEL0", "|=", "(ubyte)0x02", "configure alternate function register 0")
                Print #f, FormatCodeStatemente(2, "P3_ALTSEL1", "|=", "(ubyte)0x02", "configure alternate function register 1")
                Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & PORT_PAGE_NAME & "0, noSST);", "", "", "switch to page 0 without saving")
                Print #f, FormatCodeStatemente(2, "P3_DIR", "|=", "(ubyte)0x02", "set output direction")
          End If
          
          Print #f, ""
          
        If GetUartMode(r) = 0 Then
            If r.v("UART1_RXDO1_0_Pin_P0.3_Used") = 1 And Not r.v("UART1_TxPin_P0.4_Used") = 1 Then
                
                    Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & PORT_PAGE_NAME & "2, noSST);", "", "", "switch to page 2 without saving")
                    Print #f, FormatCodeStatemente(2, "P0_ALTSEL0", "|=", "(ubyte)0x08", "configure alternate function register 0")
                    Print #f, FormatCodeStatemente(2, "P0_ALTSEL1", "|=", "(ubyte)0x08", "configure alternate function register 1")
                    Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & PORT_PAGE_NAME & "0, noSST);", "", "", "switch to page 0 without saving")
                    Print #f, FormatCodeStatemente(2, "P0_DIR", "|=", "(ubyte)0x08", "set output direction")
            End If
            
            If r.v("UART1_RXDO1_1_Pin_P3.0_Used") = 1 And Not r.v("UART1_TxPin_P3.1_Used") = 1 Then
                    Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & PORT_PAGE_NAME & "2, noSST);", "", "", "switch to page 2 without saving")
                    Print #f, FormatCodeStatemente(2, "P3_ALTSEL0", "|=", "(ubyte)0x01", "configure alternate function register 0")
                    Print #f, FormatCodeStatemente(2, "P3_ALTSEL1", "|=", "(ubyte)0x01", "configure alternate function register 1")
                    Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & PORT_PAGE_NAME & "0, noSST);", "", "", "switch to page 0 without saving")
                    Print #f, FormatCodeStatemente(2, "P3_DIR", "|=", "(ubyte)0x01", "set output direction")
            End If
        
        End If
          
          
          
          If r.v("UART1_RxPin_P3.2_Used") = 1 Then
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "3, noSST);", "", "", "switch to page 3 without saving")
            Print #f, FormatCodeStatemente(2, SCU_PREFIX & "MODPISEL1", "|=", "(ubyte)0x08", "configure peripheral input select register")
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, noSST);", "", "", "switch to page 0 without saving")
          End If
          
          If r.v("UART1_RxPin_P0.5_Used") = 1 Then
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "3, noSST);", "", "", "switch to page 3 without saving")
            Print #f, FormatCodeStatemente(2, SCU_PREFIX & "MODPISEL1", "&=", "~(ubyte)0x08", "configure peripheral input select register")
          
            Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, noSST);", "", "", "switch to page 0 without saving")
          End If
      End If
      
      
      
      ' statement for SCON
      v = r.sfr(UART1_PREFIX & "SCON")

      ' The TI bit (Tx interrupt) is set only _after_ transmission of a character.
      ' In order to use this bit for polling of the status "transmitter is ready to send a character",
      ' the initial state=ready must be set by software:
      ' set TI bit only in case of polling (not when TI=1 generates an interrupt request)
      'If (r.v(CORE_PREFIX & "IEN0_ES") = 0) Then
      '    v = v + BitMask(r, "SCON", "TI")
      'End If


      If (GetUartMode(r) = 0) Then
            Print #f, FormatComments(2, 1, "", "", "Bit RI_1 is set in order to disable reception")
        v = v + BitMask(r, UART1_PREFIX & "SCON", "RI_1")
      End If
      
      
       ' -------------------- Set RMAP here
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SET_RMAP();", "", "", "")
            Print #f, ""
             
      
      
      
      
      
            Print #f, FormatCodeStatemente(2, SfrName(r, UART1_PREFIX & "BCON"), "=", FormatTextOutput(0, "H2"), "reset baudrate timer/reload register")
            Print #f, FormatCodeStatemente(2, SfrName(r, UART1_PREFIX & "SCON"), "=", FormatTextOutput(v, "H2"), "load serial channel control register")
            Print #f, ""

      ' statement for PCON --------------------------------------------------
'      If (r.v(UART_PREFIX & "PCON_SMOD") = 1) Then
'            Print #f, FormatComments(2, 0, "", "", "Double baud rate")
'            Print #f, "  " & SfrBitStatement(r, UART_PREFIX & "PCON_SMOD", 1)
'            Print #f, ""
'      End If
'    Since double baudrate option is not used in UART1
    ' baudrate -----------------------------------------
    
        
        
        Select Case GetUartMode(r)
          Case 0
                
                
          Case 2
                
                
          Case 1, 3
            
            If r.v(BRG_PREFIX & "FDCON_BGS") = 0 Then
                If r.v(UART1_PREFIX & "BCON_R") = 0 Then
                    Print #f, FormatComments(2, 1, "", "", "Baudrate generator is not selected")
                Else
                    Print #f, FormatSectionHeader(2, 1, "Baudrate generator settings")
                    If r.l(UART1_PREFIX & "BCON_BRPRE") = 0 Then
                        Print #f, FormatComments(2, 1, "", "", "input clock = " & "fPCLK")
                    Else
                        Print #f, FormatComments(2, 1, "", "", "input clock = " & "fPCLK/" & 2 ^ r.l(UART1_PREFIX & "BCON_BRPRE"))
                    End If
                    
                    If r.l(UART1_PREFIX & "FDCON_FDEN") = 1 Then
                        Print #f, FormatComments(2, 1, "", "", "Fractional divider is enabled")
                    Else
                        Print #f, FormatComments(2, 1, "", "", "Fractional divider is disabled")
                    End If
                    
                    If r.l(UART1_PREFIX & "FDCON_FDEN") = 1 And r.l(UART1_PREFIX & "FDSTEP_STEP") = 0 Then
                        Print #f, FormatComments(2, 1, "", "", "Baudrate generation is not possible because STEP value is 0")
                    Else
                        Baudrate = ConvertBaudrate(GetActualUartBaudrate(r))
                        d = Format(Baudrate.dValue, "0.0000")
                        Print #f, FormatComments(2, 1, "", "", "baudrate = " & d & " " & Baudrate.sUnit)
                    
                        Print #f, ""
                    
                        If r.l(UART1_PREFIX & "FDCON_FDEN") = 1 Then
                            Print #f, FormatCodeStatemente(2, SfrName(r, UART1_PREFIX & "FDSTEP"), "=", FormatTextOutput(r.sfr(UART1_PREFIX & "FDSTEP"), "H2"), "load fractional divider reload register")
                        End If
                        Print #f, FormatCodeStatemente(2, SfrName(r, UART1_PREFIX & "BG"), "=", FormatTextOutput(r.sfr(UART1_PREFIX & "BG"), "H2"), "load baudrate timer/reload register")
                        If r.sfr(UART1_PREFIX & "FDCON") <> 0 Then
                            Print #f, FormatCodeStatemente(2, SfrName(r, UART1_PREFIX & "FDCON"), "|=", FormatTextOutput(r.sfr(UART1_PREFIX & "FDCON"), "H2"), "load Fractional Divider control register")
                        End If
                        Print #f, FormatCodeStatemente(2, SfrName(r, UART1_PREFIX & "BCON"), "|=", FormatTextOutput(r.sfr(UART1_PREFIX & "BCON"), "H2"), "load baud rate control register")
                    End If
                    
                    Print #f, ""
                End If
            End If
    
        End Select
    
    
     ' -------------------- ReSet RMAP here
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "RESET_RMAP();", "", "", "")
            Print #f, ""
            
    
    
    
    
    
    
    
    Else
        Print #f, ""
        Print #f, FormatComments(2, 1, "", "", "UART1 is not configured")
        Print #f, ""
    End If
    
        If r.v(UART1_PREFIX & "FDCON_FDM") = 1 Then
            
            Print #f, FormatSectionHeader(2, 1, "BRG settings (Normal Divider Mode)")
            If r.v(UART1_PREFIX & "FDCON_FDEN") = 1 Then
                Print #f, FormatComments(2, 1, "", "", "timer run control is selected")
            End If
            ' Prescaler, Reload Value and overflow rate
            If r.l(UART1_PREFIX & "BCON_BRPRE") = 0 Then
                        Print #f, FormatComments(2, 1, "", "", "input clock = " & "fPCLK")
                    Else
                        Print #f, FormatComments(2, 1, "", "", "input clock = " & "fPCLK/" & 2 ^ r.l(UART1_PREFIX & "BCON_BRPRE"))
            End If
            
            ' -------------------- Set RMAP here
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "SET_RMAP();", "", "", "")
            Print #f, ""
             
            
            
            
            
            
            
            
            Print #f, FormatComments(2, 1, "", "", "the timer overflow is " & GetBRGTmrOverflow(r) & " " & TXT_US)
            
            Print #f, FormatCodeStatemente(2, SfrName(r, UART1_PREFIX & "FDCON"), "|=", FormatTextOutput(r.sfr(UART1_PREFIX & "FDCON"), "H2"), "load Fractional Divider control register")
            Print #f, FormatCodeStatemente(2, SfrName(r, UART1_PREFIX & "FDSTEP"), "=", FormatTextOutput(r.sfr(UART1_PREFIX & "FDSTEP"), "H2"), "load fractional divider reload register")
            
            
             ' -------------------- ReSet RMAP here
            Print #f, ""
            Print #f, FormatCodeStatemente(2, "RESET_RMAP();", "", "", "")
            Print #f, ""
            
            ' Timer interrupt
            
            If (r.v("BRG1_Interrupt_Select") = 1) Then
                Print #f, ""
                Print #f, FormatComments(2, 1, "", "", "BRG timer overflow interrupt is enabled")
                'If (r.v(CORE_PREFIX & "IEN1_EX2") <> 1) Then
                 'If (r.v("UART1_Interrupt_Select") <> 1) Then
                    'Print #f, FormatCodeStatemente(2, SfrBitStatement(r, CORE_PREFIX & "IEN1_EX2", 1), "", "", "Enable interrupt")
                   Print #f, FormatComments(2, 1, "", "", "UART1 BRG Interrupt enable bit is set in SHINT_vInit() function")
                'End If
                
            End If
        ElseIf r.v(BRG_PREFIX & "FDCON_BGS") = 1 Then
            Print #f, FormatCodeStatemente(2, SfrName(r, UART1_PREFIX & "FDCON"), "|=", FormatTextOutput(r.sfr(UART1_PREFIX & "FDCON"), "H2"), "load Fractional Divider control register")
        End If
        
        
            Print #f, ""

            Print #f, uc.GetUserCode("(UART1_Init,3)", 2)
            Print #f, ""


      ' statment for EX2 ----------------------------------------------------
      If (r.v(CORE_PREFIX & "IEN1_EX2") = 1) Then
       'If (r.v("UART1_Interrupt_Select") = 1) Then
      
       'Added by jag on 6/4/6 in order to generate if we deselect init function of T21 or IR.
       'If (r.v("UART1_Interrupt_Select") = 1) And ((r.v("T21_Interrupt_Select") <> 1) Or (r.v("T21_USE_Init") = 0) And ((r.v(CORE_PREFIX & "IEN1_EX2") <> 1) Or (r.v("IR_USE_Init") = 0))) Then
'            Print #f, FormatComments(2, 1, "", "", "UART1 interrupt enabled")
'            Print #f, "  " & SfrBitStatement(r, CORE_PREFIX & "IEN1_EX2", 1)
             Print #f, FormatComments(2, 1, "", "", "UART1 Interrupt enable bit is set in SHINT_vInit() function")
             Print #f, ""
      End If

            Print #f, "} " & END_OF_FUNCTION & r.v("UART1_NAME_Init")
            Print #f, ""
            Print #f, ""
      End If

    '------------------------------------------------------------------------------------------------
    ' UART_Isr function
    '------------------------------------------------------------------------------------------------
'    If (r.v("UART_USE_Isr") = 1) Then
'    Print #f, FormatFunctionHeader(r, func, "UART_NAME_Isr")
'            Print #f, uc.GetUserCode("(UART_Isr,1)", 0) 'USER CODE for application specific descriptions'
'            Print #f, ""
'                      '<--                     max. 78 characters                                -->'
'            Print #f, IsrDeclaration(r, r.v("UART_NAME_Isr"), "UARTINT")
'            Print #f, "{"
'            Print #f, "  if (" & SfrBitTest(r, UART_PREFIX & "SCON_TI") & ")"
'            Print #f, "  {"
'            Print #f, uc.GetUserCode("(UART_Isr,2)", 4)
'            Print #f, "    " & SfrBitStatement(r, UART_PREFIX & "SCON_TI", 0)
'            Print #f, "  }"
'            Print #f, "  if (" & SfrBitTest(r, UART_PREFIX & "SCON_RI") & ")"
'            Print #f, "  {"
'            Print #f, uc.GetUserCode("(UART_Isr,3)", 4)
'
'        If (GetUartMode(r) <> 0) Then
'            Print #f, "    " & SfrBitStatement(r, UART_PREFIX & "SCON_RI", 0)
'        End If
'
'            Print #f, "  }"
'            Print #f, ""
'            Print #f, "} " & END_OF_FUNCTION & r.v("UART_NAME_Isr")
'            Print #f, ""
'            Print #f, ""
'    End If
'    If (r.v("UART1_USE_Isr") = 1) Then
'       If (r.v("ISR_CHOICE") = 1) Then
'            Print #f, FormatFunctionHeader(r, func, "UART1_NAME_Isr")
'            Print #f, uc.GetUserCode("(UART1_Isr,1)", 0) 'USER CODE for application specific descriptions'
'            Print #f, ""
'                      '<--                     max. 78 characters                                -->'
'            Print #f, IsrDeclaration(r, r.v("UART1_NAME_Isr"), "UART1INT")
'            Print #f, "{"
'
'            Print #f, ""
'            Print #f, FormatCodeStatemente(2, "_push_(SYSCON0);", "", "", "")
'            Print #f, FormatCodeStatemente(2, "SET_RMAP();", "", "", "")
'            Print #f, ""
'
'            If (r.v(UART1_PREFIX & "SCON1_TIEN") = 1) Then
'                Print #f, "  if (" & SfrBitTest(r, UART1_PREFIX & "SCON_TI_1") & ")"
'                Print #f, "  {"
'                Print #f, uc.GetUserCode("(UART1_Isr,2)", 4)
'                Print #f, "    " & SfrBitStatement(r, UART1_PREFIX & "SCON_TI_1", 0)
'                Print #f, "  }"
'            End If
'            If (r.v(UART1_PREFIX & "SCON1_RIEN") = 1) Then
'                Print #f, "  if (" & SfrBitTest(r, UART1_PREFIX & "SCON_RI_1") & ")"
'                Print #f, "  {"
'                Print #f, uc.GetUserCode("(UAR1_Isr,3)", 4)
'                If (GetUartMode(r) <> 0) Then
'                   Print #f, "    " & SfrBitStatement(r, UART1_PREFIX & "SCON_RI_1", 0)
'                End If
'                Print #f, "  }"
'                Print #f, ""
'            End If
'
'            Print #f, ""
'            Print #f, FormatCodeStatemente(2, "RESET_RMAP();", "", "", "")
'            Print #f, FormatCodeStatemente(2, "_pop_(SYSCON0);", "", "", "")
'            Print #f, ""
'
'
'            Print #f, "} " & END_OF_FUNCTION & r.v("UART1_NAME_Isr")
'            Print #f, ""
'            Print #f, ""
'       ElseIf (r.v("ISR_CHOICE") = 2) Then
'            Print #f, FormatFunctionHeader(r, func, "UART1_NAME_Isr")
'            Print #f, uc.GetUserCode("(UART1_Isr,1)", 0) 'USER CODE for application specific descriptions'
'            Print #f, ""
'                      '<--                     max. 78 characters                                -->'
'            Print #f, IsrDeclaration(r, r.v("UART1_NAME_Isr"), "UART1INT")
'            Print #f, "{"
'
'            Print #f, FormatCodeStatemente(2, "EA = 0;", "", "", "disable global interrupt")
'            Print #f, FormatCodeStatemente(2, "_push_(SYSCON0);", "", "", "")
'            Print #f, FormatCodeStatemente(2, "SET_RMAP();", "", "", "")
'            Print #f, ""
'
'
'            If (r.v("UART1_PREFIX_SCON1_TIEN") = 1) Then
'                Print #f, "  if (" & SfrBitTest(r, UART1_PREFIX & "SCON_TI_1") & ")"
'                Print #f, "  {"
'                Print #f, uc.GetUserCode("(UART1_Isr,2)", 4)
'                Print #f, "    " & SfrBitStatement(r, UART1_PREFIX & "SCON_TI_1", 0)
'                Print #f, "  }"
'            End If
'            If (r.v("UART1_PREFIX_SCON1_RIEN") = 1) Then
'                Print #f, "  if (" & SfrBitTest(r, UART1_PREFIX & "SCON_RI_1") & ")"
'                Print #f, "  {"
'                Print #f, uc.GetUserCode("(UAR1_Isr,3)", 4)
'                If (GetUartMode(r) <> 0) Then
'                   Print #f, "    " & SfrBitStatement(r, UART1_PREFIX & "SCON_RI_1", 0)
'                End If
'
'                Print #f, "  }"
'                Print #f, ""
'            End If
'
'            Print #f, FormatCodeStatemente(2, "_pop_(SYSCON0);", "", "", "")
'            Print #f, FormatCodeStatemente(2, "EA = 1;", "", "", "enable global interrupt")
'            Print #f, ""
'
'            Print #f, "} " & END_OF_FUNCTION & r.v("UART1_NAME_Isr")
'            Print #f, ""
'            Print #f, ""
'
'       End If
'    End If



    '------------------------------------------------------------------------------------------------
    ' UART_GetData8 function
    '------------------------------------------------------------------------------------------------
    If (r.v("UART1_USE_GetData8") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART1_NAME_GetData8")
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "UART1_NAME_GetData8")
            Print #f, "{"
            Print #f, ""
            Print #f, "  ubyte ubData;"
            Print #f, ""
            'Print #f, FormatCodeStatemente(2, "_push_(SYSCON0);", "", "", "")
            
            If (r.v("MAIN_COMPILER") = "SDCC") Then
                Print #f, FormatCodeStatemente(2, "pushsyscon();", "", "", "push the current RMAP")
            ElseIf (r.v("MAIN_COMPILER") = "Keil") Then
                Print #f, FormatCodeStatemente(2, "_push_(SYSCON0);", "", "", "push the current RMAP")
            End If
            Print #f, FormatCodeStatemente(2, "SET_RMAP();", "", "", "")
            Print #f, ""
       
        If (GetUartMode(r) <> 0) Then
            Print #f, FormatComments(2, 0, "", "", "Clear the receiver interrupt flag")
            Print #f, FormatCodeStatemente(2, UART1_PREFIX & "SCON", "&=", "~(ubyte)0x01", "")
            Print #f, ""
        End If

            Print #f, ""
            Print #f, FormatComments(2, 0, "", "", "Read the received data bits 0..7")
            Print #f, "  ubData = (ubyte)" & SfrName(r, UART1_PREFIX & "SBUF") & ";"
            Print #f, ""
            Print #f, ""
            'Print #f, FormatCodeStatemente(2, "RESET_RMAP();", "", "", "")
            'Print #f, FormatCodeStatemente(2, "_pop_(SYSCON0);", "", "", "")
            If (r.v("MAIN_COMPILER") = "SDCC") Then
                Print #f, FormatCodeStatemente(2, "popsyscon();", "", "", "restore the old RMAP")
            ElseIf (r.v("MAIN_COMPILER") = "Keil") Then
                Print #f, FormatCodeStatemente(2, "_pop_(SYSCON0);", "", "", "restore the old RMAP")
            End If
            
            Print #f, ""
            Print #f, FormatComments(2, 0, "", "", "Return the received data byte")
            Print #f, "  return(ubData);"
            Print #f, ""
            
            Print #f, "} " & END_OF_FUNCTION & r.v("UART1_NAME_GetData8")
            Print #f, ""
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' UART_SendData8 function
    '------------------------------------------------------------------------------------------------
    If (r.v("UART1_USE_SendData8") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART1_NAME_SendData8")
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "UART1_NAME_SendData8")
            Print #f, "{"
            
            Print #f, ""
            'Print #f, FormatCodeStatemente(2, "_push_(SYSCON0);", "", "", "")
            
            If (r.v("MAIN_COMPILER") = "SDCC") Then
                Print #f, FormatCodeStatemente(2, "pushsyscon();", "", "", "push the current RMAP")
            ElseIf (r.v("MAIN_COMPILER") = "Keil") Then
                Print #f, FormatCodeStatemente(2, "_push_(SYSCON0);", "", "", "push the current RMAP")
            End If
            Print #f, FormatCodeStatemente(2, "SET_RMAP();", "", "", "")
            Print #f, ""
       
            Print #f, FormatComments(2, 0, "", "", "Clear the transmitter interrupt flag")
            Print #f, FormatCodeStatemente(2, UART1_PREFIX & "SCON", "&=", "~(ubyte)0x02", "")
            Print #f, ""

        If (IsMultiComMode(r)) Then
            Print #f, FormatComments(2, 0, "", "", "Set bit 8 to '0'")
            Print #f, FormatCodeStatemente(2, UART1_PREFIX & "SCON", "&=", "~(ubyte)0x08", "")
            Print #f, ""
        End If

            Print #f, FormatComments(2, 0, "", "", "Write the transmit data byte, this initiates the transmission.")
            Print #f, "  " & SfrName(r, UART1_PREFIX & "SBUF") & " = ubData;"
            
            Print #f, ""
            'Print #f, FormatCodeStatemente(2, "RESET_RMAP();", "", "", "")
            'Print #f, FormatCodeStatemente(2, "_pop_(SYSCON0);", "", "", "")
            
            If (r.v("MAIN_COMPILER") = "SDCC") Then
                Print #f, FormatCodeStatemente(2, "popsyscon();", "", "", "restore the old RMAP")
            ElseIf (r.v("MAIN_COMPILER") = "Keil") Then
                Print #f, FormatCodeStatemente(2, "_pop_(SYSCON0);", "", "", "restore the old RMAP")
            End If
            Print #f, ""
            
            Print #f, "} " & END_OF_FUNCTION & r.v("UART1_NAME_SendData8")
            Print #f, ""
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' UART_GetData9 function
    '------------------------------------------------------------------------------------------------
    If (r.v("UART1_USE_GetData9") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART1_NAME_GetData9")
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "UART1_NAME_GetData9")
            Print #f, "{"
            
            Print #f, ""
            Print #f, "  uword uwData;"
            Print #f, ""
            
            'Print #f, FormatCodeStatemente(2, "_push_(SYSCON0);", "", "", "")
            
            If (r.v("MAIN_COMPILER") = "SDCC") Then
                Print #f, FormatCodeStatemente(2, "pushsyscon();", "", "", "push the current RMAP")
            ElseIf (r.v("MAIN_COMPILER") = "Keil") Then
                Print #f, FormatCodeStatemente(2, "_push_(SYSCON0);", "", "", "push the current RMAP")
            End If
              
            Print #f, FormatCodeStatemente(2, "SET_RMAP();", "", "", "")
            Print #f, ""
           
            Print #f, FormatComments(2, 0, "", "", "Clear the receiver interrupt flag")
            Print #f, FormatCodeStatemente(2, UART1_PREFIX & "SCON", "&=", "~(ubyte)0x01", "")
            Print #f, ""
            Print #f, FormatComments(2, 0, "", "", "Read the received data bits 0..7")
            Print #f, "  uwData = (uword)" & SfrName(r, UART1_PREFIX & "SBUF") & ";"
            Print #f, ""
            Print #f, FormatComments(2, 0, "", "", "Read the received data bit 8")
            Print #f, "  if (" & SfrBitTest(r, UART1_PREFIX & "SCON_RB8_1") & ")"
            Print #f, "  {"
            Print #f, "    uwData |= 0x0100;"
            Print #f, "  }"
            Print #f, ""
            
            Print #f, ""
            'Print #f, FormatCodeStatemente(2, "RESET_RMAP();", "", "", "")
            'Print #f, FormatCodeStatemente(2, "_pop_(SYSCON0);", "", "", "")
            
            If (r.v("MAIN_COMPILER") = "SDCC") Then
                Print #f, FormatCodeStatemente(2, "popsyscon();", "", "", "restore the old RMAP")
            ElseIf (r.v("MAIN_COMPILER") = "Keil") Then
                Print #f, FormatCodeStatemente(2, "_pop_(SYSCON0);", "", "", "restore the old RMAP")
            End If
            
            Print #f, ""
            
            Print #f, "  return(uwData);"
            
            
           
            Print #f, "} " & END_OF_FUNCTION & r.v("UART1_NAME_GetData9")
            Print #f, ""
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' UART_SendData9 function
    '------------------------------------------------------------------------------------------------
    If (r.v("UART1_USE_SendData9") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART1_NAME_SendData9")
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "UART1_NAME_SendData9")
            Print #f, "{"
            
            Print #f, ""
            
            'Print #f, FormatCodeStatemente(2, "_push_(SYSCON0);", "", "", "")
            
            If (r.v("MAIN_COMPILER") = "SDCC") Then
                Print #f, FormatCodeStatemente(2, "pushsyscon();", "", "", "push the current RMAP")
            ElseIf (r.v("MAIN_COMPILER") = "Keil") Then
                Print #f, FormatCodeStatemente(2, "_push_(SYSCON0);", "", "", "push the current RMAP")
            End If
            
            
            Print #f, FormatCodeStatemente(2, "SET_RMAP();", "", "", "")
            Print #f, ""
        
            Print #f, FormatComments(2, 0, "", "", "Clear the transmitter interrupt flag")
            Print #f, FormatCodeStatemente(2, UART1_PREFIX & "SCON", "&=", "~(ubyte)0x02", "")
            Print #f, ""
            Print #f, FormatComments(2, 0, "", "", "Write the transmit data bit 8")
            Print #f, "  if (uwData & 0x0100)"
            Print #f, "  {"
            Print #f, FormatCodeStatemente(4, UART1_PREFIX & "SCON", "|=", "(ubyte)0x08", "")
            Print #f, "  }"
            Print #f, "  else"
            Print #f, "  {"
            Print #f, FormatCodeStatemente(4, UART1_PREFIX & "SCON", "&=", "~(ubyte)0x08", "")
            Print #f, "  }"
            Print #f, ""
            Print #f, FormatComments(2, 0, "", "", "Write the transmit data bits 0..7, this initiates the transmission")
            Print #f, "  " & SfrName(r, UART1_PREFIX & "SBUF") & " = (ubyte)uwData;"
            
            Print #f, ""
            'Print #f, FormatCodeStatemente(2, "RESET_RMAP();", "", "", "")
            
            'Print #f, FormatCodeStatemente(2, "_pop_(SYSCON0);", "", "", "")
            
            If (r.v("MAIN_COMPILER") = "SDCC") Then
                Print #f, FormatCodeStatemente(2, "popsyscon();", "", "", "restore the old RMAP")
            ElseIf (r.v("MAIN_COMPILER") = "Keil") Then
                Print #f, FormatCodeStatemente(2, "_pop_(SYSCON0);", "", "", "restore the old RMAP")
            End If
            
            
            Print #f, ""
            
            
            Print #f, "} " & END_OF_FUNCTION & r.v("UART1_NAME_SendData9")
            Print #f, ""
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' UART_SendAddr function
    '------------------------------------------------------------------------------------------------
    If (r.v("UART1_USE_SendAddr") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART1_NAME_SendAddr")
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "UART1_NAME_SendAddr")
            Print #f, "{"
            
            Print #f, ""
            'Print #f, FormatCodeStatemente(2, "_push_(SYSCON0);", "", "", "")
            
            If (r.v("MAIN_COMPILER") = "SDCC") Then
                Print #f, FormatCodeStatemente(2, "pushsyscon();", "", "", "push the current RMAP")
            ElseIf (r.v("MAIN_COMPILER") = "Keil") Then
                Print #f, FormatCodeStatemente(2, "_push_(SYSCON0);", "", "", "push the current RMAP")
            End If
              
            Print #f, FormatCodeStatemente(2, "SET_RMAP();", "", "", "")
            Print #f, ""
            
            Print #f, FormatComments(2, 0, "", "", "Clear the transmitter interrupt flag")
            Print #f, FormatCodeStatemente(2, UART1_PREFIX & "SCON", "&=", "~(ubyte)0x02", "")
            Print #f, ""
            Print #f, FormatComments(2, 0, "", "", "Set bit 8 to '1'")
            Print #f, FormatCodeStatemente(2, UART1_PREFIX & "SCON", "|=", "(ubyte)0x08", "")
            Print #f, ""
            Print #f, FormatComments(2, 0, "", "", "Write the transmit data byte, this initiates the transmission.")
            Print #f, "  " & SfrName(r, UART1_PREFIX & "SBUF") & " = ubAddress;"
            
            Print #f, ""
            'Print #f, FormatCodeStatemente(2, "RESET_RMAP();", "", "", "")
            
            'Print #f, FormatCodeStatemente(2, "_pop_(SYSCON0);", "", "", "")
            
            If (r.v("MAIN_COMPILER") = "SDCC") Then
                Print #f, FormatCodeStatemente(2, "popsyscon();", "", "", "restore the old RMAP")
            ElseIf (r.v("MAIN_COMPILER") = "Keil") Then
                Print #f, FormatCodeStatemente(2, "_pop_(SYSCON0);", "", "", "restore the old RMAP")
            End If
            
            Print #f, ""
            
            Print #f, "} " & END_OF_FUNCTION & r.v("UART1_NAME_SendAddr")
            Print #f, ""
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' UART_OwnAddress function
    '------------------------------------------------------------------------------------------------
    If (r.v("UART1_USE_OwnAddress") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART1_NAME_OwnAddress")
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "UART1_NAME_OwnAddress")
            Print #f, "{"
            
            Print #f, ""
            Print #f, "  ubyte ubData;"
            Print #f, ""
            
            'Print #f, FormatCodeStatemente(2, "_push_(SYSCON0);", "", "", "")
            
            If (r.v("MAIN_COMPILER") = "SDCC") Then
                Print #f, FormatCodeStatemente(2, "pushsyscon();", "", "", "push the current RMAP")
            ElseIf (r.v("MAIN_COMPILER") = "Keil") Then
                Print #f, FormatCodeStatemente(2, "_push_(SYSCON0);", "", "", "push the current RMAP")
            End If
            
            
            Print #f, FormatCodeStatemente(2, "SET_RMAP();", "", "", "")
            Print #f, ""
            
            Print #f, FormatComments(2, 0, "", "", "Clear the receiver interrupt flag")
            Print #f, FormatCodeStatemente(2, UART1_PREFIX & "SCON", "&=", "~(ubyte)0x01", "")
            Print #f, ""
           
            Print #f, FormatComments(2, 0, "", "", "Read the received data byte")
            Print #f, "  ubData = (ubyte)" & SfrName(r, UART1_PREFIX & "SBUF") & ";"
            Print #f, ""
            'Print #f, FormatCodeStatemente(2, "RESET_RMAP();", "", "", "")
            
            'Print #f, FormatCodeStatemente(2, "_pop_(SYSCON0);", "", "", "")
            
            If (r.v("MAIN_COMPILER") = "SDCC") Then
                Print #f, FormatCodeStatemente(2, "popsyscon();", "", "", "restore the old RMAP")
            ElseIf (r.v("MAIN_COMPILER") = "Keil") Then
                Print #f, FormatCodeStatemente(2, "_pop_(SYSCON0);", "", "", "restore the old RMAP")
            End If
            
            
            Print #f, ""
            
            Print #f, FormatComments(2, 0, "", "", "Return comparison result")
            Print #f, "  return (ubAddress == " & "ubData" & ");"
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("UART1_NAME_OwnAddress")
            Print #f, ""
            Print #f, ""
    End If
    
    '------------------------------------------------------------------------------------------------
    ' BRG_Isr function
    '------------------------------------------------------------------------------------------------
'    If ((r.v("BRG_Interrupt_Select") = 1) And (r.v(CORE_PREFIX & "IEN0_ET2") <> 1)) Then
'    Print #f, FormatFunctionHeader(r, func, "BRG_NAME_Isr")
'            Print #f, uc.GetUserCode("(BRG_Isr,1)", 0) 'USER CODE for application specific descriptions'
'            Print #f, ""
'                      '<--                     max. 78 characters                                -->'
'            Print #f, IsrDeclaration(r, "BRG_viIsr", "BRGINT")
'            Print #f, "{"
'            Print #f, "  if (" & SfrBitTest(r, BRG_PREFIX & "FDCON_NDOV") & ")"
'            Print #f, "  {"
'            Print #f, uc.GetUserCode("(BRG_Isr,2)", 4)
'            Print #f, ""
'            Print #f, "    " & SfrBitStatement(r, BRG_PREFIX & "FDCON_NDOV", 0)
'            Print #f, "  }"
'            Print #f, ""
'            Print #f, "} " & END_OF_FUNCTION & "BRG_viIsr"
'            Print #f, ""
'            Print #f, ""
'    End If

'    If ((r.v("BRG_Interrupt_Select") = 1) And (r.v(CORE_PREFIX & "IEN0_ET2") <> 1)) Then
'       If (r.v("ISR_CHOICE") = 1) Then
'            Print #f, FormatFunctionHeader(r, func, "BRG_NAME_Isr")
'            Print #f, uc.GetUserCode("(BRG_Isr,1)", 0) 'USER CODE for application specific descriptions'
'            Print #f, ""
'                      '<--                     max. 78 characters                                -->'
'            Print #f, IsrDeclaration(r, "BRG_viIsr", "BRGINT")
'            Print #f, "{"
'
'            Print #f, ""
'            Print #f, FormatCodeStatemente(2, "_push_(SYSCON0);", "", "", "")
'            Print #f, FormatCodeStatemente(2, "RESET_RMAP();", "", "", "")
'            Print #f, ""
'
'            Print #f, "  if (" & SfrBitTest(r, BRG_PREFIX & "FDCON_NDOV") & ")"
'            Print #f, "  {"
'            Print #f, uc.GetUserCode("(BRG_Isr,2)", 4)
'            Print #f, ""
'            Print #f, "    " & SfrBitStatement(r, BRG_PREFIX & "FDCON_NDOV", 0)
'            Print #f, "  }"
'            Print #f, ""
'
'            Print #f, ""
'            Print #f, FormatCodeStatemente(2, "_pop_(SYSCON0);", "", "", "")
'            Print #f, ""
'
'            Print #f, "} " & END_OF_FUNCTION & "BRG_viIsr"
'            Print #f, ""
'            Print #f, ""
'       ElseIf (r.v("ISR_CHOICE") = 2) Then
'            Print #f, FormatFunctionHeader(r, func, "BRG_NAME_Isr")
'            Print #f, uc.GetUserCode("(BRG_Isr,1)", 0) 'USER CODE for application specific descriptions'
'            Print #f, ""
'                      '<--                     max. 78 characters                                -->'
'            Print #f, IsrDeclaration(r, "BRG_viIsr", "BRGINT")
'            Print #f, "{"
'            Print #f, "  if (" & SfrBitTest(r, BRG_PREFIX & "FDCON_NDOV") & ")"
'            Print #f, "  {"
'            Print #f, uc.GetUserCode("(BRG_Isr,2)", 4)
'            Print #f, ""
'            Print #f, "    " & SfrBitStatement(r, BRG_PREFIX & "FDCON_NDOV", 0)
'            Print #f, "  }"
'            Print #f, ""
'            Print #f, "} " & END_OF_FUNCTION & "BRG_viIsr"
'            Print #f, ""
'            Print #f, ""
'       End If
'    End If

    '------------------------------------------------------------------------------------------------
            Print #f, uc.GetUserCode("(UART1_General,10)", 0)
            Print #f, ""

    'close file
    Close f

    UARTGenerateC = True
    Exit Function

UARTGenerateCError:
    Close f
    Exit Function
End Function


'*****************************************************************************
'Description:
'        Generates the code for the UART.h file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successful operation
'        False           else
'*****************************************************************************
Function UARTGenerateH(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo UARTGenerateHError
    
    Dim i As Integer
    Dim f As Variant
    Dim d As String
    Dim dc As DAvECollection
    Dim uc As UserCode
    Dim func As FunctionClass
    Dim l As Long
    Dim sName As String
   
    UARTGenerateH = False

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

    'refresh the decriptions of the functions and macros
    Call RefreshFunctionDescriptions(func, r)
    
    'read usercode
    uc.ReadFile ((StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v("UART1_HFILE"))
    
    'open UART.h
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v("UART1_HFILE") For Output As f
    
    '------------------------------------------------------------------------------------------------
    ' module header
    '------------------------------------------------------------------------------------------------
    d = FormatModuleDescription(GetModuleHeaderH("UART1"))
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), dc("UART1").ModuleDescription, r.v("UART1_HFILE"), r.v("MAIN_CPU_TYPE"), r.v("MAIN_COMPILER"), d)
            
            Print #f, uc.GetUserCode("(UART1_Header,1)", 0)
            Print #f, ""
            Print #f, Rtf.GenerateNote((r.v("UART1_Notes")))
            Print #f, ""
            Print #f, "#ifndef _" & StripExtension(r.v("UART1_HFILE")) & "_H_"
            Print #f, "#define _" & StripExtension(r.v("UART1_HFILE")) & "_H_"
            Print #f, ""
            Print #f, SECTION_PROJECT_INCLUDES
            Print #f, ""  'DAvE includes'
            Print #f, uc.GetUserCode("(UART1_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("(UART1_Header,3)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_DEFINES
            Print #f, ""  'DAvE global #define definitions'
            Print #f, uc.GetUserCode("(UART1_Header,4)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_TYPEDEFS
            Print #f, ""  'DAvE type definitions'
            Print #f, uc.GetUserCode("(UART1_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("(UART1_Header,6)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_GLOBAL_VARIABLES
            Print #f, ""  'DAvE definition of global variables'
            Print #f, uc.GetUserCode("(UART1_Header,7)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_GLOBAL_PROTOTYPES
            Print #f, ""  'DAvE declarations of functions which should become global'

    ' for each function in this 'func' object
    For l = 1 To func.FunctionCount
        ' get the function name which was insert in 'Include.inc' with 'AddFunction()'
        d = func.FunctionName(l)

        ' get only the third part of the function name (e.g "Init")
        sName = Right(d, Len(d) - InStr(d, "NAME") - Len("NAME"))

        ' only if the use variable of the function is selected and the function is a really function
        ' (not a macro or a ISR)
        If ((r.v("UART1_USE_" & sName) = 1) And (func.FunctionSort(d) = 1)) Then
            Print #f, FormatFunctionDeclaration(r, func, d) & ";"
        End If
    Next l

            Print #f, ""
            Print #f, uc.GetUserCode("(UART1_Header,8)", 0)
            Print #f, ""
            Print #f, ""
                          'DAvE declarations of global macros'
    '------------------------------------------------------------------------------------------------
    ' UART_RxEnable macro
    '------------------------------------------------------------------------------------------------
    If (r.v("UART1_USE_RxEnable") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART1_NAME_RxEnable")
            Print #f, FormatComments(0, 0, "", "", "RMAP should be set before calling this macro.")
            Print #f, ""
            Print #f, "#define " & r.v("UART1_NAME_RxEnable") & "() " & SfrBitStatement(r, UART1_PREFIX & "SCON_REN_1", 1, False)
            Print #f, ""
            Print #f, ""
    End If
  
    '------------------------------------------------------------------------------------------------
    ' UART_RxDisable macro
    '------------------------------------------------------------------------------------------------
    If (r.v("UART1_USE_RxDisable") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART1_NAME_RxDisable")
            Print #f, FormatComments(0, 0, "", "", "RMAP should be set before calling this macro.")
            Print #f, ""
            Print #f, "#define " & r.v("UART1_NAME_RxDisable") & "() " & SfrBitStatement(r, UART1_PREFIX & "SCON_REN_1", 0, False)
            Print #f, ""
            Print #f, ""
    End If
  
    '------------------------------------------------------------------------------------------------
    ' UART_RxReady macro
    '------------------------------------------------------------------------------------------------
    If (r.v("UART1_USE_RxReady") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART1_NAME_RxReady")
            Print #f, FormatComments(0, 0, "", "", "RMAP should be set before calling this macro.")
            Print #f, ""
            Print #f, "#define " & r.v("UART1_NAME_RxReady") & "() " & SfrBitTest(r, UART1_PREFIX & "SCON_RI_1")
            Print #f, ""
            Print #f, ""
    End If
  
    '------------------------------------------------------------------------------------------------
    ' UART_TxReady macro
    '------------------------------------------------------------------------------------------------
    If (r.v("UART1_USE_TxReady") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART1_NAME_TxReady")
            Print #f, FormatComments(0, 0, "", "", "RMAP should be set before calling this macro.")
            Print #f, ""
            Print #f, "#define " & r.v("UART1_NAME_TxReady") & "() " & SfrBitTest(r, UART1_PREFIX & "SCON_TI_1")
            Print #f, ""
            Print #f, ""
    End If
  
    '------------------------------------------------------------------------------------------------
    ' UART_WakeUp macro
    '------------------------------------------------------------------------------------------------
    If (r.v("UART1_USE_WakeUp") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART1_NAME_WakeUp")
            Print #f, FormatComments(0, 0, "", "", "RMAP should be set before calling this macro.")
            Print #f, ""
            Print #f, "#define " & r.v("UART1_NAME_WakeUp") & "() " & SfrBitStatement(r, UART1_PREFIX & "SCON_SM2_1", 0, False)
            Print #f, ""
            Print #f, ""
    End If
  
    '------------------------------------------------------------------------------------------------
    ' UART_GotoSleep macro
    '------------------------------------------------------------------------------------------------
    If (r.v("UART1_USE_GotoSleep") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART1_NAME_GotoSleep")
            Print #f, FormatComments(0, 0, "", "", "RMAP should be set before calling this macro.")
            Print #f, ""
            Print #f, "#define " & r.v("UART1_NAME_GotoSleep") & "() " & SfrBitStatement(r, UART1_PREFIX & "SCON_SM2_1", 1, False)
            Print #f, ""
            Print #f, ""
    End If
  
    '------------------------------------------------------------------------------------------------
            Print #f, SECTION_INTERRUPT_VECTORS
            Print #f, ""  'DAvE interrupt vector definitions
            

    If (r.v("UART1_USE_Isr") = 1) Then
            'Print #f, "#define UART1INT  " & IntNumber(&H43)
            Print #f, FormatComments(0, 0, "", "", "Shared interrupt vector definitions are defined in SHARED_INT.H")
    End If
    'If ((r.v("BRG1_Interrupt_Select") = 1) And (r.v(CORE_PREFIX & "IEN1_EX2") <> 1)) Then
     If ((r.v("BRG1_Interrupt_Select") = 1) And (r.v("UART1_Interrupt_Select") <> 1)) Then
            'Print #f, "#define BRG1INT   " & IntNumber(&H43)
            Print #f, FormatComments(0, 0, "", "", "Shared interrupt vector definitions are defined in SHARED_INT.H")
    End If
            Print #f, ""
            Print #f, uc.GetUserCode("(UART1_Header,9)", 0)
            Print #f, ""
            Print #f, "#endif  // ifndef _" & StripExtension(r.v("UART1_HFILE")) & "_H_"
    
    'close file
    Close f
    
    UARTGenerateH = True
    Exit Function
    
UARTGenerateHError:
    Close f
    Exit Function
End Function

