Settimer

Syntax:

    Settimer timernumber, byte_value

    Settimer timernumber, word_value

Command Availability:

Available on all microcontrollers with a Timer modules.

Explanation:

Settimer will set the value of the specified timer with either byte value or a word value. 8-bit timers use a byte value. 16-bit timers use a word value.

Settimer can be used on-the-fly, so there is no requirement to stop the timer first.

Refer to the datasheet for timer specific information.




Example:

This example shows the operation of setting two timers — it is not intended as a meaningful solution.

    #chip 16f877a, 4
    On Interrupt Timer1Overflow call Overflowed
    Set PORTB.0 On

    InitTimer1 Osc, PS1_8
    SetTimer 1, 1          ' <<< the Settimer instruction (byte value)
    StartTimer 1

    InitTimer2 PS2_16, PS2_16
    SetTimer 2, 255
    StartTimer 2

    'Manually set Timer2Overflow to create a second event
    'this will event will be handled by the Interrupt sub routine
    TMR2IE = 1
    end

    Sub Interrupt
      Set PORTB.2 On
      TMR2IF = 0
    End Sub

    Sub Overflowed
      Set PORTB.1 On
      TMR1IF = 0
    End Sub

Key line: SetTimer 1, 1 — preloads Timer 1 with the value 1 immediately before starting it, so its very first overflow happens sooner than a fresh, cleared timer would produce.

See Also:

  • StartTimer — starting the timer after preloading it, as used above
  • ClearTimer — resetting a timer’s count to 0 instead of a specific value
  • On Interrupt — handling the overflow event, as used above

Supported in <TIMER.H>