Timer Functions

 

Table of Contents

1    Function Descriptions. 3

1.1      CloseTimer 3

1.1.1      CloseTimer0. 3

1.1.2      CloseTimer1. 3

1.1.3      CloseTimer2. 3

1.1.4      CloseTimer3. 3

1.1.5      CloseTimer4. 3

1.1.6      CloseTimer5. 4

1.2      OpenTimer 4

1.2.1      OpenTimer0. 4

1.2.2      OpenTimer1 (V1, V2, V3, V4, V5) 5

1.2.3      OpenTimer1 (V6) 6

1.2.4      OpenTimer2. 8

1.2.5      OpenTimer3 (V2, V4) 9

1.2.6      OpenTimer3 (V6) 10

1.2.7      OpenTimer4. 12

1.2.8      OpenTimer5. 13

1.3      ReadTimer 14

1.3.1      ReadTimer0. 14

1.3.2      ReadTimer1. 15

1.3.3      ReadTimer2. 15

1.3.4      ReadTimer3. 15

1.3.5      ReadTimer4. 15

1.3.6      ReadTimer5. 15

1.4      WriteTimer 16

1.4.1      WriteTimer0. 16

1.4.2      WriteTimer1. 16

1.4.3      WriteTimer2. 16

1.4.4      WriteTimer3. 17

1.4.5      WriteTimer4. 17

1.4.6      WriteTimer5. 17

1.5      SetTmrCCPSrc. 18

1.5.1      SetTmrCCPSrc. 18

2.  mACROS……………………………………………………………………………21

       2.1      T3_OSC1EN_ON……………………………………………………….…………………..21

       2.2       T3_OSC1EN_OFF ………………………………………………………………………...21


Introduction

The timer peripherals are supported with the following functions:

Table:             Timer Functions

Function

Description

CloseTimerx

Disable timer x.

OpenTimerx

Configure and enable timer x.

ReadTimerx

Read the value of timer x.

WriteTimerx

Write a value into timer x.

SetTmrCCPSrc

Configure the timer as a clock source to CCP module.

 

Based on the availability of on-chip timer peripherals, all PIC 18 devices are divided into following different timer versions. So, before calling the LIB functions care has to be taken to know the version of the configured device and to call the appropriate functions.

 

Below is the table to find the timer version for the configured device:

TABLE:          VERSION vs. DEVICES

Version name

Device number

TMR_V1

18F1230,  18F1330

TMR_V2

18C242, 18C252, 18C442, 18C452, 18F242, 18F252, 18F442,

18F452, 18F248, 18F258, 18F448, 18F458, 18F2439, 18F2539,

18F4439, 18F4539, 18C601, 18C801, 18C658, 18C858,

18F1220, 18F1320, 18F2220, 18F2320, 18F4220, 18F4320,

18F2420, 18F2520, 18F4420, 18F4520, 18F2423, 18F2523,

18F4423, 18F4523, 18F2455, 18F2550, 18F4455, 18F4550,

18F2480, 18F2580, 18F4480, 18F4580, 18F2410, 18F2510,

18F2515, 18F2610, 18F4410, 18F4510, 18F4515, 18F4610,

18F2525, 18F2620, 18F4525, 18F4620, 18F2585, 18F2680,

18F4585, 18F4680, 18F2682, 18F2685, 18F4682, 18F4685,

18F2221, 18F2321, 18F4221, 18F4321, 18F6310, 18F6410,

18F8310, 18F8410, 18F6390, 18F6490, 18F8390, 18F8490,

18F6585, 18F6680, 18F8585, 18F8680, 18F63J11, 18F64J11,

18F65J11, 18F83J11, 18F84J11, 18F85J11, 18F63J90,

18F64J90, 18F65J90, 18F83J90, 18F84J90, 18F85J90,

18F23K20, 18F24K20, 18F25K20, 18F43K20, 18F44K20,

18F45K20, 18F13K50, 18LF13K50, 18F14K50, 18LF14K50, 18F13K22, 18F14K22, 18LF13K22, 18LF14K22, 18F66J90,

18F67J90, 18F86J90,18F87J90, 18F66J93, 18F67J93, 18F86J93, 18F87J93

TMR_V3

18F2450, 18F4450, 18F24J10, 18F25J10, 18F44J10, 18F45J10

