InitTimer4

Syntax: (MicroChip PIC)

    InitTimer4 prescaler, postscaler

or, where you required to state the clock source, use the following

    InitTimer4 clocksource, prescaler, postscaler

Syntax: (Atmel AVR)

    InitTimer4 source, prescaler

Command Availability:

Available on all microcontrollers with a Timer 4 module.  As shown above a Microchip microcontroller can potentially support two types of methods for initialisation.

The first method is:

    InitTimer4 prescaler, postscaler

This the most common method to initialise a Microchip microcontroller timer.  With this method the timer has only one possible clock source, this mandated by the microcontrollers architecture, and that clock source is the System Clock/4 also known as FOSC/4.


The second method is much more flexible in term of the clock source.  Microcontrollers that support this second method enable you to select different clock sources and to select more prescale values.  The method is shown below:

    InitTimer4 clocksource, prescaler, postscaler

How do you determine which method to use for your specific Microchip microcontroller ?

The timer type for a Microchip microcontroller can be determined by checking for the existance of a T2CLKCON register, either in the Datasheet or in the GCBASIC "dat file" for the specific device.

If the Microchip microcontroller DOES NOT have a T4CLKCON register then timers 2/4/6/8 for that specific microcontroller chip use the first method, and are configured using:

    InitTimer4 (PreScale, PostScale)

If the microcontroller DOES have a T2CLKCON register then ALL timers 2/4/6/8 for that specific microcontroller chip use the second method, and are configured using:

    InitTimer4 (Source,PreScale,PostScale)

The possible Source, Prescale and Postscale constants for each type are shown in the tables below.  These table are summary tables from the Microchip datasheets.

Period of the Timers

The Period of the timer is determined by the system clock speed, the prescale value and 8-bit value in the respective timer period register.  The timer period for timer 4 is held in register PR4.  

When the timer is enabled, by starting the timer, it will increment until the TMR4 register matches the value in the PR4 register.  At this time the TMR4 register is cleared to 0 and the timer continues to increment until the next match, and so on.

The lower the value of the PR4 register, the shorter the timer period will be.  The default value for the PR4 register at power up is 255.

The timer interrupt flag (TMR4IF) is set based upon the number of match conditions as determine by the postscaler.  The postscaler does not actually change the timer period, it changes the time between interrupt conditions.

Timer constants for the MicroChip microcontrollers

Parameters for this timer are detailed in the tables below:

Parameter Description

clocksource

If required by method select.  
Source can be one of the following numeric values:
 
1 equates to OSC (FOSC/4).    The default clock source

6 equates to EXTOSC same as SOSC
5 equates to MFINTOSC
4 equates to LFINTOSC
3 equates to HFINTOSC
2 equates to FOSC
1 equates to FOSC/4 same as OSC
0 equates to TxCKIPPS same as EXTOSC and EXT (T1CKIPPS)

Other sources may be available but can vary from microcontroller to microcontroller and these can be included manually per the specific microcontrollers datasheet.

prescaler

The value of the prescaler for this specific timer. See the tables below for permitted values.

postscaler

The value of the postscaler for this specific timer. See the tables below for permitted values.

Table 1 shown above



prescaler can be one of the following settings, if you MicroChip microcontroller has the T4CKPS4 bit then refer to table 2:

Prescaler Value Primary GCB Constant Constant Equates
to value

1:1

PS4_1

0

1:4

PS4_4

1

1:16

PS4_16

2

1:64

PS4_64

3

Table 2

Note that a 1:64 prescale is only avaialable on certain midrange microcontrollers. Please refer to the datasheet to determine if a 1:64 prescale is supported by a spectific microcontroller.

Prescaler Value Primary GCB Constant Constant Equates
to value

1:1

PS4_1

0

1:2

PS4_2

1

1:4

PS4_4

2

1:8

PS4_8

3

1:16

PS4_16

4

1:32

PS4_32

5

1:64

PS4_64

6

1:128

PS4_128

7

Table 3

postscaler slows the rate of the interrupt generation (or WDT reset) from a counter/timer by dividing it down.

On Microchip PIC microcontroller one of the following constants where the Postscaler Rate Select bits are in the range of 1 to 16.

Postcaler Value Use Numeric Constant

1:1 Postscaler

0

1:2 Postscaler

1

1:3 Postscaler

2

1:4 Postscaler

3

1:5 Postscaler

4

1:6 Postscaler

5

1:7 Postscaler

6

1:8 Postscaler

7

1:9 Postscaler

8

1:10 Postscaler

9

1:11 Postscaler

10

1:12 Postscaler

11

1:13 Postscaler

12

1:14 Postscaler

13

1:15 Postscaler

14

1:16 Postscaler

15



Explanation:(Atmel AVR)

InitTimer4 will set up timer 4, according to the settings given.

source can be one of the following settings: Parameters for this timer are detailed in the table below:

Parameter Description

source

The clock source for this specific timer. Can be either Osc or Ext where`Osc` is an internal oscillator and Ext is an external oscillator.



prescaler for Atmel AVR Timer 4 can be selected from the table below.

Prescaler Rate Select bits are in the range of 1 to 1024

Prescaler Value Primary GCB Constant Secondary GCB Constant Constant Equates
to value

1:0

PS_0

PS_4_0

1

1:1

PS_1

PS_4_1

1

1:8

PS_8

PS_4_8

2

1:64

PS_64

PS_4_64

3

1:256

PS_256

PS4_256

4

1:1024

PS_1024

PS_4_1024

5




Example:

This code uses Timer 4 and On Interrupt to generate a 1ms pulse 20 ms.

    #chip 18F25K80, 8

    #DEFINE PIN3 PORTA.1
    DIR PIN3 OUT

    #Define Match_Val PR4  'PR4 is the timer 2 match register
    Match_Val = 154        'Interrupt afer 154 Timer ticks (~20ms)

    On interrupt timer4Match call PulsePin3  'Interrupt on match
    Inittimer4 PS4_16, 15 'Prescale 1:64 /Postscale 1:16 (15)
    Starttimer 4

    Do
       'Waiting for interrupt on match val of 154
    Loop

    Sub PulsePin3
       pulseout Pin3, 1 ms
    End Sub