Name:
time
Syntax:

{LET} wordvariable = TIME

{LET} TIME = expression

if TIME > value then

Description:

Use the elapsed time counter variable.

The M2 series have an internal elapsed time counter. This is a word variable called 'time' which increments once per second (at 4 or 16MHz operation). The time variable is designed as an approximate elapsed seconds counter, it is not a real time clock. This seconds counter starts automatically on a power-on reset, but can also be enabled/disabled by the disabletime / enabletime commands.

Note that the background internal timer used to count seconds is also used by several other commands such as servo. Therefore when re-enabling the time variable incrementing function with enabletime (after a disabletime command has previously been used) the first tick could happen any time during that initial one second time period. When the time function is disabled the time variable may be altered safely with a command such as 'let time = 0'.

The 'time' variable will not increment while a sleep command is executing.

Effect of increased clock speed

The time function will increment once every second at 4 MHz or 16 MHz. At 2 MHz and 8 MHz the interval will be 2s, at 32 MHz the time interval will be 0.5s. The number of seconds per time variable increment is shown below -

32 MHz 0.5s  
16 MHz 1s  
8 MHz 2s  
4 MHz - 1s
2 MHz - 2s
1 MHz 16s  
500 kHz 32s  
250 kHz 64s  
125 kHz - 32s
31 kHz - 128s
Applies To:
08M2, 14M2, 18M2, 20M2
See Also:
Related Create:
    Share:
    Print:

    Time example

    Activates an LED after 10 seconds

    Code Example:
    let w0 = time + 10
    
    do
      if time > w0 then 
        high B.0
      end if
    loop
    Copy Code Submit an Example

    Flashing a LED

    Flashes a LED every two seconds

    Code Example:
    'flashes every 0.5 seconds
    
    main:
    	setfreq m32
    	w0 = time // 2
    
    	if w0 = 0 then
    	        high b.0
    	else
    	        low b.0
    	end if
    
    	goto main
    Copy Code Submit an Example

    Submit Your Own Code!

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