TMR_V4

18F6620, 18F6720, 18F8620, 18F8720, 18F6520, 18F8520,

18F6527, 18F6622, 18F6627, 18F6722, 18F8527, 18F8622,

18F8627, 18F8722, 18F6525, 18F6621, 18F8525, 18F8621,

18F65J10, 18F65J15, 18F66J10, 18F66J15, 18F67J10,

18F85J10, 18F85J15, 18F86J10, 18F86J15, 18F87J10,

18F66J60, 18F66J65, 18F67J60, 18F86J60, 18F86J65,

18F87J60, 18F96J60, 18F96J65, 18F97J60, 18F66J11,

18F66J16, 18F67J11, 18F86J11, 18F86J16, 18F87J11,

18F65J50, 18F66J50, 18F66J55, 18F67J50, 18F85J50,

18F86J50, 18F86J55, 18F87J50

TMR_V5

18F2331, 18F2431, 18F4331, 18F4431

TMR_V6

18F24J11, 18F25J11, 18F26J11, 18F44J11, 18F45J11, 18F46J11, 18F24J50, 18F25J50, 18F26J50, 18F44J50, 18F45J50, 18F46J50, 18LF24J11, 18LF25J11, 18LF26J11, 18LF44J11, 18LF45J11, 18LF46J11, 18LF24J50, 18LF25J50, 18LF26J50, 18LF44J50, 18LF45J50, 18LF46J50

 

1          Function Descriptions

1.1         CloseTimer

1.1.1        CloseTimer0

For TMR_V1, TMR_V2, TMR_V3, TMR_V4, TMR_V5 and TMR_V6

Function:

Disable the specified timer.

Include:

timers.h

Prototype:

void CloseTimer0( void );

Remarks:

This function disables the interrupt and the specified timer.

File Name:

t0close.c

1.1.2        CloseTimer1

For TMR_V1, TMR_V2, TMR_V3, TMR_V4, TMR_V5 and TMR_V6

Function:

Disable the specified timer.

Include:

timers.h

Prototype:

void CloseTimer1( void );

Remarks:

This function disables the interrupt and the specified timer.

File Name:

t1close.c

1.1.3        CloseTimer2

For TMR_V2, TMR_V3, TMR_V4, TMR_V5 and TMR_V6

Function:

Disable the specified timer.

Include:

timers.h

Prototype:

void CloseTimer2( void );

Remarks:

This function disables the interrupt and the specified timer.

File Name:

t2close.c

 

1.1.4        CloseTimer3

For TMR_V2, TMR_V4 and TMR_V6

Function:

Disable the specified timer.

Include:

timers.h

Prototype:

void CloseTimer3( void );

Remarks:

This function disables the interrupt and the specified timer.

File Name:

t3close.c

 

1.1.5        CloseTimer4

For TMR_V4 and TMR_V6

Function:

Disable the specified timer.

Include:

timers.h

Prototype:

void CloseTimer4( void );

Remarks:

This function disables the interrupt and the specified timer.

File Name:

t4close.c

 

1.1.6        CloseTimer5

For TMR_V5

Function:

Disable the specified timer.

Include:

timers.h

Prototype:

void CloseTimer5( void );

Remarks:

This function disables the interrupt and the specified timer.

File Name:

t5close.c

 

 

1.2         OpenTimer

1.2.1        OpenTimer0

For TMR_V1, TMR_V2, TMR_V3, TMR_V4, TMR_V5 and TMR_V6

Function:

Configure and enable timer0.

Include:

timers.h

Prototype:

void OpenTimer0( unsigned char config );

Arguments:

config

A bitmask that is created by performing either a bitwise AND operation (‘&’) or bitwise OR operation (‘|’) , which is user configurable, with a value from each of the categories listed below. These values are defined in the file timers.h.

 

Enable Timer0 Interrupt:

            TIMER_INT_ON             Interrupt enabled

            TIMER_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

Remarks:

This function configures timer0 according to the options specified and then enables it.

File Name:

t0open.c

Code Example:

With bitwise AND (‘&’) mask:

OpenTimer0( TIMER_INT_OFF &

            T0_8BIT       &

            T0_SOURCE_INT &

            T0_PS_1_32 );

 

With bitwise OR (‘|’) mask:

