analog input capability

This commit is contained in:
Siwat Sirichai 2023-08-26 23:16:51 +07:00
parent 81f0e80f8c
commit 94443393d0
5 changed files with 68 additions and 19 deletions

View File

@ -6,6 +6,10 @@ uint8_t inputBufferB;
PCF8574 inputBankA(INPUT_BANK_A_ADDRESS); PCF8574 inputBankA(INPUT_BANK_A_ADDRESS);
PCF8574 inputBankB(INPUT_BANK_B_ADDRESS); PCF8574 inputBankB(INPUT_BANK_B_ADDRESS);
Adafruit_PWMServoDriver pwmBank = Adafruit_PWMServoDriver(PWM_BANK_ADDRESS); Adafruit_PWMServoDriver pwmBank = Adafruit_PWMServoDriver(PWM_BANK_ADDRESS);
#ifdef ANALOG_CARD_ENABLE
Adafruit_ADS1115 analogInputBankA;
Adafruit_ADS1115 analogInputBankB;
#endif
void ESPMega_begin() void ESPMega_begin()
{ {
@ -25,10 +29,15 @@ void ESPMega_begin()
attachInterrupt(digitalPinToInterrupt(INPUT_BANK_A_INTERRUPT), refreshInputBankA, FALLING); attachInterrupt(digitalPinToInterrupt(INPUT_BANK_A_INTERRUPT), refreshInputBankA, FALLING);
attachInterrupt(digitalPinToInterrupt(INPUT_BANK_B_INTERRUPT), refreshInputBankB, FALLING); attachInterrupt(digitalPinToInterrupt(INPUT_BANK_B_INTERRUPT), refreshInputBankB, FALLING);
#endif #endif
#ifdef ANALOG_CARD_ENABLE
analogInputBankA.begin(ANALOG_INPUT_BANK_A_ADDRESS);
analogInputBankA.begin(ANALOG_INPUT_BANK_B_ADDRESS);
#endif
} }
void ESPMega_loop() { void ESPMega_loop()
{
} }
bool ESPMega_digitalRead(int id) bool ESPMega_digitalRead(int id)
@ -76,3 +85,14 @@ void IRAM_ATTR refreshInputBankB()
{ {
inputBufferB = inputBankB.read8(); inputBufferB = inputBankB.read8();
} }
#ifdef ANALOG_CARD_ENABLE
uint16_t ESPMega_analogRead(int id)
{
if (id >= 0 && id <= 3)
return analogInputBankA.readADC_SingleEnded(id);
else if (id >= 4 && id <= 7)
return analogInputBankB.readADC_SingleEnded(id - 4);
return 0;
}
#endif

View File

@ -1,16 +1,21 @@
#ifndef ESPMEGA #ifndef ESPMEGA
#define ESPMEGA #define ESPMEGA
#define ANALOG_CARD_ENABLE
#include <Arduino.h> #include <Arduino.h>
#include <Wire.h> #include <Wire.h>
#include <Adafruit_PWMServoDriver.h> #include <Adafruit_PWMServoDriver.h>
#include <PCF8574.h> #include <PCF8574.h>
#ifdef ANALOG_CARD_ENABLE
#include <Adafruit_ADS1X15.h>
#endif
#define INPUT_BANK_A_ADDRESS 0x21 #define INPUT_BANK_A_ADDRESS 0x21
#define INPUT_BANK_B_ADDRESS 0x22 #define INPUT_BANK_B_ADDRESS 0x22
#define PWM_BANK_ADDRESS 0x5F #define PWM_BANK_ADDRESS 0x5F
#define OUTPUT_BANK_ADDRESS 0x21 #define OUTPUT_BANK_ADDRESS 0x21
#define EEPROM_ADDRESS 0x22 #define EEPROM_ADDRESS 0x22
#define ANALOG_INPUT_BANK_A_ADDRESS 0x60
#define ANALOG_INPUT_BANK_B_ADDRESS 0x61
//#define USE_INTERRUPT //#define USE_INTERRUPT
#define INPUT_BANK_A_INTERRUPT 36 #define INPUT_BANK_A_INTERRUPT 36
@ -59,4 +64,14 @@ void ESPMega_digitalWrite(int id, bool value);
void IRAM_ATTR refreshInputBankA(); void IRAM_ATTR refreshInputBankA();
void IRAM_ATTR refreshInputBankB(); void IRAM_ATTR refreshInputBankB();
#ifdef ANALOG_CARD_ENABLE
/**
* Read one of the ESPMega Analog Card's Analog Input pins (A0-A7)
*
* @param id The number of the pin to be read
* @return The value of the pin (0-4095)
*/
uint16_t ESPMega_analogRead(int id);
#endif
#endif #endif

View File

@ -14,5 +14,9 @@ board = wt32-eth01
framework = arduino framework = arduino
;siwats/ESPMegaPROR3@1.0.1 ;siwats/ESPMegaPROR3@1.0.1
lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.1 lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.1
adafruit/Adafruit ADS1X15@^2.4.0
adafruit/Adafruit BusIO
robtillaart/PCF8574@^0.3.7 robtillaart/PCF8574@^0.3.7
arduino-libraries/Arduino_BuiltIn@^1.0.0
SPI
monitor_speed = 115200 monitor_speed = 115200

View File

@ -0,0 +1,10 @@
#include <ESPMegaPRO.h>
void setup() {
ESPMega_begin();
Serial.begin(115200);
}
void loop() {
int a0 = ESPMega_analogRead(0);
Serial.println(a0);
}