logo

有关于定时器的问题?

2020-09-21 11:04
E
尤觊

怎样使定时器在发生中断后使一个变量加一

2020-09-22 19:26
I
网红小智智 星瞳科技
# Timer Control Example
#
# This example shows how to use a timer for callbacks.

import time
from pyb import Pin, Timer

# we will receive the timer object when being called
# Note: functions that allocate memory are Not allowed in callbacks
cnt = 0

def tick(timer):  
    global cnt;
    cnt +=1
    
tim = Timer(2, freq=1)      # create a timer object using timer 2 - trigger at 1Hz
tim.callback(tick)          # set the callback to our tick function

while (True):
    time.sleep(2000)
    print(cnt)