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

Option Explicit
'*****************************************************************************
' Module:
'        ASC
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions for generating code for the
'        module 'ASC'.
'
'-----------------------------------------------------------------------------
' History:
'                 
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        1.0  rkt            10.12.99    initialize version
'        1.1  ste            04.02.00    final version
'        1.2  ste            09.03.00    IrDA mode deleted
'
'        1.3  ahe            08.10.00    final version
'                                        automatic calculation of FDV
'                                        - no changes
'
'        1.4  ste            14.11.00    register names changed:
'                                        _SRCTB   =>   _TBSRC
'                                        _SRCT    =>   _TSRC
'                                        _SRCR    =>   _RSRC
'                                        _SRCE    =>   _ESRC
'                                        _BG_RL   =>   _BG_BR_VALUE
'                                        _FDV_FD  =>   _FDV_FD_VALUE
'                                        _RBUF_RD =>   _RBUF_RD_VALUE
'                                        _TBUF_TD =>   _TBUF_TD_VALUE
'        ---------------------------------------------------------------------
'        3.0  ste            25.01.01    final version for S-GOLDv1
'
'        ---------------------------------------------------------------------
'        4.0  ahe            14.02.01    final version for Pegasus
'
'        4.1  ahe            08.08.02     no changes
'
'*****************************************************************************



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

    StartGenerateCode = False

    Dim sRegPre As String
    Dim sVarPre As String

    sRegPre = "ASC" & Format$(vIndex) & "_"
    sVarPre = "ASC" & Format$(vIndex)

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

    ' write file ASC.c
    If (r.v(sVarPre & "_USE_Init") = 1) Then
      If (Not ASCGenerateC(r, vIndex)) Then Exit Function
	 	   If (Not r.AddFile(r.s(sVarPre & "_CFILE"))) Then Exit Function
    End if

        
    StartGenerateCode = True
    Exit Function
    
StartGenerateCodeError:
    Errorbox("Code generation for " & sVarPre & " failed", Error$)
    Exit Function
End Function


