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)
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).
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.
It is recommended to use a 'settimer off' command before any 'settimer count' command. This will ensure the silicon interrupt is disabled before the new preload value is written to it.
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.
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.
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.
Using 'settimer' with interrupts
This program will cause an interrupt to be generated every second with an output message to produced within that interrupt.
Submit Your Own Code!
You must be logged in to submit code examples. Login now.