OpenTimer0( TIMER_INT_OFF |

            T0_8BIT       |

            T0_SOURCE_INT |

            T0_PS_1_32 );

 

 

1.2.2        OpenTimer1 (V1, V2, V3, V4, V5)

For TMR_V1, TMR_V2, TMR_V3, TMR_V4 and TMR_V5

Function:

Configure and enable timer1.

Include:

timers.h

Prototype:

void OpenTimer1( unsigned char config );

Arguments:

config

A bitmask that is created by performing either a bitwise AND operation (‘&’) or bitwise OR operation (‘|’) , which is user configurable, with a value from each of the categories listed below. These values are defined in the file timers.h.

 

Enable Timer1 Interrupt:

      TIMER_INT_ON                  Interrupt enabled

   TIMER_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

Remarks:

This function configures timer1 according to the options specified and then enables it.

File Name:

t1open.c

Code Example:

With bitwise AND (‘&’) mask:

OpenTimer1( TIMER_INT_ON    &

            T1_8BIT_RW      &

            T1_SOURCE_EXT   &

            T1_PS_1_1       &

            T1_OSC1EN_OFF   &

            T1_SYNC_EXT_OFF );

 

With bitwise OR (‘|’) mask:

OpenTimer1( TIMER_INT_ON    |

            T1_8BIT_RW      |

            T1_SOURCE_EXT   |

            T1_PS_1_1       |

            T1_OSC1EN_OFF   |

            T1_SYNC_EXT_OFF );

 

1.2.3        OpenTimer1 (V6)

For TMR_V6

Function:

Configure and enable timer1.

Include:

timers.h

Prototype:

void OpenTimer1( unsigned char config , unsigned char config1 );

Arguments:

config

A bitmask that is created by performing either a bitwise AND operation (‘&’) or bitwise OR operation (‘|’) , which is user configurable, with a value from each of the categories listed below. These values are defined in the file timers.h.

 

Enable Timer1 Interrupt:

      TIMER_INT_ON                  Interrupt enabled

      TIMER_INT_OFF                 Interrupt disabled

Clock Source:

   T1_SOURCE_PINOSC          Ext clock or Crystal oscillator

      T1_SOURCE_CAPOSC          Capacitive sensing oscillator  

      T1_SOURCE_FOSC_4          Instruction cycle (Fosc/4)

      T1_SOURCE_FOSC       System Clock (Fosc)

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

Timer Width:

      T1_8BIT_RW                       8-bit mode

      T1_16BIT_RW                    16-bit mode

 

 

Config1:

A bitmask that is created by performing either a bitwise AND operation (‘&’) or bitwise OR operation (‘|’) , which is user configurable, with a value from each of the categories listed below. These values are defined in the file timers.h.

 

Timer1 Gate Enable

      TIMER_GATE_ON  counting is controlled by Timer1 gate         

   TIMER_GATE_OFF Timer is always counting

Timer1 Gate Polarity

   TIMER_GATE_POL_HI          Gate  is active-high

   TIMER_GATE_POL_LO         Gate is active-low

Timer1 Gate Toggle Mode

   TIMER_GATE_TOGGLE_ON       Gate Toggle mode enabled

   TIMER_GATE_TOGGLE_OFF      Gate Toggle mode disabled

Timer1 Gate One Shot Enable

   TIMER_GATE_1SHOT_ON        Gate one shot is enabled

   TIMER_GATE_1SHOT_OFF       Gate one shot is disabled

Timer1 Gate Source Select

   TIMER_GATE_SRC_T1GPIN      Timer1 gate pin

   TIMER_GATE_SRC_T0          Timer0 overflow output

   TIMER_GATE_SRC_T2          Timer2 match PR2  output

Enable Timer1 Gate Interrupt:

   TIMER_GATE_INT_OFF         Interrupts disabled

   TIMER_GATE_INT_ON             Interrupts enabled

Remarks:

This function configures timer1 according to the options specified and then enables it.

File Name:

t1open.c

Code Example:

With bitwise AND (‘&’) mask:

OpenTimer1( TIMER_INT_ON     &

            T1_SOURCE_PINOSC &            

            T1_PS_1_1        &

            T1_OSC1EN_OFF    &

            T1_SYNC_EXT_OFF  &

            T1_8BIT_RW,

            TIMER_GATE_ON  &

            TIMER_GATE_POL_HI       &

            TIMER_GATE_TOGGLE_ON    &

            TIMER_GATE_1SHOT_ON     &

            TIMER_GATE_SRC_T1GPIN   &

            TIMER_GATE_INT_ON);

 

