From 55b8af5d3b02105ab689057c5a92b82077e1db25 Mon Sep 17 00:00:00 2001 From: Daniel Melendrez Date: Wed, 20 May 2020 16:44:42 +0100 Subject: [PATCH 1/4] New SetDAC example This is an enhanced version for Setting the Voltage of the DAC through the Serial Port. The example code queries a channel and the desired voltage.Implemented using a state machine and failure detection. It requires the new version for the .h and .cpp files created to improve the compatibility of the library with Arduino and Teensy platforms Tested on Teensy 3.6 --- .../SetDAC_Voltage-Serial.ino | 232 ++++++++++++++++++ 1 file changed, 232 insertions(+) create mode 100644 examples/SetDAC_Voltage-Serial/SetDAC_Voltage-Serial.ino diff --git a/examples/SetDAC_Voltage-Serial/SetDAC_Voltage-Serial.ino b/examples/SetDAC_Voltage-Serial/SetDAC_Voltage-Serial.ino new file mode 100644 index 0000000..099da55 --- /dev/null +++ b/examples/SetDAC_Voltage-Serial/SetDAC_Voltage-Serial.ino @@ -0,0 +1,232 @@ + +/* + Analog Instruments AD5592R(-1) Arduino Library + + Author: Florian (Metaln00b: https://github.com/Metaln00b/Arduino-libad5592r) + + Contributor: dzalf (https://github.com/dzalf) + + libad5592r Library Example: Setting DAC voltage via the serial ports + + Enhanced version of the original example SetDAC which parsed a voltage value + from the serial port and wrote it to Port 0 of the DAC + + This update queries the channel and the voltage to be written in the selected port + + It uses the updated .cpp and .h files uploaded through dzalf's fork + + Date: 20- May-2020 (COVID-19 Lock-down aftermath) + + +*/ + + +#include + +#define VDD 5000 +#define CHATTY_CODE true + +const int syncPin = 10; + +volatile bool serialReceived = false; +bool channelSet; +bool voltageSet; + +typedef enum{ + + WAITING_INPUT, + REQUEST_CHANNEL, + REQUEST_VOLTAGE, + RESET_FLAGS + +} sysStatus; + +sysStatus serialStatus = REQUEST_CHANNEL; + +byte channel; + +String serialIn = ""; + +ad5592r_t ad5592r; + +void setup() { + + Serial.begin (115200); + + ad5592r_init(&ad5592r, syncPin); + ad5592r_reset(&ad5592r); + ad5592r_setup_source_ref_set(&ad5592r, AD5592R_VREF_SOURCE_INTERNAL); + ad5592r_setup_double_vref_set(&ad5592r, true); + ad5592r_setup_supply_voltage_mV(&ad5592r, VDD); // My board uses VDD = 5.0 V + + /* When using this method, the second argument + can be a byte [0,255] as the implementation compares each bit from + the number with a mask. For instance, 255 will set all 8 ports as DAC pins while + 170 (DEC) sets pins: 1010 1010 (BIN) or AA (HEX)*/ + + /* Previous --> ad5592r_setup_dac_all(&ad5592r, AD5592R_IO(0)); + This form created some issues + Instead, the ad5592r_setup_dac method could be used for individual Ports or in a for loop + for setting all ports + */ + + ad5592r_setup_dac_all(&ad5592r, 255); //Setting all pins as DAC + + ad5592r_setup_ldac(&ad5592r, AD5592R_LDAC_MODE_IMMEDIATELY); + + channelSet = false; + voltageSet = false; + + delay(3000); // On Teensy there is no reset on serial +} + +void loop() { + + switch (serialStatus){ + + case(REQUEST_CHANNEL): + + Serial.print("Set channel >> "); + + serialStatus = WAITING_INPUT; + + break; + + case(REQUEST_VOLTAGE): + + Serial.print("Set voltage [0, VDD] >> "); + + serialStatus = WAITING_INPUT; + + break; + + case(WAITING_INPUT): + + + if (serialReceived) { + + if(CHATTY_CODE) Serial.println("Input string received!"); + + serialReceived = false; + + + if((channelSet == false) && (voltageSet == false)){ + + retrieveChannel(); + + }else if((voltageSet == false) && (channelSet == true)){ + + retrieveVoltage(); + + } + + } + + break; + + case(RESET_FLAGS): + + channelSet = false; + voltageSet = false; + + if(CHATTY_CODE) Serial.println("Ready to set channel!"); + + serialStatus = REQUEST_CHANNEL; + + break; + } + +} + + +void retrieveChannel(){ + + channel = serialIn.toInt(); + + serialIn = ""; + + if((channel >=0) && (channel <= 7)){ + + if(CHATTY_CODE) Serial.print("Writing to channel: "); + + Serial.println(channel); + + channelSet = true; + serialStatus = REQUEST_VOLTAGE; + + } else { + + Serial.println("Channel out of bounds!"); + + channelSet = false; + serialStatus = REQUEST_CHANNEL; + + } +} + +void retrieveVoltage(){ + + double voltage_mV_set = serialIn.toInt(); + + serialIn = ""; + + + if((voltage_mV_set >=0) && (voltage_mV_set <= VDD)){ + + + if(CHATTY_CODE) Serial.print("Value received: "); + + Serial.print(voltage_mV_set); + Serial.print("\n"); + + if(CHATTY_CODE) Serial.println("Setting voltage!"); + + bool DACResponse = ad5592r_dac_voltage_set_mV(&ad5592r, AD5592R_IO(channel), voltage_mV_set); + + if (DACResponse){ + + Serial.println("OK!"); + + serialStatus = RESET_FLAGS; + + } else{ + + Serial.println("Something went wrong :'( _____ Try again..."); + + voltageSet = false; + + serialStatus = REQUEST_VOLTAGE; + + } + + + } else{ + + + Serial.println("Voltage out of bounds!"); + + voltageSet = false; + + serialStatus = REQUEST_VOLTAGE; + + } + +} + + +void serialEvent() { + +// From the serialEvent examples + + while (Serial.available()) { + // get the new byte: + char inChar = (char)Serial.read(); + // add it to the inputString: + serialIn += inChar; + // if the incoming character is a newline, set a flag so the main loop can + // do something about it: + if (inChar == '\n') { + serialReceived = true; + } + } +} From 05bfba36a4d8046adf837356c304c5c4b344e9f6 Mon Sep 17 00:00:00 2001 From: Daniel Melendrez Date: Wed, 20 May 2020 16:49:28 +0100 Subject: [PATCH 2/4] Add files via upload Some enhancements for improving usability of the library. This version can be ported to Arduino and Teensy Boards. Tested on a Teensy 3.6 --- libad5592r.cpp | 30 +++++++++++++++++++++++++++--- libad5592r.h | 5 ++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/libad5592r.cpp b/libad5592r.cpp index 62fd253..59a35cf 100755 --- a/libad5592r.cpp +++ b/libad5592r.cpp @@ -8,15 +8,30 @@ ad5592r_t * -ad5592r_init(ad5592r_t *obj) +ad5592r_init(ad5592r_t *obj, uint8_t pinSS) { + /* + // Old implementation. Not recommended anymore: https://www.arduino.cc/en/Reference/SPISetClockDivider SPI.begin(); SPI.setDataMode(SPI_MODE1); SPI.setBitOrder(MSBFIRST); SPI.setClockDivider(SPI_CLOCK_DIV2); + */ obj->supply_voltage_mV = 3300; /* Voreingestellt Versorgungsspannung (Vdd) */ obj->external_ref_voltage_mV = 3300; /* Voreingestellte externe Referenzsspannung (Vref) */ + obj->syncPin = pinSS; + + SPI.begin(); + + #ifdef CORE_TEENSY + // Teensy can go faster. The DAC is comfy at 10 MHz + SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE1)); + pinMode(obj->syncPin, OUTPUT); // Worked on tempe Readin example! + #else + // Arduino boards can do slower SPI + SPI.beginTransaction(SPISettings(SPI_CLOCK_DIV2, MSBFIRST, SPI_MODE1)); + #endif return obj; } @@ -155,7 +170,12 @@ ad5592r_comm(ad5592r_t *obj __attribute__((unused)), ad5592r_word sixteen_bits, uint8_t send[2]; ad5592r_split_word(send, sixteen_bits, sizeof(send)); - digitalWrite(SS, LOW); + + #ifdef CORE_TEENSY + digitalWrite(obj->syncPin, LOW); + #else + digitalWrite(SS,LOW); + #endif for (size_t i = 0; i < sizeof(send); i++) { @@ -168,8 +188,12 @@ ad5592r_comm(ad5592r_t *obj __attribute__((unused)), ad5592r_word sixteen_bits, Serial.println(rx_data[0], HEX); Serial.println(rx_data[1], HEX); #endif - digitalWrite(SS, HIGH); + #ifdef CORE_TEENSY + digitalWrite(obj->syncPin, HIGH); //SS + #else + digitalWrite(SS,HIGH); + #endif return true; } diff --git a/libad5592r.h b/libad5592r.h index 26c400e..72ac52d 100755 --- a/libad5592r.h +++ b/libad5592r.h @@ -101,6 +101,9 @@ typedef struct { bool double_vref_adc; bool double_vref_dac; + // Added the option to set your own SS pin --> github.com/dzalf On May 2020 + uint8_t syncPin; + ad5592r_ldac_mode_t ldac_mode; /* Spannung welche an Pin (Vdd) anliegt */ @@ -121,7 +124,7 @@ typedef struct { * Initialisierung von ad5592r-Objekt * @param obj: Zeiger auf Objekt */ -ad5592r_t *ad5592r_init(ad5592r_t *obj); +ad5592r_t *ad5592r_init(ad5592r_t *obj, uint8_t pin); /** * Zurücksetzen From c049ca7b83d06d3572ec187150b7afa5f76fcbb5 Mon Sep 17 00:00:00 2001 From: Daniel Melendrez Date: Wed, 20 May 2020 16:55:04 +0100 Subject: [PATCH 3/4] Add files via upload --- libad5592r.cpp | 30 +++--------------------------- libad5592r.h | 5 +---- 2 files changed, 4 insertions(+), 31 deletions(-) diff --git a/libad5592r.cpp b/libad5592r.cpp index 59a35cf..62fd253 100755 --- a/libad5592r.cpp +++ b/libad5592r.cpp @@ -8,30 +8,15 @@ ad5592r_t * -ad5592r_init(ad5592r_t *obj, uint8_t pinSS) +ad5592r_init(ad5592r_t *obj) { - /* - // Old implementation. Not recommended anymore: https://www.arduino.cc/en/Reference/SPISetClockDivider SPI.begin(); SPI.setDataMode(SPI_MODE1); SPI.setBitOrder(MSBFIRST); SPI.setClockDivider(SPI_CLOCK_DIV2); - */ obj->supply_voltage_mV = 3300; /* Voreingestellt Versorgungsspannung (Vdd) */ obj->external_ref_voltage_mV = 3300; /* Voreingestellte externe Referenzsspannung (Vref) */ - obj->syncPin = pinSS; - - SPI.begin(); - - #ifdef CORE_TEENSY - // Teensy can go faster. The DAC is comfy at 10 MHz - SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE1)); - pinMode(obj->syncPin, OUTPUT); // Worked on tempe Readin example! - #else - // Arduino boards can do slower SPI - SPI.beginTransaction(SPISettings(SPI_CLOCK_DIV2, MSBFIRST, SPI_MODE1)); - #endif return obj; } @@ -170,12 +155,7 @@ ad5592r_comm(ad5592r_t *obj __attribute__((unused)), ad5592r_word sixteen_bits, uint8_t send[2]; ad5592r_split_word(send, sixteen_bits, sizeof(send)); - - #ifdef CORE_TEENSY - digitalWrite(obj->syncPin, LOW); - #else - digitalWrite(SS,LOW); - #endif + digitalWrite(SS, LOW); for (size_t i = 0; i < sizeof(send); i++) { @@ -188,12 +168,8 @@ ad5592r_comm(ad5592r_t *obj __attribute__((unused)), ad5592r_word sixteen_bits, Serial.println(rx_data[0], HEX); Serial.println(rx_data[1], HEX); #endif + digitalWrite(SS, HIGH); - #ifdef CORE_TEENSY - digitalWrite(obj->syncPin, HIGH); //SS - #else - digitalWrite(SS,HIGH); - #endif return true; } diff --git a/libad5592r.h b/libad5592r.h index 72ac52d..26c400e 100755 --- a/libad5592r.h +++ b/libad5592r.h @@ -101,9 +101,6 @@ typedef struct { bool double_vref_adc; bool double_vref_dac; - // Added the option to set your own SS pin --> github.com/dzalf On May 2020 - uint8_t syncPin; - ad5592r_ldac_mode_t ldac_mode; /* Spannung welche an Pin (Vdd) anliegt */ @@ -124,7 +121,7 @@ typedef struct { * Initialisierung von ad5592r-Objekt * @param obj: Zeiger auf Objekt */ -ad5592r_t *ad5592r_init(ad5592r_t *obj, uint8_t pin); +ad5592r_t *ad5592r_init(ad5592r_t *obj); /** * Zurücksetzen From cf79a4ef98a32be3e7412ca9cab2c4076c99b278 Mon Sep 17 00:00:00 2001 From: Daniel Melendrez Date: Wed, 20 May 2020 16:56:33 +0100 Subject: [PATCH 4/4] Updated versions These new versions improve compatibility with other platforms. The SPI definition was updated to the recommended syntaxis --- libad5592r.cpp | 30 +++++++++++++++++++++++++++--- libad5592r.h | 5 ++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/libad5592r.cpp b/libad5592r.cpp index 62fd253..59a35cf 100755 --- a/libad5592r.cpp +++ b/libad5592r.cpp @@ -8,15 +8,30 @@ ad5592r_t * -ad5592r_init(ad5592r_t *obj) +ad5592r_init(ad5592r_t *obj, uint8_t pinSS) { + /* + // Old implementation. Not recommended anymore: https://www.arduino.cc/en/Reference/SPISetClockDivider SPI.begin(); SPI.setDataMode(SPI_MODE1); SPI.setBitOrder(MSBFIRST); SPI.setClockDivider(SPI_CLOCK_DIV2); + */ obj->supply_voltage_mV = 3300; /* Voreingestellt Versorgungsspannung (Vdd) */ obj->external_ref_voltage_mV = 3300; /* Voreingestellte externe Referenzsspannung (Vref) */ + obj->syncPin = pinSS; + + SPI.begin(); + + #ifdef CORE_TEENSY + // Teensy can go faster. The DAC is comfy at 10 MHz + SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE1)); + pinMode(obj->syncPin, OUTPUT); // Worked on tempe Readin example! + #else + // Arduino boards can do slower SPI + SPI.beginTransaction(SPISettings(SPI_CLOCK_DIV2, MSBFIRST, SPI_MODE1)); + #endif return obj; } @@ -155,7 +170,12 @@ ad5592r_comm(ad5592r_t *obj __attribute__((unused)), ad5592r_word sixteen_bits, uint8_t send[2]; ad5592r_split_word(send, sixteen_bits, sizeof(send)); - digitalWrite(SS, LOW); + + #ifdef CORE_TEENSY + digitalWrite(obj->syncPin, LOW); + #else + digitalWrite(SS,LOW); + #endif for (size_t i = 0; i < sizeof(send); i++) { @@ -168,8 +188,12 @@ ad5592r_comm(ad5592r_t *obj __attribute__((unused)), ad5592r_word sixteen_bits, Serial.println(rx_data[0], HEX); Serial.println(rx_data[1], HEX); #endif - digitalWrite(SS, HIGH); + #ifdef CORE_TEENSY + digitalWrite(obj->syncPin, HIGH); //SS + #else + digitalWrite(SS,HIGH); + #endif return true; } diff --git a/libad5592r.h b/libad5592r.h index 26c400e..72ac52d 100755 --- a/libad5592r.h +++ b/libad5592r.h @@ -101,6 +101,9 @@ typedef struct { bool double_vref_adc; bool double_vref_dac; + // Added the option to set your own SS pin --> github.com/dzalf On May 2020 + uint8_t syncPin; + ad5592r_ldac_mode_t ldac_mode; /* Spannung welche an Pin (Vdd) anliegt */ @@ -121,7 +124,7 @@ typedef struct { * Initialisierung von ad5592r-Objekt * @param obj: Zeiger auf Objekt */ -ad5592r_t *ad5592r_init(ad5592r_t *obj); +ad5592r_t *ad5592r_init(ad5592r_t *obj, uint8_t pin); /** * Zurücksetzen