'*****************************************************************************
'Description:
'        Generates the code for the ASC.c file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function ASCGenerateC(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo ASCGenerateCError
    
    Dim i As Integer
    Dim f As Variant
    Dim v As Variant
    Dim d As String
    Dim g As String
    Dim n As String
    Dim dc As DAvECollection
    Dim uc As UserCode
    Dim func As FunctionClass
    Dim sRegPre As String
    Dim sVarPre As String
    Dim sAlternatePinName(1 To 8) As String
    Dim Baudrate As UnitType

    sRegPre = "ASC" & Format$(vIndex) & "_"
    sVarPre = "ASC" & Format$(vIndex)

    
    ASCGenerateC = False

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

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

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

    'open ASC.c
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v(sVarPre & "_CFILE") For Output As f
    
    '------------------------------------------------------------------------------------------------
    ' module header
    '------------------------------------------------------------------------------------------------
    d = FormatModuleDescription(GetModuleHeaderC(sVarPre))
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), dc(sVarPre).ModuleDescription, r.v(sVarPre & "_CFILE"), r.v("MAIN_CPU_TYPE"), r.v("MAIN_COMPILER"), d)
          
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,1)",0)    'USER CODE for application specific descriptions'
    Print #f, ""
    Print #f, Rtf.GenerateNote((r.v(sVarPre & "_Notes")))
    Print #f, ""
    Print #f, SECTION_PROJECT_INCLUDES
    Print #f, ""  'DAvE includes'
    Print #f, "#include """ & r.v("MAIN_HFile") & """"
    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,2)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_MACROS
    Print #f, ""  'DAvE local macro definitions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,3)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_DEFINES
    Print #f, ""  'DAvE local #define definitions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,4)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_TYPEDEFS
    Print #f, ""  'DAvE type defintions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,5)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_IMPORTED_GLOBAL_VARIABLES
    Print #f, ""  'DAvE external declarations of imported global variables'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,6)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_GLOBAL_VARIABLES
    Print #f, ""  'DAvE definitions of global variables'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,7)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_EXTERNAL_PROTOTYPES
    Print #f, ""  'DAvE external declarations of imported functions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,8)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_LOCAL_PROTOTYPES
    Print #f, ""  'DAvE declarations of internal local functions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_General,9)",0)
    Print #f, ""
    Print #f, ""
    
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASC_vInit function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "Init"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
        Print #f, ""
        Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
        Print #f, "{"
        Print #f, uc.GetUserCode("(" & n & ",2)",2) 
        Print #f, ""

        If (Not IsAutobaudSelected(r, vIndex)) Then
            '------------------------------------------------------------------------------------------------
            ' Configuration of the ASC BaudRate Generator:
            '------------------------------------------------------------------------------------------------
            Print #f, FormatSectionHeader(2, 1, "Configuration of the " & sVarPre & " Baudrate Generator:")
            If (r.v(sRegPre & "CON_FDE") = 0) Then    'fractional divider is disabled
              d = "additionally reduce serial clock to " & r.v(sRegPre & "CON_BRS") + 2
              Print #f, FormatCommentsForInit(1, d)
            Else                                      'fractional divider is enabled
              d = "fractional divider as prescaler for baud rate timer is used"
              Print #f, FormatCommentsForInit(1, d)

              Print #f, ""
              v = r.v(sRegPre & "FDV_FD_VALUE")
              Print #f, FormatCodeStatemente(2, sRegPre & "FDV", "=", _
                        FormatTextOutput(v, "H4"), _
                        "load " & sVarPre & " fractional divider register")
              Print #f, ""

            End If

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

            Baudrate = ConvertBaudrate(GetASCxRealBaudrate(r, vIndex))
            d = "real baud rate     = " & Format(Baudrate.dValue, ASC_PRECISION) & " " & Baudrate.sUnit
            Print #f, FormatCommentsForInit(1, d)    

            d = "deviation          = " & Format((GetASCxRealBaudrate(r, vIndex) - r.d(sVarPre & "_Baudrate")) / r.d(sVarPre & "_Baudrate") * 100, ASC_PRECISION) & " %"
            Print #f, FormatCommentsForInit(1, d)    
          
            If (r.v(sRegPre & "CON_R") = 0) Then
              d = "baud rate generator is disabled (" & sVarPre & " inactive)"
              Print #f, FormatCommentsForInit(2, d)
            End If
          
            Print #f, ""
            v = r.sfr(sRegPre & "BG")
            Print #f, FormatCodeStatemente(2, sRegPre & "BG", "=", _
                      FormatTextOutput(v, "H4"), _
                      "load " & sVarPre & " baud rate time reload register")
            Print #f, ""

            '------------------------------------------------------------------------------------------------
            ' Configuration of the ASC Operation Mode:
            '------------------------------------------------------------------------------------------------
            Print #f, FormatSectionHeader(2, 1, "Configuration of the " & sVarPre & " Operation Mode:")

            If (r.v(sRegPre & "CON_STP") = 0 ) Then    'stop bits
              g = "one stop bit"
            Else
              g = "two stop bits"
            End If

            Select Case (r.v(sRegPre & "CON_M"))                 'ASC mode
                   Case (0)
                        d = "8-bit data sychronous operation"
                   Case (1)
                        d = "8-bit data asychronous operation with " & g
                   Case (2)
                        d = "IrDA mode, 8-bit data asychronous operation with " & g
                   Case (3)
                        If (r.v(sRegPre & "CON_ODD") = 0) Then
                            d = "7-bit data + even parity asychronous operation with " & g
                        Else
                            d = "7-bit data + odd parity asychronous operation with " & g
                        End If
                   Case (4)
                        d = "9-bit data asychronous operation with " & g
                   Case (5)
                        d = "8-bit data + wake up bit asychronous operation with " & g
                   Case (7)
                        If (r.v(sRegPre & "CON_ODD") = 0) Then
                            d = "8-bit data + even parity asychronous operation with " & g
                        Else
                            d = "8-bit data + odd parity asychronous operation with " & g
                        End If
            End Select
            Print #f, FormatCommentsForInit(1, d)

            If (r.v(sRegPre & "CON_LB") = 1) Then                'loopback mode is enabled
                d = "loopback mode is enabled"
                Print #f, FormatCommentsForInit(1, d)
            End If

            If (r.v(sRegPre & "CON_M") = 2) Then                 'IrDA mode
                If (r.v(sRegPre & "PMW_IRPW") = 0) Then            'if bit IRPW is not set
                    d = "IrDA puls width is 3/16 of the bit time (tbit = " & Format(1000 / GetASCxRealBaudrate(r, vIndex), "0.000") & " s) 
                    Print #f, FormatCommentsForInit(1, d)
                    d = "IrDA puls width          = " & Format(GetIrDAPW(r, vIndex), "0.000") & " s"
                    Print #f, FormatCommentsForInit(1, d)
                    d = "required min. puls width = " & Format(r.v(sVarPre & "_IRDA_REQ"), "0.000") & " s"
                    Print #f, FormatCommentsForInit(1, d)
                    d = "real min. puls width     = " & Format(GetIrDARealPW(r, vIndex), "0.000") & " s"
                    Print #f, FormatCommentsForInit(1, d)
                Else
                    d = "IrDA puls width is defined by PW"
                    Print #f, FormatCommentsForInit(1, d)
                    d = "IrDA min. puls width     = " & Format(GetIrDAPW(r, vIndex), "0.000") & " s"
                    Print #f, FormatCommentsForInit(1, d)
                    d = "required puls width      = " & Format(r.v(sVarPre & "_IRDA_REQ"), "0.000") & " s"
                    Print #f, FormatCommentsForInit(1, d)
                    d = "real puls width          = " & Format(GetIrDARealPW(r, vIndex), "0.000") & " s"
                    Print #f, FormatCommentsForInit(1, d)
                End If
                
                d = "deviation                = " & Format((GetIrDARealPW(r, vIndex) - r.v(sVarPre & "_IRDA_REQ")) * _
                                                            100 / r.v(sVarPre & "_IRDA_REQ"), "0.000") & " %"
                Print #f, FormatCommentsForInit(1, d)

                Print #f, ""
                v = 0
                v = v + r.v(sRegPre & "PMW_IRPW") * 2 ^ 8
                v = v + r.v(sRegPre & "PMW_PW_VALUE")
                Print #f, FormatCodeStatemente(2, sRegPre & "PMW", "=", _
                          FormatTextOutput(v, "H4"), _
                          "load " & sVarPre & " IrDA puls mode/width register")
                Print #f, ""

            End If

        Else  ' is autobaud mode

            '------------------------------------------------------------------------------------------------
            ' Configuration of the ASCx autobaud detection
            '------------------------------------------------------------------------------------------------      
            Print #f, FormatSectionHeader(2, 1, "Configuration of the " & sVarPre & " Autobaud Detection:")

            d = "autobaud detection is enabled"
            Print #f, FormatCommentsForInit(1, d)

            If (r.v(sRegPre & "ABCON_AUREN") = 0) Then    
                d = "the receiver will not be enabled after a successful autobaud detection"
                Print #f, FormatCommentsForInit(2, d)
            Else
                d = "the receiver will be enabled after a successful autobaud detection"
                Print #f, FormatCommentsForInit(1, d)
            End If

            Select Case (r.v(sRegPre & "ABCON_ABEM"))       'echo mode
                  Case (0)
                       d = "echo mode is disabled"
                  Case (1)
                       d = "echo mode is enabled only during autobaud detection"
                  Case (2)
                       d = "echo mode is always enabled"
            End Select
            Print #f, FormatCommentsForInit(1, d)

            If (r.v(sRegPre & "ABCON_RXINV") = 1) Then  
                d = "receive inverter is enabled"
                Print #f, FormatCommentsForInit(1, d)
            End If

            If (r.v(sRegPre & "ABCON_TXINV") = 1) Then
                d = "transmit inverter is enabled"
                Print #f, FormatCommentsForInit(1, d)
            End If

            Print #f, ""
            d = "definition of the baud rates to be detected:"
            Print #f, FormatCommentsForInit(1, d)
            For i = 1 To 9
                d = "  baud rate " & i & "  =  " & Format(1000 * r.v(sRegPre & "FDV_FD_VALUE") * GetASCxModuleClock(r, 0) _
                         / (512 * GetDivideFactor(i - 1)), "0.000000") & "    kBaud "
                Print #f, FormatCommentsForInit(1, d)
            Next i
            Print #f, ""

            If (r.v(sRegPre & "ABCON_ABDETEN") = 1) Then
                d = "enable interrupt generation after successful autobaud detection"
                Print #f, FormatCommentsForInit(1, d)
            End If

            If (r.v(sRegPre & "ABCON_FCDETEN") = 1) Then
                d = "enable interrupt generation also after first character of two-byte frame detection"
                Print #f, FormatCommentsForInit(1, d)
            End If
    
            If (r.v(sRegPre & "ABCON_ABSTEN") = 1) Then
                d = "enable interrupt generation after a start bit has been detected"
                Print #f, FormatCommentsForInit(1, d)
            End If

            If (r.v(sRegPre & "CON_R") = 0) Then           
                d = "baud rate generator is disabled (" & sVarPre & " inactive)"
                Print #f, FormatCommentsForInit(2, d)
            End If
          
            Print #f, ""
            ' the ABEN bit (bit 0) is set at the end of the function
            v = r.sfr(sRegPre & "ABCON") And &HFFFFFFFE
            Print #f, FormatCodeStatemente(2, sRegPre & "ABCON", "=", _
                      FormatTextOutput(v, "H4"), _
                      "load " & sVarPre & " autobaud control register")
            v = r.sfr(sRegPre & "FDV")

            Print #f, FormatCodeStatemente(2, sRegPre & "FDV", "=", _
                      FormatTextOutput(v, "H4"), _
                      "load " & sVarPre & " fractional divider register")
            Print #f, ""
            Print #f, FormatSectionHeader(2, 1, "Configuration of the general Settings:")

        End If  ' IsAutobaudSelected 

        If (Not IsAutobaudSelected(r, vIndex)) Then
            If (r.v(sRegPre & "CON_REN") = 0 ) Then 
                d = "receiver is disabled"
            Else
                d = "receiver is enabled"
            End If
            Print #f, FormatCommentsForInit(1, d)
        End If

        If (r.v(sRegPre & "CON_FDE") = 1 ) Then 
            d = "fractional divider is enabled"
            Print #f, FormatCommentsForInit(1, d)
        End If
              
        If (r.v(sRegPre & "CON_OEN") = 1 ) Then 
            d = "check overrun error"
            Print #f, FormatCommentsForInit(1, d)
        End If
  
        If (r.v(sRegPre & "CON_FEN") = 1 ) Then 
            d = "check framing error"
            Print #f, FormatCommentsForInit(1, d)
        End If

        If (r.v(sRegPre & "CON_PEN_RXDI") = 1 ) Then 
            If (r.v(sRegPre & "CON_M") = 2 ) Then
                d = "RXD input is inverted"
            Else
                d = "check parity"
            End If
            Print #f, FormatCommentsForInit(1, d)
        End If

        Print #f, ""
        If (IsAutobaudSelected(r, vIndex)) Then
            d = "with a successful autobaud detection the mode contol bits (M), the parity bit (ODD) " &  _
                "and the reload value for the baud rate timer are overwritten." 
            Print #f, FormatCommentsForInit(1, d)
            Print #f, ""
        End If

     '   the R bit (bit 15) is set at the end of the function
        v = r.sfr(sRegPre & "CON") And &H7FFF
        Print #f, FormatCodeStatemente(2, sRegPre & "CON", "=", _
                  FormatTextOutput(v, "H4"), _
                  "load " & sVarPre & " control register")
        Print #f, ""

        If (IsFIFOAvailable(r, vIndex)) Then
            '------------------------------------------------------------------------------------------------
            ' FIFO Configuration
            '------------------------------------------------------------------------------------------------
            Print #f, FormatSectionHeader(2, 1, "FIFO Configuration:")

            If (r.v(sRegPre & "RXFCON_RXFEN") = 0 ) Then 
                d = "receive FIFO is disabled"
                Print #f, FormatCommentsForInit(1, d)
            Else
                d = "receive FIFO is enabled"
                Print #f, FormatCommentsForInit(1, d)

                If (r.v(sRegPre & "RXFCON_RXTMEN") = 1) Then 
                    d = "receive FIFO transparent mode is enabled"
                    Print #f, FormatCommentsForInit(1, d)
                Else
                    d = "receive interrupt trigger level = " & r.v(sRegPre & "RXFCON_RXFITL")
                    Print #f, FormatCommentsForInit(1, d)
                End If
            End If

            If (r.v(sRegPre & "TXFCON_TXFEN") = 0 ) Then 
                d = "transmit FIFO is disabled"
                Print #f, FormatCommentsForInit(1, d)
            Else
                d = "transmit FIFO is enabled"
                Print #f, FormatCommentsForInit(1, d)

                If (r.v(sRegPre & "TXFCON_TXTMEN") = 1) Then 
                    d = "transmit FIFO transparent mode is enabled"
                    Print #f, FormatCommentsForInit(1, d)
                Else
                    d = "transmit interrupt trigger level = " & r.v(sRegPre & "TXFCON_TXFITL")
                    Print #f, FormatCommentsForInit(1, d)
                End If
          End If
          
          Print #f, ""
          ' set the RXFFLU bit
          v = r.sfr(sRegPre & "RXFCON") Or &H02
          Print #f, FormatCodeStatemente(2, sRegPre & "RXFCON", "=", _
                    FormatTextOutput(v, "H4"), _
                    "load " & sVarPre & " receive FIFO control register")

          ' set the TXFFLU bit
          v = r.sfr(sRegPre & "TXFCON") Or &H02
          Print #f, FormatCodeStatemente(2, sRegPre & "TXFCON", "=", _
                    FormatTextOutput(v, "H4"), _
                    "load " & sVarPre & " transmit FIFO control register")
          Print #f, ""
        End If  ' IsFIFOAvailable

        '------------------------------------------------------------------------------------------------
        ' Configuration of the used ASCx Port Pins:
        '------------------------------------------------------------------------------------------------
        Print #f, FormatSectionHeader(2, 1, "Configuration of the used " & sVarPre & " Port Pins:")

        sAlternatePinName(1) = sVarPre & "_TxD_1"
        sAlternatePinName(2) = sVarPre & "_RxD_1_O"
        sAlternatePinName(3) = sVarPre & "_RxD_1_I"
        Call SetAlternatePinStatements(f, r, sAlternatePinName())

        Print #f, ""
 
        '------------------------------------------------------------------------------------------------
        ' Configuration of the used ASC Interrupts:
        '------------------------------------------------------------------------------------------------
        Print #f, FormatSectionHeader(2, 1, "Configuration of the used " & sVarPre & " Interrupts:")
        ' transmit interrupt
        Call GenerateIR(f, r, sRegPre & "TIC", "Tx")
        ' transmit buffer interrupt
        Call GenerateIR(f, r, sRegPre & "TBIC", "TxBuffer")
        ' receive interrupt
        Call GenerateIR(f, r, sRegPre & "RIC", "Rx")
        ' error interrupt
        Call GenerateIR(f, r, sRegPre & "EIC", "Error")

        If (IsAutobaudAvailable(r, vIndex)) Then
          ' autobaud interrupt
          Call GenerateIR(f, r, sRegPre & "ABIC", "Autobaud")
        End If

        If (r.v(sVarPre & "_USE_TxBufFree") = 1 Or r.v(sVarPre & "_USE_TxDataReady") = 1) Then
            Print #f, ""
            '------------------------------------------------------------------------------------------------
            ' Default Settings for Service Request Flags:
            '------------------------------------------------------------------------------------------------
            Print #f, FormatSectionHeader(2, 0, "Default Settings for Service Request Flags:")
            If (r.v(sVarPre & "_USE_TxBufFree") = 1) Then
                Print #f, FormatCodeStatemente(2, sRegPre & "TBIC_IR", "=", 1, "indicates that the transmit buffer is empty")
            End If
            If (r.v(sVarPre & "_USE_TxDataReady") = 1) Then
                Print #f, FormatCodeStatemente(2, sRegPre & "TIC_IR", "=", 1, "indicates that the transmit register is empty")
            End If
        End If
    
      
        Print #f, ""
        Print #f, uc.GetUserCode("(" & sVarPre & "_Function,3)",2) 
        Print #f, ""

        If (r.v(sRegPre & "CON_R") = 1) Then
            v = 2 ^ 15 ' CON_R
            Print #f, FormatCodeStatemente(2, sRegPre & "CON", "|=", _
                      FormatTextOutput(v, "H4"), "enable baud rate generator")
        End If

        If (IsAutobaudSelected(r, vIndex)) Then
            v = 1
            Print #f, FormatCodeStatemente(2, sRegPre & "ABCON", "|=", _
                      FormatTextOutput(v, "H4"), "enable autobaud detection")
        End If

        Print #f, ""

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

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASC_vSendData function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SendData"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
        Print #f, ""
        Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
        Print #f, "{"

	      If (IsSynchronMode(r, vIndex)) Then	
            If (GetPinFunction(r, sVarPre & "_RxD_1_O") = 2) Then ' if output
                d = GetDirectionRegNameFromAlternatName(sVarPre & "_RxD_1_O")
            End If
            Print #f, "  " & d & "  = 1;        // set direction control" 
            Print #f, ""
	      End If

        If (r.v(sVarPre & "_USE_Tx") = 0) Then
            Print #f, "  " & sRegPre & "TBIC_IR = 0;        //  reset transmit buffer interrupt request "
        End If

        Print #f, "  " & sRegPre & "TBUF    = uwData;   //  load transmit buffer register"
        Print #f, ""
        Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
        Print #f, ""
        Print #f, ""
    End If


    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASC_uwGetData function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetData"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
        Print #f, ""
        Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
        Print #f, "{"

        If (r.v(sVarPre & "_USE_Rx") = 0) Then
            Print #f, "  " & sRegPre & "RIC_IR = 0;       // reset receive interrupt request"
        End If

        Print #f, "  return(" & sRegPre & "RBUF);     // return receive buffer register"
        Print #f, ""
        Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
        Print #f, ""
        Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASCx_viTx function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "Tx"
    If (IsISRSelected(r, sVarPre, sRegPre & "TIC", n)) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
        Print #f, ""
        Print #f, GenerateIsrDeclaration(r, r.v(sVarPre & "_NAME_" & n), sRegPre & "TINT")
        Print #f, "{"
        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & ",2)",2)
        Print #f, ""
        Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
        Print #f, ""
        Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASCx_viRx function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "Rx"
    If (IsISRSelected(r, sVarPre, sRegPre & "RIC", n)) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
        Print #f, ""
        Print #f, GenerateIsrDeclaration(r, r.v(sVarPre & "_NAME_" & n), sRegPre & "RINT")
        Print #f, "{"
        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & ",2)",2)
        Print #f, ""
        Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
        Print #f, ""
        Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASCx_viError function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "Error"
    If (IsISRSelected(r, sVarPre, sRegPre & "EIC", n)) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
        Print #f, ""
        Print #f, GenerateIsrDeclaration(r, r.v(sVarPre & "_NAME_" & n), sRegPre & "EINT")
        Print #f, "{"
        Print #f, uc.GetUserCode("(" & n & ",2)",2) 
        Print #f, ""
        Print #f, ""
      
        If (r.v(sRegPre & "CON_M") = 3 Or r.v(sRegPre & "CON_M") = 7) Then  
            If (r.v(sRegPre & "CON_PEN_RXDI") = 1) Then
                Print #f, "  if (" & sRegPre & "CON_PE)  // if parity error"
                Print #f, "  {"
                Print #f, "    " & sRegPre & "CON_PE = 0;"
                Print #f, ""
                Print #f, uc.GetUserCode("(" & n & ",3)",4) 
                Print #f, "  }"
            End If
        End If

        If (r.v(sRegPre & "CON_FEN") = 1) Then
            Print #f, "  if (" & sRegPre & "CON_FE)  // if framing error"
            Print #f, "  {"
            Print #f, "    " & sRegPre & "CON_FE = 0;"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",4)",4) 
            Print #f, "  }"
        End If

        If (r.v(sRegPre & "CON_OEN") = 1) Then
            Print #f, "  if (" & sRegPre & "CON_OE)  // if overrun error"
            Print #f, "  {"
            Print #f, "    " & sRegPre & "CON_OE = 0;"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",5)",4) 
            Print #f, "  }"
        End If

        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & ",7)",2) 
        Print #f, ""
        Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
        Print #f, ""
        Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASCx_viTxBuffer function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "TxBuffer"
    If (IsISRSelected(r, sVarPre, sRegPre & "TBIC", n)) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
        Print #f, ""
        Print #f, GenerateIsrDeclaration(r, r.v(sVarPre & "_NAME_" & n), sRegPre & "TBINT")
        Print #f, "{"
        Print #f, ""
        Print #f, uc.GetUserCode("(" & n & ",2)",2)
        Print #f, ""
        Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
        Print #f, ""
        Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASC_ubTxDataReady function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "TxDataReady"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
        Print #f, ""
        Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
        Print #f, "{"
        Print #f, "  ubyte ubReturnValue;"
        Print #f, ""
        Print #f, "  ubReturnValue = 0;"
        Print #f, ""
        Print #f, "  if(" & sRegPre & "TIC_IR)               // if sending of data is terminated"
        Print #f, "  {"
        Print #f, "    ubReturnValue = 1;"
        Print #f, "    " & sRegPre & "TIC_IR   = 0;"
        Print #f, "  }"
        Print #f, "  return(ubReturnValue);         // return receive buffer register"
        Print #f, ""
        Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
        Print #f, ""
        Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASCx_ubErrorCheck function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ErrorCheck"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
        Print #f, ""
        Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
        Print #f, "{"
        Print #f, "  ubyte ReturnValue;"
        Print #f, ""
        Print #f, "  ReturnValue = 0;"
        Print #f, ""
        Print #f, "  if(" & sRegPre & "EIC_IR)"
        Print #f, "  {"

        If (r.v(sRegPre & "CON_FEN") = 1) Then
            Print #f, "    if (" & sRegPre & "CON_FE)          // if framing error"
            Print #f, "    {"
            Print #f, "      " & sRegPre & "CON_FE  = 0;"
            Print #f, "      ReturnValue |= 0x01;"
            Print #f, "    }"
        End If

        If (r.v(sRegPre & "CON_PEN_RXDI") = 1 AND r.v(sRegPre & "CON_M") <> 2) Then
            Print #f, "    if (" & sRegPre & "CON_PE)          // if parity error"
            Print #f, "    {"
            Print #f, "      " & sRegPre & "CON_PE  = 0;"
            Print #f, "      ReturnValue |= 0x02;"
            Print #f, "    }"
        End If

        If (r.v(sRegPre & "CON_OEN") = 1) Then
            Print #f, "    if (" & sRegPre & "CON_OE)          // if overrun error"
            Print #f, "    {"
            Print #f, "      " & sRegPre & "CON_OE  = 0;"
            Print #f, "      ReturnValue |= 0x04;"
            Print #f, "    }"
        End If

        Print #f, "    " & sRegPre & "EIC_IR = 0;"
        Print #f, "  }"
        Print #f, ""
        Print #f, "  return(ReturnValue);"
        Print #f, ""
        Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
        Print #f, ""
        Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASC_vReceiverOn function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ReceiverOn"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
        Print #f, ""
        Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
        Print #f, "{"

	      If (IsSynchronMode(r, vIndex)) Then	
            If (GetPinFunction(r, sVarPre & "_RxD_1_O") = 2) Then ' if output
                d = GetDirectionRegNameFromAlternatName(sVarPre & "_RxD_1_O")
            End If
            Print #f, "  " & d & "  = 0;                 // set direction control" 
            Print #f, ""
            Print #f, ""
	      End If

        Print #f, "  " & sRegPre & "CON_REN = 1;               // enable the receiver"
        Print #f, ""
        Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
        Print #f, ""
        Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASC_ubOwnAddress function
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "OwnAddress"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
        Print #f, ""
        Print #f, FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n)  
        Print #f, "{"
        Print #f, ""
        Print #f, "  ubyte ubReturnValue;"
        Print #f, ""
        Print #f, "  ubReturnValue = 0;"
        Print #f, ""
        Print #f, "  " & sRegPre & "RIC_IR   = 0;   //  reset receive interrupt request flag"
        Print #f, "  "
        Print #f, "  if((" & sRegPre & "RBUF & 0x0ff) == (ubAddress & 0x0ff))"
        Print #f, "  {"
        Print #f, "    ubReturnValue = 1;"
        Print #f, "  }"
        Print #f, ""
        Print #f, "  return(ubReturnValue);"
        Print #f, ""
        Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
        Print #f, ""
        Print #f, ""
    End If


    If (IsAutobaudAvailable(r, vIndex)) Then
        '------------------------------------------------------------------------------------------------
        '------------------------------------------------------------------------------------------------
        ' ASCx_viAutobaud function
        '------------------------------------------------------------------------------------------------
        '------------------------------------------------------------------------------------------------
        n = "Autobaud"
        If (IsISRSelected(r, sVarPre, sRegPre & "ABIC", n)) Then
            Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
            Print #f, uc.GetUserCode("(" & n & ",1)",0)      'USER CODE for application specific descriptions'
            Print #f, ""
            Print #f, GenerateIsrDeclaration(r, r.v(sVarPre & "_NAME_" & n), sRegPre & "ABINT")
            Print #f, "{"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",2)",2)
            Print #f, ""

            If (r.v(sRegPre & "ABCON_FCDETEN") = 1) Then
                Print #f, "  if(" & sRegPre & "ABSTAT_FCSDET)  // if small 'a' character detected"
                Print #f, "  {"
                Print #f, uc.GetUserCode("(" & n & ",3)",4)
                Print #f, "  }"
                Print #f, "  if(" & sRegPre & "ABSTAT_FCCDET)  // if capital 'A' character detected"
                Print #f, "  {"
                Print #f, uc.GetUserCode("(" & n & ",4)",4)
                Print #f, "  }"
            End If

            Print #f, "  if(" & sRegPre & "ABSTAT_SCSDET)  // if small 't' character detected"
            Print #f, "  {"
            Print #f, uc.GetUserCode("(" & n & ",5)",4)
            Print #f, "  }"
            Print #f, "  if(" & sRegPre & "ABSTAT_SCCDET)  // if capital 'T' character detected"
            Print #f, "  {"
            Print #f, uc.GetUserCode("(" & n & ",6)",4)
            Print #f, "  }"
            Print #f, ""
            Print #f, uc.GetUserCode("(" & n & ",7)",2)
            Print #f, ""
            Print #f, "} //  End of function " & r.v(sVarPre & "_NAME_" & n)
            Print #f, ""
            Print #f, ""
        End If
    End If

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

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