With bitwise OR (‘|’) mask:

OpenTimer1(TIMER_INT_ON          |

            T1_SOURCE_PINOSC     |            

            T1_PS_1_1            |

            T1_OSC1EN_OFF        |

            T1_SYNC_EXT_OFF      |

            T1_8BIT_RW,

            TIMER_GATE_ON        |

            TIMER_GATE_POL_HI    |

            TIMER_GATE_TOGGLE_ON |

            TIMER_GATE_1SHOT_ON  |

            TIMER_GATE_SRC_T1GPIN|

            TIMER_GATE_INT_ON);

 

1.2.4        OpenTimer2

For TMR_V2, TMR_V3, TMR_V4, TMR_V5 and TMR_V6

Function:

Configure and enable timer2.

Include:

timers.h

Prototype:

void OpenTimer2( unsigned char config );

Arguments:

config

A bitmask that is created by performing either a bitwise AND operation (‘&’) or bitwise OR operation (‘|’) , which is user configurable, with a value from each of the categories listed below. These values are defined in the file timers.h.

 

Enable Timer2 Interrupt:

      TIMER_INT_ON                  Interrupt enabled

      TIMER_INT_OFF                Interrupt disabled

 

Prescale Value:

      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

Remarks:

This function configures timer2 according to the options specified and then enables it.

File Name:

t2open.c

Code Example:

With bitwise AND (‘&’) mask:

OpenTimer2( TIMER_INT_OFF &

            T2_PS_1_1     &

            T2_POST_1_8   );

 

With bitwise OR (‘|’) mask:

OpenTimer2( TIMER_INT_OFF |

            T2_PS_1_1     |

            T2_POST_1_8   );

 

 

1.2.5        OpenTimer3 (V2, V4)

For TMR_V2 and TMR_V4

Function:

Configure and enable timer3.

Include:

timers.h

Prototype:

void OpenTimer3( unsigned char config );

Arguments:

config

A bitmask that is created by performing either a bitwise AND operation (‘&’) or bitwise OR operation (‘|’) , which is user configurable, with a value from each of the categories listed below. These values are defined in the file timers.h.

 

Enable Timer3 Interrupt:

            TIMER_INT_ON            Interrupt enabled

            TIMER_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

Note: Refer Macros for external clock selection between oscillator( in Timer 1) & external pin input.

 

Synchronize Clock Input:

      T3_SYNC_EXT_ON                Sync external clock input

      T3_SYNC_EXT_OFF              Don’t sync external clock input

Remarks:

This function configures timer3 according to the options specified and then enables it.

File Name:

t3open.c

Code Example:

With bitwise AND (‘&’) mask:

OpenTimer3( TIMER_INT_ON    &

            T3_8BIT_RW      &

            T3_SOURCE_EXT   &

            T3_PS_1_1       &

            T3_SYNC_EXT_OFF );

 

With bitwise OR (‘|’) mask:

OpenTimer3( TIMER_INT_ON    |

            T3_8BIT_RW      |

            T3_SOURCE_EXT   |

            T3_PS_1_1       |

            T3_SYNC_EXT_OFF );

 

1.2.6        OpenTimer3 (V6)

For TMR_V6

Function:

Configure and enable timer3.

Include:

timers.h

Prototype:

void OpenTimer3( unsigned char config , unsigned char config1 );

Arguments:

config

A bitmask that is created by performing either a bitwise AND operation (‘&’) or bitwise OR operation (‘|’) , which is user configurable, with a value from each of the categories listed below. These values are defined in the file timers.h.

 

Enable Timer3 Interrupt:

      TIMER_INT_ON                  Interrupt enabled

      TIMER_INT_OFF                 Interrupt disabled

Clock Source:

   T3_SOURCE_PINOSC          Ext clock or Crystal oscillator

      T3_SOURCE_CAPOSC          Capacitive sensing oscillator  

      T3_SOURCE_FOSC_4          Instruction cycle (Fosc/4)

      T3_SOURCE_FOSC       System Clock (Fosc)

Prescaler:

      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   

