Name:
settimer
Syntax:

SETTIMER OFF

SETTIMER preload

SETTIMER COUNT preload

Preload - is the constant/variable that selects the appropriate timing. For convenience timer 1s value constants are predefined in the compiler:

t1s_4 (preload value 49910 - 1 second at 4MHz)
t1s_8 (preload value 34286 - 1 second at 8MHz)
t1s_16 (preload value 3036 - 1 second at 16MHz)
Description:

Configure and start the internal timer / counter.

The settimer command is used to configure the hardware timer / counter function. The timer function can be used in two way - as an internal timer or as an external counter (input 0 (C.0) only).

Note that the debug command temporarily disables the timer (during the actual variables transmission). Therefore use of the debug command at the same time as the timer will cause false readings.

External Counter (not available on 20X2)

In external counter mode an internal counter register (not accessible to the end user) is incremented on every positive going edge detected on input 0. This pulse counting occurs in the background, so the PICAXE program can perform other tasks at the same time as it is counting (unlike the count command, which stops other processing during the count command time period). When the internal counter register overflows from 65535 to 0, the special 'timer' variable is automatically incremented.

Therefore to increment the timer variable on every 10 external pulses set the preload value to 65536 - 10 = 65526. After ten pulses the counter register will overflow and hence increment the 'timer' variable. To increment the 'timer' variable on every external pulse simply set the preload value to 65535.

If the timer word variable overflows (ie from 65535 to 0) the timer overflow flag (toflag) is set. The toflag is automatically cleared upon the settimer command, but can also be cleared manually in software via 'let toflag = 0'. If desired an interrupt can be set to detect this overflow by use of the setintflags command.

Internal Timer

In internal timer mode the time elapsed is stored in the word variable 'timer' which can be accessed as if was a normal variable.

e.g. if timer > 200 then skip

When the timer word variable overflows (ie from 65535 to 0) the timer overflow flag (toflag) is set . The toflag is automatically cleared upon the settimer command, but can also be cleared manually via 'let toflag = 0'. If desired an interrupt can be set to detect this overflow by use of the setintflags command.

The period of the timer can be used defined. The timer operates with 'minor ticks' and 'major ticks'. A minor tick occurs every 1/(clock freq / 256) seconds. With a 4MHz resonator this means a minor tick occurs every 64us (32us at 8MHz, 16us at 16MHz, 8us at 32MHz, 4us at 64MHz). When the minor tick word variable (not accessible by the end user) overflows (from 65535 to 0) a major tick occurs. The major tick increments the timer variable, and so the number of major ticks passed can be determined by reading the 'timer' variable.

The preload value is used to preload the minor tick variable after it overflows. This means it is not always necessary to wait the full 65536 minor ticks, for instance, if the preload value is set to 60000 you then only have to wait 5536 minor ticks before the major tick occurs.

As an example, assume you wish the timer to increment every second at 4MHz. We know that at 4MHz each minor tick takes 64us and 1 second is equivalent to 1000000 us. Therefore we require 15625 (1000000 / 64) minor ticks to give us a 1 second delay. Finally 65536 - 15625 = 49910, so our preload value become 49910. Timer cannot be used at the same time as the servo command, as the servo command requires sole use of the timer to calculate the servo pulse intervals.

Applies To:
20X2, 28X1, 28X2, 40X1, 40X2
See Also:
Related Create:
    Share:
    Print:

    Counting input pulses

    This program will set the timer into counting mode and sets the pre-count value to 65535 causing the count to overflow and the 'timer' variable to increment on every input pulse. When the debug command is executed the 'timer' variable will show the number of pulses received during the ten second pause.

    Code Example:
    	settimer count 65535	; settimer to count mode
    
    main:	pause 10000		; wait 10 seconds, counting pulses
    	debug			; display timer value
    	goto main		; loop
    Copy Code Submit an Example

    Counting seconds

    This program sets the timer pre-counts such that the 'timer' variable will increment every second. When the 'debug' command is executed the 'timer' variable will show how many seconds have elapsed during the preceding 'pause' command.

    Code Example:
    	settimer t1s_4		; settimer to 1 second ticks at 4MHz
    
    main:	pause 10000		; wait 10 seconds
    	debug			; display timer value
    	goto main		; loop
    Copy Code Submit an Example

    Using 'settimer' with interrupts

    This program will cause an interrupt to be generated every second with an output message to produced within that interrupt.

    Code Example:
    main:
    	settimer t1s_8
    	gosub Interrupt_Enable
    
    idle:
    	goto idle
     
    interrupt:
    	sertxd( "Interrupt ", #w0, cr, lf )
    	w0 = w0 + 1
     
    interrupt_Enable:
    	setintflags %10000000, %10000000
    	timer = $FFFF
    	toflag = 0
    	return 
    Copy Code Submit an Example

    Submit Your Own Code!

    You must be logged in to submit code examples. Login now.

    Comments

    • Login to leave a comment.