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:
'        T01
'-----------------------------------------------------------------------------
' Description:
'        This file contains functions for generating code for the
'        module 'T01'.
'
'-----------------------------------------------------------------------------
' History:
'
'        Rev. Author         Date        Description
'        ---------------------------------------------------------------------
'        0.1  Sunil          14-10-04    initial version
'        0.2  Sunil          29-12-04    module prefix added.
'
'
'
'*****************************************************************************


'*****************************************************************************
'Description:
'        This functions initiates the code generation for the DUMMY 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

    ' write files T01.h
    If (r.v("T01_USE_Init")) Then
      If (Not T01GenerateH(r, vIndex)) Then Exit Function
                   If (Not r.AddFile(r.s("T01_HFILE"), "H")) Then Exit Function
    End If

    ' write file T01.c
    If (r.v("T01_USE_Init")) Then
      If (Not T01GenerateC(r, vIndex)) Then Exit Function
                   If (Not r.AddFile(r.s("T01_CFILE"), "C")) Then Exit Function
    End If

        
    StartGenerateCode = True
    Exit Function
    
StartGenerateCodeError:
    Errorbox "Code generation for T01 failed", Error$
    Exit Function
End Function


'*****************************************************************************
'Description:
'        Generates the code for the T01.c file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function T01GenerateC(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo T01GenerateCError
    
    Dim i As Integer
    Dim f As Variant
    Dim v As Variant
    Dim d As String
    Dim dc As DAvECollection
    Dim uc As UserCode
    Dim func As FunctionClass
    
    T01GenerateC = 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("T01_CFILE"))

    'open T01.c
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v("T01_CFILE") For Output As f
    
    '------------------------------------------------------------------------------------------------
    ' module header
    '------------------------------------------------------------------------------------------------
    d = FormatModuleDescription(GetModuleHeaderC("T01"))
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), dc("T01").ModuleDescription, r.v("T01_CFILE"), r.v("MAIN_CPU_TYPE"), r.v("MAIN_COMPILER"), d)
          
            Print #f, uc.GetUserCode("(T01_General,1)", 0)   'USER CODE for application specific descriptions'
            Print #f, ""
            Print #f, Rtf.GenerateNote((r.v("T01_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("(T01_General,2)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_MACROS
            Print #f, ""  'DAvE local macro definitions'
            Print #f, uc.GetUserCode("(T01_General,3)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_DEFINES
            Print #f, ""  'DAvE local #define definitions'
            Print #f, uc.GetUserCode("(T01_General,4)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_TYPEDEFS
            Print #f, ""  'DAvE type defintions'
            Print #f, uc.GetUserCode("(T01_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("(T01_General,6)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_GLOBAL_VARIABLES
            Print #f, ""  'DAvE definitions of global variables'
            Print #f, uc.GetUserCode("(T01_General,7)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_EXTERNAL_PROTOTYPES
            Print #f, ""  'DAvE external declarations of imported functions'
            Print #f, uc.GetUserCode("(T01_General,8)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_LOCAL_PROTOTYPES
            Print #f, ""  'DAvE declarations of internal local functions'
            Print #f, uc.GetUserCode("(T01_General,9)", 0)
            Print #f, ""
            Print #f, ""
    '------------------------------------------------------------------------------------------------
    ' T01_vInit function
    '------------------------------------------------------------------------------------------------
    If (r.v("T01_USE_Init") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "T01_NAME_Init")
            Print #f, uc.GetUserCode("(T01_Init,1)", 0)     'USER CODE for application specific descriptions'
            Print #f, ""
                      '<--                     max. 78 characters                                -->'
            Print #f, FormatFunctionDeclaration(r, func, "T01_NAME_Init")
            Print #f, "{"
            Print #f, uc.GetUserCode("(T01_Init,2)", 2)
            Print #f, ""

            Print #f, FormatSectionHeader(2, 1, "Timer 0")
            
            If r.v(T01_PREFIX & "TMOD_T0S") = 1 Then
                Print #f, FormatComments(2, 1, "", "", "Counter operation is enabled.")
            End If
            
      Select Case GetTimer0Mode(r)
        Case 0
            Print #f, FormatComments(2, 1, "", "", "Mode 0: 8-bit timer (TH0) with a divide-by-32 prescaler (TL0)")
            Print #f, FormatComments(2, 1, "", "", "the timer 0 overflow is " & GetTimerOverflow(r, 0, 0) & " " & TXT_US)
        Case 1
            Print #f, FormatComments(2, 1, "", "", "Mode 1: 16-bit timer (TH0, TL0)")
            Print #f, FormatComments(2, 1, "", "", "the timer 0 overflow is " & GetTimerOverflow(r, 0, 1) & " " & TXT_US)
        Case 2
            Print #f, FormatComments(2, 1, "", "", "Mode 2: 8-bit timer (TL0) with 8-bit auto-reload (TH0)")
            Print #f, FormatComments(2, 1, "", "", "the timer 0 overflow, calculated from the reload-value, is " & GetTimerOverflow(r, 0, 2) & " " & TXT_US)
        Case 3
            Print #f, FormatComments(2, 1, "", "", "Mode 3: one 8-bit timer in (TL0) interrupt controlled by ET0 (when enabled)")
            Print #f, FormatComments(2, 1, "", "", "+ one 8-bit timer in (TH0) interrupt controlled by ET1 (when enabled)")
            Print #f, FormatComments(2, 1, "", "", "the TL0 timer overflow is " & GetTimerOverflow(r, 0, 3) & " " & TXT_US)
            Print #f, FormatComments(2, 1, "", "", "the TH0 timer overflow is " & GetTimerOverflow(r, 1, 3) & " " & TXT_US)
      End Select

               
    If (r.v(T01_PREFIX & "TMOD_GATE0") = 1) Then
            Print #f, FormatComments(2, 1, "", "", "timer/counter 0 is enabled only while pin EXINT0 is high (GATE0)")
    End If
            Print #f, ""
            Print #f, ""


            Print #f, FormatSectionHeader(2, 1, "Timer 1")
            
            If r.v(T01_PREFIX & "TMOD_T1S") = 1 Then
                Print #f, FormatComments(2, 1, "", "", "Counter operation is enabled.")
            End If
            
      Select Case GetTimer1Mode(r)
        Case 0
            Print #f, FormatComments(2, 1, "", "", "Mode 0: 8-bit timer (TH1) with a divide-by-32 prescaler (TL1)")
          If (GetTimer0Mode(r) <> 3) Then
            Print #f, FormatComments(2, 1, "", "", "the timer 1 overflow is " & GetTimerOverflow(r, 1, 0) & " " & TXT_US)
          End If
        Case 1
            Print #f, FormatComments(2, 1, "", "", "Mode 1: 16-bit timer (TH1, TL1)")
          If (GetTimer0Mode(r) <> 3) Then
            Print #f, FormatComments(2, 1, "", "", "the timer 1 overflow is " & GetTimerOverflow(r, 1, 1) & " " & TXT_US)
          End If
        Case 2
            Print #f, FormatComments(2, 1, "", "", "Mode 2: 8-bit timer (TL1) with 8-bit auto-reload (TH1)")
          If (GetTimer0Mode(r) <> 3) Then
            Print #f, FormatComments(2, 1, "", "", "the timer 1 overflow, calculated from the reload-value, is " & GetTimerOverflow(r, 1, 2) & " " & TXT_US)
          End If
        Case 3
            Print #f, FormatComments(2, 1, "", "", "Mode 3: timer holds the count")
      End Select

           
    If (r.v(T01_PREFIX & "TMOD_GATE1") = 1) Then
            Print #f, FormatComments(2, 1, "", "", "timer/counter 1 is enabled only while pin EXINT1 is high (GATE1)")
    End If
            Print #f, ""
            Print #f, ""
   
    
    ' init of TMOD
            Print #f, "  " & SfrName(r, T01_PREFIX & "TMOD") & " = " & FormatTextOutput(r.sfr(T01_PREFIX & "TMOD"), "H2") & ";"
            Print #f, ""
 
    ' init of timer registers
            Print #f, "  " & SfrName(r, T01_PREFIX & "TL0") & " = " & FormatTextOutput(r.sfr(T01_PREFIX & "TL0"), "H2") & ";"
            Print #f, "  " & SfrName(r, T01_PREFIX & "TH0") & " = " & FormatTextOutput(r.sfr(T01_PREFIX & "TH0"), "H2") & ";"
            Print #f, ""
            Print #f, "  " & SfrName(r, T01_PREFIX & "TL1") & " = " & FormatTextOutput(r.sfr(T01_PREFIX & "TL1"), "H2") & ";"
            Print #f, "  " & SfrName(r, T01_PREFIX & "TH1") & " = " & FormatTextOutput(r.sfr(T01_PREFIX & "TH1"), "H2") & ";"
            Print #f, ""
            
    If ((r.v(T01_PREFIX & "TMOD_GATE0") = 1) Or (r.v(T01_PREFIX & "TMOD_GATE1") = 1)) Then
            Print #f, "  // Bypass edge detection circuitry"
            Print #f, "  SFR_PAGE(" & SCU_PAGE_NAME & "0, SST2);  // switch to page 0"
            Print #f, "  " & SfrName(r, SCU_PREFIX & "EXICON0") & " |= " & FormatTextOutput(r.v(T01_PREFIX & "TMOD_GATE1") * &HC + r.v(T01_PREFIX & "TMOD_GATE0") * &H3, "H2") & ";"
            Print #f, "  SFR_PAGE(" & SCU_PAGE_NAME & "0, RST2);  // restore the old SCU page"
    End If
    
    If r.v(T01_PREFIX & "TMOD_T0S") = 1 And r.v(T01_PREFIX & "TMOD_T1S") = 1 Then
        Print #f, ""
        Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "3, noSST);", "", "", "switch to page 3")
        Print #f, FormatCodeStatemente(2, SCU_PREFIX & "MODPISEL2", "|=", "(ubyte)0x03", "configure peripheral input select register2")
        Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, noSST);", "", "", "switch to page 0")
    End If
    
    If r.v(T01_PREFIX & "TMOD_T0S") = 1 And r.v(T01_PREFIX & "TMOD_T1S") = 0 Then
        Print #f, ""
        Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "3, noSST);", "", "", "switch to page 3")
        Print #f, FormatCodeStatemente(2, SCU_PREFIX & "MODPISEL2", "|=", "(ubyte)0x01", "configure peripheral input select register2")
        Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, noSST);", "", "", "switch to page 0")
    End If
    
    If r.v(T01_PREFIX & "TMOD_T0S") = 0 And r.v(T01_PREFIX & "TMOD_T1S") = 1 Then
        Print #f, ""
        Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "3, noSST);", "", "", "switch to page 3")
        Print #f, FormatCodeStatemente(2, SCU_PREFIX & "MODPISEL2", "|=", "(ubyte)0x02", "configure peripheral input select register2")
        Print #f, FormatCodeStatemente(2, "SFR_PAGE(" & SCU_PAGE_NAME & "0, noSST);", "", "", "switch to page 0")
    End If
    
    
    If (r.v(CORE_PREFIX & "IEN0_ET0") = 1) Then
            Print #f, FormatComments(2, 1, "", "", "Enable Timer 0 interrupt")
            Print #f, "  " & SfrBitStatement(r, CORE_PREFIX & "IEN0_ET0", 1)
    End If

    If (r.v(CORE_PREFIX & "IEN0_ET1") = 1) Then
            Print #f, FormatComments(2, 1, "", "", "Enable Timer 1 interrupt")
            Print #f, "  " & SfrBitStatement(r, CORE_PREFIX & "IEN0_ET1", 1)
    End If

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

    If (r.v(T01_PREFIX & "TCON_TR0") = 1) Then
            Print #f, FormatComments(2, 1, "", "", "Start Timer 0")
            Print #f, "  " & SfrBitStatement(r, T01_PREFIX & "TCON_TR0", 1)
    End If

    If (r.v(T01_PREFIX & "TCON_TR1") = 1) Then
            Print #f, FormatComments(2, 1, "", "", "Start Timer 1")
            Print #f, "  " & SfrBitStatement(r, T01_PREFIX & "TCON_TR1", 1)
    End If

            Print #f, "} " & END_OF_FUNCTION & r.v("T01_NAME_Init")
            Print #f, ""
            Print #f, ""
    End If
    '------------------------------------------------------------------------------------------------
    ' T01_IsrTmr0 function
    '------------------------------------------------------------------------------------------------
