InitTimer0 8bit/16bit

Syntax:

    InitTimer0 source, prescaler, postscaler

Command Availability:

Available on microcontrollers with a Timer 0 with an 8 bit/16 bit timer module.

Timer 0 on Microchip PIC 18(L)F, as well as small number of 18C and 16(L)F microcontrollers, can be configured for either 8-bit or 16-bit operation.

The default operation is as an 8-bit timer.

Refer to the datasheet for your microcontroller to determine if it supports both 8-bit and 16-bit operation.

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

Explanation:

InitTimer0 will set up timer 0.

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

The value of the prescaler for this specific timer. See the tables below for permitted values for Microchip PIC or the Atmel AVR microcontrollers.

When the timer overflows to 0, a Timer0Overflow interrupt will be generated. This can be used in conjunction with On Interrupt to run a section of code when the overflow occurs.



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.

Timer0 Match (8/16-bit Timer0)

On Chips with 8/16-bit Timer0, when 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.

Microchip PIC microcontrollers 16bit Timer 0 support:

To configure PIC Timer 0 for 16_bit operation add the following line to the source code.

    #define TMR0_16bit


As shown above, where Timer 0 supports 16bit you must use the following syntax:

    InitTimer0 source, prescaler, postscaler

An example is shown below using the Great Cow BASIC constants. See the tables below for the constants.

    InitTimer0 Osc, PRE0_256 + TMR0_HFINTOSC ,  POST0_2

In this example specfic values have been passed to the method. Great Cow BASIC supports passing specfic values to setup Timer 0. These specfic values be obtained from the MicroChip Configurator.

    InitTimer0 Osc, 0x91 ,  0x48      ' Where these values are specfic to the timer setup.





Great Cow BASIC constants as 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

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 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 generate a Pulse Width Modulation signal, that will allow the speed of a motor to be easily controlled.

    #chip 16F88, 8
    #config osc = int

    #define MOTOR PORTB.0

    'Call the initialisation routine
    InitMotorControl

    'Main routine
    Do
        'Increase speed to full over 2.5 seconds
        For Speed = 0 to 100
            MotorSpeed = Speed
            Wait 25 ms
        Next
        'Hold speed
        Wait 1 s
        'Decrease speed to zero over 2.5 seconds
        For Speed = 100 to 0
            MotorSpeed = Speed
            Wait 25 ms
        Next
        'Hold speed
        Wait 1 s
    Loop

    'Setup routine
    Sub InitMotorControl
        'Clear variables
        MotorSpeed = 0
        PWMCounter = 0

        'Add a handler for the interrupt
        On Interrupt Timer0Overflow Call PWMHandler

        'Set up the timer using the internal oscillator with a prescaler of 1/2 (Equates to 0)
        'Timer 0 starts automatically on a Microchip PIC microcontroller, therefore, StartTimer is not required.
        InitTimer0 Osc, PS0_2

    End Sub

    'PWM sub
    'This will be called when Timer 0 overflows
    Sub PWMHandler
        If MotorSpeed > PWMCounter Then
            Set MOTOR On
        Else
            Set MOTOR Off
        End If
        PWMCounter += 1
        If PWMCounter = 100 Then PWMCounter = 0
    End Sub

Supported in <TIMER.H>