• Pic® Basic


  • Timer Macros

    Proton+ Timer Macros

    Open_Timer0
    Open_Timer1
    Open_Timer2
    Open_Timer3
    Open_Timer4


    Close_Timer0
    Close_Timer1
    Close_Timer2
    Close_Timer3
    Close_Timer4


    Read_Timer0
    Read_Timer1
    Read_Timer2
    Read_Timer3
    Read_Timer4


    Write_Timer0
    Write_Timer1
    Write_Timer2
    Write_Timer3
    Write_Timer4


    Introduction
    Setting up the various timers on PIC® micros can be a tedious task, with constant referrals to the, sometimes obscure, datasheets. Therefore I have created a series of macros to help alleviate some of the problems.

    The macros are only for use with 18F devices, but could easily be converted for 16F types. They should also be used only with the PROTON+ Compiler version 3.1 onwards.

    To use a macro for a particular timer, simply include the appropriate timer file in the main BASIC program.

    The files available are zipped together in the file :timer_macro_inc.zip - and you will find in there the following files:

    Timer0_18.inc (Timer0 Macros)

    Timer1_18.inc (Timer1 Macros)

    Timer2_18.inc (Timer2 Macros)

    Timer3_18.inc (Timer3 Macros)

    Timer4_18.inc (Timer4 Macros)

    Copy all of these files into the compiler’s INC folder and they will be available to all programs without specifying a location.

    Of course, this doesn’t mean that the datasheet is redundant, and the datasheet for the particular PIC® micro you’re using should always be at hand, but I hope it will help overcome some of the hurdles when dealing with timers.

    --------------------------------------------------------------------------------

    Open_Timer0

    Syntax
    Open_Timer0 configs

    Overview
    Configure Timer0

    Operator
    Configs is a bitmask that is created by performing a bitwise AND operation (‘&’) with a value from each of the categories listed below. These values are defined in the Timer0_18.inc file.

    Enable Timer0 Interrupt:
    T0_INT_ON Interrupt enabled
    T0_INT_OFF Interrupt disabled


    Timer Width:
    T0_8BIT 8-bit mode
    T0_16BIT 16-bit mode


    Clock Source:
    T0_SOURCE_EXT External clock source (I/O pin)
    T0_SOURCE_INT Internal clock source (TOSC)


    External Clock Trigger (for T0_SOURCE_EXT):
    T0_EDGE_FALL External clock on falling edge
    T0_EDGE_RISE External clock on rising edge


    Prescale Value:
    T0_PS_1_1 1:1 prescale
    T0_PS_1_2 1:2 prescale
    T0_PS_1_4 1:4 prescale
    T0_PS_1_8 1:8 prescale
    T0_PS_1_16 1:16 prescale
    T0_PS_1_32 1:32 prescale
    T0_PS_1_64 1:64 prescale
    T0_PS_1_128 1:128 prescale
    T0_PS_1_256 1:256 prescale

    Example

    Optimiser_Level = 3 ' Enable the optimiser

    Device = 18F452 ' Choose a 16-bit core device

    Include "Timer0_18.inc" ' Include the macros into the program

    Open_Timer0 [T0_INT_OFF & T0_8BIT & T0_SOURCE_INT & T0_PS_1_32]
    --------------------------------------------------------------------------------

    Open_Timer1

    Syntax
    Open_Timer1 configs

    Overview
    Configure Timer1

    Operator
    Configs is a bitmask that is created by performing a bitwise AND operation (‘&’) with a value from each of the categories listed below. These values are defined in the Timer1_18.inc file.

    Timer Interrupt:
    T1_INT_ON Interrupt enabled
    T1_INT_OFF Interrupt disabled

    Timer Width:
    T1_8BIT_RW 8-bit mode
    T1_16BIT_RW 16-bit mode

    Clock Source:
    T1_SOURCE_EXT External clock source (I/O pin)
    T1_SOURCE_INT Internal clock source (TOSC)

    Prescaler:
    T1_PS_1_1 1:1 prescale
    T1_PS_1_2 1:2 prescale
    T1_PS_1_4 1:4 prescale
    T1_PS_1_8 1:8 prescale

    Oscillator Use:
    T1_OSC1EN_ON Enable Timer1 oscillator
    T1_OSC1EN_OFF Disable Timer1 oscillator

    Synchronize Clock Input:
    T1_SYNC_EXT_ON Sync external clock input
    T1_SYNC_EXT_OFF Don’t sync external clock input

    Example

    Optimiser_Level = 3 ' Enable the optimiser

    Device = 18F452 ' Choose a 16-bit core device

    Include "Timer1_18.inc" ' Include the macros into the program

    Open_Timer1 [T1_INT_OFF & T1_8BIT_RW & T1_SOURCE_EXT & T1_PS_1_1 & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF & T1_SOURCE_CCP]
    --------------------------------------------------------------------------------

    Open_Timer2

    Syntax
    Open_Timer2 configs

    Overview
    Configure Timer2

    Operator
    Configs is a bitmask that is created by performing a bitwise AND operation (‘&’) with a value from each of the categories listed below. These values are defined in the Timer2_18.inc file.

    Timer Interrupt:
    T2_INT_ON Interrupt enabled
    T2_INT_OFF Interrupt disabled

    Prescaler:
    T2_PS_1_1 1:1 prescale
    T2_PS_1_4 1:4 prescale
    T2_PS_1_16 1:16 prescale

    Postscale Value:
    T2_POST_1_1 1:1 postscale
    T2_POST_1_2 1:2 postscale
    T2_POST_1_15 1:15 postscale
    T2_POST_1_16 1:16 postscale

    Example

    Optimiser_Level = 3 ' Enable the optimiser

    Device = 18F452 ' Choose a 16-bit core device

    Include "Timer2_18.inc" ' Include the macros into the program

    Open_Timer2 [T2_INT_OFF & T2_PS_1_1 & T2_POST_1_8]
    --------------------------------------------------------------------------------

    Open_Timer3

    Syntax
    Open_Timer3 configs

    Overview
    Configure Timer3

    Operator
    Configs is a bitmask that is created by performing a bitwise AND operation (‘&’) with a value from each of the categories listed below. These values are defined in the Timer3_18.inc file.

    Timer Interrupt:
    T3_INT_ON Interrupt enabled
    T3_INT_OFF Interrupt disabled

    Timer Width:
    T3_8BIT_RW 8-bit mode
    T3_16BIT_RW 16-bit mode

    Clock Source:
    T3_SOURCE_EXT External clock source (I/O pin)
    T3_SOURCE_INT Internal clock source (TOSC)

    Prescale Value:
    T3_PS_1_1 1:1 prescale
    T3_PS_1_2 1:2 prescale
    T3_PS_1_4 1:4 prescale
    T3_PS_1_8 1:8 prescale

    Synchronize Clock Input:
    T3_SYNC_EXT_ON Sync external clock input
    T3_SYNC_EXT_OFF Don’t sync external clock input

    Use With CCP:
    T1_SOURCE_CCP Timer1 source for both CCP’s
    T3_SOURCE_CCP Timer3 source for both CCP’s
    T1_CCP1_T3_CCP2 Timer1 source for CCP1 and Timer3 source for CCP2

    Example

    Optimiser_Level = 3 ' Enable the optimiser

    Device = 18F8720 ' Choose a 16-bit core device with a timer3

    Include "Timer3_18.inc" ' Include the macros into the program

    Open_Timer3 [T3_INT_OFF & T3_8BIT_RW & T3_SOURCE_EXT & T3_PS_1_1 & T3_OSC1EN_OFF & T3_SYNC_EXT_OFF & T3_SOURCE_CCP]
    --------------------------------------------------------------------------------

    Open_Timer4

    Syntax
    Open_Timer4 configs

    Overview
    Configure Timer4

    Operator
    Configs is a bitmask that is created by performing a bitwise AND operation (‘&’) with a value from each of the categories listed below. These values are defined in the Timer4_18.inc file.

    Timer Interrupt:
    T4_INT_ON Interrupt enabled
    T4_INT_OFF Interrupt disabled

    Prescale Value:
    T4_PS_1_1 1:1 prescale
    T4_PS_1_4 1:4 prescale
    T4_PS_1_16 1:16 prescale

    Postscale Value:
    T4_POST_1_1 1:1 postscale
    T4_POST_1_2 1:2 postscale
    T4_POST_1_15 1:15 postscale
    T4_POST_1_16 1:16 postscale

    Example

    Optimiser_Level = 3 ' Enable the optimiser

    Device = 18F8720 ' Choose a 16-bit core device with a timer4

    Include "Timer4_18.inc" ' Include the macros into the program

    Open_Timer4 [T4_INT_OFF & T4_PS_1_1 & T4_POST_1_8]
    --------------------------------------------------------------------------------

    Close_Timer0

    Close_Timer1

    Close_Timer2

    Close_Timer3

    Close_Timer4

    Overview
    Disable the specified timer.

    Example

    Optimiser_Level = 3 ' Enable the optimiser

    Device = 18F452 ' Choose a 16-bit core device

    Include "Timer0_18.inc" ' Include the Timer0 macros into the program

    Include "Timer1_18.inc" ' Include the Timer1 macros into the program

    Include "Timer2_18.inc" ' Include the Timer2 macros into the program

    Close_Timer0

    Close_Timer1

    Close_Timer2
    Notes

    These macros disable the appropriate interrupt and specified timer.


    --------------------------------------------------------------------------------

    Read_Timer0

    Read_Timer1

    Read_Timer2

    Read_Timer3

    Read_Timer4

    Syntax
    Variable = Read_Timer0 Timer_Size
    Variable = Read_Timer1 Timer_Size
    Variable = Read_Timer2
    Variable = Read_Timer3 Timer_Size
    Variable = Read_Timer4


    Overview
    Read the value of the specified timer.

    Operators
    Variable Can be a Byte, Word or Dword variable that will contain the value of the specified timer.

    Timer_Size Can be the constant value 8 or 16 depending on which timer is being used. Value 8 will read the low byte of a 16-bit timer, and 16 will read the full timer. Not all timers are 16-bit types, therefore the Timer_Size parameter is not required for Timers 2 and 4

    Example

    Optimiser_Level = 3 ' Enable the optimiser

    Device = 18F452 ' Choose a 16-bit core device

    Include "Timer1_18.inc" ' Include the Timer1 macros into the program

    Dim Wrd1 as Word

    Open_Timer1 [T1_INT_OFF & T1_16BIT_RW & T1_SOURCE_INT & T1_PS_1_1 & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF]

    Wrd1 = Read_Timer1 16 ' Read all of Timer1 into variable Wrd1
    --------------------------------------------------------------------------------

    Write_Timer0

    Write_Timer1

    Write_Timer2

    Write_Timer3

    Write_Timer4

    Syntax
    Write_Timer0 Timer_Size , Variable
    Write_Timer1 Timer_Size , Variable
    Write_Timer2 Variable
    Write_Timer3 Timer_Size , Variable
    Write_Timer4 Variable


    Overview
    Write a value into the specified timer.

    Operators
    Timer_Size Can be the constant value 8 or 16 depending on which timer is being used. Value 8 will write only to the low byte of a 16-bit timer, and 16 will write to the full timer. Not all timers are 16-bit types, therefore the Timer_Size parameter is not required for Timers 2 and 4.

    Variable Can be a Byte, Word or Dword variable, or a user constant value that will be written to the specified timer.

    Example

    Optimiser_Level = 3 ' Enable the optimiser

    Device = 18F452 ' Choose a 16-bit core device

    Include "Timer1_18.inc" ' Include the Timer1 macros into the program

    Dim Wrd1 as Word

    Open_Timer1 [T1_INT_OFF & T1_16BIT_RW & T1_SOURCE_INT & T1_PS_1_1 & T1_OSC1EN_OFF & T1_SYNC_EXT_OFF]

    Write_Timer1 16 , Wrd1 ' Write variable Wrd1 into Timer1
    contributed by Les Johnson.