'    If (r.v("T01_USE_IsrTmr0") = 1) Then
'    Print #f, FormatFunctionHeader(r, func, "T01_NAME_IsrTmr0")
'            Print #f, uc.GetUserCode("(T01_IsrTmr0,1)", 0) 'USER CODE for application specific descriptions'
'            Print #f, ""
'
'            Print #f, IsrDeclaration(r, r.v("T01_NAME_IsrTmr0"), "T0INT")
'            Print #f, "{"
'            Print #f, ""
'            Print #f, uc.GetUserCode("(T01_IsrTmr0,2)", 2)
'            Print #f, ""
'            Print #f, "} " & END_OF_FUNCTION & r.v("T01_NAME_IsrTmr0")
'            Print #f, ""
'            Print #f, ""
'    End If
    If (r.v("T01_USE_IsrTmr0") = 1) Then
       If (r.v("ISR_CHOICE") = 1) Then
            Print #f, FormatFunctionHeader(r, func, "T01_NAME_IsrTmr0")
            Call GetDescriptionForISR(r, f)
            Print #f, uc.GetUserCode("(T01_IsrTmr0,1)", 0) 'USER CODE for application specific descriptions'
            Print #f, ""
            
            Print #f, IsrDeclaration(r, r.v("T01_NAME_IsrTmr0"), "T0INT")
            Print #f, "{"
            Print #f, ""
                     
            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();", "", "", "")
            Print #f, ""
            
            Print #f, uc.GetUserCode("(T01_IsrTmr0,2)", 2)
            Print #f, ""
            
            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("T01_NAME_IsrTmr0")
            Print #f, ""
            Print #f, ""
        ElseIf (r.v("ISR_CHOICE") = 2) Then
            Print #f, FormatFunctionHeader(r, func, "T01_NAME_IsrTmr0")
            Call GetDescriptionForISR(r, f)
            Print #f, uc.GetUserCode("(T01_IsrTmr0,1)", 0) 'USER CODE for application specific descriptions'
            Print #f, ""
            
            Print #f, IsrDeclaration(r, r.v("T01_NAME_IsrTmr0"), "T0INT")
            Print #f, "{"
            Print #f, ""
            Print #f, uc.GetUserCode("(T01_IsrTmr0,2)", 2)
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("T01_NAME_IsrTmr0")
            Print #f, ""
            Print #f, ""
        End If
    End If

    '------------------------------------------------------------------------------------------------
    ' T01_IsrTmr1 function
    '------------------------------------------------------------------------------------------------
