logo

如何定时500毫秒串口发送数据

2023-09-06 11:13
H
大力士

如何定时500毫秒串口发送数据,Timer类给的示例运行不成功

2023-09-06 17:07
I
网红小智智 星瞳科技
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')
2023-09-14 14:55
H
大力士

你好,试了一下这个定时500ms会因程序周期变化而变化,中断定时给其它单片机通讯改怎么写呢?谢谢!!!

2023-09-14 18:11
I
网红小智智 星瞳科技
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)