Syntax:
InitTimer0 source, prescaler
Command Availability:
Available on all microcontrollers with a Timer 0 module.
See also see: InitTimer0 8bit/16bit for support for microcontrollers with a 8 bit/16 bit Timer 0 module.
Explanation:
InitTimer0 will set up timer 0.
Parameters are required as detailed in the table below:
| Parameter | Description |
|---|---|
|
|
The clock source for this specific timer. Can be either
|
|
|
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 from 255 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.
Microchip PIC microcontrollers:
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 |
|
0 |
|
1:4 |
|
1 |
|
1:8 |
|
2 |
|
1:16 |
|
3 |
|
1:32 |
|
4 |
|
1:64 |
|
5 |
|
1:128 |
|
6 |
|
1: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.
Atmel AVR microcontrollers:
On Atmel AVR microcontrollers prescaler must be one of the following constants:
The prescaler will only apply when the timer is driven from the Osc the
internal oscillator - the prescaler has no effect when the external clock
source is specified.
| Prescaler Value | Primary GCB Constant | Secondary GCB Constant | Constant Equates to value |
|---|---|---|---|
|
1:1 |
|
|
1 |
|
1:8 |
|
|
2 |
|
1:64 |
|
|
3 |
|
1:256 |
|
|
4 |
|
1:1024 |
|
|
5 |
Example 1 for 8-bit timer 0:
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
#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 ' <<< the InitTimer0 instruction
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 SubKey line: InitTimer0 Osc, PS0_2 — sets Timer 0’s clock source to the internal oscillator with a 1:2 prescaler; Timer 0 free-runs on a Microchip PIC microcontroller
once initialised, so StartTimer is not needed.
Example 1 for 18-bit timer 0 operating an 8-bit timer:
The same example for a 16-bit timer 0 operating as an 8-bit timer.
#chip 16f18855,32
#option Explicit
'timer test Program
dim speed, MotorSpeed, PWMCounter as byte
#define MOTOR PORTb.0
dir MOTOR out
'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
InitTimer0(Osc, TMR0_FOSC4 + PRE0_1 , POST0_1) ' <<< the InitTimer0 instruction (16-bit timer in 8-bit mode)
StartTimer 0
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 SubKey line: InitTimer0(Osc, TMR0_FOSC4 + PRE0_1 , POST0_1) — on this 16-bit-capable Timer 0, explicitly selects the FOSC/4 clock source with a 1:1 prescale and postscale, then calls
StartTimer since this variant does not start automatically.
See Also:
- InitTimer0 8bit/16bit — for microcontrollers whose Timer 0 supports 8-bit or 16-bit operation
- On Interrupt — attaching the Timer0Overflow handler, as used above
- Settimer / StartTimer — setting a start value and starting a timer explicitly
Supported in <TIMER.H>