'    If (r.v("T01_USE_IsrTmr1") = 1) Then
'    Print #f, FormatFunctionHeader(r, func, "T01_NAME_IsrTmr1")
'            Print #f, uc.GetUserCode("(T01_IsrTmr1,1)", 0) 'USER CODE for application specific descriptions'
'            Print #f, ""
'
'            Print #f, IsrDeclaration(r, r.v("T01_NAME_IsrTmr1"), "T1INT")
'            Print #f, "{"
'            Print #f, ""
'            Print #f, uc.GetUserCode("(T01_IsrTmr1,2)", 2)
'            Print #f, ""
'            Print #f, "} " & END_OF_FUNCTION & r.v("T01_NAME_IsrTmr1")
'            Print #f, ""
'            Print #f, ""
'    End If
    If (r.v("T01_USE_IsrTmr1") = 1) Then
       If (r.v("ISR_CHOICE") = 1) Then
            Print #f, FormatFunctionHeader(r, func, "T01_NAME_IsrTmr1")
            Call GetDescriptionForISR(r, f)
            Print #f, uc.GetUserCode("(T01_IsrTmr1,1)", 0) 'USER CODE for application specific descriptions'
            Print #f, ""
                      
            Print #f, IsrDeclaration(r, r.v("T01_NAME_IsrTmr1"), "T1INT")
            Print #f, "{"
            Print #f, ""
            
            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();", "", "", "")
            Print #f, ""
                       
            Print #f, uc.GetUserCode("(T01_IsrTmr1,2)", 2)
            Print #f, ""
            
            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("T01_NAME_IsrTmr1")
            Print #f, ""
            Print #f, ""
       ElseIf (r.v("ISR_CHOICE") = 2) Then
            Print #f, FormatFunctionHeader(r, func, "T01_NAME_IsrTmr1")
            Call GetDescriptionForISR(r, f)
            Print #f, uc.GetUserCode("(T01_IsrTmr1,1)", 0) 'USER CODE for application specific descriptions'
            Print #f, ""
                      
            Print #f, IsrDeclaration(r, r.v("T01_NAME_IsrTmr1"), "T1INT")
            Print #f, "{"
            Print #f, ""
            Print #f, uc.GetUserCode("(T01_IsrTmr1,2)", 2)
            Print #f, ""
            Print #f, "} " & END_OF_FUNCTION & r.v("T01_NAME_IsrTmr1")
            Print #f, ""
            Print #f, ""
       End If
    End If
 '------------------------------------------------------------------------------------------------
            Print #f, uc.GetUserCode("(T01_General,10)", 0)
            Print #f, ""
    
    'close file
    Close f
    
    T01GenerateC = True
    Exit Function
    
