From d73466140460ed4abf1b61a2df4cc25a8d33fc61 Mon Sep 17 00:00:00 2001 From: sean <1026025056@qq.com> Date: Tue, 9 Jan 2024 16:45:37 +0800 Subject: [PATCH] add hat/unit dac2 example --- examples/Hat/DAC2_GP8413/DAC2_GP8413.ino | 90 +++++++++++++++++++++++ examples/Unit/DAC2_GP8413/DAC2_GP8413.ino | 90 +++++++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 examples/Hat/DAC2_GP8413/DAC2_GP8413.ino create mode 100644 examples/Unit/DAC2_GP8413/DAC2_GP8413.ino diff --git a/examples/Hat/DAC2_GP8413/DAC2_GP8413.ino b/examples/Hat/DAC2_GP8413/DAC2_GP8413.ino new file mode 100644 index 0000000..ebf5a1a --- /dev/null +++ b/examples/Hat/DAC2_GP8413/DAC2_GP8413.ino @@ -0,0 +1,90 @@ +/** + * @file DAC2_GP8413.ino + * @author SeanKwok (shaoxiang@m5stack.com) + * @brief M5StickC Hat DAC2 Test + * @version 0.1 + * @date 2024-01-09 + * + * + * @Hardwares: M5StickC + Hat DAC2(GP8413) + * @Platform Version: Arduino M5Stack Board Manager v2.0.9 + * @Dependent Library: + * M5GFX: https://github.com/m5stack/M5GFX + * M5Unified: https://github.com/m5stack/M5Unified + * DFRobot_GP8XXX: https://github.com/DFRobot/DFRobot_GP8XXX + */ + +#include +#include +#include + +DFRobot_GP8XXX_IIC GP8413(RESOLUTION_12_BIT, 0x59, &Wire); + +// range is 0~10000mv +void setDacVoltage(uint16_t vol, uint8_t ch) { + uint16_t setting_vol = 0; + if (vol > 10000) { + vol = 10000; + } + if (ch > 1) ch = 1; + setting_vol = (int16_t)((float)vol / 10000.0f * 32767.0f); + if (setting_vol > 32767) { + setting_vol = 32767; + } + GP8413.setDACOutVoltage(setting_vol, ch); +} + +void AllOutputCtl(uint16_t vol) { + M5.Display.fillRect(0, 0, M5.Display.width(), 22, vol > 0 ? GREEN : ORANGE); + M5.Display.drawString("OUTPUT " + String(vol) + "mv", + M5.Display.width() / 2, 0); + // set channel0 + setDacVoltage(vol, 0); + // set channel1 + setDacVoltage(vol, 1); +} + +void setup(void) { + auto cfg = M5.config(); + + M5.begin(cfg); + M5.Display.setRotation(1); + M5.Display.setTextDatum(top_center); + M5.Display.setTextColor(WHITE); + M5.Display.setFont(&fonts::FreeSansBoldOblique9pt7b); + M5.Display.setTextSize(0.9); + M5.Display.drawString("HAT DAC2", M5.Display.width() / 2, + M5.Display.height() / 2 - 12); + Wire.end(); + Wire.begin(0, 26); + + while (GP8413.begin() != 0) { + Serial.println("Init Fail!"); + M5.Display.drawString("Init Fail!", M5.Display.width() / 2, + M5.Display.height() / 2); + delay(1000); + } + M5.Display.clear(); + M5.Display.drawString("HAT DAC2", M5.Display.width() / 2, + M5.Display.height() / 2 - 12); + GP8413.setDACOutRange(GP8413.eOutputRange10V); + M5.Display.drawString("BtnA En/Dis Output", M5.Display.width() / 2, + M5.Display.height() / 2 + 12); + + AllOutputCtl(0); +} + +bool output = false; + +void loop(void) { + M5.update(); + if (M5.BtnA.wasClicked()) { + output = !output; + if (output) { + AllOutputCtl(3300); + // AllOutputCtl(10000); + } else { + AllOutputCtl(0); + } + } +} diff --git a/examples/Unit/DAC2_GP8413/DAC2_GP8413.ino b/examples/Unit/DAC2_GP8413/DAC2_GP8413.ino new file mode 100644 index 0000000..16848e9 --- /dev/null +++ b/examples/Unit/DAC2_GP8413/DAC2_GP8413.ino @@ -0,0 +1,90 @@ +/** + * @file DAC2_GP8413.ino + * @author SeanKwok (shaoxiang@m5stack.com) + * @brief M5StickC Unit DAC2 Test + * @version 0.1 + * @date 2024-01-09 + * + * + * @Hardwares: M5StickC + Unit DAC2(GP8413) + * @Platform Version: Arduino M5Stack Board Manager v2.0.9 + * @Dependent Library: + * M5GFX: https://github.com/m5stack/M5GFX + * M5Unified: https://github.com/m5stack/M5Unified + * DFRobot_GP8XXX: https://github.com/DFRobot/DFRobot_GP8XXX + */ + +#include +#include +#include + +DFRobot_GP8XXX_IIC GP8413(RESOLUTION_12_BIT, 0x59, &Wire); + +// range is 0~10000mv +void setDacVoltage(uint16_t vol, uint8_t ch) { + uint16_t setting_vol = 0; + if (vol > 10000) { + vol = 10000; + } + if (ch > 1) ch = 1; + setting_vol = (int16_t)((float)vol / 10000.0f * 32767.0f); + if (setting_vol > 32767) { + setting_vol = 32767; + } + GP8413.setDACOutVoltage(setting_vol, ch); +} + +void AllOutputCtl(uint16_t vol) { + M5.Display.fillRect(0, 0, M5.Display.width(), 22, vol > 0 ? GREEN : ORANGE); + M5.Display.drawString("OUTPUT " + String(vol) + "mv", + M5.Display.width() / 2, 0); + // set channel0 + setDacVoltage(vol, 0); + // set channel1 + setDacVoltage(vol, 1); +} + +void setup(void) { + auto cfg = M5.config(); + + M5.begin(cfg); + M5.Display.setRotation(1); + M5.Display.setTextDatum(top_center); + M5.Display.setTextColor(WHITE); + M5.Display.setFont(&fonts::FreeSansBoldOblique9pt7b); + M5.Display.setTextSize(0.9); + M5.Display.drawString("HAT DAC2", M5.Display.width() / 2, + M5.Display.height() / 2 - 12); + Wire.end(); + Wire.begin(32, 33); + + while (GP8413.begin() != 0) { + Serial.println("Init Fail!"); + M5.Display.drawString("Init Fail!", M5.Display.width() / 2, + M5.Display.height() / 2); + delay(1000); + } + M5.Display.clear(); + M5.Display.drawString("HAT DAC2", M5.Display.width() / 2, + M5.Display.height() / 2 - 12); + GP8413.setDACOutRange(GP8413.eOutputRange10V); + M5.Display.drawString("BtnA En/Dis Output", M5.Display.width() / 2, + M5.Display.height() / 2 + 12); + + AllOutputCtl(0); +} + +bool output = false; + +void loop(void) { + M5.update(); + if (M5.BtnA.wasClicked()) { + output = !output; + if (output) { + AllOutputCtl(3300); + // AllOutputCtl(10000); + } else { + AllOutputCtl(0); + } + } +}