diff --git a/examples/Hat/ADC_ADS1100/ADC_ADS1100.ino b/examples/Hat/ADC_ADS1100/ADC_ADS1100.ino deleted file mode 100644 index b0fe088..0000000 --- a/examples/Hat/ADC_ADS1100/ADC_ADS1100.ino +++ /dev/null @@ -1,93 +0,0 @@ -/* - hardware: M5Stack unit ADC - connect: PORTA(21, 22) or PORTC(17, 16) -*/ -#include -#include -#include "ADS1100.h" - -ADS1100 ads; -#define REF_VOL 3.3 -#define ADC_BASE REF_VOL / 32768 - -void setup(void) { - M5.begin(true, true, false); - Serial.begin(115200); - - M5.Lcd.fillScreen(BLACK); - M5.Lcd.setTextColor(ORANGE); - - // The address can be changed making the option of connecting multiple - // devices - ads.getAddr_ADS1100( - ADS1100_DEFAULT_ADDRESS); // 0x48, 1001 000 (ADDR = GND) - - // The ADC gain (PGA), Device operating mode, Data rate - // can be changed via the following functions - - ads.setGain(GAIN_ONE); // 1x gain(default) - // ads.setGain(GAIN_TWO); // 2x gain - // ads.setGain(GAIN_FOUR); // 4x gain - // ads.setGain(GAIN_EIGHT); // 8x gain - - ads.setMode(MODE_CONTIN); // Continuous conversion mode (default) - // ads.setMode(MODE_SINGLE); // Single-conversion mode - - ads.setRate(RATE_8); // 8SPS (default) - // ads.setRate(RATE_16); // 16SPS - // ads.setRate(RATE_32); // 32SPS - // ads.setRate(RATE_128); // 128SPS - - ads.setOSMode(OSMODE_SINGLE); // Set to start a single-conversion - - ads.begin(); -} - -void loop(void) { - byte error; - int8_t address; - - address = ads.ads_i2cAddress; - // The i2c_scanner uses the return value of - // the Write.endTransmisstion to see if - // a device did acknowledge to the address. - Wire.beginTransmission(address); - error = Wire.endTransmission(); - if (error == 0) { - int16_t result, vol; - float temp; - - Serial.println("Getting Differential Reading from ADS1100"); - Serial.println(" "); - result = ads.Measure_Differential(); - Serial.print("Digital Value of Analog Input between Channel 0 and 1: "); - Serial.println(result); - M5.Lcd.fillScreen(BLACK); - char data[20] = {0}; - sprintf(data, "%d", result); - temp = result * ADC_BASE * 4; - vol = temp * 1000; - M5.Lcd.drawCentreString("Raw data ", 10, 10, 2); - M5.Lcd.drawCentreString(data, 50, 35, 4); - sprintf(data, "%d", vol); - M5.Lcd.setTextColor(GREEN); - M5.Lcd.drawCentreString("Convert to ", 10, 90, 2); - M5.Lcd.drawCentreString(data, 50, 115, 4); - - M5.Lcd.drawCentreString("mV", 110, 118, 2); - - Serial.println(" "); - Serial.println(" *************************** "); - Serial.println(" "); - } else { - Serial.println("ADS1100 Disconnected!"); - Serial.println(" "); - Serial.println(" ************ "); - Serial.println(" "); - M5.Lcd.setTextFont(4); - M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK); - M5.Lcd.drawString("Not Found.", 0, 20, 2); - } - - delay(1000); -} diff --git a/examples/Hat/ADC_ADS1100/ADS1100.cpp b/examples/Hat/ADC_ADS1100/ADS1100.cpp deleted file mode 100644 index 08aa816..0000000 --- a/examples/Hat/ADC_ADS1100/ADS1100.cpp +++ /dev/null @@ -1,198 +0,0 @@ -/**************************************************************************/ -/* - Distributed with a free-will license. - Use it any way you want, profit or free, provided it fits in the licenses of - its associated works. ADS1100 This code is designed to work with the - ADS1100_I2CADC I2C Mini Module available from ControlEverything.com. - https://www.controleverything.com/content/Analog-Digital-Converters?sku=ADS1100_I2CADC#tabs-0-product_tabset-2 -*/ -/**************************************************************************/ - -#if ARDUINO >= 100 -#include "Arduino.h" -#else -#include "WProgram.h" -#endif - -#include - -#include "ADS1100.h" - -/**************************************************************************/ -/* - Abstract away platform differences in Arduino wire library -*/ -/**************************************************************************/ -static uint8_t i2cread(void) { -#if ARDUINO >= 100 - return Wire.read(); -#else - return Wire.receive(); -#endif -} - -/**************************************************************************/ -/* - Abstract away platform differences in Arduino wire library -*/ -/**************************************************************************/ -static void i2cwrite(uint8_t x) { -#if ARDUINO >= 100 - Wire.write((uint8_t)x); -#else - Wire.send(x); -#endif -} - -/**************************************************************************/ -/* - Writes 8-bits to the destination register -*/ -/**************************************************************************/ -static void writeRegister(uint8_t i2cAddress, uint8_t value) { - Wire.beginTransmission(i2cAddress); - i2cwrite((uint8_t)value); - Wire.endTransmission(); -} - -/**************************************************************************/ -/* - Reads 16-bits from the destination register -*/ -/**************************************************************************/ -static uint16_t readRegister(uint8_t i2cAddress) { - Wire.beginTransmission(i2cAddress); - Wire.endTransmission(); - Wire.requestFrom(i2cAddress, (uint8_t)2); - return (int16_t)((i2cread() << 8) | i2cread()); -} - -/**************************************************************************/ -/* - Instantiates a new ADS1100 class with appropriate properties -*/ -/**************************************************************************/ -void ADS1100::getAddr_ADS1100(uint8_t i2cAddress) { - ads_i2cAddress = i2cAddress; - ads_conversionDelay = ADS1100_CONVERSIONDELAY; -} - -/**************************************************************************/ -/* - Sets up the Hardware -*/ -/**************************************************************************/ -void ADS1100::begin() { - Wire.begin(0, 26); -} - -/**************************************************************************/ -/* - Sets the Operational status/single-shot conversion start - This determines the operational status of the device -*/ -/**************************************************************************/ -void ADS1100::setOSMode(adsOSMode_t osmode) { - ads_osmode = osmode; -} - -/**************************************************************************/ -/* - Gets the Operational status/single-shot conversion start -*/ -/**************************************************************************/ -adsOSMode_t ADS1100::getOSMode() { - return ads_osmode; -} - -/**************************************************************************/ -/* - Sets the Device operating mode - This controls the current operational mode of the ADS1100 -*/ -/**************************************************************************/ -void ADS1100::setMode(adsMode_t mode) { - ads_mode = mode; -} - -/**************************************************************************/ -/* - Gets the Device operating mode -*/ -/**************************************************************************/ -adsMode_t ADS1100::getMode() { - return ads_mode; -} - -/**************************************************************************/ -/* - Sets the Date Rate - This controls the data rate setting -*/ -/**************************************************************************/ -void ADS1100::setRate(adsRate_t rate) { - ads_rate = rate; -} - -/**************************************************************************/ -/* - Gets the Date Rate -*/ -/**************************************************************************/ -adsRate_t ADS1100::getRate() { - return ads_rate; -} - -/**************************************************************************/ -/* - Sets the gain and input voltage range - This configures the programmable gain amplifier -*/ -/**************************************************************************/ -void ADS1100::setGain(adsGain_t gain) { - ads_gain = gain; -} - -/**************************************************************************/ -/* - Gets a gain and input voltage range -*/ -/**************************************************************************/ -adsGain_t ADS1100::getGain() { - return ads_gain; -} - -/**************************************************************************/ -/* - Reads the conversion results, measuring the voltage - difference between the P and N input - Generates a signed value since the difference can be either - positive or negative -*/ -/**************************************************************************/ -int16_t ADS1100::Measure_Differential() { - // Start with default values - uint16_t config = 0; - - // Set Operational status/single-shot conversion start - config |= ads_osmode; - - // Set Device operating mode - config |= ads_mode; - - // Set Data rate - config |= ads_rate; - - // Set PGA/voltage range - config |= ads_gain; - - // Write config register to the ADC - writeRegister(ads_i2cAddress, config); - - // Wait for the conversion to complete - delay(ads_conversionDelay); - - // Read the conversion results - uint16_t raw_adc = readRegister(ads_i2cAddress); - return (int16_t)raw_adc; -} diff --git a/examples/Hat/ADC_ADS1100/ADS1100.h b/examples/Hat/ADC_ADS1100/ADS1100.h deleted file mode 100644 index 1d4620f..0000000 --- a/examples/Hat/ADC_ADS1100/ADS1100.h +++ /dev/null @@ -1,111 +0,0 @@ -/**************************************************************************/ -/* - Distributed with a free-will license. - Use it any way you want, profit or free, provided it fits in the - licenses of its associated works. ADS1100 This code is designed to work with - the ADS1100_I2CADC I2C Mini Module available from ControlEverything.com. - https://www.controleverything.com/content/Analog-Digital-Converters?sku=ADS1100_I2CADC#tabs-0-product_tabset-2 -*/ -/**************************************************************************/ - -#if ARDUINO >= 100 -#include "Arduino.h" -#else -#include "WProgram.h" -#endif - -#include - -/************************************************************************** - I2C ADDRESS/BITS -**************************************************************************/ -#define ADS1100_DEFAULT_ADDRESS (0x48) // 1001 000 (ADDR = GND) - -/************************************************************************** - CONVERSION DELAY (in mS) -**************************************************************************/ -#define ADS1100_CONVERSIONDELAY (100) - -/************************************************************************** - CONFIG REGISTER -**************************************************************************/ -#define ADS1100_REG_CONFIG_OS_MASK (0x80) // Conversion -#define ADS1100_REG_CONFIG_OS_NOEFFECT (0x00) // Write: Bit = 0 No effect -#define ADS1100_REG_CONFIG_OS_SINGLE \ - (0x80) // Write: Bit = 1 Begin a conversion (default) -#define ADS1100_REG_CONFIG_OS_BUSY \ - (0x00) // Read: Bit = 0 Device is not performing a conversion -#define ADS1100_REG_CONFIG_OS_NOTBUSY \ - (0x80) // Read: Bit = 1 Device is busy performing a conversion - -#define ADS1100_REG_CONFIG_MODE_MASK (0x10) // Device operating mode -#define ADS1100_REG_CONFIG_MODE_CONTIN \ - (0x00) // Continuous conversion mode (default) -#define ADS1100_REG_CONFIG_MODE_SINGLE (0x10) // Single-conversion mode - -#define ADS1100_REG_CONFIG_DR_MASK (0x0C) // Data rate -#define ADS1100_REG_CONFIG_DR_128SPS (0x00) // 128 samples per second -#define ADS1100_REG_CONFIG_DR_32SPS (0x04) // 32 samples per second -#define ADS1100_REG_CONFIG_DR_16SPS (0x08) // 16 samples per second -#define ADS1100_REG_CONFIG_DR_8SPS (0x0C) // 8 samples per second (default) - -#define ADS1100_REG_CONFIG_PGA_MASK \ - (0x03) // Programmable gain amplifier configuration -#define ADS1100_REG_CONFIG_PGA_1 (0x00) // Gain 1 (default) -#define ADS1100_REG_CONFIG_PGA_2 (0x01) // Gain 2 -#define ADS1100_REG_CONFIG_PGA_4 (0x02) // Gain 4 -#define ADS1100_REG_CONFIG_PGA_8 (0x03) // Gain 8 - -/**************************************************************************/ - -typedef enum { - OSMODE_SINGLE = ADS1100_REG_CONFIG_OS_SINGLE, - OSMODE_BUSY = ADS1100_REG_CONFIG_OS_BUSY, - OSMODE_NOTBUSY = ADS1100_REG_CONFIG_OS_NOTBUSY - -} adsOSMode_t; - -typedef enum { - MODE_CONTIN = ADS1100_REG_CONFIG_MODE_CONTIN, - MODE_SINGLE = ADS1100_REG_CONFIG_MODE_SINGLE -} adsMode_t; - -typedef enum { - RATE_1288 = ADS1100_REG_CONFIG_DR_128SPS, - RATE_32 = ADS1100_REG_CONFIG_DR_32SPS, - RATE_16 = ADS1100_REG_CONFIG_DR_16SPS, - RATE_8 = ADS1100_REG_CONFIG_DR_8SPS -} adsRate_t; - -typedef enum { - GAIN_ONE = ADS1100_REG_CONFIG_PGA_1, - GAIN_TWO = ADS1100_REG_CONFIG_PGA_2, - GAIN_FOUR = ADS1100_REG_CONFIG_PGA_4, - GAIN_EIGHT = ADS1100_REG_CONFIG_PGA_8 -} adsGain_t; - -class ADS1100 { - protected: - // Instance-specific properties - uint8_t ads_conversionDelay; - adsOSMode_t ads_osmode; - adsMode_t ads_mode; - adsRate_t ads_rate; - adsGain_t ads_gain; - - public: - uint8_t ads_i2cAddress; - void getAddr_ADS1100(uint8_t i2cAddress); - void begin(void); - int16_t Measure_Differential(); - void setOSMode(adsOSMode_t osmode); - adsOSMode_t getOSMode(void); - void setMode(adsMode_t mode); - adsMode_t getMode(void); - void setRate(adsRate_t rate); - adsRate_t getRate(void); - void setGain(adsGain_t gain); - adsGain_t getGain(void); - - private: -}; \ No newline at end of file diff --git a/examples/Unit/ADC_ADS1100/ADC_ADS1100.ino b/examples/Unit/ADC_ADS1100/ADC_ADS1100.ino deleted file mode 100644 index 1b0f9e4..0000000 --- a/examples/Unit/ADC_ADS1100/ADC_ADS1100.ino +++ /dev/null @@ -1,77 +0,0 @@ -/* - hardware: M5Stack unit ADC - connect: PORTA(21, 22) or PORTC(17, 16) -*/ -#include -#include -#include "ADS1100.h" - -ADS1100 ads; - -void setup(void) { - M5.begin(true, false, false); - Serial.begin(115200); - - M5.Lcd.fillScreen(BLACK); - M5.Lcd.setTextColor(ORANGE); - - // The address can be changed making the option of connecting multiple - // devices - ads.getAddr_ADS1100( - ADS1100_DEFAULT_ADDRESS); // 0x48, 1001 000 (ADDR = GND) - - // The ADC gain (PGA), Device operating mode, Data rate - // can be changed via the following functions - - ads.setGain(GAIN_ONE); // 1x gain(default) - // ads.setGain(GAIN_TWO); // 2x gain - // ads.setGain(GAIN_FOUR); // 4x gain - // ads.setGain(GAIN_EIGHT); // 8x gain - - ads.setMode(MODE_CONTIN); // Continuous conversion mode (default) - // ads.setMode(MODE_SINGLE); // Single-conversion mode - - ads.setRate(RATE_8); // 8SPS (default) - // ads.setRate(RATE_16); // 16SPS - // ads.setRate(RATE_32); // 32SPS - // ads.setRate(RATE_128); // 128SPS - - ads.setOSMode(OSMODE_SINGLE); // Set to start a single-conversion - - ads.begin(); -} - -void loop(void) { - byte error; - int8_t address; - - address = ads.ads_i2cAddress; - // The i2c_scanner uses the return value of - // the Write.endTransmisstion to see if - // a device did acknowledge to the address. - Wire.beginTransmission(address); - error = Wire.endTransmission(); - if (error == 0) { - int16_t result; - - Serial.println("Getting Differential Reading from ADS1100"); - Serial.println(" "); - result = ads.Measure_Differential(); - Serial.print("Digital Value of Analog Input between Channel 0 and 1: "); - Serial.println(result); - M5.Lcd.fillScreen(BLACK); - char data[20] = {0}; - sprintf(data, "%d", result); - M5.Lcd.drawCentreString(data, 70, 120, 4); - Serial.println(" "); - Serial.println(" *************************** "); - Serial.println(" "); - } else { - Serial.println("ADS1100 Disconnected!"); - Serial.println(" "); - Serial.println(" ************ "); - Serial.println(" "); - } - - delay(1000); -} diff --git a/examples/Unit/ADC_ADS1100/ADS1100.cpp b/examples/Unit/ADC_ADS1100/ADS1100.cpp deleted file mode 100644 index 3bda96e..0000000 --- a/examples/Unit/ADC_ADS1100/ADS1100.cpp +++ /dev/null @@ -1,198 +0,0 @@ -/**************************************************************************/ -/* - Distributed with a free-will license. - Use it any way you want, profit or free, provided it fits in the licenses of - its associated works. ADS1100 This code is designed to work with the - ADS1100_I2CADC I2C Mini Module available from ControlEverything.com. - https://www.controleverything.com/content/Analog-Digital-Converters?sku=ADS1100_I2CADC#tabs-0-product_tabset-2 -*/ -/**************************************************************************/ - -#if ARDUINO >= 100 -#include "Arduino.h" -#else -#include "WProgram.h" -#endif - -#include - -#include "ADS1100.h" - -/**************************************************************************/ -/* - Abstract away platform differences in Arduino wire library -*/ -/**************************************************************************/ -static uint8_t i2cread(void) { -#if ARDUINO >= 100 - return Wire.read(); -#else - return Wire.receive(); -#endif -} - -/**************************************************************************/ -/* - Abstract away platform differences in Arduino wire library -*/ -/**************************************************************************/ -static void i2cwrite(uint8_t x) { -#if ARDUINO >= 100 - Wire.write((uint8_t)x); -#else - Wire.send(x); -#endif -} - -/**************************************************************************/ -/* - Writes 8-bits to the destination register -*/ -/**************************************************************************/ -static void writeRegister(uint8_t i2cAddress, uint8_t value) { - Wire.beginTransmission(i2cAddress); - i2cwrite((uint8_t)value); - Wire.endTransmission(); -} - -/**************************************************************************/ -/* - Reads 16-bits from the destination register -*/ -/**************************************************************************/ -static uint16_t readRegister(uint8_t i2cAddress) { - Wire.beginTransmission(i2cAddress); - Wire.endTransmission(); - Wire.requestFrom(i2cAddress, (uint8_t)2); - return (int16_t)((i2cread() << 8) | i2cread()); -} - -/**************************************************************************/ -/* - Instantiates a new ADS1100 class with appropriate properties -*/ -/**************************************************************************/ -void ADS1100::getAddr_ADS1100(uint8_t i2cAddress) { - ads_i2cAddress = i2cAddress; - ads_conversionDelay = ADS1100_CONVERSIONDELAY; -} - -/**************************************************************************/ -/* - Sets up the Hardware -*/ -/**************************************************************************/ -void ADS1100::begin() { - Wire.begin(); -} - -/**************************************************************************/ -/* - Sets the Operational status/single-shot conversion start - This determines the operational status of the device -*/ -/**************************************************************************/ -void ADS1100::setOSMode(adsOSMode_t osmode) { - ads_osmode = osmode; -} - -/**************************************************************************/ -/* - Gets the Operational status/single-shot conversion start -*/ -/**************************************************************************/ -adsOSMode_t ADS1100::getOSMode() { - return ads_osmode; -} - -/**************************************************************************/ -/* - Sets the Device operating mode - This controls the current operational mode of the ADS1100 -*/ -/**************************************************************************/ -void ADS1100::setMode(adsMode_t mode) { - ads_mode = mode; -} - -/**************************************************************************/ -/* - Gets the Device operating mode -*/ -/**************************************************************************/ -adsMode_t ADS1100::getMode() { - return ads_mode; -} - -/**************************************************************************/ -/* - Sets the Date Rate - This controls the data rate setting -*/ -/**************************************************************************/ -void ADS1100::setRate(adsRate_t rate) { - ads_rate = rate; -} - -/**************************************************************************/ -/* - Gets the Date Rate -*/ -/**************************************************************************/ -adsRate_t ADS1100::getRate() { - return ads_rate; -} - -/**************************************************************************/ -/* - Sets the gain and input voltage range - This configures the programmable gain amplifier -*/ -/**************************************************************************/ -void ADS1100::setGain(adsGain_t gain) { - ads_gain = gain; -} - -/**************************************************************************/ -/* - Gets a gain and input voltage range -*/ -/**************************************************************************/ -adsGain_t ADS1100::getGain() { - return ads_gain; -} - -/**************************************************************************/ -/* - Reads the conversion results, measuring the voltage - difference between the P and N input - Generates a signed value since the difference can be either - positive or negative -*/ -/**************************************************************************/ -int16_t ADS1100::Measure_Differential() { - // Start with default values - uint16_t config = 0; - - // Set Operational status/single-shot conversion start - config |= ads_osmode; - - // Set Device operating mode - config |= ads_mode; - - // Set Data rate - config |= ads_rate; - - // Set PGA/voltage range - config |= ads_gain; - - // Write config register to the ADC - writeRegister(ads_i2cAddress, config); - - // Wait for the conversion to complete - delay(ads_conversionDelay); - - // Read the conversion results - uint16_t raw_adc = readRegister(ads_i2cAddress); - return (int16_t)raw_adc; -} diff --git a/examples/Unit/ADC_ADS1100/ADS1100.h b/examples/Unit/ADC_ADS1100/ADS1100.h deleted file mode 100644 index 1d4620f..0000000 --- a/examples/Unit/ADC_ADS1100/ADS1100.h +++ /dev/null @@ -1,111 +0,0 @@ -/**************************************************************************/ -/* - Distributed with a free-will license. - Use it any way you want, profit or free, provided it fits in the - licenses of its associated works. ADS1100 This code is designed to work with - the ADS1100_I2CADC I2C Mini Module available from ControlEverything.com. - https://www.controleverything.com/content/Analog-Digital-Converters?sku=ADS1100_I2CADC#tabs-0-product_tabset-2 -*/ -/**************************************************************************/ - -#if ARDUINO >= 100 -#include "Arduino.h" -#else -#include "WProgram.h" -#endif - -#include - -/************************************************************************** - I2C ADDRESS/BITS -**************************************************************************/ -#define ADS1100_DEFAULT_ADDRESS (0x48) // 1001 000 (ADDR = GND) - -/************************************************************************** - CONVERSION DELAY (in mS) -**************************************************************************/ -#define ADS1100_CONVERSIONDELAY (100) - -/************************************************************************** - CONFIG REGISTER -**************************************************************************/ -#define ADS1100_REG_CONFIG_OS_MASK (0x80) // Conversion -#define ADS1100_REG_CONFIG_OS_NOEFFECT (0x00) // Write: Bit = 0 No effect -#define ADS1100_REG_CONFIG_OS_SINGLE \ - (0x80) // Write: Bit = 1 Begin a conversion (default) -#define ADS1100_REG_CONFIG_OS_BUSY \ - (0x00) // Read: Bit = 0 Device is not performing a conversion -#define ADS1100_REG_CONFIG_OS_NOTBUSY \ - (0x80) // Read: Bit = 1 Device is busy performing a conversion - -#define ADS1100_REG_CONFIG_MODE_MASK (0x10) // Device operating mode -#define ADS1100_REG_CONFIG_MODE_CONTIN \ - (0x00) // Continuous conversion mode (default) -#define ADS1100_REG_CONFIG_MODE_SINGLE (0x10) // Single-conversion mode - -#define ADS1100_REG_CONFIG_DR_MASK (0x0C) // Data rate -#define ADS1100_REG_CONFIG_DR_128SPS (0x00) // 128 samples per second -#define ADS1100_REG_CONFIG_DR_32SPS (0x04) // 32 samples per second -#define ADS1100_REG_CONFIG_DR_16SPS (0x08) // 16 samples per second -#define ADS1100_REG_CONFIG_DR_8SPS (0x0C) // 8 samples per second (default) - -#define ADS1100_REG_CONFIG_PGA_MASK \ - (0x03) // Programmable gain amplifier configuration -#define ADS1100_REG_CONFIG_PGA_1 (0x00) // Gain 1 (default) -#define ADS1100_REG_CONFIG_PGA_2 (0x01) // Gain 2 -#define ADS1100_REG_CONFIG_PGA_4 (0x02) // Gain 4 -#define ADS1100_REG_CONFIG_PGA_8 (0x03) // Gain 8 - -/**************************************************************************/ - -typedef enum { - OSMODE_SINGLE = ADS1100_REG_CONFIG_OS_SINGLE, - OSMODE_BUSY = ADS1100_REG_CONFIG_OS_BUSY, - OSMODE_NOTBUSY = ADS1100_REG_CONFIG_OS_NOTBUSY - -} adsOSMode_t; - -typedef enum { - MODE_CONTIN = ADS1100_REG_CONFIG_MODE_CONTIN, - MODE_SINGLE = ADS1100_REG_CONFIG_MODE_SINGLE -} adsMode_t; - -typedef enum { - RATE_1288 = ADS1100_REG_CONFIG_DR_128SPS, - RATE_32 = ADS1100_REG_CONFIG_DR_32SPS, - RATE_16 = ADS1100_REG_CONFIG_DR_16SPS, - RATE_8 = ADS1100_REG_CONFIG_DR_8SPS -} adsRate_t; - -typedef enum { - GAIN_ONE = ADS1100_REG_CONFIG_PGA_1, - GAIN_TWO = ADS1100_REG_CONFIG_PGA_2, - GAIN_FOUR = ADS1100_REG_CONFIG_PGA_4, - GAIN_EIGHT = ADS1100_REG_CONFIG_PGA_8 -} adsGain_t; - -class ADS1100 { - protected: - // Instance-specific properties - uint8_t ads_conversionDelay; - adsOSMode_t ads_osmode; - adsMode_t ads_mode; - adsRate_t ads_rate; - adsGain_t ads_gain; - - public: - uint8_t ads_i2cAddress; - void getAddr_ADS1100(uint8_t i2cAddress); - void begin(void); - int16_t Measure_Differential(); - void setOSMode(adsOSMode_t osmode); - adsOSMode_t getOSMode(void); - void setMode(adsMode_t mode); - adsMode_t getMode(void); - void setRate(adsRate_t rate); - adsRate_t getRate(void); - void setGain(adsGain_t gain); - adsGain_t getGain(void); - - private: -}; \ No newline at end of file diff --git a/examples/Unit/AMeter_ADS1115/AMeter_ADS1115.ino b/examples/Unit/AMeter_ADS1115/AMeter_ADS1115.ino deleted file mode 100644 index ebe2fac..0000000 --- a/examples/Unit/AMeter_ADS1115/AMeter_ADS1115.ino +++ /dev/null @@ -1,143 +0,0 @@ -/* - Description: Measure current and display - EEPROM (0x51) has built-in calibration parameters when leaving the factory. - Please do not write to the EEPROM, otherwise the calibration data will be - overwritten and the measurement results will be inaccurate. -*/ - -#include -#include "ammeter.h" -#include - -Ammeter ammeter; - -float page512_volt = 2000.0F; - -int16_t volt_raw_list[10]; -uint8_t raw_now_ptr = 0; -int16_t adc_raw = 0; - -int16_t hope = 0.0; - -ammeterGain_t now_gain = PAG_512; - -void setup(void) { - M5.begin(); - Wire.begin(); - - ammeter.setMode(SINGLESHOT); - ammeter.setRate(RATE_8); - ammeter.setGain(PAG_512); - hope = page512_volt / ammeter.resolution; - // | PAG | Max Input Voltage(V) | - // | PAG_6144 | 128 | - // | PAG_4096 | 64 | - // | PAG_2048 | 32 | - // | PAG_512 | 16 | - // | PAG_256 | 8 | - - M5.Lcd.fillScreen(BLACK); - M5.Lcd.setTextFont(2); - - M5.Lcd.setCursor(51, 225); - M5.Lcd.printf("2A"); - - // M5.Lcd.setCursor(118, 90); - // M5.Lcd.printf("SAVE"); - - // bool result1 = ammeter.saveCalibration2EEPROM(PAG_256, 1024, 1024); - // delay(10); -} - -void loop(void) { - M5.update(); - if (M5.BtnA.wasPressed()) { - ammeter.setMode(SINGLESHOT); - ammeter.setRate(RATE_8); - ammeter.setGain(PAG_512); - now_gain = PAG_512; - hope = page512_volt / ammeter.resolution; - - for (uint8_t i = 0; i < 10; i++) { - volt_raw_list[i] = 0; - } - } - - // if (M5.BtnB.wasPressed()) { - // bool success = ammeter.saveCalibration2EEPROM(now_gain, hope, - // adc_raw); M5.Lcd.setCursor(118, 90); - // - // if (success) { - // M5.Lcd.setTextColor(GREEN, BLACK); - // } else { - // M5.Lcd.setTextColor(RED, BLACK); - // } - // - // M5.Lcd.printf("SAVE"); - // - // delay(300); - // M5.Lcd.setCursor(118, 90); - // M5.Lcd.setTextColor(WHITE, BLACK); - // M5.Lcd.printf("SAVE"); - // - // ammeter.setGain(now_gain); - // } - - float current = ammeter.getCurrent(); - - volt_raw_list[raw_now_ptr] = ammeter.adc_raw; - raw_now_ptr = (raw_now_ptr == 9) ? 0 : (raw_now_ptr + 1); - - int count = 0; - int total = 0; - - for (uint8_t i = 0; i < 10; i++) { - if (volt_raw_list[i] == 0) { - continue; - } - total += volt_raw_list[i]; - count += 1; - } - - if (count == 0) { - adc_raw = 0; - } else { - adc_raw = total / count; - } - - M5.Lcd.setTextColor(WHITE, BLACK); - M5.Lcd.setCursor(3, 0); - M5.Lcd.printf("Hope volt:"); - M5.Lcd.setCursor(9, 15); - M5.Lcd.printf("%.2f mAn", page512_volt); - - M5.Lcd.setCursor(3, 37); - M5.Lcd.printf("Hope ADC:"); - M5.Lcd.setCursor(9, 52); - M5.Lcd.printf("%d", hope); - - M5.Lcd.setTextColor(WHITE, BLACK); - M5.Lcd.setCursor(3, 75); - M5.Lcd.printf("Cal volt:"); - M5.Lcd.fillRect(65, 90, 40, 15, BLACK); - M5.Lcd.setCursor(9, 90); - M5.Lcd.printf("%.2f mA", current); - - M5.Lcd.setTextColor(WHITE, BLACK); - M5.Lcd.setCursor(3, 112); - M5.Lcd.printf("Cal ADC:"); - M5.Lcd.fillRect(15, 127, 40, 15, BLACK); - M5.Lcd.setCursor(9, 127); - M5.Lcd.printf("%.0f", adc_raw * ammeter.calibration_factor); - - M5.Lcd.setCursor(3, 150); - if (abs(adc_raw) <= hope * 1.005 && abs(adc_raw) >= hope * 0.995) { - M5.Lcd.setTextColor(GREEN, BLACK); - } else { - M5.Lcd.setTextColor(RED, BLACK); - } - M5.Lcd.printf("RAW ADC:"); - M5.Lcd.fillRect(15, 165, 40, 15, BLACK); - M5.Lcd.setCursor(9, 165); - M5.Lcd.printf("%d", adc_raw); -} diff --git a/examples/Unit/AMeter_ADS1115/ammeter.cpp b/examples/Unit/AMeter_ADS1115/ammeter.cpp deleted file mode 100644 index 34447a1..0000000 --- a/examples/Unit/AMeter_ADS1115/ammeter.cpp +++ /dev/null @@ -1,293 +0,0 @@ -#include "ammeter.h" -#include "Wire.h" - -void Ammeter::i2cBegin() { - // Wire.begin(); -} - -bool Ammeter::i2cReadBytes(uint8_t addr, uint8_t reg_addr, uint8_t* buff, - uint16_t len) { - Wire.beginTransmission(addr); - Wire.write(reg_addr); - uint8_t i = 0; - if (Wire.endTransmission(false) == 0 && - Wire.requestFrom(addr, (uint8_t)len)) { - while (Wire.available()) { - buff[i++] = Wire.read(); - } - return true; - } - - return false; -} - -bool Ammeter::i2cWriteBytes(uint8_t addr, uint8_t reg_addr, uint8_t* buff, - uint16_t len) { - bool function_result = false; - - Wire.beginTransmission(addr); - Wire.write(reg_addr); - for (int i = 0; i < len; i++) { - Wire.write(*(buff + i)); - } - function_result = (Wire.endTransmission() == 0); - return function_result; -} - -bool Ammeter::i2cReadU16(uint8_t addr, uint8_t reg_addr, uint16_t* value) { - uint8_t read_buf[2] = {0x00, 0x00}; - bool result = i2cReadBytes(addr, reg_addr, read_buf, 2); - *value = (read_buf[0] << 8) | read_buf[1]; - return result; -} - -bool Ammeter::i2cWriteU16(uint8_t addr, uint8_t reg_addr, uint16_t value) { - uint8_t write_buf[2]; - write_buf[0] = value >> 8; - write_buf[1] = value & 0xff; - return i2cWriteBytes(addr, reg_addr, write_buf, 2); -} - -float Ammeter::getResolution(ammeterGain_t gain) { - switch (gain) { - case PAG_6144: - return ADS1115_MV_6144 / AMMETER_PRESSURE_COEFFICIENT; - case PAG_4096: - return ADS1115_MV_4096 / AMMETER_PRESSURE_COEFFICIENT; - case PAG_2048: - return ADS1115_MV_2048 / AMMETER_PRESSURE_COEFFICIENT; - case PAG_1024: - return ADS1115_MV_1024 / AMMETER_PRESSURE_COEFFICIENT; - case PAG_512: - return ADS1115_MV_512 / AMMETER_PRESSURE_COEFFICIENT; - case PAG_256: - return ADS1115_MV_256 / AMMETER_PRESSURE_COEFFICIENT; - default: - return ADS1115_MV_256 / AMMETER_PRESSURE_COEFFICIENT; - }; -} - -uint8_t Ammeter::getPGAEEEPROMAddr(ammeterGain_t gain) { - switch (gain) { - case PAG_6144: - return AMMETER_PAG_6144_CAL_ADDR; - case PAG_4096: - return AMMETER_PAG_4096_CAL_ADDR; - case PAG_2048: - return AMMETER_PAG_2048_CAL_ADDR; - case PAG_1024: - return AMMETER_PAG_1024_CAL_ADDR; - case PAG_512: - return AMMETER_PAG_512_CAL_ADDR; - case PAG_256: - return AMMETER_PAG_256_CAL_ADDR; - default: - return 0x00; - }; -} - -uint16_t Ammeter::getCoverTime(ammeterRate_t rate) { - switch (rate) { - case RATE_8: - return 1000 / 8; - case RATE_16: - return 1000 / 16; - case RATE_32: - return 1000 / 32; - case RATE_64: - return 1000 / 64; - case RATE_128: - return 1000 / 128; - case RATE_250: - return 1000 / 250; - case RATE_475: - return 1000 / 475; - case RATE_860: - return 1000 / 860; - default: - return 1000 / 128; - }; -} - -Ammeter::Ammeter(uint8_t ads1115_addr, uint8_t eeprom_addr) { - _ads1115_addr = ads1115_addr; - _eeprom_addr = eeprom_addr; - _gain = PAG_2048; - _mode = SINGLESHOT; - _rate = RATE_128; - calibration_factor = 1; - adc_raw = 0; - resolution = getResolution(_gain); - cover_time = getCoverTime(_rate); -} - -void Ammeter::setGain(ammeterGain_t gain) { - uint16_t reg_value = 0; - bool result = i2cReadU16(_ads1115_addr, ADS1115_RA_CONFIG, ®_value); - - if (result == false) { - return; - } - - reg_value &= ~(0b0111 << 9); - reg_value |= gain << 9; - - result = i2cWriteU16(_ads1115_addr, ADS1115_RA_CONFIG, reg_value); - - if (result) { - _gain = gain; - resolution = getResolution(gain); - int16_t hope = 1; - int16_t actual = 1; - if (readCalibrationFromEEPROM(gain, &hope, &actual)) { - calibration_factor = fabs((double)hope / actual); - } - } -} - -void Ammeter::setRate(ammeterRate_t rate) { - uint16_t reg_value = 0; - bool result = i2cReadU16(_ads1115_addr, ADS1115_RA_CONFIG, ®_value); - if (result == false) { - return; - } - - reg_value &= ~(0b0111 << 5); - reg_value |= rate << 5; - - result = i2cWriteU16(_ads1115_addr, ADS1115_RA_CONFIG, reg_value); - - if (result) { - _rate = rate; - cover_time = getCoverTime(_rate); - } - - return; -} - -void Ammeter::setMode(ammeterMode_t mode) { - uint16_t reg_value = 0; - bool result = i2cReadU16(_ads1115_addr, ADS1115_RA_CONFIG, ®_value); - if (result == false) { - return; - } - - reg_value &= ~(0b0001 << 8); - reg_value |= mode << 8; - - result = i2cWriteU16(_ads1115_addr, ADS1115_RA_CONFIG, reg_value); - if (result) { - _mode = mode; - } - - return; -} - -bool Ammeter::isInConversion() { - uint16_t value = 0x00; - i2cReadU16(_ads1115_addr, ADS1115_RA_CONFIG, &value); - - return (value & (1 << 15)) ? false : true; -} - -void Ammeter::startSingleConversion() { - uint16_t reg_value = 0; - bool result = i2cReadU16(_ads1115_addr, ADS1115_RA_CONFIG, ®_value); - - if (result == false) { - return; - } - - reg_value &= ~(0b0001 << 15); - reg_value |= 0x01 << 15; - - i2cWriteU16(_ads1115_addr, ADS1115_RA_CONFIG, reg_value); -} - -float Ammeter::getCurrent(bool calibration) { - if (calibration) { - return resolution * calibration_factor * getConversion() * - AMMETER_MEASURING_DIR; - } else { - return resolution * getConversion() * AMMETER_MEASURING_DIR; - } -} - -int16_t Ammeter::getAdcRaw() { - uint16_t value = 0x00; - i2cReadU16(_ads1115_addr, ADS1115_RA_CONVERSION, &value); - adc_raw = value; - return value; -} - -int16_t Ammeter::getConversion(uint16_t timeout) { - if (_mode == SINGLESHOT) { - startSingleConversion(); - delay(cover_time); - uint64_t time = millis() + timeout; - while (time > millis() && isInConversion()) - ; - } - - return getAdcRaw(); -} - -bool Ammeter::EEPORMWrite(uint8_t address, uint8_t* buff, uint8_t len) { - return i2cWriteBytes(_eeprom_addr, address, buff, len); -} - -bool Ammeter::EEPORMRead(uint8_t address, uint8_t* buff, uint8_t len) { - return i2cReadBytes(_eeprom_addr, address, buff, len); -} - -bool Ammeter::saveCalibration2EEPROM(ammeterGain_t gain, int16_t hope, - int16_t actual) { - if (hope == 0 || actual == 0) { - return false; - } - - uint8_t buff[8]; - memset(buff, 0, 8); - buff[0] = gain; - buff[1] = hope >> 8; - buff[2] = hope & 0xFF; - - buff[3] = actual >> 8; - buff[4] = actual & 0xFF; - - for (uint8_t i = 0; i < 5; i++) { - buff[5] ^= buff[i]; - } - - uint8_t addr = getPGAEEEPROMAddr(gain); - return EEPORMWrite(addr, buff, 8); -} - -bool Ammeter::readCalibrationFromEEPROM(ammeterGain_t gain, int16_t* hope, - int16_t* actual) { - uint8_t addr = getPGAEEEPROMAddr(gain); - uint8_t buff[8]; - memset(buff, 0, 8); - - *hope = 1; - *actual = 1; - - bool result = EEPORMRead(addr, buff, 8); - - if (result == false) { - return false; - } - - uint8_t xor_result = 0x00; - for (uint8_t i = 0; i < 5; i++) { - xor_result ^= buff[i]; - } - - if (xor_result != buff[5]) { - return false; - } - - *hope = (buff[1] << 8) | buff[2]; - *actual = (buff[3] << 8) | buff[4]; - return true; -} diff --git a/examples/Unit/AMeter_ADS1115/ammeter.h b/examples/Unit/AMeter_ADS1115/ammeter.h deleted file mode 100644 index 5ba763b..0000000 --- a/examples/Unit/AMeter_ADS1115/ammeter.h +++ /dev/null @@ -1,130 +0,0 @@ -#pragma once - -#include "Arduino.h" - -#define ADS115_ADDR 0x48 -#define EEPROM_ADDR 0x51 - -#define ADS1115_RA_CONVERSION 0x00 -#define ADS1115_RA_CONFIG 0x01 - -#define ADS1115_PGA_6144 0x00 -#define ADS1115_PGA_4096 0x01 -#define ADS1115_PGA_2048 0x02 // default -#define ADS1115_PGA_1024 0x03 -#define ADS1115_PGA_512 0x04 -#define ADS1115_PGA_256 0x05 - -#define ADS1115_MV_6144 0.187500F -#define ADS1115_MV_4096 0.125000F -#define ADS1115_MV_2048 0.062500F // default -#define ADS1115_MV_1024 0.031250F -#define ADS1115_MV_512 0.015625F -#define ADS1115_MV_256 0.007813F - -#define ADS1115_RATE_8 0x00 -#define ADS1115_RATE_16 0x01 -#define ADS1115_RATE_32 0x02 -#define ADS1115_RATE_64 0x03 -#define ADS1115_RATE_128 0x04 // default -#define ADS1115_RATE_250 0x05 -#define ADS1115_RATE_475 0x06 -#define ADS1115_RATE_860 0x07 - -#define ADS1115_MUX_P0N1 0x00 // ammeter only support - -#define ADS1115_COMP_MODE_HYSTERESIS 0x00 // default -#define ADS1115_COMP_MODE_WINDOW 0x01 - -#define ADS1115_MODE_CONTINUOUS 0x00 -#define ADS1115_MODE_SINGLESHOT 0x01 // default - -#define AMMETER_PRESSURE_COEFFICIENT 0.05 - -#define AMMETER_PAG_6144_CAL_ADDR 208 -#define AMMETER_PAG_4096_CAL_ADDR 216 -#define AMMETER_PAG_2048_CAL_ADDR 224 -#define AMMETER_PAG_1024_CAL_ADDR 232 -#define AMMETER_PAG_512_CAL_ADDR 240 -#define AMMETER_PAG_256_CAL_ADDR 248 - -#define AMMETER_MEASURING_DIR -1 - -typedef enum { - PAG_6144 = ADS1115_PGA_6144, - PAG_4096 = ADS1115_PGA_4096, - PAG_2048 = ADS1115_PGA_2048, // default - PAG_1024 = ADS1115_PGA_1024, - PAG_512 = ADS1115_PGA_512, - PAG_256 = ADS1115_PGA_256, -} ammeterGain_t; - -typedef enum { - RATE_8 = ADS1115_RATE_8, - RATE_16 = ADS1115_RATE_16, - RATE_32 = ADS1115_RATE_32, - RATE_64 = ADS1115_RATE_64, - RATE_128 = ADS1115_RATE_128, // default - RATE_250 = ADS1115_RATE_250, - RATE_475 = ADS1115_RATE_475, - RATE_860 = ADS1115_RATE_860, -} ammeterRate_t; - -typedef enum { - SINGLESHOT = ADS1115_MODE_SINGLESHOT, - CONTINUOUS = ADS1115_MODE_CONTINUOUS, -} ammeterMode_t; - -class Ammeter { - private: - void i2cBegin(); - bool i2cReadBytes(uint8_t addr, uint8_t reg_addr, uint8_t* buff, - uint16_t len); - bool i2cWriteBytes(uint8_t addr, uint8_t reg_addr, uint8_t* buff, - uint16_t len); - bool i2cReadU16(uint8_t addr, uint8_t reg_addr, uint16_t* value); - bool i2cWriteU16(uint8_t addr, uint8_t reg_addr, uint16_t value); - - float getResolution(ammeterGain_t gain); - uint16_t getCoverTime(ammeterRate_t rate); - uint8_t getPGAEEEPROMAddr(ammeterGain_t gain); - - uint8_t _ads1115_addr; - uint8_t _eeprom_addr; - - float pga_256_calibration_5v_value = 10187; - float pga_2048_calibration_25v_value = 6368; - - public: - ammeterGain_t _gain; - ammeterRate_t _rate; - ammeterMode_t _mode; - - float resolution; - uint16_t cover_time; - int16_t adc_raw; - float calibration_factor; - - public: - Ammeter(uint8_t ads1115_addr = ADS115_ADDR, - uint8_t eeprom_addr = EEPROM_ADDR); - - void setGain(ammeterGain_t gain); - void setRate(ammeterRate_t rate); - void setMode(ammeterMode_t mode); - - float getCurrent(bool calibration = true); - int16_t getConversion(uint16_t timeout = 125); - int16_t getAdcRaw(); - - bool isInConversion(); - void startSingleConversion(); - - bool EEPORMWrite(uint8_t address, uint8_t* buff, uint8_t len); - bool EEPORMRead(uint8_t address, uint8_t* buff, uint8_t len); - - bool saveCalibration2EEPROM(ammeterGain_t gain, int16_t hope, - int16_t actual); - bool readCalibrationFromEEPROM(ammeterGain_t gain, int16_t* hope, - int16_t* actual); -}; diff --git a/examples/Unit/KEY/KEY.ino b/examples/Unit/KEY/KEY.ino index 92a9988..59fbece 100644 --- a/examples/Unit/KEY/KEY.ino +++ b/examples/Unit/KEY/KEY.ino @@ -1,18 +1,14 @@ /* ******************************************************************************* * Copyright (c) 2022 by M5Stack -* Equipped with M5StickC Plus sample source code -* 配套 M5StickC Plus 示例源代码 -* Visit the website for more information: -* 获取更多资料请访问: +* Equipped with M5StickCPlus sample source code +* 配套 M5StickCPlus 示例源代码 * -* Please follow the steps below to add FastLED library: -* - Arduino menu --> Manage Libraries... --> FastLED --> install -* 在烧录前请按以下步骤添加 FastLED 库: -* - Arduino menu --> Manage Libraries... --> FastLED --> install +* Visit for more information: https://docs.m5stack.com/en/unit/key +* 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/key * -* Describe: UNIT-KEY example -* date: 2022/6/1 +* Product: UNIT-KEY example +* Date: 2022/6/1 ******************************************************************************* */