Syntax:
InitTimer1 source, prescalerCommand Availability:
Available on all microcontrollers with a Timer 1 module.
Explanation:
InitTimer1 will set up timer 1.
Parameters are required as detailed in the table below:
| Parameter | Description |
|---|---|
|
|
The clock source for this specific timer. Can be either
Enhanced Microchip PIC microcontrollers with a dedicated TMRxCLK register support additional clock sources. This includes, but limited to, the following devices: 16F153xx, 16F16xx, 16F188xx and 18FxxK40 Microchip PIC microcontroller series. On these devices the clock source can be one of the following:
|
|
|
The value of the prescaler for this specific timer. See the tables below for permitted vales for Microchip PIC or the Atmel AVR microcontrollers. |
When the timer overflows an interrupt event will be generated.
This interrupt event can be used in conjunction with On Interrupt to run a section of code when the interrupt event occurs.
Microchip PIC microcontrollers:
On Microchip PIC microcontrollers prescaler must be one of the following constants:
| Prescaler Value | Primary GCB Constant | Constant Equates to value |
|---|---|---|
|
1:1 |
|
0 |
|
1:2 |
|
16 |
|
1:4 |
|
32 |
|
1:8 |
|
48 |
These correspond to a prescaler of between 1:2 and 1:8 of the oscillator (FOSC/4)
speed. The prescaler will apply to either the oscillator or the external
clock input.
Atmel AVR microcontrollers:
On the majority of 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:0 |
|
|
0 |
|
1:1 |
|
|
1 |
|
1:8 |
|
|
2 |
|
1:64 |
|
|
3 |
|
1:256 |
|
|
4 |
|
1:1024 |
|
|
5 |
On Atmel AVR ATtiny15/25/45/85/216/461/861 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 | Constant Equates to value |
|---|---|---|
|
1:0 |
|
0 |
|
1:1 |
|
1 |
|
1:2 |
|
2 |
|
1:4 |
|
3 |
|
1:8 |
|
4 |
|
1:16 |
|
5 |
|
1:32 |
|
6 |
|
1:64 |
|
7 |
|
1:128 |
|
8 |
|
1:256 |
|
9 |
|
1:512 |
|
10 |
|
1:1024 |
|
11 |
|
1:2048 |
|
12 |
|
1:4096 |
|
13 |
|
1:8192 |
|
14 |
|
1:16384 |
|
15 |
Example 1 (Microchip):
This example will measure that time that a switch is depressed (or on) and will write the results to the EEPROM.
#chip 16F819, 20
#define SWITCH PORTA.0
Dir SWITCH In
DataCount = 0
'Initilise Timer 1
InitTimer1 Osc, PS1_8 ' <<< the InitTimer1 instruction
Dim TimerValue As Word
Do
ClearTimer 1
Wait Until SWITCH = On
StartTimer 1
Wait Until SWITCH = Off
StopTimer 1
'Read the timer
TimerValue = Timer1
'Log the timer value
EPWrite(DataCount, TimerValue_H)
EPWrite(DataCount + 1, TimerValue)
DataCount += 2
LoopKey line: InitTimer1 Osc, PS1_8 — selects the internal oscillator as the clock source with a 1:8 prescaler; the timer is not counting yet until StartTimer is called.
Example 2 (Atmel AVR):
This example will flash the yellow LED on an Arduino Uno (R3) once every second.
#Chip mega328p, 16 'Using Arduino Uno R3
#define LED PORTB.5
Dir LED OUT
Inittimer1 OSC, PS_256 ' <<< the InitTimer1 instruction (AVR-style prescaler constant)
Starttimer 1
Settimer 1, 3200 ;Preload Timer
On Interrupt Timer1Overflow Call Flash_LED
Do
'Wait for interrupt
loop
Sub Flash_LED
Settimer 1, 3200 'Preload timer
pulseout LED, 100 ms
End SubKey line: Inittimer1 OSC, PS_256 — on Atmel AVR, prescaler constants use the PS_n form rather than the Microchip PIC PSn_n form shown in Example 1.
See Also:
- StartTimer / StopTimer — starting and stopping the timer configured here
- Settimer — preloading the timer’s count value, as used above
- On Interrupt — attaching an interrupt handler to the timer overflow, as used above
- ClearTimer — resetting the timer’s count to zero
- PulseOut — generating the LED pulse in Example 2
Supported in <TIMER.H>