Oscillator Use:

      T3_OSC1EN_ON                  Enable Timer1 oscillator

      T3_OSC1EN_OFF                Disable Timer1 oscillator

Synchronize Clock Input:

      T3_SYNC_EXT_ON              Sync external clock input

      T3_SYNC_EXT_OFF            Don’t sync external clock input

Timer Width:

      T3_8BIT_RW                       8-bit mode

      T3_16BIT_RW                    16-bit mode

 

 

Config1:

A bitmask that is created by performing either a bitwise AND operation (‘&’) or bitwise OR operation (‘|’) , which is user configurable, with a value from each of the categories listed below. These values are defined in the file timers.h.

 

Timer1 Gate Enable

      TIMER_GATE_ON  counting is controlled by Timer1 gate         

   TIMER_GATE_OFF Timer is always counting

Timer1 Gate Polarity

   TIMER_GATE_POL_HI          Gate  is active-high

   TIMER_GATE_POL_LO         Gate is active-low

Timer1 Gate Toggle Mode

   TIMER_GATE_TOGGLE_ON       Gate Toggle mode enabled

   TIMER_GATE_TOGGLE_OFF      Gate Toggle mode disabled

Timer1 Gate One Shot Enable

   TIMER_GATE_1SHOT_ON        Gate one shot is enabled

   TIMER_GATE_1SHOT_OFF       Gate one shot is disabled

Timer1 Gate Source Select

   TIMER_GATE_SRC_T1GPIN      Timer1 gate pin

   TIMER_GATE_SRC_T0          Timer0 overflow output

   TIMER_GATE_SRC_T2          Timer2 match PR2  output

Enable Timer1 Gate Interrupt:

   TIMER_GATE_INT_OFF         Interrupts disabled

   TIMER_GATE_INT_ON          Interrupts enabled

Remarks:

This function configures timer1 according to the options specified and then enables it.

File Name:

t1open.c

Code Example:

With bitwise AND (‘&’) mask:

OpenTimer1( TIMER_INT_ON     &

            T3_SOURCE_PINOSC &            

            T3_PS_1_1        &

            T3_OSC1EN_OFF    &

            T3_SYNC_EXT_OFF  &

            T3_8BIT_RW,

            TIMER_GATE_ON  &

            TIMER_GATE_POL_HI       &

            TIMER_GATE_TOGGLE_ON    &

            TIMER_GATE_1SHOT_ON     &

            TIMER_GATE_SRC_T1GPIN   &

            TIMER_GATE_INT_ON);

 

With bitwise OR (‘|’) mask:

OpenTimer1(TIMER_INT_ON          |

            T3_SOURCE_PINOSC     |            

            T3_PS_1_1            |

            T3_OSC1EN_OFF        |

            T3_SYNC_EXT_OFF      |

            T3_8BIT_RW,

            TIMER_GATE_ON        |

            TIMER_GATE_POL_HI    |

            TIMER_GATE_TOGGLE_ON |

            TIMER_GATE_1SHOT_ON  |

            TIMER_GATE_SRC_T1GPIN|

            TIMER_GATE_INT_ON  );

 

1.2.7         OpenTimer4

For TMR_V4 and TMR_V6

Function:

Configure and enable timer4.

Include:

timers.h

Prototype:

void OpenTimer4( unsigned char config );

Arguments:

config

A bitmask that is created by performing either a bitwise AND operation (‘&’) or bitwise OR operation (‘|’), which is user configurable, with a value from each of the categories listed below. These values are defined in the file timers.h.

 

Enable Timer4 Interrupt:

            TIMER_INT_ON      Interrupt enabled

            TIMER_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

Remarks:

This function configures timer4 according to the options specified and then enables it.

File Name:

t4open.c

Code Example:

With bitwise AND (‘&’) mask:

OpenTimer4( TIMER_INT_OFF &

            T4_PS_1_1     &

            T4_POST_1_8   );

 

With bitwise OR (‘|’) mask:

OpenTimer4( TIMER_INT_OFF |

            T4_PS_1_1     |

            T4_POST_1_8   );

 

1.2.8        OpenTimer5

For TMR_V5

Function:

Configure and enable timer5.

Include:

timers.h

Prototype:

void OpenTimer5(unsigned char config, unsigned int t5pr)

Arguments:

config

