add Analog Functionality

This commit is contained in:
Siwat Sirichai 2023-08-27 23:42:08 +07:00
parent 94443393d0
commit 1e4e8f7347
4 changed files with 57 additions and 12 deletions

View File

@ -9,6 +9,10 @@ Adafruit_PWMServoDriver pwmBank = Adafruit_PWMServoDriver(PWM_BANK_ADDRESS);
#ifdef ANALOG_CARD_ENABLE
Adafruit_ADS1115 analogInputBankA;
Adafruit_ADS1115 analogInputBankB;
MCP4725 DAC0(DAC0_ADDRESS);
MCP4725 DAC1(DAC1_ADDRESS);
MCP4725 DAC2(DAC2_ADDRESS);
MCP4725 DAC3(DAC3_ADDRESS);
#endif
void ESPMega_begin()
@ -32,7 +36,11 @@ void ESPMega_begin()
#ifdef ANALOG_CARD_ENABLE
analogInputBankA.begin(ANALOG_INPUT_BANK_A_ADDRESS);
analogInputBankA.begin(ANALOG_INPUT_BANK_B_ADDRESS);
analogInputBankB.begin(ANALOG_INPUT_BANK_B_ADDRESS);
DAC0.begin();
DAC1.begin();
DAC2.begin();
DAC3.begin();
#endif
}
@ -87,12 +95,31 @@ void IRAM_ATTR refreshInputBankB()
}
#ifdef ANALOG_CARD_ENABLE
uint16_t ESPMega_analogRead(int id)
int16_t ESPMega_analogRead(int id)
{
if (id >= 0 && id <= 3)
return analogInputBankA.readADC_SingleEnded(id);
return analogInputBankA.readADC_SingleEnded(3-id);
else if (id >= 4 && id <= 7)
return analogInputBankB.readADC_SingleEnded(id - 4);
return analogInputBankB.readADC_SingleEnded(7-id);
return 0;
}
void ESPMega_dacWrite(int id, int value) {
switch (id)
{
case 0:
DAC0.setValue(value);
break;
case 1:
DAC1.setValue(value);
break;
case 2:
DAC2.setValue(value);
break;
case 3:
DAC3.setValue(value);
break;
default:
break;
}
}
#endif

View File

@ -7,6 +7,7 @@
#include <PCF8574.h>
#ifdef ANALOG_CARD_ENABLE
#include <Adafruit_ADS1X15.h>
#include <MCP4725.h>
#endif
#define INPUT_BANK_A_ADDRESS 0x21
@ -14,8 +15,12 @@
#define PWM_BANK_ADDRESS 0x5F
#define OUTPUT_BANK_ADDRESS 0x21
#define EEPROM_ADDRESS 0x22
#define ANALOG_INPUT_BANK_A_ADDRESS 0x60
#define ANALOG_INPUT_BANK_B_ADDRESS 0x61
#define ANALOG_INPUT_BANK_A_ADDRESS 0x48
#define ANALOG_INPUT_BANK_B_ADDRESS 0x49
#define DAC0_ADDRESS 0x60
#define DAC1_ADDRESS 0x61
#define DAC2_ADDRESS 0x62
#define DAC3_ADDRESS 0x63
//#define USE_INTERRUPT
#define INPUT_BANK_A_INTERRUPT 36
@ -71,7 +76,15 @@ void IRAM_ATTR refreshInputBankB();
* @param id The number of the pin to be read
* @return The value of the pin (0-4095)
*/
uint16_t ESPMega_analogRead(int id);
int16_t ESPMega_analogRead(int id);
/**
* Write a True Analog Signal to one of the ESPMega Analog Card's
* Analog Output pins (AO0-AO3)
*
* @param id The number of the pin to write to
* @param value the analog value of the pin (0-4095)
*/
void ESPMega_dacWrite(int id, int value);
#endif
#endif

View File

@ -19,4 +19,5 @@ lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.1
robtillaart/PCF8574@^0.3.7
arduino-libraries/Arduino_BuiltIn@^1.0.0
SPI
robtillaart/MCP4725@^0.3.7
monitor_speed = 115200

View File

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