#include<Wire.h> //Include Wire library for using I2C functions #define MCP4725 0x60 //MCP4725 address as 0x60 Change yours accordingly
unsigned int adc;
byte buffer[3];
void setup()
{
Serial.begin(9600);
Wire.begin(); //Begins the I2C communication
}
void loop()
{
buffer
= 0b01000000; //Sets the buffer0 with control byte (010-Sets in Write mode
adc = analogRead(A0) * 4; //Read Analog value from pin A0 and convert into digital (0-1023) multiply with 4 gives (0-4096)
float ipvolt = (5.0 / 4096.0) * adc;
buffer[1] = adc >> 4;
buffer[2] = adc << 4;
unsigned int analogread = analogRead(A0)4 ;
float opvolt = (5.0/4096.0) analogread;
Wire.beginTransmission(MCP4725);
Wire.write(buffer
); //Sends the control byte to I2C
Wire.write(buffer [1] ); //Sends the MSB to I2C
Wire.write(buffer [2]); //Sends the LSB to I2C
Wire.endTransmission(); //Ends the transmission
delay(1000);
input voltage will go to voltage (0 ... 5)
output voltage
Must be (5 ... 0) v
I want it to be
anologread A0 input voltage (0 .... 5v)
I want from the output (5 ..... 0v)
can this be achieved by changing the address
or lice ~~ or !! I tried to use it, did not
I could not do the inverting process in the output example I would like your help, maybe a simple operation
gcjr:
from your code it looks like you want to send the 10-bit adc value read from A0 to the I2C device
don't understand the 4-bit shifts left and right.
in order to break the 10-bit value in the a least significant and most significant byte, you need to shift and/or mask
byte lsb = adc & 0xFF;
byte msb = (adc >> 8) & 0xFF;
on the other hand, you say the input is 0-5 but you want the output 5-0.
one approach is to simply subtract the 10-bit input value from 2<sup>10</sup>, 1024.
--
gcjr:
from your code it looks like you want to send the 10-bit adc value read from A0 to the I2C device
don't understand the 4-bit shifts left and right.
in order to break the 10-bit value in the a least significant and most significant byte, you need to shift and/or mask
byte lsb = adc & 0xFF;
byte msb = (adc >> 8) & 0xFF;
on the other hand, you say the input is 0-5 but you want the output 5-0.
one approach is to simply subtract the 10-bit input value from 2<sup>10</sup>, 1024.
--
thank the
as far as I understand this is not done with a different code
I will try to apply the code you specified above, I'm new to ardunio please I need your help very much
if you have different comments please I want you to write
I am grateful to this forum
you added code to calculate lsb and msb, but you're still sending buffer [1] and buffer [2]. lsb/msb are the values for buffer 1/2
As you said in writing the codes yes, I am not very good, I am learning slowly.
In which cycle will I use this code?
It is not enough to know the codes one by one, it is very important to interpret the codes as a whole
I'm new, thank you, thankful
Configuration bytes:
// 12-bit device values from 0-4095
// page 18-19 spec sheet
buffer[0] = 0b01000000; // control byte
// bits 7-5; 010 write DAC; 011 write DAC and EEPROM
// bits 4-3 unused
// bits 2-1 PD1, PD, PWR down P19 00 normal.
// bit 0 unused
buffer[1] = 0b00000000; //HIGH data
// bits 7-0 D11-D4
You continue to post code without using code tags. The code tags make the code look
like this
when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower right corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.
// Output of the potentiometer value in normal mode
DAC.setVoltage (potiWert);
endTime = micros ();
Serial.print ("Time:");
Serial.print (endTime - startTime);
Serial.println ("us");
// Read back the DAC register
Serial.print ("DAC:");
Serial.println (DAC.readRegister ());
// Read back the EEPROM
Serial.print ("EEPROM:");
Serial.println (DAC.readEEPROM ());
delay (1000);
}
}
(( STRAY '/302' in program ))
I got the program error. What could be the reason?
Please read the post at the start of any forum , entitled "How to use this Forum".
OR http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Hi,
The error STRAY means that there is a hidden character in your sketch that is not recognizable.
This a reason using code tags are so important.
Also the structure of the code for the Adafruit library that you have posted looks nothing like that.
Have a look at the Examples in the IDE for that library.
Can you post the ENTIRE code you had working in post#1 please, using code tags.
Hi,
Try this edit of your code, I have used the map() function to reverse the response.
Also the code was outputting the analog input and not the scaled output.
// Test program for DAC MCP4725
// Code for Arduino
// Author Retian
// Version 2.2
#include <MyMCP4725.h>
MyMCP4725 DAC (0x62); // I2C address = 0x62
#define potiPin 0
int16_t potiVert;
unsigned long startTime, endTime;
bool dacStatus = false;
void setup ()
{
Serial.begin (115200);
if (DAC.isReady ())
{
Serial.println ("\ nDAC ok!");
dacStatus = true;
}
else Serial.println ("DAC error!");
// Start output value of the DAC at 0 V (basic setting = VDD / 2)
//DAC.setVoltage(0, MCP4725_WRITE_EEPROM); // EEPROM is written !!
}
void loop ()
{
if (dacStatus)
{
// Read in the potentiometer
potiWert = analogRead (potiPin);
potiValue = map (potiValue, 0, 1023, 4095, 0); // use map function to reverse the output response
Serial.print (" Potiwert: ");
Serial.print (potiWert);
Serial.print (" Potiwert: ");
Serial.println (potiValue);
startTime = micros ();
// Output of the potentiometer value in normal mode
DAC.setVoltage (potiValue);
endTime = micros ();
Serial.print ("Time:");
Serial.print (endTime - startTime);
Serial.println ("us");
// Read back the DAC register
Serial.print ("DAC:");
Serial.println (DAC.readRegister ());
// Read back the EEPROM
Serial.print ("EEPROM:");
Serial.println (DAC.readEEPROM ());
delay (1000);
}
}