A bitmask that is created by performing either a bitwise AND operation (‘&’) or bitwise OR operation (‘|’), which is user configurable, with a value from each of the categories listed below. These values are defined in the file timers.h

 

t5pr

t5pr value will be loaded in to low and high byte of the Timer5 Period Register (i.e. PR5L =   t5pr, PR5H =  (t5pr>>8))

 

Enable Timer5 Interrupt:

            TIMER_INT_ON              Interrupt enabled

            TIMER_INT_OFF            Interrupt disabled

 

Timer5 Sleep Enable:

      T5_SLP_EN          Enable during sleep   

      T5_SLP_DIS         Disable during sleep

Special Event Reset:

      T5 _SP_EVNT_REN     Special event reset enable  

      T5_SP_EVNT_RDIS     Special event reset disable

Timer Mode:

      T5_MD_SNGL_SHOT     Single-shot mode

      T5_MD_CONT_COUNT    Continuous count mode

Prescale Value:

      T5_PS_1_1            1:1 prescale

      T5_PS_1_2            1:2 prescale

      T5_PS_1_4            1:4 prescale

      T5_PS_1_8            1:8 prescale

Synchronize Clock Input:

      T5_EX_CLK_SYNC       Sync external clock input

      T5_EX_CLK_NOSYNC     Don’t sync external clock input

Clock Source:

      T5_CLK_EXTRN         External clock source

      T5_CLK_INT           Internal clock source

Remarks:

This function configures timer5 according to the options specified, enables it, and then writes the value in to Timer5 period register.

File Name:

t5open.c 

Code Example:

With bitwise AND (‘&’) mask:

OpenTimer5(T5_MD_CONT_COUNT &

           T5_PS_1_1        &

           T5_CLK_INT,

           0XC350);

 

With bitwise OR (‘|’) mask:

OpenTimer5(T5_MD_CONT_COUNT |

           T5_PS_1_1        |

           T5_CLK_INT,

           0XC350);

 

 

1.3         ReadTimer

1.3.1        ReadTimer0

For TMR_V1, TMR_V2, TMR_V3, TMR_V4, TMR_V5 and TMR_V6

Function:

Reads the value of the timer0.

Include:

timers.h

Prototype:

unsigned int  ReadTimer0( void );

Remarks:

This function reads the value of the timer0 register.

            Timer0:                         TMR0L,TMR0H

 

Note: When using a timer in 8-bit mode that may be configured in 16-bit mode (e.g., timer0), the upper byte is not ensured to be zero. The user may wish to cast the result to a char for correct results. For example:

 

  // Example of reading a 16-bit result

  // from a 16-bit timer operating in

  // 8-bit mode:

  unsigned int result;

  result = (unsigned char) ReadTimer0();

Return Value:

The current value of the timer.

File Name:

t0read.c

 

1.3.2        ReadTimer1

For TMR_V1, TMR_V2, TMR_V3, TMR_V4, TMR_V5 and TMR_V6

Function:

Reads the value of the timer1.

Include:

timers.h

Prototype:

unsigned int  ReadTimer1( void );

Remarks:

This function reads the value of the timer1 register.

            Timer1:                           TMR1L,TMR1H

Return Value:

The current value of the timer.

File Name:

t1read.c

 

1.3.3        ReadTimer2

For TMR_V2, TMR_V3, TMR_V4, TMR_V5 and TMR_V6

Function:

Reads the value of the timer2.

Include:

timers.h

Prototype:

unsigned char ReadTimer2( void );

Remarks:

This function reads the value of the timer2 register.

            Timer2:                           TMR2

Return Value:

The current value of the timer.

File Name:

t2read.c

 

1.3.4        ReadTimer3

For TMR_V2 ,TMR_V4 and TMR_V6

Function:

Reads the value of the timer3.

Include:

timers.h

Prototype:

unsigned int  ReadTimer3( void );

Remarks:

This function reads the value of the timer3 register.

            Timer3:                           TMR3L,TMR3H

Return Value:

The current value of the timer.

File Name:

t3read.c

 

1.3.5        ReadTimer4

For TMR_V4 and TMR_V6

Function:

Reads the value of the timer4.

Include:

timers.h

Prototype:

unsigned char ReadTimer4( void );

Remarks:

This function reads the value of the timer4 register.

            Timer4:                         TMR4

Return Value:

