Skip to content

inittimer0_8bit_16bit

Anobium edited this page Oct 18, 2020 · 1 revision
InitTimer0 8bit/16bit

Syntax:

    InitTimer0 source, prescaler + clocksource, postscaler

Timer are useful as timers can generate interrupts. Timers can be used in conjunction with On Interrupt to run a section of code when a specific timer event occurs. Example events are when the timer matches a specific value, or, the timer resets to a zero value. For more details on timer events see On Interrupt.

Command Availability:

Available on microcontrollers with a Timer 0 where the timer has the capability of operating as an 8 bit or 16 bit timer.   This type of Timer 0 can be found on Microchip PIC 18(L)F, as well as small number of 18C and 16(L)F microcontrollers.   These timers can be configured for either 8-bit or 16-bit operation.

You may need to refer to the datasheet for your microcontroller to determine if it supports both 8-bit and 16-bit operations.

Explanation for 8-bit timer:

The default operation is as an 8-bit timer.   InitTimer0 will set up timer 0.

Parameters are required as shown 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.



Osc - Selects the clock source in use, as set by the microcontroller specific configuration (fuses or #config).  This could be an internal clock or an external clock source ( external clock sources are typically attached to the XTAL pins).

Ext - Selects the clock source attached to a specific exernal interrupt input port.  This allows a different clock frequency than the main clock to be used, such as 32.768 kHz crystals commonly used for real time circuits.

prescaler

The value of the prescaler for this specific timer, and, the clocksource

See the tables below for permitted values for Microchip PIC or the Atmel AVR microcontrollers.

postscaler

The value of the postscaler for this specific timer.

See the tables below for permitted values for Microchip PIC or the Atmel AVR microcontrollers.

8 bit Example:

The example show in the osc as an internal source, a prescaler value of 256 witht the HFINTOSC clocksource and a postscaler value of 2

    InitTimer0 Osc, PRE0_256 + TMR0_HFINTOSC ,  POST0_2
    'also, note when in 8-bit mode you MUST set the 8bit timer value to the upper byte of a WORD, when setting the `SetTimer`
    SetTimer 0, 0x5800   'Setting the HIGH byte!!!

16 bit Example:

To use the 16 bit timer you need to add the constant #define TMR0_16bit.

The example show in the osc as an internal source, a prescaler value of 256 witht the HFINTOSC clocksource and a postscaler value of 2

    #define TMR0_16bit
    InitTimer0 Osc, PRE0_256 + TMR0_HFINTOSC ,  POST0_2

Differences in Timer0 Operations

The section refers to chips with a 8/16-bit Timer0.

When these chips are operating in 8-bit mode, Timer0 behaves much like Timers2/4/6. In 8-bit mode the TMR0H register does not increment. It instead becomes the Period or Match register and is aliased as "PR0" (Period Register 0).

In 8-bit mode, Timer0 does not technically overflow. Instead when TMR0L increments and matches the value in the PR0 register, TMR0L is reset to 0. The interrupt flag bit (TMR0IF) bit is then set (based upon Postscaler).

The default value in the PR0 "match register" is 255. This value can be set/changed in the user program to set/change the timer period. This can be used to fine tune the timer period.

Timer 0 mandated constants:

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

The value of the prescaler for this specific timer. See the tables below for permitted values for Microchip PIC or the Atmel AVR microcontrollers. You may also be required to specify one of the following clock sources.

TMR0_CLC1
TMR0_SOSC
TMR0_LFINTOSC
TMR0_HFINTOSC
TMR0_FOSC4
TMR0_T0CKIPPS_Inverted
TMR0_T0CKIPPS_True

You should use a simple addition to concatenate the prescaler with a specific clock source. For example.

PRE0_16 + TMR0_HFINTOSC

postscaler

See the tables below for permitted values for Microchip.
Also, refer to the specific datasheet postcaler values.

Microchip PIC microcontrollers where the prescaler rate select bits are in the range of 1 to 32768 you should use one of the following constants.

Prescaler Value Primary GCB Constant Constant Equates
to value

1:1

PRE0_1

0

1:2

PRE0_2

1

1:4

PRE0_4

2

1:8

PRE0_8

3

1:16

PRE0_16

4

1:32

PRE0_32

5

1:64

PRE0_64

6

1:128

PRE0_128

7

1:256

PRE0_256

8

1:512

PRE0_512

9

1:1024

PRE0_1024

10

1:2048

PRE0_2048

11

1:4096

PRE0_4096

12

1:8192

PRE0_8192

13

1:16384

PRE0_16384

14

1:32768

PRE0_32768

15

These correspond to a prescaler of between 1:1 and 1:32768 of the oscillator speed where the oscillator speed is (FOSC/4). The prescaler applies to both the internal oscillator or the external clock.

On Microchip PIC microcontrollers where the prescaler rate select bits are in the range of 2 to 256 you should use one of the following constants. If the prescaler rate select bits are in the range of 1 to 32768 then see the subsequent table.

Prescaler Value Primary GCB Constant Constant Equates
to value

1:2

PS0_2

0

1:4

PS0_4

1

1:8

PS0_8

2

1:16

PS0_16

3

1:32

PS0_32

4

1:64

PS0_64

5

1:128

PS0_128

6

1:256

PS0_256

7

These correspond to a prescaler of between 1:2 and 1:256 of the oscillator speed where the oscillator speed is (FOSC/4). The prescaler applies to both the internal oscillator or the external clock.

On Microchip PIC microcontroller that require postscaler is can be one of the following constants where the Postscaler Rate Select bits are in the range of 1 to 16.

Postcaler Value Primary GCB Constant Use Numeric Constant
1:1 POST0_1 0
1:2 POST0_2 1
1:3 POST0_3 2
1:4 POST0_4 3
1:5 POST0_5 4
1:6 POST0_6 5
1:7 POST0_7 6
1:8 POST0_8 7
1:9 POST0_9 8
1:10 POST0_10 9
1:11 POST0_11 10
1:12 POST0_12 11
1:13 POST0_13 12
1:14 POST0_14 13
1:15 POST0_15 14
1:16 POST0_16 15

Example:

This code uses Timer 0 and On Interrupt to flash an LED.

/*

Remember four things to setup a timer.

1.  InitTimer0 source, prescaler + clocksource, postscaler

2.  SetTimer (byte_value, value ), or
    SetTimer (word_value [where the High byte sets the timer], value )

3.  StartTimer 0

    and, optionally use

4.  ClearTimer 0

*/

    'Chip Settings.
    #CHIP 16f18313, 32

    Dir porta.1 Out

    'Setup the timer.
    '          Source, Prescaler  + Clock Source    , Postscaler
    InitTimer0 Osc,    PRE0_16384 + TMR0_HFINTOSC   , POST0_11

    ' Set the Timer start value.  Use the HIGH byte of the word when using an 8/16bit timer in 8 bit mode
    SetTimer ( 0, 0x5800 )

    ' Start the Timer by writing to TMR0ON bit
    StartTimer 0


    Do
        Wait While TMR0IF = 0
        ' Clearing timer flag
        TMR0IF = 0
        porta.1 = ! porta.1

    Loop

Supported in <TIMER.H>

See also see: InitTimer0 for microcontroller with only an 8 bit Timer 0 module.

Clone this wiki locally