|
| 1 | +/** |
| 2 | + * @file DAC2_GP8413.ino |
| 3 | + * @author SeanKwok ([email protected]) |
| 4 | + * @brief Basic Unit DAC2 Test |
| 5 | + * @version 0.1 |
| 6 | + * @date 2024-01-09 |
| 7 | + * |
| 8 | + * |
| 9 | + * @Hardwares: Basic + Unit DAC2(GP8413) |
| 10 | + * @Platform Version: Arduino M5Stack Board Manager v2.0.9 |
| 11 | + * @Dependent Library: |
| 12 | + * M5GFX: https://github.com/m5stack/M5GFX |
| 13 | + * M5Unified: https://github.com/m5stack/M5Unified |
| 14 | + * DFRobot_GP8XXX: https://github.com/DFRobot/DFRobot_GP8XXX |
| 15 | + */ |
| 16 | + |
| 17 | +#include <M5GFX.h> |
| 18 | +#include <M5Unified.h> |
| 19 | +#include <DFRobot_GP8XXX.h> |
| 20 | + |
| 21 | +DFRobot_GP8XXX_IIC GP8413(RESOLUTION_15_BIT, 0x59, &Wire); |
| 22 | + |
| 23 | +// range is 0~10000mv |
| 24 | +void setDacVoltage(uint16_t vol, uint8_t ch) { |
| 25 | + uint16_t setting_vol = 0; |
| 26 | + if (vol > 10000) { |
| 27 | + vol = 10000; |
| 28 | + } |
| 29 | + if (ch > 1) ch = 1; |
| 30 | + setting_vol = (int16_t)((float)vol / 10000.0f * 32767.0f); |
| 31 | + if (setting_vol > 32767) { |
| 32 | + setting_vol = 32767; |
| 33 | + } |
| 34 | + GP8413.setDACOutVoltage(setting_vol, ch); |
| 35 | +} |
| 36 | + |
| 37 | +void AllOutputCtl(uint16_t vol) { |
| 38 | + M5.Display.fillRect(0, 0, M5.Display.width(), 30, vol > 0 ? GREEN : ORANGE); |
| 39 | + M5.Display.drawString("OUTPUT " + String(vol) + "mv", |
| 40 | + M5.Display.width() / 2, 0); |
| 41 | + // set channel0 |
| 42 | + setDacVoltage(vol, 0); |
| 43 | + // set channel1 |
| 44 | + setDacVoltage(vol, 1); |
| 45 | +} |
| 46 | + |
| 47 | +void setup(void) { |
| 48 | + auto cfg = M5.config(); |
| 49 | + |
| 50 | + M5.begin(cfg); |
| 51 | + M5.Display.setRotation(1); |
| 52 | + M5.Display.setTextDatum(top_center); |
| 53 | + M5.Display.setTextColor(WHITE); |
| 54 | + M5.Display.setFont(&fonts::FreeSansBoldOblique12pt7b); |
| 55 | + M5.Display.setTextSize(1); |
| 56 | + M5.Display.drawString("DAC2", M5.Display.width() / 2, |
| 57 | + M5.Display.height() / 2 - 20); |
| 58 | + Wire.end(); |
| 59 | + Wire.begin(21, 22); |
| 60 | + |
| 61 | + while (GP8413.begin() != 0) { |
| 62 | + Serial.println("Init Fail!"); |
| 63 | + M5.Display.drawString("Init Fail!", M5.Display.width() / 2, |
| 64 | + M5.Display.height() / 2); |
| 65 | + delay(1000); |
| 66 | + } |
| 67 | + M5.Display.clear(); |
| 68 | + M5.Display.drawString("DAC2", M5.Display.width() / 2, |
| 69 | + M5.Display.height() / 2 - 20); |
| 70 | + GP8413.setDACOutRange(GP8413.eOutputRange10V); |
| 71 | + M5.Display.drawString("BtnA En/Dis Output", M5.Display.width() / 2, |
| 72 | + M5.Display.height() / 2 + 20); |
| 73 | + |
| 74 | + AllOutputCtl(0); |
| 75 | +} |
| 76 | + |
| 77 | +bool output = false; |
| 78 | + |
| 79 | +void loop(void) { |
| 80 | + M5.update(); |
| 81 | + if (M5.BtnA.wasClicked()) { |
| 82 | + output = !output; |
| 83 | + if (output) { |
| 84 | + AllOutputCtl(3300); |
| 85 | + // AllOutputCtl(10000); |
| 86 | + } else { |
| 87 | + AllOutputCtl(0); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments