|
| 1 | +/** |
| 2 | + * | Supported IO Expanders | CH422G | |
| 3 | + * | ------------------------- | ------ | |
| 4 | + * |
| 5 | + * # CH422G Test Example |
| 6 | + * |
| 7 | + * The hardware device used in this example is waveshare ESP32-S3-Touch-LCD-4.3B-BOX. To test the simultaneous use of I/O input and OC output, connect DO0 to DI0, and connect DO1 to DI1. |
| 8 | + * |
| 9 | + * ## How to use |
| 10 | + * |
| 11 | + * 1. Enable USB CDC. |
| 12 | + * 2. Verify and upload the example to your board. |
| 13 | + * |
| 14 | + * ## Serial Output |
| 15 | + * |
| 16 | + * ``` |
| 17 | + * ... |
| 18 | + * Test begin |
| 19 | + * Set the OC pin to push-pull output mode. |
| 20 | + * Set the IO0-7 pin to input mode. |
| 21 | + * Set pint 8 and 9 to:0, 1 |
| 22 | + * |
| 23 | + * Read pin 0 and 5 level: 0, 1 |
| 24 | + * |
| 25 | + * Set pint 8 and 9 to:1, 0 |
| 26 | + * |
| 27 | + * Read pin 0 and 5 level: 1, 0 |
| 28 | + * ... |
| 29 | + * ``` |
| 30 | + * |
| 31 | + * ## Troubleshooting |
| 32 | + * |
| 33 | + * The driver initialization by default sets CH422G's IO0-7 to output high-level mode. |
| 34 | + Since the input/output mode of CH422G's IO0-7 must remain consistent, the driver will only set IO0-7 to |
| 35 | + input mode when it determines that all pins are configured as input. |
| 36 | + Using pinMode and multiPinMode will be invalid. You can only set the pin working mode through enableAllIO_Input, enableAllIO_Output, enableOC_PushPull and enableOC_OpenDrain |
| 37 | + * |
| 38 | + */ |
| 39 | + |
| 40 | +#include <Arduino.h> |
| 41 | +#include <ESP_IOExpander_Library.h> |
| 42 | + |
| 43 | +#define EXAMPLE_I2C_NUM (0) |
| 44 | +#define EXAMPLE_I2C_SDA_PIN (8) |
| 45 | +#define EXAMPLE_I2C_SCL_PIN (9) |
| 46 | +#define EXAMPLE_I2C_ADDR (ESP_IO_EXPANDER_I2C_CH422G_ADDRESS) |
| 47 | + |
| 48 | +ESP_IOExpander_CH422G *expander = NULL; |
| 49 | + |
| 50 | +void setup() { |
| 51 | + Serial.begin(115200); |
| 52 | + delay(1000); |
| 53 | + Serial.println("Test begin"); |
| 54 | + |
| 55 | + expander = new ESP_IOExpander_CH422G((i2c_port_t)EXAMPLE_I2C_NUM, EXAMPLE_I2C_ADDR, EXAMPLE_I2C_SCL_PIN, EXAMPLE_I2C_SDA_PIN); |
| 56 | + expander->init(); |
| 57 | + expander->begin(); |
| 58 | + |
| 59 | + /* For CH422G */ |
| 60 | + Serial.println("Set the OC pin to push-pull output mode."); |
| 61 | + expander->enableOC_PushPull(); |
| 62 | + |
| 63 | + // Serial.println("Set the OC pin to open_drain output mode."); |
| 64 | + // expander->enableOC_OpenDrain(); |
| 65 | + |
| 66 | + Serial.println("Set the IO0-7 pin to input mode."); |
| 67 | + expander->enableAllIO_Input(); |
| 68 | + |
| 69 | + // Serial.println("Set the IO0-7 pin to output mode."); |
| 70 | + // expander->enableAllIO_Output(); |
| 71 | +} |
| 72 | + |
| 73 | +int level[2] = { 0, 0 }; |
| 74 | + |
| 75 | +void loop() { |
| 76 | + for (int i = 0; i < 100; i++) { |
| 77 | + bool toggle = i % 2; |
| 78 | + |
| 79 | + Serial.print("Set pint 8 and 9 to:"); |
| 80 | + Serial.print(toggle); |
| 81 | + Serial.print(", "); |
| 82 | + Serial.println(!toggle); |
| 83 | + Serial.println(); |
| 84 | + |
| 85 | + // Set pin 8 and 9 level |
| 86 | + expander->digitalWrite(8, toggle); |
| 87 | + expander->digitalWrite(9, !toggle); |
| 88 | + delay(1); |
| 89 | + |
| 90 | + // Read pin 0 and 5 level |
| 91 | + level[0] = expander->digitalRead(0); |
| 92 | + level[1] = expander->digitalRead(5); |
| 93 | + |
| 94 | + Serial.print("Read pin 0 and 5 level: "); |
| 95 | + Serial.print(level[0]); |
| 96 | + Serial.print(", "); |
| 97 | + Serial.println(level[1]); |
| 98 | + Serial.println(); |
| 99 | + |
| 100 | + delay(1000); |
| 101 | + } |
| 102 | +} |
0 commit comments