analog card

This commit is contained in:
Siwat Sirichai 2023-12-27 23:41:58 +07:00
parent 1f8ad10609
commit 51e9f5258a
2 changed files with 52 additions and 19 deletions

View File

@ -1,30 +1,44 @@
#include <ESPMegaPRO.h>
#include <AnalogCard.hpp>
void AnalogCard::dacWrite(uint8_t pin, uint16_t value) {
switch(pin) {
case 0:
this->dac0.writeDAC(value);
break;
case 1:
this->dac1.writeDAC(value);
break;
case 2:
this->dac2.writeDAC(value);
break;
case 3:
this->dac3.writeDAC(value);
break;
AnalogCard::AnalogCard() : dac0(DAC0_ADDRESS),
dac1(DAC1_ADDRESS),
dac2(DAC2_ADDRESS),
dac3(DAC3_ADDRESS)
{
}
void AnalogCard::dacWrite(uint8_t pin, uint16_t value)
{
switch (pin)
{
case 0:
this->dac0.writeDAC(value);
break;
case 1:
this->dac1.writeDAC(value);
break;
case 2:
this->dac2.writeDAC(value);
break;
case 3:
this->dac3.writeDAC(value);
break;
}
}
uint16_t AnalogCard::analogRead(uint8_t pin) {
if (pin >= 0 && pin <= 3) {
uint16_t AnalogCard::analogRead(uint8_t pin)
{
if (pin >= 0 && pin <= 3)
{
return this->analogInputBankA.readADC_SingleEnded(pin);
} else if (pin >= 4 && pin <= 7) {
}
else if (pin >= 4 && pin <= 7)
{
return this->analogInputBankB.readADC_SingleEnded(pin - 4);
}
}
void AnalogCard::begin() {
void AnalogCard::begin()
{
this->dac0 = MCP4725(DAC0_ADDRESS);
this->dac1 = MCP4725(DAC1_ADDRESS);
this->dac2 = MCP4725(DAC2_ADDRESS);

View File

@ -1,19 +1,38 @@
#include <ESPMegaPRO.h>
#include <DigitalInputCard.hpp>
#include <DigitalOutputCard.hpp>
#include <AnalogCard.hpp>
DigitalInputCard icard = DigitalInputCard(0x20, 0x21);
// This code demonstrates how to use the cards
DigitalInputCard icard = DigitalInputCard(0x21, 0x22);
DigitalOutputCard ocard = DigitalOutputCard(0x5F);
AnalogCard acard = AnalogCard();
void setup() {
Serial.begin(115200);
// Instantiate Input Card
icard.begin();
// Instantiate Output Card
ocard.begin();
// Instantiate Analog Card
acard.begin();
// Set output 0 to 4095
ocard.analogWrite(0, 4095);
}
void loop() {
// put your main code here, to run repeatedly:
for (int i = 0; i < 16; i++) {
Serial.print("Input ");
Serial.print(i);
Serial.print(": ");
Serial.print(icard.digitalRead(i));
Serial.println();
}
// Pad the output
Serial.println();
Serial.println();
// Delay for 1 second
delay(1000);
}