0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

micropythonでshrike-lite その42

0
Last updated at Posted at 2026-09-29

概要

micropythonでshrike-lite、やってみた。
uartで嵌ったので、ボーレイトを検出してみた。

サンプルコード


from machine import UART, Pin
import time

RX_PIN = 1
TIMEOUT_US = 100000

def detect_baudrate(pin_num):
	rx_pin = Pin(pin_num, Pin.IN, Pin.PULL_UP)
	print("scan----")
	samples = []
	start_time = time.ticks_ms()
	while len(samples) < 20:
		duration = machine.time_pulse_us(rx_pin, 0, TIMEOUT_US)
		if duration > 0:
			samples.append(duration)
		if time.ticks_diff(time.ticks_ms(), start_time) > 3000:
			break
	if not samples:
		print("err: not found")
		return None
	min_pulse = min(samples)
	calculated_baud = int(1_000_000 / min_pulse)
	standard_bauds = [1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200]
	closest_baud = min(standard_bauds, key=lambda x: abs(x - calculated_baud))
	error = abs(closest_baud - calculated_baud) / closest_baud
	if error > 0.10:
		print(f"err: calc ({calculated_baud}) not standerd")
		return calculated_baud
	return closest_baud


detected_baud = detect_baudrate(RX_PIN)
if detected_baud:
	print(f"baurate: {detected_baud} bps")



以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?