DAC Unit
The Dac2 class interfaces with a GP8413 15-bit Digital to Analog Converter (DAC), capable of converting digital signals into two channels of analog voltage output, ranging from 0-5V and 0-10V.
Support the following products:
Micropython Example:
1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD 2# 3# SPDX-License-Identifier: MIT 4 5import os, sys, io 6import M5 7from M5 import * 8from hardware import * 9from unit import DACUnit 10 11 12label0 = None 13label1 = None 14label2 = None 15label3 = None 16i2c0 = None 17dac_0 = None 18 19 20v = None 21 22 23def btnA_wasClicked_event(state): # noqa: N802 24 global label0, label1, label2, label3, i2c0, dac_0, v 25 if v >= 0.1: 26 v = v - 0.1 27 dac_0.set_voltage(v) 28 29 30def btnB_wasClicked_event(state): # noqa: N802 31 global label0, label1, label2, label3, i2c0, dac_0, v 32 if v < 3.3: 33 v = v + 0.1 34 dac_0.set_voltage(v) 35 36 37def btnC_wasClicked_event(state): # noqa: N802 38 global label0, label1, label2, label3, i2c0, dac_0, v 39 v = 0 40 dac_0.set_voltage(v) 41 42 43def setup(): 44 global label0, label1, label2, label3, i2c0, dac_0, v 45 46 M5.begin() 47 Widgets.fillScreen(0x222222) 48 label0 = Widgets.Label("label0", 133, 110, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18) 49 label1 = Widgets.Label("-", 60, 210, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24) 50 label2 = Widgets.Label("+", 143, 210, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24) 51 label3 = Widgets.Label("reset", 214, 210, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24) 52 53 BtnA.setCallback(type=BtnA.CB_TYPE.WAS_CLICKED, cb=btnA_wasClicked_event) 54 BtnB.setCallback(type=BtnB.CB_TYPE.WAS_CLICKED, cb=btnB_wasClicked_event) 55 BtnC.setCallback(type=BtnC.CB_TYPE.WAS_CLICKED, cb=btnC_wasClicked_event) 56 57 i2c0 = I2C(0, scl=Pin(22), sda=Pin(21), freq=100000) 58 dac_0 = DACUnit(i2c0) 59 v = 0 60 61 62def loop(): 63 global label0, label1, label2, label3, i2c0, dac_0, v 64 M5.update() 65 label0.setText(str(dac_0.get_voltage())) 66 67 68if __name__ == "__main__": 69 try: 70 setup() 71 while True: 72 loop() 73 except (Exception, KeyboardInterrupt) as e: 74 try: 75 from utility import print_error_msg 76 77 print_error_msg(e) 78 except ImportError: 79 print("please update to latest firmware")
UIFLOW2 Example:
class DACUnit
Constructors
Methods
- DACUnit.get_value() int
Get the current value of the DAC.
- 返回:
The DAC value as a 16-bit unsigned value.
UIFLOW2:
- DACUnit.get_voltage() float
Get the current voltage of the DAC.
- 返回:
The DAC voltage as a float.
UIFLOW2:
- DACUnit.set_value(value: int) None
Set the value of the DAC.
- 参数:
value – The DAC value as a 16-bit unsigned value.
UIFLOW2:
- DACUnit.set_voltage(voltage: float) None
Set the voltage of the DAC.
- 参数:
voltage – The DAC voltage as a float. The voltage must be between 0 and 3.3V.
UIFLOW2:
- DACUnit.get_raw_value() int
Get the raw value of the DAC.
- 返回:
The raw DAC value as a 12-bit unsigned value.
UIFLOW2:
- DACUnit.set_raw_value(value: int) None
Set the raw value of the DAC.
- 参数:
value – The raw DAC value as a 12-bit unsigned value.
UIFLOW2:
- DACUnit.get_normalized_value() float
Get the normalized value of the DAC.
- 返回:
The normalized DAC value as a float.
UIFLOW2: