如何定时500毫秒串口发送数据,Timer类给的示例运行不成功
import sensor, image
import pyb
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
last_time = pyb.millis()
while(True):
img = sensor.snapshot()
if pyb.millis() - last_time > 500:
last_time = pyb.millis()
print('send')你好,试了一下这个定时500ms会因程序周期变化而变化,中断定时给其它单片机通讯改怎么写呢?谢谢!!!
import time
from pyb import UART
from pyb import Timer
uart = UART(3, 19200)
data_bytes = b'123456'
# we will receive the timer object when being called
# Note: functions that allocate memory are Not allowed in callbacks
def tick(timer):
global data_bytes
global uart
print(data_bytes)
uart.write(data_bytes)
tim = Timer(2, freq=2) # create a timer object using timer 2 - trigger at 1Hz
tim.callback(tick) # set the callback to our tick function
while True:
data_bytes = b'123456'
time.sleep_ms(1000)
data_bytes = b'654321'
time.sleep_ms(1000)