'*****************************************************************************
'Description:
'        Generates the code for the ASC.h file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function ASCGenerateH(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo ASCGenerateHError
    
    Dim i As Integer
    Dim v As Variant
    Dim f As Variant
    Dim n As String
    Dim l As Long
    Dim d As String
    Dim dc As DAvECollection
    Dim uc As UserCode
    Dim func As FunctionClass
    Dim para As ParameterClass
    Dim sRegPre As String
    Dim sVarPre As String

    sRegPre = "ASC" & Format$(vIndex) & "_"
    sVarPre = "ASC" & Format$(vIndex)

    ASCGenerateH = False

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

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

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

    If (IsAutobaudAvailable(r, vIndex)) Then
        If (r.v(sVarPre & "_USE_GetAutobaudStatus") = 1) Then
            n = "AUTOBAUD_WAITING"
            Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "0x0010")
            n = "FIRST_CHARACTER_SMALL"
            Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "0x0001")
            n = "FIRST_CHARACTER_CAPITAL"
            Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "0x0002")
            n = "SECOND_CHARACTER_SMALL" 
            Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "0x0004")
            n = "SECOND_CHARACTER_CAPITAL"
            Print #f, FormatParameterStatement(r, para, sVarPre & "_PARA_" & n, "0x0008")
            Print #f, ""
        End If
    End If

    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,4)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_TYPEDEFS
    Print #f, ""  'DAvE type definitions'
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,5)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_IMPORTED_GLOBAL_VARIABLES
    Print #f, ""  'DAvE external declarations of imported global variables'
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,6)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_GLOBAL_VARIABLES
    Print #f, ""  'DAvE definition of global variables'
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,7)",0)
    Print #f, ""
    Print #f, ""
    Print #f, SECTION_GLOBAL_PROTOTYPES
    Print #f, ""  'DAvE declarations of functions which should become global'
    ' for each function in this 'func' object
    For l = 1 To func.FunctionCount
      ' get the function name which was insert in 'Include.inc' with
      'AddFunction()'
      d = func.FunctionName(l)
      ' get only the third part of the function name (e.g "Init")
      n = Right(d, Len(d) - Instr(d, "NAME") - Len("NAME"))
      ' only if the use variable of the function is selected and the function is a really function
      ' not a macro or a ISR
      If (r.v(sVarPre & "_USE_" & n) = 1 AND func.FunctionSort(d) = 1) Then
            Print #f, FormatFunctionDeclaration(r, func, d) & ";"
      End If
    Next l

    Print #f, ""
    Print #f, ""
    Print #f, uc.GetUserCode("(" & sVarPre & "_Header,8)",0)
    Print #f, ""
    Print #f, ""
                          'DAvE declarations of global macros'

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASCx_ubTxBufFree macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "TxBufFree"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                  " " & sRegPre & "TBIC_IR" 
        Print #f, ""                                                                                    
        Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASCx_ubRxDataReady macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "RxDataReady"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                  " " & sRegPre & "RIC_IR" 
        Print #f, ""                                                                                    
        Print #f, ""
    End If

    If (IsAutobaudAvailable(r, vIndex)) Then
        '------------------------------------------------------------------------------------------------
        '------------------------------------------------------------------------------------------------
        ' ASCx_ubGetAutobaudStatus macro
        '------------------------------------------------------------------------------------------------
        '------------------------------------------------------------------------------------------------
        n = "GetAutobaudStatus"
        If (r.v(sVarPre & "_USE_" & n) = 1) Then
            Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
            Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                      " " & sRegPre & "ABSTAT" 
            Print #f, ""                                                                                    
            Print #f, ""
        End If
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASCx_vReceiverOff macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "ReceiverOff"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                  " " & sRegPre & "CON_REN = 0" 
        Print #f, ""                                                                                    
        Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASCx_vSendSlaveAdr macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SendSlaveAdr"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                  " " & sRegPre & "TBUF = Address | 0x0100" 
        Print #f, ""                                                                                    
        Print #f, ""                                                                          
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASCx_vWakeUp macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "WakeUp"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                  " " & sRegPre & "CON = (" & sRegPre & "CON & 0xFFF8) | 0x0004" 
        Print #f, ""                                                                                    
        Print #f, ""                                                                       
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASCx_vGotoSleep macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GotoSleep"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                  " " & sRegPre & "CON = (" & sRegPre & "CON & 0xFFF8) | 0x0005" 
        Print #f, ""                                                                                    
        Print #f, ""                                                                          
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASCx_vSetBaudrate macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "SetBaudrate"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                  " " & sRegPre & "BG = Value" 
        Print #f, ""                                                                                    
        Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASCx_vStopBaudGen macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "StopBaudGen"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                  " " & sRegPre & "CON_R = 0" 
        Print #f, ""                                                                                    
        Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASCx_vStartBaudGen macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "StartBaudGen"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                  " " & sRegPre & "CON_R = 1" 
        Print #f, ""                                                                                    
        Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASCx_vFlushRxFIFO macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "FlushRxFIFO"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                  " " & sRegPre & "RXFCON_RXFFLU = 1" 
        Print #f, ""                                                                                    
        Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASCx_ubGetRxFIFOFillingLevel macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetRxFIFOFillingLevel"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                  " (" & sRegPre & "FSTAT & 0x003F)" 
        Print #f, ""                                                                                    
        Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASCx_vFlushTxFIFO macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "FlushTxFIFO"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                  " " & sRegPre & "TXFCON_TXFFLU = 1" 
        Print #f, ""                                                                                    
        Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    ' ASCx_ubGetTxFIFOFillingLevel macro
    '------------------------------------------------------------------------------------------------
    '------------------------------------------------------------------------------------------------
    n = "GetTxFIFOFillingLevel"
    If (r.v(sVarPre & "_USE_" & n) = 1) Then
        Print #f, FormatFunctionHeader(r, func, sVarPre & "_NAME_" & n)        
        Print #f, "#define " & FormatFunctionDeclaration(r, func, sVarPre & "_NAME_" & n) & _
                  " ((" & sRegPre & "FSTAT & 0x3F00) >> 8)" 
        Print #f, ""                                                                                    
        Print #f, ""
    End If

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

    n = "Tx"
    If (IsISRSelected(r, sVarPre, sRegPre & "TIC", n)) Then
            If (vIndex = 0) Then
                v = 42
            Else
                v = 72
            End If
            Print #f, "#define " & sRegPre & "TINT    " & FormatTextOutput(v, "H2")
    End If

    n = "TxBuffer"
    If (IsISRSelected(r, sVarPre, sRegPre & "TBIC", n)) Then
            If (vIndex = 0) Then
                v = 71
            Else
                v = 94
            End If
            Print #f, "#define " & sRegPre & "TBINT    " & FormatTextOutput(v, "H2")
    End If

    n = "Rx"
    If (IsISRSelected(r, sVarPre, sRegPre & "RIC", n)) Then
            If (vIndex = 0) Then
                v = 43
            Else
                v = 73
            End If
            Print #f, "#define " & sRegPre & "RINT    " & FormatTextOutput(v, "H2")
    End If

    n = "Error"
    If (IsISRSelected(r, sVarPre, sRegPre & "EIC", n)) Then
            If (vIndex = 0) Then
                v = 44
            Else
                v = 74
            End If
            Print #f, "#define " & sRegPre & "EINT    " & FormatTextOutput(v, "H2")
    End If

    If (IsAutobaudAvailable(r, vIndex)) Then
      n = "Autobaud"
      If (IsISRSelected(r, sVarPre, sRegPre & "ABIC", n)) Then
            If (vIndex = 0) Then
                v = 95
            Else
                v = 66
            End If
              Print #f, "#define " & sRegPre & "ABINT    " & FormatTextOutput(v, "H2")
      End If
    End If

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

