-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updated source files and new example #2
Open
dzalf
wants to merge
6
commits into
Metaln00b:master
Choose a base branch
from
dzalf:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
55b8af5
New SetDAC example
dzalf 811c30b
Merge pull request #1 from dzalf/dzalf-new-SetDAC_Example
dzalf 05bfba3
Add files via upload
dzalf c049ca7
Add files via upload
dzalf cf79a4e
Updated versions
dzalf e2afb6c
Merge pull request #2 from dzalf/dzalf-patch-1
dzalf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
232 changes: 232 additions & 0 deletions
232
examples/SetDAC_Voltage-Serial/SetDAC_Voltage-Serial.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <libad5592r.h> | ||
|
||
#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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I forgot that we can move this assignment inside the #ifdef CORE_TEENSY