|
| 1 | +// SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | +// https://chat.openai.com/share/5dddee44-3196-4a6b-b445-58ac6ef18501 |
| 5 | + |
| 6 | +#include <SoftI2C.h> |
| 7 | + |
| 8 | +extern uint8_t scl_pin; |
| 9 | +extern uint8_t sda_pin; |
| 10 | + |
| 11 | +void Wire_begin(uint8_t scl, uint8_t sda); |
| 12 | +bool Wire_scan(uint8_t i2caddr); |
| 13 | +bool Wire_writeBytes(uint8_t i2caddr, uint8_t *data, uint8_t bytes); |
| 14 | +bool Wire_readBytes(uint8_t i2caddr, uint8_t *data, uint8_t bytes); |
| 15 | +bool Wire_readRegister(uint8_t i2caddr, uint8_t regaddr, uint8_t *data, uint8_t bytes); |
| 16 | + |
| 17 | +bool readAHT20(float *temperature, float *humidity); |
| 18 | +#define AHTX0_I2CADDR_DEFAULT 0x38 |
| 19 | + |
| 20 | +void setup() { |
| 21 | + while (!USBSerial()); // wait for serial port to connect. Needed for native USB port only |
| 22 | + delay(100); |
| 23 | + |
| 24 | + USBSerial_println("CH552 QT Py I2C sensor test"); |
| 25 | + Wire_begin(33, 34); // set up I2C on CH552 QT Py |
| 26 | + |
| 27 | + USBSerial_print("I2C Scan: "); |
| 28 | + for (uint8_t a=0; a<=0x7F; a++) { |
| 29 | + if (!Wire_scan(a)) continue; |
| 30 | + USBSerial_print("0x"); |
| 31 | + USBSerial_print(a, HEX); |
| 32 | + USBSerial_print(", "); |
| 33 | + } |
| 34 | + USBSerial_println(); |
| 35 | + |
| 36 | + if (! Wire_scan(AHTX0_I2CADDR_DEFAULT)) { |
| 37 | + USBSerial_println("No AHT20 found!"); |
| 38 | + while (1); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +void loop() { |
| 43 | + delay(100); |
| 44 | + |
| 45 | + float t, h; |
| 46 | + if (!readAHT20(&t, &h)) { |
| 47 | + USBSerial_println("Failed to read from AHT20"); |
| 48 | + } |
| 49 | + USBSerial_print("Temp: "); |
| 50 | + USBSerial_print(t); |
| 51 | + USBSerial_print(" *C, Hum: "); |
| 52 | + USBSerial_print(h); |
| 53 | + USBSerial_println(" RH%"); |
| 54 | +} |
| 55 | + |
| 56 | +/*********************** AHT20 'driver */ |
| 57 | + |
| 58 | +#define AHTX0_CMD_TRIGGER 0xAC |
| 59 | +#define AHTX0_STATUS_BUSY 0x80 |
| 60 | + |
| 61 | +bool AHT20_getStatus(uint8_t *status) { |
| 62 | + return Wire_readBytes(AHTX0_I2CADDR_DEFAULT, status, 1); |
| 63 | +} |
| 64 | + |
| 65 | +bool readAHT20(float *temperature, float *humidity) { |
| 66 | + uint8_t cmd[3] = {AHTX0_CMD_TRIGGER, 0x33, 0x00}; |
| 67 | + uint8_t data[6], status; |
| 68 | + uint32_t rawHumidity, rawTemperature; |
| 69 | + |
| 70 | + // Trigger AHT20 measurement |
| 71 | + if (!Wire_writeBytes(AHTX0_I2CADDR_DEFAULT, cmd, 3)) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + // Wait until the sensor is no longer busy |
| 76 | + do { |
| 77 | + if (!AHT20_getStatus(&status)) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + delay(10); // Delay 10ms to wait for measurement |
| 81 | + } while (status & AHTX0_STATUS_BUSY); |
| 82 | + |
| 83 | + // Read the measurement data |
| 84 | + if (!Wire_readBytes(AHTX0_I2CADDR_DEFAULT, data, 6)) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + // Parse humidity data |
| 89 | + rawHumidity = data[1]; |
| 90 | + rawHumidity = (rawHumidity << 8) | data[2]; |
| 91 | + rawHumidity = (rawHumidity << 4) | (data[3] >> 4); |
| 92 | + *humidity = ((float)rawHumidity * 100.0) / 0x100000; |
| 93 | + |
| 94 | + // Parse temperature data |
| 95 | + rawTemperature = (data[3] & 0x0F); |
| 96 | + rawTemperature = (rawTemperature << 8) | data[4]; |
| 97 | + rawTemperature = (rawTemperature << 8) | data[5]; |
| 98 | + *temperature = ((float)rawTemperature * 200.0 / 0x100000) - 50.0; |
| 99 | + |
| 100 | + return true; |
| 101 | +} |
| 102 | + |
| 103 | +/**************************** Wire I2C interface */ |
| 104 | + |
| 105 | +void Wire_begin(uint8_t scl, uint8_t sda) { |
| 106 | + scl_pin = scl; //extern variable in SoftI2C.h |
| 107 | + sda_pin = sda; |
| 108 | + I2CInit(); |
| 109 | +} |
| 110 | + |
| 111 | +bool Wire_scan(uint8_t i2caddr) { |
| 112 | + return Wire_writeBytes(i2caddr, NULL, 0); |
| 113 | +} |
| 114 | + |
| 115 | +bool Wire_readRegister(uint8_t i2caddr, uint8_t regaddr, uint8_t *data, uint8_t bytes) { |
| 116 | + if (!Wire_writeBytes(i2caddr, ®addr, 1)) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + return Wire_readBytes(i2caddr, data, bytes); |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | +bool Wire_writeBytes(uint8_t i2caddr, uint8_t *data, uint8_t bytes) { |
| 125 | + uint8_t ack_bit; |
| 126 | + |
| 127 | + I2CStart(); |
| 128 | + ack_bit = I2CSend(i2caddr << 1 | 0); // Shift address and append write bit |
| 129 | + if (ack_bit != 0) { |
| 130 | + I2CStop(); |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + for (uint8_t i = 0; i < bytes; i++) { |
| 135 | + if (I2CSend(data[i]) != 0) { |
| 136 | + I2CStop(); |
| 137 | + return false; |
| 138 | + } |
| 139 | + } |
| 140 | + I2CStop(); |
| 141 | + return true; |
| 142 | +} |
| 143 | + |
| 144 | +bool Wire_readBytes(uint8_t i2caddr, uint8_t *data, uint8_t bytes) { |
| 145 | + uint8_t ack_bit; |
| 146 | + |
| 147 | + I2CStart(); |
| 148 | + ack_bit = I2CSend(i2caddr << 1 | 1); // Shift address and append read bit |
| 149 | + if (ack_bit != 0) { |
| 150 | + I2CStop(); |
| 151 | + return false; |
| 152 | + } |
| 153 | + |
| 154 | + for (uint8_t i = 0; i < bytes; i++) { |
| 155 | + data[i] = I2CRead(); |
| 156 | + if (i == bytes - 1) { |
| 157 | + I2CNak(); // NAK on last byte |
| 158 | + } else { |
| 159 | + I2CAck(); // ACK on other bytes |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + I2CStop(); |
| 164 | + return true; |
| 165 | +} |
0 commit comments