T01GenerateCError:
    Close f
    Exit Function
End Function


'*****************************************************************************
'Description:
'        Generates the code for the T01.h file.
'-----------------------------------------------------------------------------
'Parameters:
'        r                reference to the Registers object
'        vIndex           a parameter
'-----------------------------------------------------------------------------
'Returns:
'        True            on successfull operation
'        False           else
'*****************************************************************************
Function T01GenerateH(r As ProjectData, vIndex As Variant) As Boolean
    On Error GoTo T01GenerateHError
    
    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
   
    T01GenerateH = 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("T01_HFILE"))
    
    'open T01.h
    f = FreeFile
    Open CStr(StripFilenameAndExtension(r.LoadedFile)) & "\" & r.v("T01_HFILE") For Output As f
    
    '------------------------------------------------------------------------------------------------
    ' module header
    '------------------------------------------------------------------------------------------------
    d = FormatModuleDescription(GetModuleHeaderH("T01"))
    Print #f, Rtf.GenerateModuleHeader(DefGetValue("ProductVersion"), StripPath(r.LoadedFile), dc("T01").ModuleDescription, r.v("T01_HFILE"), r.v("MAIN_CPU_TYPE"), r.v("MAIN_COMPILER"), d)
            
            Print #f, uc.GetUserCode("(T01_Header,1)", 0)
            Print #f, ""
            Print #f, Rtf.GenerateNote((r.v("T01_Notes")))
            Print #f, ""
            Print #f, "#ifndef _" & StripExtension(r.v("T01_HFILE")) & "_H_"
            Print #f, "#define _" & StripExtension(r.v("T01_HFILE")) & "_H_"
            Print #f, ""
            Print #f, SECTION_PROJECT_INCLUDES
            Print #f, ""  'DAvE includes'
            Print #f, uc.GetUserCode("(T01_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("(T01_Header,3)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_DEFINES
            Print #f, ""  'DAvE global #define definitions'
            Print #f, uc.GetUserCode("(T01_Header,4)", 0)
            Print #f, ""

            Print #f, "#define " & FillRight(20, r.v("T01_PARA_T01_TIMER_0")) & " 0"
            Print #f, "#define " & FillRight(20, r.v("T01_PARA_T01_TIMER_1")) & " 1"
            Print #f, "#define " & FillRight(20, r.v("T01_PARA_T01_MODE_0")) & " " & FormatTextOutput(0, "H2")
            Print #f, "#define " & FillRight(20, r.v("T01_PARA_T01_MODE_1")) & " " & FormatTextOutput(&H1 & String(BFldPos(r, T01_PREFIX & "TMOD_T1M"), "0"), "H2")
            Print #f, "#define " & FillRight(20, r.v("T01_PARA_T01_MODE_2")) & " " & FormatTextOutput(&H2 & String(BFldPos(r, T01_PREFIX & "TMOD_T1M"), "0"), "H2")

            Print #f, ""
            Print #f, SECTION_TYPEDEFS
            Print #f, ""  'DAvE type definitions'
            Print #f, uc.GetUserCode("(T01_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("(T01_Header,6)", 0)
            Print #f, ""
            Print #f, ""
            Print #f, SECTION_GLOBAL_VARIABLES
            Print #f, ""  'DAvE definition of global variables'
            Print #f, uc.GetUserCode("(T01_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("T01_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("(T01_Header,8)", 0)
            Print #f, ""
            Print #f, ""
                          'DAvE declarations of global macros'
    '------------------------------------------------------------------------------------------------
    ' T01_StartTmr macro
    '------------------------------------------------------------------------------------------------
    If (r.v("T01_USE_StartTmr") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "T01_NAME_StartTmr")

            Print #f, "#define " & FillRight(25, r.v("T01_NAME_StartTmr") & "(TimerNr)") & " _TR(TimerNr) = 1"
            Print #f, ""
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' T01_StopTmr macro
    '------------------------------------------------------------------------------------------------
    If (r.v("T01_USE_StopTmr") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "T01_NAME_StopTmr")

            Print #f, "#define " & FillRight(25, r.v("T01_NAME_StopTmr") & "(TimerNr)") & " _TR(TimerNr) = 0"
            Print #f, ""
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' T01_ClearTmr macro
    '------------------------------------------------------------------------------------------------
    If (r.v("T01_USE_ClearTmr") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "T01_NAME_ClearTmr")

            Print #f, "#define " & FillRight(25, r.v("T01_NAME_ClearTmr") & "(TimerNr)") & " { _TH(TimerNr) = 0; _TL(TimerNr) = 0; }"
            Print #f, "#define " & FillRight(25, r.v("T01_NAME_ClearTmr") & "H(TimerNr)") & " _TH(TimerNr) = 0"
            Print #f, "#define " & FillRight(25, r.v("T01_NAME_ClearTmr") & "L(TimerNr)") & " _TL(TimerNr) = 0"
            Print #f, ""
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' T01_ReadTmr macro
    '------------------------------------------------------------------------------------------------
    If (r.v("T01_USE_ReadTmr") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "T01_NAME_ReadTmr")
            Print #f, "#define " & FillRight(25, r.v("T01_NAME_ReadTmr") & "(TimerNr)") & " ( ((uword) _TH(TimerNr)) << 8 ) + _TL(TimerNr)"
            Print #f, "#define " & FillRight(25, r.v("T01_NAME_ReadTmr") & "H(TimerNr)") & " _TH(TimerNr)"
            Print #f, "#define " & FillRight(25, r.v("T01_NAME_ReadTmr") & "L(TimerNr)") & " _TL(TimerNr)"
            Print #f, ""
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' T01_LoadTmr macro
    '------------------------------------------------------------------------------------------------
    If (r.v("T01_USE_LoadTmr") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "T01_NAME_LoadTmr")

            Print #f, "#define " & r.v("T01_NAME_LoadTmr") & "(TimerNr, Value) \"
            Print #f, "         { _TH(TimerNr) = (ubyte) ( ((uword) (Value)) >> 8 ); \"
            Print #f, "           _TL(TimerNr) = ((ubyte) (Value)) & 0xff; }"
            Print #f, "#define " & FillRight(25, r.v("T01_NAME_LoadTmr") & "H(TimerNr, Value)") & " _TH(TimerNr) = Value"
            Print #f, "#define " & FillRight(25, r.v("T01_NAME_LoadTmr") & "L(TimerNr, Value)") & " _TL(TimerNr) = Value"
            Print #f, ""
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' T01_StartTmr1 macro
    '------------------------------------------------------------------------------------------------
    If (r.v("T01_USE_StartTmr1") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "T01_NAME_StartTmr1")

            Print #f, "#define " & FillRight(25, r.v("T01_NAME_StartTmr1") & "(Mode)") & " " & _
                      SfrName(r, T01_PREFIX & "TMOD") & " = " & SfrName(r, T01_PREFIX & "TMOD") & _
                      " & ~(ubyte)" & FormatTextOutput(&H3 & String(BFldPos(r, T01_PREFIX & "TMOD_T1M"), "0"), "H2") & " | (Mode)"
            Print #f, ""
            Print #f, ""
    End If

    '------------------------------------------------------------------------------------------------
    ' T01_StopTmr1 macro
    '------------------------------------------------------------------------------------------------
    If (r.v("T01_USE_StopTmr1") = 1) Then
    Print #f, FormatFunctionHeader(r, func, "T01_NAME_StopTmr1")

            Print #f, "#define " & FillRight(25, r.v("T01_NAME_StopTmr1") & "()") & " " & _
                      SfrName(r, T01_PREFIX & "TMOD") & " = " & SfrName(r, T01_PREFIX & "TMOD") & _
                      " | " & FormatTextOutput(&H3 & String(BFldPos(r, T01_PREFIX & "TMOD_T1M"), "0"), "H2")
            Print #f, ""
            Print #f, ""
    End If
    '------------------------------------------------------------------------------------------------
            Print #f, "#define _TH(TimerNr) " & CORE_PREFIX & "TH##TimerNr"
            Print #f, "#define _TL(TimerNr) " & CORE_PREFIX & "TL##TimerNr"
            Print #f, "#define _TR(TimerNr) TR##TimerNr"

  
            Print #f, SECTION_INTERRUPT_VECTORS
            Print #f, ""  'DAvE interrupt vector definitions
            Print #f, ""
    If (r.v("T01_USE_IsrTmr0") = 1) Then
            Print #f, "#define T0INT " & IntNumber(&HB)
    End If
    If (r.v("T01_USE_IsrTmr1") = 1) Then
            Print #f, "#define T1INT " & IntNumber(&H1B)
    End If
            Print #f, uc.GetUserCode("(T01_Header,9)", 0)
            Print #f, ""
            Print #f, "#endif  // ifndef _" & StripExtension(r.v("T01_HFILE")) & "_H_"
    
    'close file
    Close f
    
    T01GenerateH = True
    Exit Function
    
T01GenerateHError:
    Close f
    Exit Function
End Function

