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:
'        UART
'-----------------------------------------------------------------------------
' 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 UART 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("UART_USE_Init")) Then
        If (Not UARTGenerateH(r, vIndex)) Then Exit Function
        If (Not r.AddFile(r.s("UART_HFILE"), "H")) Then Exit Function
    End If

    ' write file UART.c
    If (r.v("UART_USE_Init")) Then
        If (Not UARTGenerateC(r, vIndex)) Then Exit Function
        If (Not r.AddFile(r.s("UART_CFILE"), "C")) Then Exit Function
    End If
        
    StartGenerateCode = True
    Exit Function
    
StartGenerateCodeError:
    Errorbox "Code generation for UART 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("UART_CFILE"))

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

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

            Print #f, uc.GetUserCode("(UART_General,1)", 0)   'USER CODE for application specific descriptions'
            Print #f, ""
            Print #f, Rtf.GenerateNote((r.v("UART_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("(UART_General,2)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_MACROS
            Print #f, ""  'DAvE local macro definitions'
            Print #f, uc.GetUserCode("(UART_General,3)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_DEFINES
            Print #f, ""  'DAvE local #define definitions'
            Print #f, uc.GetUserCode("(UART_General,4)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_TYPEDEFS
            Print #f, ""  'DAvE type defintions'
            Print #f, uc.GetUserCode("(UART_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("(UART_General,6)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_GLOBAL_VARIABLES
            Print #f, ""  'DAvE definitions of global variables'
            Print #f, uc.GetUserCode("(UART_General,7)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_EXTERNAL_PROTOTYPES
            Print #f, ""  'DAvE external declarations of imported functions'
            Print #f, uc.GetUserCode("(UART_General,8)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_LOCAL_PROTOTYPES
            Print #f, ""  'DAvE declarations of internal local functions'
            Print #f, uc.GetUserCode("(UART_General,9)", 0)
            Print #f, ""
            Print #f, ""
    '------------------------------------------------------------------------------------------------
    ' UART_Init function
    '------------------------------------------------------------------------------------------------
    If (r.v("UART_USE_Init") = 1) Then
            Print #f, FormatFunctionHeader(r, func, "UART_NAME_Init")
            Print #f, uc.GetUserCode("(UART_Init,1)", 0)     'USER CODE for application specific descriptions'
            Print #f, ""
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "UART_NAME_Init")
            Print #f, "{"
            Print #f, uc.GetUserCode("(UART_Init,2)", 2)
            Print #f, ""
            
            bUARTConfigured = Not (r.v("UART_TxPin_P0.2_Used") = 0 And r.v("UART_TxPin_P1.1_Used") = 0 And r.v(UART_PREFIX & "SCON_REN") = 0)
            
