[Document sharing]-Timer

It mainly tells about how to use QuecPython_Timer based on EC600S, for the rest, they are just similar. In most cases, the timer is used for timing and counting, which can be used as precise delay processing, but also used to calculate the total number of pulses under the premise of connecting a clock source. In this article, you will learn all the settings of the Timer and how to use it.

Interaction operation

Interact with module via QPYcom. The next case is based on Timer0 and Timer1. As for Timer2 and Timer3, it is similar.

Note

  1. The reason to execute command ”from machine import Timer’’ is to make the Timer module invisible in current space.
  2. Only execute “from machine import Timer” command in module can the function and variate in Timer be used.
  3. Please do remember above operations have on connection with any peripheral, it just serves as reference to get familiar with commands.

Download and verify

SW codes

The referential coded of the matched demo is the timer_file.py file that in the same directory. Download .py file and run it on module. The following link shows the codes: Click to download codes.

import log from machine import Timer log.basicConfig(level=log.INFO) # Set log output level Timer_Log = log.getLogger(“Quectel”) # Get logger object log_print_num = 5 state = 1 timer0 = Timer(Timer.Timer1) # Make an executable function and import timer def timer_test(t): global log_print_num global state Timer_Log.info(‘log_print_num is %d’ % log_print_num) log_print_num -= 1 if log_print_num <= 0: Timer_Log.info(‘timer exit’) state = 0 timer0.stop() # End this timer timer0.start(period=1000, mode=timer0.PERIODIC, callback=timer_test) # Start timer while state: pass

HW connection

This is just aimed at timer, as a result, there is no needed peripheral.

Result

  1. Run timer_file.py in QPYcom.
  2. Check the output in interactive surface of QPYcom.

HW description

Currently, 4 timers are available; as for details, please refer to the link: HW support

SW design

SW-related API, please refer to link: Timer

Constant illustration

Constant Illustration
Timer.Timer0 Timer0
Timer.Timer1 Timer1
Timer.Timer2 Timer2
Timer.Timer3 Timer3
Timer.ONE_SHOT One shot mode, the timer just executes for one time
Timer.PERIODIC Periodic mode, the timer executes periodically

Create Timer object

timer = Timer(Timer)

The port parameter for creating timer object is shown as next:

Parameter Type Illustration
Timer int Timer number, the EC600S supports Timer 0 to Timer 3.

Start timer

timer.start(period, mode, callback):

Start corresponding timer. Here shows the port parameter.

Parameter Type Illustration
period int Interrupt period, the unit is ms.
mode int Running mode, as for Timer. ONE_SHOT, the timer just executes one time; while for Timer.PERIODIC, the timer executes periodically.
callback function Timer execution function

Return value: if it is a success to start, it returns the integer 0, otherwise, it returns integer -1.

Stop timer

timer.stop():Stop corresponding timer without parameter.

Return value: if it is a success to start, it returns the integer 0, otherwise, it returns integer -1.