AIN4 Module

支持以下产品:

AIN4Module

MicroPython 应用示例:

import os, sys, io
import M5
from M5 import *
from module import AIN4Module

title0 = None
label0 = None
label1 = None
label2 = None
label3 = None
ain4_20ma_0 = None

def setup():
    global title0, label0, label1, label2, label3, ain4_20ma_0

    M5.begin()
    Widgets.fillScreen(0x222222)
    title0 = Widgets.Title("AIN 4-20mA Module Test", 3, 0xffffff, 0x0000FF, Widgets.FONTS.DejaVu18)
    label0 = Widgets.Label("CH1 Current:", 1, 60, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18)
    label1 = Widgets.Label("CH2 Current:", 1, 96, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18)
    label2 = Widgets.Label("CH3 Current:", 1, 131, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18)
    label3 = Widgets.Label("CH4 Current:", 1, 164, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18)

    ain4_20ma_0 = AIN4Module(address=0x55)
    ain4_20ma_0.set_cal_current(1, 20)
    ain4_20ma_0.set_cal_current(2, 20)
    ain4_20ma_0.set_cal_current(3, 20)
    ain4_20ma_0.set_cal_current(4, 20)

def loop():
    global title0, label0, label1, label2, label3, ain4_20ma_0
    M5.update()
    label0.setText(str((str('CH1 Current:') + str((ain4_20ma_0.get_current_value(1))))))
    label1.setText(str((str('CH2 Current:') + str((ain4_20ma_0.get_current_value(2))))))
    label2.setText(str((str('CH3 Current:') + str((ain4_20ma_0.get_current_value(3))))))
    label3.setText(str((str('CH4 Current:') + str((ain4_20ma_0.get_current_value(4))))))

if __name__ == '__main__':
    try:
        setup()
        while True:
            loop()
    except (Exception, KeyboardInterrupt) as e:
        try:
            from utility import print_error_msg
            print_error_msg(e)
        except ImportError:
            print("please update to latest firmware")

UiFlow2 应用示例:

example.png

ain4_core2_example.m5f2

class AIN4Module

构造函数

class AIN4Module(address)

初始化 I2C 模块 AIN 4-20mA 的 I2C 地址。

参数:

address (int|list|tuple) – AIN4Module 的 I2C 地址。

UIFLOW2:

init.png

Methods

AIN4Module.get_adc_raw_value(channel) int

从指定通道获取原始 ADC 值。

参数:

channel (int) – 要从中读取 ADC 值的通道号(1 到 4)。

返回:

原始 ADC 值,作为 12 位整数。

UIFLOW2:

get_adc_raw_value.png

AIN4Module.get_current_value(channel) int

从指定通道读取当前电流值(单位:mA)。

参数:

channel (int) – 用于读取当前值的通道编号(1 到 4)。

返回:

以毫安(mA)为单位的当前值。

UIFLOW2:

get_current_value.png

AIN4Module.set_cal_current(channel, val)

为指定通道设置校准电流。

参数:
  • channel (int) – 要设置校准的通道编号(1 到 4)。

  • val (int) – 校准电流值,范围为 4 到 20 mA。

UIFLOW2:

set_cal_current.png

AIN4Module.get_firmware_version() int

获取 AIN 4-20mA 模块的固件版本。

返回:

固件版本。

UIFLOW2:

get_firmware_version.png

AIN4Module.get_i2c_address() str

获取 AIN 4-20mA 模块的当前 I2C 地址。

返回:

I2C 地址为十六进制格式的字符串。

UIFLOW2:

get_i2c_address.png

AIN4Module.set_i2c_address(addr)

为 AIN 4-20mA 模块设置新的 I2C 地址。

参数:

addr (int) – 新的 I2C 地址必须在 0x08 到 0x77 之间。

UIFLOW2:

set_i2c_address.png