The current value of the timer.

File Name:

t4read.c

 

1.3.6        ReadTimer5

For TMR_V5

Function:

Reads the value of the timer5.

Include:

timers.h

Prototype:

unsigned int ReadTimer5(void);

Remarks:

This function reads the value of the timer4 register.

            Timer5:                           TMR5L,TMR5H

Return Value:

The current value of the timer.

File Name:

t5read.c

 

1.4         WriteTimer

1.4.1        WriteTimer0

For TMR_V1, TMR_V2, TMR_V3, TMR_V4, TMR_V5 and TMR_V6

Function:

Write a value into the timer0.

Include:

timers.h

Prototype:

void WriteTimer0( unsigned int  timer );

Arguments:

timer

The value that will be loaded into the specified timer.

Remarks:

This function writes a value to the respective timer register:

            Timer0:                         TMR0L,TMR0H

File Name:

t0write.c

Code Example:

WriteTimer0( 10000 );

 

1.4.2        WriteTimer1

For TMR_V1, TMR_V2, TMR_V3, TMR_V4, TMR_V5 and TMR_V6

Function:

Write a value into the timer1.

Include:

timers.h

Prototype:

void WriteTimer1( unsigned int  timer );

Arguments:

timer

The value that will be loaded into the specified timer.

Remarks:

This function writes a value to the respective timer register:

            Timer1:                         TMR1L,TMR1H

File Name:

t1write.c

Code Example:

WriteTimer1( 10000 );

 

1.4.3        WriteTimer2

For TMR_V2, TMR_V3, TMR_V4, TMR_V5 and TMR_V6

Function:

Write a value into the timer2.

Include:

timers.h

Prototype:

void WriteTimer2( unsigned char timer );

Arguments:

timer

The value that will be loaded into the specified timer.

Remarks:

This function writes a value to the respective timer register:

            Timer2:                         TMR2

File Name:

t2write.c

Code Example:

WriteTimer2( 100 );

 

1.4.4        WriteTimer3

For TMR_V2, TMR_V4 and TMR_V6

Function:

Write a value into the timer3.

Include:

timers.h

Prototype:

void WriteTimer3( unsigned int  timer );

Arguments:

timer

The value that will be loaded into the specified timer.

Remarks:

This function writes a value to the respective timer register:

            Timer3:                         TMR3L,TMR3H

File Name:

t3write.c

Code Example:

WriteTimer3( 10000 );

 

1.4.5        WriteTimer4

For TMR_V4 and TMR_V6

Function:

Write a value into the timer4.

Include:

timers.h

Prototype:

void WriteTimer4( unsigned char timer );

Arguments:

timer

The value that will be loaded into the specified timer.

Remarks:

This function writes a value to the respective timer register:

            Timer4:                         TMR4

File Name:

t4write.c

Code Example:

WriteTimer0( 100 );

 

1.4.6        WriteTimer5

For TMR_V5

Function:

Write a value into the timer5.

Include:

timers.h

Prototype:

void WriteTimer5( unsigned int  timer );

Arguments:

timer

The value that will be loaded into the specified timer.

Remarks:

This function writes a value to the respective timer register:

            Timer5:                         TMR5L,TMR5H

File Name:

t5write.c

Code Example:

WriteTimer5( 10000 );

 

 

1.5         SetTmrCCPSrc

1.5.1        SetTmrCCPSrc

For TMR_V2, TMR_V4 and TMR_V6

Function:

Enable timer source to CCP module.

Include:

timers.h

Prototype:

void SetTmrCCPSrc( unsigned char config );

Arguments:

config

A bitmask that is created by performing either a bitwise AND operation (‘&’) or bitwise OR operation (‘|’), which is user configurable, with a value from each of the categories listed below. These values are defined in the file timers.h.

 

TMR_V2:

      T3_SOURCE_CCP                  Timer3 source for both CCP’s

      T1_CCP1_T3_CCP2              Timer1 source for CCP1 and
                                                  Timer3 source for CCP2

      T1_SOURCE_CCP                  Timer1 source for both CCP’s

 

