Hello all,
I have created a code for 10 bit DAC using Arduino MKR zero board with SAMD21 controller. Here is the code. it created with default voltage reference 3.3V. i get desired output. Problem statement stated below the code:
**Voltage reference 3.3V**
void setup()
{
Serial.begin(9600); //start serial communication
analogWriteResolution(10); //set the Arduino DAC for 10 bits of resolution (max)
//Get user entered voltage, convert to DAC value, output DAC value
analogWrite(A0,setDAC(getVoltValue()));
}
void loop()
{
}
//this function converts a user entered voltage value into a 10 bit DAC value
int setDAC(float volt) {
//formula for calculating DAC output voltage Vdac = (dVal / 1023)*3.3V
int ipval = (int)((volt*1023)/3.3);
return ipval;
}
//this function gets the user entered DAC voltage value from serial monitor
float getVoltValue() {
float v = 0; //variable to store voltage
Serial.println("Enter the voltage you want the DAC to output (range 0V to 3.3V)");
v = readParameter();
if (v < 0 || v > 3.3) v = 0; //check to make sure it is between 0 and 3.3V
Serial.print("DAC voltage set to ");
Serial.println(v);
Serial.println("Outputting DAC value........");
return v;
}
//waits for serial data and reads it in. This function reads in the parameters
// that are entered into the serial terminal
float readParameter() {
while(!Serial.available()); //Wait for user to enter setting
return Serial.parseFloat(); //get int that was entered on Serial monitor
}
Problem Statement:
To increase the resolution. i am trying to use the internal reference(1V) in place of Default reference (3.3V)
here is the modified code but i am not getting appropriate output. Please check this modified code and add your directions.
**Voltage reference 1.0V**
void setup()
{
Serial.begin(9600); //start serial communication
analogWriteResolution(10); //set the Arduino DAC for 10 bits of resolution (max)
analogReference(AR_INTERNAL1V0);
//analogReference(AR_DEFAULT);
//Get user entered voltage, convert to DAC value, output DAC value
analogWrite(A0,setDAC(getVoltValue()));
}
void loop()
{
}
//this function converts a user entered voltage value into a 10 bit DAC value
int setDAC(float volt) {
//formula for calculating DAC output voltage Vdac = (dVal / 1023)*1.0V
int ipval = (int)((volt*1023)/1.0);
return ipval;
}
//this function gets the user entered DAC voltage value from serial monitor
float getVoltValue() {
float v = 0; //variable to store voltage
Serial.println("Enter the voltage you want the DAC to output (range 0V to 1.0V)");
v = readParameter();
if (v < 0 || v > 1.0) v = 0; //check to make sure it is between 0 and 3.3V
Serial.print("DAC voltage set to ");
Serial.println(v);
Serial.println("Outputting DAC value........");
return v;
}
//waits for serial data and reads it in. This function reads in the parameters
// that are entered into the serial terminal
float readParameter() {
while(!Serial.available()); //Wait for user to enter setting
return Serial.parseFloat(); //get int that was entered on Serial monitor
}
Working code for 1V Reference
void setup()
{
Serial.begin(9600); //start serial communication
DAC->CTRLB.bit.REFSEL = 0;
delay(1000);
analogWriteResolution(10); //set the Arduino DAC for 10 bits of resolution (max)
//Get user entered voltage, convert to DAC value, output DAC value
analogWrite(A0,setDAC(getVoltValue()));
}
void loop()
{
}
//this function converts a user entered voltage value into a 10 bit DAC value
int setDAC(float volt) {
//formula for calculating DAC output voltage Vdac = (dVal / 1023)*3.3V
int ipval = (int)((volt*1023)/1.0);
return ipval;
}
//this function gets the user entered DAC voltage value from serial monitor
float getVoltValue() {
float v = 0; //variable to store voltage
Serial.println("Enter the voltage you want the DAC to output (range 0V to 3.3V)");
v = readParameter();
if (v < 0 || v > 1.0) v = 0; //check to make sure it is between 0 and 3.3V
Serial.print("DAC voltage set to ");
Serial.println(v);
Serial.println("Outputting DAC value........");
return v;
}
//waits for serial data and reads it in. This function reads in the parameters
// that are entered into the serial terminal
float readParameter() {
while(!Serial.available()); //Wait for user to enter setting
return Serial.parseFloat(); //get int that was entered on Serial monitor
}
End of query