timer.py 1.4 KB

123456789101112131415161718192021222324252627
  1. #
  2. # Copyright (c) 2006-2019, RT-Thread Development Team
  3. #
  4. # SPDX-License-Identifier: MIT License
  5. #
  6. # Change Logs:
  7. # Date Author Notes
  8. # 2019-06-29 ChenYong first version
  9. #
  10. def callback_periodic(obj): # defined preiodic mode timeout callback
  11. print("Timer callback periodic test")
  12. def callback_oneshot(obj): # defined ont shot mode timeout callback
  13. print("Timer callback oneshot test")
  14. from machine import Timer
  15. import utime as time
  16. timer = Timer(15) # Create Timer object. Timer device number 15 are used.
  17. timer.init(timer.PERIODIC, 1000, callback_periodic) # Initialize the Timer device object
  18. # Set Timer mode to preiodic mode, set timeout to 1 seconds and set callback fucntion
  19. time.sleep_ms(5500) # Execute 5 times timeout callback in the delay time
  20. timer.init(timer.ONE_SHOT, 1000, callback_oneshot) # Reset initialize the Timer device object
  21. # Set Timer mode to one shot mode, set timeout to 1 seconds and set callback fucntion
  22. time.sleep_ms(1500) # Execute 1 times timeout callback in the delay time
  23. timer.deinit() # Stop and close Timer device object