TMR_V4:

      T34_SOURCE_CCP                Timer3 and Timer4 are sources for all
                                                  CCP’s

      T12_CCP12_T34_CCP345    Timer1 and Timer2 are sources for
                                                  CCP1 and CCP2 and Timer3 and
                                                  Timer4 are sources for CCP3
                                                  through CCP5

      T12_CCP1_T34_CCP2345    Timer1 and Timer2 are sources for
                                                  CCP1 and Timer3 and Timer4 are
                                                  sources for CCP2 through CCP5

   T12_SOURCE_CCP                 Timer1 and Timer2 are sources for all
                                                    CCP’s

 

 

TMR_V6:

      T34_SOURCE_CCP12          Timer1 and 2 source for both CCP’s

      T12_CCP1_T34_CCP2        Timer1 and Timer2 are sources for
                                                CCP1 and Timer3 and Timer4 are sources                                                 CCP2

   T12_SOURCE_CCP              Timer1 and Timer2 are sources for all
                                                CCP’s

Remarks:

This function configures timer as a clock sources for the CCP module.

File Name:

tmrccpsrc.c

Code Example:

With bitwise AND (‘&’) mask:

SetTmrCCPSrc (T34_SOURCE_CCP12 );

 

With bitwise OR (‘|’) mask:

SetTmrCCPSrc (T34_SOURCE_CCP12 );

 

 

Example Use of the Timer Routines with AND mask:

#include <p18C452.h>

#include <timers.h>

#include <usart.h>

#include <stdlib.h>

 

void main( void )

{

  int result;

  char str[7];

 

  // configure timer0

  OpenTimer0( TIMER_INT_OFF  &

              T0_SOURCE_INT  &

              T0_PS_1_32 );

 

  // configure USART

  OpenUSART( USART_TX_INT_OFF  &

             USART_RX_INT_OFF  &

             USART_ASYNCH_MODE &

             USART_EIGHT_BIT   &

             USART_CONT_RX,

             64                );

 

  while( 1 )

  {

    while( ! PORTBbits.RB3 ); // wait for RB3 high

    result = ReadTimer0();    // read timer

 

    if( result > 0xc000 )     // exit loop if value

      break;                  //   is out of range

 

    WriteTimer0( 0 );         // restart timer

 

    ultoa( result, str );     // convert timer to string

    putsUSART( str );         // print string

  }

 

  CloseTimer0();              // close modules

  CloseUSART();

}

 

 

Example Use of the Timer Routines with OR mask:

 

#include <p18C452.h>

#define USE_AND_OR

#include <timers.h>

#include <usart.h>

#include <stdlib.h>

 

void main( void )

{

  int result;

  char str[7];

 

  // configure timer0

  OpenTimer0( TIMER_INT_OFF  |

                T0_8BIT        |   

              T0_SOURCE_INT  |

              T0_PS_1_32 );

 

  // configure USART

  OpenUSART( USART_TX_INT_OFF  |

             USART_RX_INT_OFF  |

             USART_ASYNCH_MODE |

             USART_BRGH_HIGH   |

             USART_EIGHT_BIT   |

             USART_CONT_RX,

             64                );

 

  while( 1 )

  {

    while( ! PORTBbits.RB3 ); // wait for RB3 high

    result = ReadTimer0();    // read timer

 

    if( result > 0xc000 )     // exit loop if value

      break;                  //   is out of range

 

    WriteTimer0( 0 );         // restart timer

 

    ultoa( result, str );     // convert timer to string

    putsUSART( str );         // print string

  }

 

  CloseTimer0();              // close modules

  CloseUSART();

}

 

 

 

2           Macro Description.

 

2.1         T3_OSC1EN_ON() :

 

For TMR_V1, TMR_V2 &  TMR_V4

       Macro: Enables Oscillator as external source of input

     Include: timers.h

     Prototype: T3_OSC1EN_ON() ;

     Remarks: This Macro enables the oscillator associated with Timer 1 as source of

                      external clock input for Timer3.

 

P.S: This Macro directly affects the configuration of Timer 1.

 

 

 

 

 

 

2.2          T3_OSC1EN_OFF() :

 

For TMR_V1, TMR_V2 &  TMR_V4

       Macro: Disables Oscillator as external source of input

     Include: timers.h

     Prototype: T3_OSC1EN_OFF() ;

     Remarks: This Macro disables the oscillator associated with Timer 1 and selects the 

                       signal on pin T13CKI as source of external clock input  for Timer3.

 

P.S: This Macro directly affects the configuration of Timer 1.