PWRCAN

PWRCAN 是一种可用于与其他设备通信的 CAN 接口。

以下是主机对 PWRCAN 的支持情况:

控制器

状态

PowerHub

UiFlow2 示例

pwrcan_send_receive

在 UiFlow2 中打开 pwrcan_send_receive_powerhub_example.m5f2 项目。

此示例演示如何使用 PWRCAN 接口进行数据发送与接收。

UiFlow2 代码块:

pwrcan_send_receive_powerhub_example.png

示例输出:

None

MicroPython 示例

pwrcan_send_receive

此示例演示如何使用 PWRCAN 接口进行数据发送与接收。

MicroPython 代码块:

 1# SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from hardware import PWRCAN
 9
10
11pwrcan = None
12
13
14def setup():
15    global pwrcan
16
17    M5.begin()
18    Power.setExtPortBusConfig(direction=1, output_enable=1, voltage=12000, current_limit=232)
19    pwrcan = PWRCAN(id=0, port=(40, 39), mode=PWRCAN.NORMAL, baudrate=25000)
20
21
22def loop():
23    global pwrcan
24    M5.update()
25    if BtnA.wasPressed():
26        pwrcan.send("uiflow2", 0, timeout=0, rtr=False, extframe=False)
27    if pwrcan.any(0):
28        print(pwrcan.recv(0, timeout=5000))
29
30
31if __name__ == "__main__":
32    try:
33        setup()
34        while True:
35            loop()
36    except (Exception, KeyboardInterrupt) as e:
37        try:
38            from utility import print_error_msg
39
40            print_error_msg(e)
41        except ImportError:
42            print("please update to latest firmware")

示例输出:

None

API

PWRCAN

class PWRCAN(id, mode, tx, rx, prescaler=32, sjw=3, bs1=15, bs2=4, triple_sampling=False)

使用给定参数初始化 CAN 总线。

参数:
  • id (int) – CAN 总线 ID。

  • mode (int) – NORMAL、NO_ACKNOWLEDGE、LISTEN_ONLY 之一。

  • tx (int) – 用于发送数据的引脚。

  • rx (int) – 用于接收数据的引脚。

  • prescaler (int) – 用于分频 CAN 输入时钟以生成标称位时间量子的值。对于经典 CAN,取值范围为 1 至 1024(含)。

  • sjw (int) – 标称位的重同步跳跃宽度(单位为时间量子);对于经典 CAN,取值范围为 1 至 4(含)。

  • bs1 (int) – 定义标称位采样点的位置(单位为时间量子);对于经典 CAN,取值范围为 1 至 16(含)。

  • bs2 (int) – 定义标称位发送点的位置(单位为时间量子);对于经典 CAN,取值范围为 1 至 8(含)。

  • triple_sampling (bool) – 在 TWAI 控制器采样位时启用三重采样。

UiFlow2 代码块:

init.png

MicroPython 代码块:

from hardware import PWRCAN

can = PWRCAN(id=0, port=(40, 39), mode=PWRCAN.NORMAL, baudrate=25000)

PWRCAN 类继承自 CAN 类。更多详情请参阅 hardware.CAN