For values near 0 DAC (of the 10-bit true not PWM DAC) I get anomalous values out (measured on an oscilloscope) where lower values produce slightly higher output voltage. I cannot set the DAC to 0 (which produces about 20mV output). It seems as if the DAC is going into a protection mode that is disabled only by writing a slightly higher DAC value. I can slowly reduce the DAC value from eg. 10 down to 2 and the voltage decreases as expected. If I set the DAC down to 1 or to 0, the voltage INCREASES, to a value equivalent to if it had been set to DAC=7. It stays in this state until I write a DAC value of at least 7, after which I can vary the DAC until I again set it down to 0 or 1.
Below is example code. I put in a short 20mS delay after each Serial command read so I can see the changes on the oscilloscope.
One can simply type voltage set commands into the serial monitor all at once and the GetSerialCommandUSB() will sequentially apply the DAC settings:
d10 d9 d8 d7 d6 d5 d4 d3 d2 d3 d4 d5 d6 d7 d8 d9 d10 d10 d9 d8 d7 d6 d5 d4 d3 d2 d1 d1 d1 d1 d2 d3 d2 d4 d2 d5 d2 d6 d2 d7 d2 d8 d2 d9 d2 d10 d2 d20 d2
In this example the DAC will output a decreasing then increasing triangle wave during the intial portion:
d10 d9 d8 d7 d6 d5 d4 d3 d2 d3 d4 d5 d6 d7 d8 d9 d10
During the next portion the voltage output will decrease until the first d1 command is received. Setting the A0 to 1 (or 0) causes the voltage to jump up as if I'd set it to 7. Then it stays stuck in this d7-equivalent output value (DC) state until at least a DAC 7 value is output, after which it will properly toggle between the high (7,8,9,10,20) to d2 values.
On another M0 board with an output resistor (330 ohm series) leading to a 100uF cap, I see a similar behavior but can slowly set DAC down to 1 without the anomaly occuring. However, if I QUICKLY go from eg. DAC 10 to anything lower than DAC 6, the DAC goes into the anomalous state until driven above DAC=6. But I can slowly go from DAC 6 down to DAC1 on that board. So I'm thinking it perhaps is related to output loading on the DAC. In the above example I only have an oscilloscope probe attached to A0, so is pretty lightly loaded capacitively.
----------------------------------
Here is code
Code: Select all
#define ANALOG_OUT A0
/*Test of FeatherM0 analog out using user input via USB*/
void setup()
{
// analogWriteResolution(10);
analogWrite(ANALOG_OUT, 0); //analog output to send signal to Davis Weather station input
}
void loop()
{
while (Serial.available() > 0) GetSerialCommandUSB(); //Check for user command inputs from USB port
}
//--------- User Input Selections----------//
int GetSerialCommandUSB()
{
String commandBuffer;
int channel;
int setValue;
long int setValueInt;
float setFloat;
switch (Serial.read()) //read first character of serial command
{
case ('d'): //set Davis WX DAC ouptut value for testing
{
setValue = Serial.parseInt();
analogWrite(ANALOG_OUT, setValue); //analog output to send signal to Davis Weather station input]
Serial.print("DAC ="); Serial.println(setValue);
delay(20);
break;
}
default:
{
// Serial.println("Invalid command, type '?' for help menu.");
}
}
return 0;
}