If bUARTConfigured Then
            Print #f, FormatSectionHeader(2, 1, "UART settings")
        If r.v("UART_TxPin_P1.1_Used") = 1 Then
            Print #f, FormatComments(2, 1, "", "", "Pin TXD_0 (P1.1) is selected for transmission")
        Else
            If r.v("UART_TxPin_P0.2_Used") = 1 Then
                Print #f, FormatComments(2, 1, "", "", "Pin TXD_1 (P0.2) is selected for transmission")
            Else
                Print #f, FormatComments(2, 1, "", "", "No pins are selected for transmission")
            End If
        End If
                  
        If r.v("UART_RxPin_P1.0_Used") = 1 Then
            Print #f, FormatComments(2, 1, "", "", "Pin RXD_0 (P1.0) is selected for reception")
        Else
        
            If r.v("UART_RxPin_P0.1_Used") = 1 Then
                Print #f, FormatComments(2, 1, "", "", "Pin RXD_1 (P0.1) is selected for reception")
            Else
                Print #f, FormatComments(2, 1, "", "", "No pins are selected for reception")
            End If
        End If
        
        If (r.v(UART_PREFIX & "SCON_REN") = 1) Then
            Print #f, FormatComments(2, 1, "", "", "Receiver enabled")
        Else
            Print #f, FormatComments(2, 1, "", "", "Receiver disabled")
        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("UART_RXDO_Pin_P1.5_Used") = 1 Then
                Print #f, FormatComments(2, 1, "", "", "Pin P1.5 (RXDO_0) is selected for RXDO")
            ElseIf r.v("UART_RXDO_Pin_P0.0_Used") = 1 Then
                Print #f, FormatComments(2, 1, "", "", "Pin P0.0 (RXDO_1) is selected for RXDO")
            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(UART_PREFIX & "SCON_SM2") = 1) Then
            Print #f, FormatComments(2, 1, "", "", "Receiver interrupt flag RI 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, ""
            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, ""
            End If
            
      End Select

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

      '-------------------------------------------------
      ' Initialization code
      '-------------------------------------------------
      If (r.v("UART_TxPin_P1.1_Used") = 1 Or r.v("UART_TxPin_P0.2_Used") = 1 Or r.v("UART_RxPin_P1.0_Used") = 1 Or r.v("UART_RxPin_P0.1_Used") = 1) Then
          Print #f, ""
                   
          If GetUartMode(r) = 0 And r.v("UART_RXDO_Pin_P1.5_Used") = 1 And r.v("UART_TxPin_P1.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, "P1_ALTSEL0", "&=", "~(ubyte)0x22", "configure alternate function register 0")
                Print #f, FormatCodeStatemente(2, "P1_ALTSEL1", "|=", "(ubyte)0x22", "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, "P1_DIR", "|=", "(ubyte)0x22", "set output direction")
          ElseIf r.v("UART_TxPin_P1.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, "P1_ALTSEL0", "&=", "~(ubyte)0x02", "configure alternate function register 0")
                Print #f, FormatCodeStatemente(2, "P1_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, "P1_DIR", "|=", "(ubyte)0x02", "set output direction")
          End If
        
        
          If GetUartMode(r) = 0 And r.v("UART_RXDO_Pin_P0.0_Used") = 1 And r.v("UART_TxPin_P0.2_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)0x01", "configure alternate function register 0")
                Print #f, FormatCodeStatemente(2, "P0_ALTSEL1", "|=", "(ubyte)0x05", "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)0x05", "set output direction")
          ElseIf r.v("UART_TxPin_P0.2_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)0x04", "configure alternate function register 0")
                Print #f, FormatCodeStatemente(2, "P0_ALTSEL1", "|=", "(ubyte)0x04", "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)0x04", "set output direction")
          End If
          
          Print #f, ""
          
        If GetUartMode(r) = 0 Then
            If r.v("UART_RXDO_Pin_P1.5_Used") = 1 And Not r.v("UART_TxPin_P1.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, "P1_ALTSEL0", "&=", "~(ubyte)0x20", "configure alternate function register 0")
                    Print #f, FormatCodeStatemente(2, "P1_ALTSEL1", "|=", "(ubyte)0x20", "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, "P1_DIR", "|=", "(ubyte)0x20", "set output direction")
            End If
            
            If r.v("UART_RXDO_Pin_P0.0_Used") = 1 And Not r.v("UART_TxPin_P0.2_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)0x01", "configure alternate function register 0")
                    Print #f, FormatCodeStatemente(2, "P0_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, "P0_DIR", "|=", "(ubyte)0x01", "set output direction")
            End If
        
        End If
          
          
          If r.v("UART_RxPin_P0.1_Used") = 1 Then
            Print #f, ""
            Print #f, FormatCodeStatemente(2, SCU_PREFIX & "MODPISEL", "|=", "(ubyte)0x01", "configure peripheral input select register")
          End If
          
          If r.v("UART_RxPin_P1.0_Used") = 1 Then
            Print #f, ""
            Print #f, FormatCodeStatemente(2, SCU_PREFIX & "MODPISEL", "&=", "~(ubyte)0x01", "configure peripheral input select register")
          End If
          
      End If
      
      
      
      ' statement for SCON
      v = r.sfr(UART_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 is set in order to disable reception")
        v = v + BitMask(r, UART_PREFIX & "SCON", "RI")
      End If
      
            Print #f, FormatCodeStatemente(2, SfrName(r, BRG_PREFIX & "BCON"), "=", FormatTextOutput(0, "H2"), "reset baudrate timer/reload register")
            Print #f, FormatCodeStatemente(2, SfrName(r, UART_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

    ' baudrate -----------------------------------------
    
        
        
        Select Case GetUartMode(r)
          Case 0
                
                
          Case 2
                
                
          Case 1, 3
            
            If r.v(BRG_PREFIX & "FDCON_BGS") = 0 Then
                If r.v(BRG_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(BRG_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(BRG_PREFIX & "BCON_BRPRE"))
                    End If
                    
                    If r.l(BRG_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(BRG_PREFIX & "FDCON_FDEN") = 1 And r.l("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(BRG_PREFIX & "FDCON_FDEN") = 1 Then
                            Print #f, FormatCodeStatemente(2, SfrName(r, BRG_PREFIX & "FDSTEP"), "=", FormatTextOutput(r.sfr(BRG_PREFIX & "FDSTEP"), "H2"), "load fractional divider reload register")
                        End If
                        Print #f, FormatCodeStatemente(2, SfrName(r, BRG_PREFIX & "BG"), "=", FormatTextOutput(r.sfr(BRG_PREFIX & "BG"), "H2"), "load baudrate timer/reload register")
                        If r.sfr(BRG_PREFIX & "FDCON") <> 0 Then
                            Print #f, FormatCodeStatemente(2, SfrName(r, BRG_PREFIX & "FDCON"), "|=", FormatTextOutput(r.sfr(BRG_PREFIX & "FDCON"), "H2"), "load Fractional Divider control register")
                        End If
                        Print #f, FormatCodeStatemente(2, SfrName(r, BRG_PREFIX & "BCON"), "|=", FormatTextOutput(r.sfr(BRG_PREFIX & "BCON"), "H2"), "load baud rate control register")
                    End If
                    
                    Print #f, ""
                End If
            End If
    
        End Select
    Else
        Print #f, ""
        Print #f, FormatComments(2, 1, "", "", "UART is not configured")
        Print #f, ""
    End If
    
        If r.v(BRG_PREFIX & "FDCON_FDM") = 1 Then
            
            Print #f, FormatSectionHeader(2, 1, "BRG settings (Normal Divider Mode)")
            If r.v(BRG_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(BRG_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(BRG_PREFIX & "BCON_BRPRE"))
            End If
            Print #f, FormatComments(2, 1, "", "", "the timer overflow is " & GetBRGTmrOverflow(r) & " " & TXT_US)
            
            Print #f, FormatCodeStatemente(2, SfrName(r, BRG_PREFIX & "FDCON"), "|=", FormatTextOutput(r.sfr(BRG_PREFIX & "FDCON"), "H2"), "load Fractional Divider control register")
            Print #f, FormatCodeStatemente(2, SfrName(r, BRG_PREFIX & "FDSTEP"), "=", FormatTextOutput(r.sfr(BRG_PREFIX & "FDSTEP"), "H2"), "load fractional divider reload register")
            
            ' Timer interrupt
            
            If (r.v("BRG_Interrupt_Select") = 1) Then
                Print #f, ""
                Print #f, FormatComments(2, 1, "", "", "BRG timer overflow interrupt is enabled")
                'If (r.v(CORE_PREFIX & "IEN0_ET2") <> 1) Then
                'If ((r.v(CORE_PREFIX & "IEN0_ET2") <> 1) Or (r.v("T2_USE_Init") = 0)) Then 'Added by jag on 6/4/6 in order to generate if we deselect init function of T2.
                    'Print #f, FormatCodeStatemente(2, SfrBitStatement(r, CORE_PREFIX & "IEN0_ET2", 1), "", "", "Enable interrupt")
                  Print #f, FormatComments(2, 1, "", "", "UART 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, BRG_PREFIX & "FDCON"), "|=", FormatTextOutput(r.sfr(BRG_PREFIX & "FDCON"), "H2"), "load Fractional Divider control register")
        End If
        
        
            Print #f, ""

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


      ' statment for ES ----------------------------------------------------
      If (r.v(CORE_PREFIX & "IEN0_ES") = 1) Then
            Print #f, FormatComments(2, 1, "", "", "UART interrupt enabled")
            Print #f, "  " & SfrBitStatement(r, CORE_PREFIX & "IEN0_ES", 1)
            Print #f, ""
      End If

            Print #f, "} " & END_OF_FUNCTION & r.v("UART_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("UART_USE_Isr") = 1) Then
       If (r.v("ISR_CHOICE") = 1) Then
            Print #f, FormatFunctionHeader(r, func, "UART_NAME_Isr")
            Call GetDescriptionForISR(r, f)
            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, ""
            Print #f, ""
            Print #f, uc.GetUserCode("(UART_Isr,2)", 4)
            Print #f, ""
            'Print #f, FormatCodeStatemente(2, "_push_(SYSCON0);", "", "", "push the current RMAP")
            
            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, "RESET_RMAP();", "", "", "resets RMAP")
            
           If ((r.v(CORE_PREFIX & "IPH_PSH") * 2 ^ r.v(CORE_PREFIX & "IPH_PSH") + r.v(CORE_PREFIX & "IP_PS")) = 0) Then
              Print #f, "" & FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST0);", "", "", "switch to page 0")
           ElseIf ((r.v(CORE_PREFIX & "IPH_PSH") * 2 ^ r.v(CORE_PREFIX & "IPH_PSH") + r.v(CORE_PREFIX & "IP_PS")) = 1) Then
              Print #f, "" & FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST1);", "", "", "switch to page 0")
           ElseIf ((r.v(CORE_PREFIX & "IPH_PSH") * 2 ^ r.v(CORE_PREFIX & "IPH_PSH") + r.v(CORE_PREFIX & "IP_PS")) = 2) Then
              Print #f, "" & FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST2);", "", "", "switch to page 0")
           ElseIf ((r.v(CORE_PREFIX & "IPH_PSH") * 2 ^ r.v(CORE_PREFIX & "IPH_PSH") + r.v(CORE_PREFIX & "IP_PS")) = 3) Then
              Print #f, "" & FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST3);", "", "", "switch to page 0")
           End If
            Print #f, "  if (" & SfrBitTest(r, UART_PREFIX & "SCON_TI") & ")"
            Print #f, "  {"
            'Print #f, uc.GetUserCode("(UART_Isr,2)", 4)
            If (r.v("GEN_UserDef3") = 1) Then
               Print #f, uc.GetUserCode("(UART_Isr,3)" & vbCrLf & vbCrLf & "       " & SfrBitStatement(r, UART_PREFIX & "SCON_TI", 0), 4)
               r.v("GEN_UserDef3") = 2
            ElseIf ((r.v("GEN_UserDef3") = 2) Or (r.v("GEN_UserDef3") = 3)) Then
               Print #f, uc.GetUserCode("(UART_Isr,3)", 4)
            ElseIf (r.v("GEN_UserDef3") = 0) Then
               Print #f, uc.GetUserCode("(UART_Isr,3)", 4)
            End If
            '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)
               If (r.v("GEN_UserDef3") = 2) Then
                  Print #f, uc.GetUserCode("(UART_Isr,4)" & vbCrLf & vbCrLf & "       " & SfrBitStatement(r, UART_PREFIX & "SCON_RI", 0), 4)
                  r.v("GEN_UserDef3") = 3
               ElseIf (r.v("GEN_UserDef3") = 3) Then
                  Print #f, uc.GetUserCode("(UART_Isr,4)", 4)
               ElseIf (r.v("GEN_UserDef3") = 0) Then
                  Print #f, uc.GetUserCode("(UART_Isr,4)", 4)
               End If
            Else
               Print #f, uc.GetUserCode("(UART_Isr,4)", 4)
            End If

            Print #f, "  }"
            Print #f, ""
            Print #f, uc.GetUserCode("(UART_Isr,5)", 4)
            Print #f, ""
            If ((r.v(CORE_PREFIX & "IPH_PSH") * 2 ^ r.v(CORE_PREFIX & "IPH_PSH") + r.v(CORE_PREFIX & "IP_PS")) = 0) Then
              Print #f, "" & FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST0);", "", "", "restore the old page")
            ElseIf ((r.v(CORE_PREFIX & "IPH_PSH") * 2 ^ r.v(CORE_PREFIX & "IPH_PSH") + r.v(CORE_PREFIX & "IP_PS")) = 1) Then
              Print #f, "" & FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST1);", "", "", "restore the old page")
            ElseIf ((r.v(CORE_PREFIX & "IPH_PSH") * 2 ^ r.v(CORE_PREFIX & "IPH_PSH") + r.v(CORE_PREFIX & "IP_PS")) = 2) Then
              Print #f, "" & FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST2);", "", "", "restore the old page")
            ElseIf ((r.v(CORE_PREFIX & "IPH_PSH") * 2 ^ r.v(CORE_PREFIX & "IPH_PSH") + r.v(CORE_PREFIX & "IP_PS")) = 3) Then
              Print #f, "" & FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST3);", "", "", "restore the old page")
            End If
            
            Print #f, ""
            'Print #f, FormatCodeStatemente(2, "_pop_(SYSCON0);", "", "", "restore the old RMAP")
            
            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("UART_NAME_Isr")
            Print #f, ""
            Print #f, ""
       ElseIf (r.v("ISR_CHOICE") = 2) Then
            Print #f, FormatFunctionHeader(r, func, "UART_NAME_Isr")
            Call GetDescriptionForISR(r, f)
            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, ""
            Print #f, uc.GetUserCode("(UART_Isr,2)", 4)
            If ((r.v(CORE_PREFIX & "IPH_PSH") * 2 ^ r.v(CORE_PREFIX & "IPH_PSH") + r.v(CORE_PREFIX & "IP_PS")) = 0) Then
              Print #f, "" & FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST0);", "", "", "switch to page 0")
            ElseIf ((r.v(CORE_PREFIX & "IPH_PSH") * 2 ^ r.v(CORE_PREFIX & "IPH_PSH") + r.v(CORE_PREFIX & "IP_PS")) = 1) Then
              Print #f, "" & FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST1);", "", "", "switch to page 0")
            ElseIf ((r.v(CORE_PREFIX & "IPH_PSH") * 2 ^ r.v(CORE_PREFIX & "IPH_PSH") + r.v(CORE_PREFIX & "IP_PS")) = 2) Then
              Print #f, "" & FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST2);", "", "", "switch to page 0")
            ElseIf ((r.v(CORE_PREFIX & "IPH_PSH") * 2 ^ r.v(CORE_PREFIX & "IPH_PSH") + r.v(CORE_PREFIX & "IP_PS")) = 3) Then
              Print #f, "" & FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, SST3);", "", "", "switch to page 0")
            End If
            Print #f, "  if (" & SfrBitTest(r, UART_PREFIX & "SCON_TI") & ")"
            Print #f, "  {"
            'Print #f, uc.GetUserCode("(UART_Isr,2)", 4)
            If (r.v("GEN_UserDef3") = 1) Then
               Print #f, uc.GetUserCode("(UART_Isr,3)" & vbCrLf & vbCrLf & "       " & SfrBitStatement(r, UART_PREFIX & "SCON_TI", 0), 4)
               r.v("GEN_UserDef3") = 2
            ElseIf ((r.v("GEN_UserDef3") = 2) Or (r.v("GEN_UserDef3") = 3)) Then
               Print #f, uc.GetUserCode("(UART_Isr,3)", 4)
            ElseIf (r.v("GEN_UserDef3") = 0) Then
               Print #f, uc.GetUserCode("(UART_Isr,3)", 4)
            End If
            '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)
               If (r.v("GEN_UserDef3") = 2) Then
                  Print #f, uc.GetUserCode("(UART_Isr,4)" & vbCrLf & vbCrLf & "       " & SfrBitStatement(r, UART_PREFIX & "SCON_RI", 0), 4)
                  r.v("GEN_UserDef3") = 3
               ElseIf (r.v("GEN_UserDef3") = 3) Then
                  Print #f, uc.GetUserCode("(UART_Isr,4)", 4)
               ElseIf (r.v("GEN_UserDef3") = 0) Then
                  Print #f, uc.GetUserCode("(UART_Isr,4)", 4)
               End If
            Else
               Print #f, uc.GetUserCode("(UART_Isr,4)", 4)
            End If

            Print #f, "  }"
            Print #f, ""
            Print #f, uc.GetUserCode("(UART_Isr,5)", 4)
            If ((r.v(CORE_PREFIX & "IPH_PSH") * 2 ^ r.v(CORE_PREFIX & "IPH_PSH") + r.v(CORE_PREFIX & "IP_PS")) = 0) Then
              Print #f, "" & FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST0);", "", "", "restore the old page")
            ElseIf ((r.v(CORE_PREFIX & "IPH_PSH") * 2 ^ r.v(CORE_PREFIX & "IPH_PSH") + r.v(CORE_PREFIX & "IP_PS")) = 1) Then
              Print #f, "" & FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST1);", "", "", "restore the old page")
            ElseIf ((r.v(CORE_PREFIX & "IPH_PSH") * 2 ^ r.v(CORE_PREFIX & "IPH_PSH") + r.v(CORE_PREFIX & "IP_PS")) = 2) Then
              Print #f, "" & FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST2);", "", "", "restore the old page")
            ElseIf ((r.v(CORE_PREFIX & "IPH_PSH") * 2 ^ r.v(CORE_PREFIX & "IPH_PSH") + r.v(CORE_PREFIX & "IP_PS")) = 3) Then
              Print #f, "" & FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, RST3);", "", "", "restore the old page")
            End If
            Print #f, "} " & END_OF_FUNCTION & r.v("UART_NAME_Isr")
            Print #f, ""
            Print #f, ""
       
       End If
    End If



    '------------------------------------------------------------------------------------------------
    ' UART_GetData8 function
    '------------------------------------------------------------------------------------------------
    If (r.v("UART_USE_GetData8") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART_NAME_GetData8")
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "UART_NAME_GetData8")
            Print #f, "{"

        If (GetUartMode(r) <> 0) Then
            Print #f, FormatComments(2, 0, "", "", "Clear the receiver interrupt flag")
            Print #f, "  " & SfrBitStatement(r, UART_PREFIX & "SCON_RI", 0)
            Print #f, ""
        End If

            Print #f, FormatComments(2, 0, "", "", "Return the received data byte")
            Print #f, "  return(" & SfrName(r, UART_PREFIX & "SBUF") & ");"
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("UART_NAME_GetData8")
            Print #f, ""
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' UART_SendData8 function
    '------------------------------------------------------------------------------------------------
    If (r.v("UART_USE_SendData8") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART_NAME_SendData8")
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "UART_NAME_SendData8")
            Print #f, "{"
            Print #f, FormatComments(2, 0, "", "", "Clear the transmitter interrupt flag")
            Print #f, "  " & SfrBitStatement(r, UART_PREFIX & "SCON_TI", 0)
            Print #f, ""

        If (IsMultiComMode(r)) Then
            Print #f, FormatComments(2, 0, "", "", "Set bit 8 to '0'")
            Print #f, "  " & SfrBitStatement(r, UART_PREFIX & "SCON_TB8", 0)
            Print #f, ""
        End If

            Print #f, FormatComments(2, 0, "", "", "Write the transmit data byte, this initiates the transmission.")
            Print #f, "  " & SfrName(r, UART_PREFIX & "SBUF") & " = ubData;"
            Print #f, "} " & END_OF_FUNCTION & r.v("UART_NAME_SendData8")
            Print #f, ""
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' UART_GetData9 function
    '------------------------------------------------------------------------------------------------
    If (r.v("UART_USE_GetData9") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART_NAME_GetData9")
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "UART_NAME_GetData9")
            Print #f, "{"
            Print #f, "  uword uwData;"
            Print #f, ""
            Print #f, FormatComments(2, 0, "", "", "Clear the receiver interrupt flag")
            Print #f, "  " & SfrBitStatement(r, UART_PREFIX & "SCON_RI", 0)
            Print #f, ""
            Print #f, FormatComments(2, 0, "", "", "Read the received data bits 0..7")
            Print #f, "  uwData = (uword)" & SfrName(r, UART_PREFIX & "SBUF") & ";"
            Print #f, ""
            Print #f, FormatComments(2, 0, "", "", "Read the received data bit 8")
            Print #f, "  if (" & SfrBitTest(r, UART_PREFIX & "SCON_RB8") & ")"
            Print #f, "  {"
            Print #f, "    uwData |= 0x0100;"
            Print #f, "  }"
            Print #f, ""
            Print #f, "  return(uwData);"
            Print #f, "} " & END_OF_FUNCTION & r.v("UART_NAME_GetData9")
            Print #f, ""
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' UART_SendData9 function
    '------------------------------------------------------------------------------------------------
    If (r.v("UART_USE_SendData9") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART_NAME_SendData9")
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "UART_NAME_SendData9")
            Print #f, "{"
            Print #f, FormatComments(2, 0, "", "", "Clear the transmitter interrupt flag")
            Print #f, "  " & SfrBitStatement(r, UART_PREFIX & "SCON_TI", 0)
            Print #f, ""
            Print #f, FormatComments(2, 0, "", "", "Write the transmit data bit 8")
            Print #f, "  if (uwData & 0x0100)"
            Print #f, "  {"
            Print #f, "    " & SfrBitStatement(r, UART_PREFIX & "SCON_TB8", 1)
            Print #f, "  }"
            Print #f, "  else"
            Print #f, "  {"
            Print #f, "    " & SfrBitStatement(r, UART_PREFIX & "SCON_TB8", 0)
            Print #f, "  }"
            Print #f, ""
            Print #f, FormatComments(2, 0, "", "", "Write the transmit data bits 0..7, this initiates the transmission")
            Print #f, "  " & SfrName(r, UART_PREFIX & "SBUF") & " = (ubyte)uwData;"
            Print #f, "} " & END_OF_FUNCTION & r.v("UART_NAME_SendData9")
            Print #f, ""
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' UART_SendAddr function
    '------------------------------------------------------------------------------------------------
    If (r.v("UART_USE_SendAddr") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART_NAME_SendAddr")
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "UART_NAME_SendAddr")
            Print #f, "{"
            Print #f, FormatComments(2, 0, "", "", "Clear the transmitter interrupt flag")
            Print #f, "  " & SfrBitStatement(r, UART_PREFIX & "SCON_TI", 0)
            Print #f, ""
            Print #f, FormatComments(2, 0, "", "", "Set bit 8 to '1'")
            Print #f, "  " & SfrBitStatement(r, UART_PREFIX & "SCON_TB8", 1)
            Print #f, ""
            Print #f, FormatComments(2, 0, "", "", "Write the transmit data byte, this initiates the transmission.")
            Print #f, "  " & SfrName(r, UART_PREFIX & "SBUF") & " = ubAddress;"
            Print #f, "} " & END_OF_FUNCTION & r.v("UART_NAME_SendAddr")
            Print #f, ""
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' UART_OwnAddress function
    '------------------------------------------------------------------------------------------------
    If (r.v("UART_USE_OwnAddress") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART_NAME_OwnAddress")
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "UART_NAME_OwnAddress")
            Print #f, "{"
            Print #f, FormatComments(2, 0, "", "", "Clear the receiver interrupt flag")
            Print #f, "  " & SfrBitStatement(r, UART_PREFIX & "SCON_RI", 0)
            Print #f, ""
            Print #f, FormatComments(2, 0, "", "", "Read the received data byte, return comparison result")
            Print #f, "  return (ubAddress == " & SfrName(r, UART_PREFIX & "SBUF") & ");"
            Print #f, "} " & END_OF_FUNCTION & r.v("UART_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

    '------------------------------------------------------------------------------------------------
            Print #f, uc.GetUserCode("(UART_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("UART_HFILE"))
    
    'open UART.h
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v("UART_HFILE") For Output As f
    
    '------------------------------------------------------------------------------------------------
    ' module header
    '------------------------------------------------------------------------------------------------
    d = FormatModuleDescription(GetModuleHeaderH("UART"))
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), dc("UART").ModuleDescription, r.v("UART_HFILE"), r.v("MAIN_CPU_TYPE"), r.v("MAIN_COMPILER"), d)
            
            Print #f, uc.GetUserCode("(UART_Header,1)", 0)
            Print #f, ""
            Print #f, Rtf.GenerateNote((r.v("UART_Notes")))
            Print #f, ""
            Print #f, "#ifndef _" & StripExtension(r.v("UART_HFILE")) & "_H_"
            Print #f, "#define _" & StripExtension(r.v("UART_HFILE")) & "_H_"
            Print #f, ""
            Print #f, SECTION_PROJECT_INCLUDES
            Print #f, ""  'DAvE includes'
            Print #f, uc.GetUserCode("(UART_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("(UART_Header,3)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_DEFINES
            Print #f, ""  'DAvE global #define definitions'
            Print #f, uc.GetUserCode("(UART_Header,4)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_TYPEDEFS
            Print #f, ""  'DAvE type definitions'
            Print #f, uc.GetUserCode("(UART_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("(UART_Header,6)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_GLOBAL_VARIABLES
            Print #f, ""  'DAvE definition of global variables'
            Print #f, uc.GetUserCode("(UART_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("UART_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("(UART_Header,8)", 0)
            Print #f, ""
            Print #f, ""
                          'DAvE declarations of global macros'
    '------------------------------------------------------------------------------------------------
    ' UART_RxEnable macro
    '------------------------------------------------------------------------------------------------
    If (r.v("UART_USE_RxEnable") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART_NAME_RxEnable")

            Print #f, "#define " & r.v("UART_NAME_RxEnable") & "() " & SfrBitStatement(r, UART_PREFIX & "SCON_REN", 1, False)
            Print #f, ""
            Print #f, ""
    End If
  
    '------------------------------------------------------------------------------------------------
    ' UART_RxDisable macro
    '------------------------------------------------------------------------------------------------
    If (r.v("UART_USE_RxDisable") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART_NAME_RxDisable")

            Print #f, "#define " & r.v("UART_NAME_RxDisable") & "() " & SfrBitStatement(r, UART_PREFIX & "SCON_REN", 0, False)
            Print #f, ""
            Print #f, ""
    End If
  
    '------------------------------------------------------------------------------------------------
    ' UART_RxReady macro
    '------------------------------------------------------------------------------------------------
    If (r.v("UART_USE_RxReady") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART_NAME_RxReady")

            Print #f, "#define " & r.v("UART_NAME_RxReady") & "() " & SfrBitTest(r, UART_PREFIX & "SCON_RI")
            Print #f, ""
            Print #f, ""
    End If
  
    '------------------------------------------------------------------------------------------------
    ' UART_TxReady macro
    '------------------------------------------------------------------------------------------------
    If (r.v("UART_USE_TxReady") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART_NAME_TxReady")

            Print #f, "#define " & r.v("UART_NAME_TxReady") & "() " & SfrBitTest(r, UART_PREFIX & "SCON_TI")
            Print #f, ""
            Print #f, ""
    End If
  
    '------------------------------------------------------------------------------------------------
    ' UART_WakeUp macro
    '------------------------------------------------------------------------------------------------
    If (r.v("UART_USE_WakeUp") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART_NAME_WakeUp")

            Print #f, "#define " & r.v("UART_NAME_WakeUp") & "() " & SfrBitStatement(r, UART_PREFIX & "SCON_SM2", 0, False)
            Print #f, ""
            Print #f, ""
    End If
  
    '------------------------------------------------------------------------------------------------
    ' UART_GotoSleep macro
    '------------------------------------------------------------------------------------------------
    If (r.v("UART_USE_GotoSleep") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "UART_NAME_GotoSleep")

            Print #f, "#define " & r.v("UART_NAME_GotoSleep") & "() " & SfrBitStatement(r, UART_PREFIX & "SCON_SM2", 1, False)
            Print #f, ""
            Print #f, ""
    End If
  
    '------------------------------------------------------------------------------------------------
            Print #f, SECTION_INTERRUPT_VECTORS
            Print #f, ""  'DAvE interrupt vector definitions
            

    If (r.v("UART_USE_Isr") = 1) Then
            Print #f, "#define UARTINT  " & IntNumber(&H23)
    End If
    'If ((r.v("BRG_Interrupt_Select") = 1) And (r.v(CORE_PREFIX & "IEN0_ET2") <> 1)) Then
    ' Added by jag on 7/4/6
    If ((r.v("BRG_Interrupt_Select") = 1) And ((r.v(CORE_PREFIX & "IEN0_ET2") <> 1) Or (r.v("T2_USE_Init") = 0))) Then
            'Print #f, "#define BRGINT   " & IntNumber(&H2B)
             Print #f, FormatComments(0, 0, "", "", "Shared interrupt vector definitions are defined in SHARED_INT.H")
    End If
            Print #f, ""
            Print #f, uc.GetUserCode("(UART_Header,9)", 0)
            Print #f, ""
            Print #f, "#endif  // ifndef _" & StripExtension(r.v("UART_HFILE")) & "_H_"
    
    'close file
    Close f
    
    UARTGenerateH = True
    Exit Function
    
UARTGenerateHError:
    Close f
    Exit Function
End Function

