MCP4725 12 bit i2c DAC problem

sghioto

Senior Member
Having troubles trying to get the dac to generate an output voltage with the schematic and code as posted.
The address code for the MCP4725 is 11000000 from the data sheet

Basically just trying to take the adc reading from c.4
write the value to eeprom location 0
generate the analog value from the dac

With the code as written the output of the dac sits at half the supply voltage even if not connected to the 08M2.
Apparently I do not have the hi2csetup command correctly and/or the code is not correct.

I searched the archives and found only one thread with some code but honestly having difficulty understanding this whole hi2c system.
Any suggestions please.

Steve G

Code:
[code]       [COLOR=Blue]hi2csetup i2cmaster[/COLOR][COLOR=Black], [/COLOR][COLOR=Navy]%11000000[/COLOR][COLOR=Black], [/COLOR][COLOR=Blue]i2cslow[/COLOR][COLOR=Black], [/COLOR][COLOR=Blue]i2cbyte
 [/COLOR][COLOR=Black]main: [/COLOR][COLOR=Blue]readadc [/COLOR][COLOR=Navy]4[/COLOR][COLOR=Black],[/COLOR][COLOR=Purple]b0
         [/COLOR][COLOR=Blue]write [/COLOR][COLOR=Navy]0[/COLOR][COLOR=Black],[/COLOR][COLOR=Purple]b0
[/COLOR][COLOR=Blue]         pause [/COLOR][COLOR=Navy]5[/COLOR][COLOR=Purple]
         [/COLOR][COLOR=Blue]hi2cout [/COLOR][COLOR=Navy]0[/COLOR][COLOR=Black],[/COLOR][COLOR=Blue]([/COLOR][COLOR=Purple]b0[/COLOR][COLOR=Blue])
        [/COLOR][COLOR=Navy] [/COLOR][COLOR=Blue]goto [/COLOR][COLOR=Black]main[/COLOR]
[/CODE]
 

Attachments

Last edited:

AllyCat

Senior Member
Hi,

Did you find the code from Pongo in post #8 here ? It's for an X2, but should be easy to modify (by changing the w1<<4 to w1*16 ).

I've never used that DAC but it appears to need 3 bytes after the address: a control byte, the DAC high bits (8) and the (4) low bits with 4 "don't care"s (0). You appear to be sending only one byte; the data sheet says "The device executes the Master&#8217;s write command after receiving the last byte (4th byte)."

The half-supply output is probably correct as the data says: "The factory default settings for the EEPROM prior to shipment are shown in Table 4 - 3 (set for a middle scale output)." But I can't find table 4.3.

Another test is to see if you can read data (or anything!) back from the device. Failure might indicate an incorrect address or I2C bus hardware fault.

Cheers, Alan.
 

hippy

Technical Support
Staff member
Basically just trying to take the adc reading from c.4
write the value to eeprom location 0
generate the analog value from the dac
Start with something simpler; just use a constant value to output, then a varying or stepped output.
 

sghioto

Senior Member
Start with something simpler
I thought I did but nothing works. I used the code from Pongo modified as Alan suggested for the 08M2 and still the output just sits at half supply. Maybe the chip is bad.

Steve G
 

inglewoodpete

Senior Member
Your addressing appears to be correct. Check that the A0 pin is pulled low.

Did you buy the chip(s) from a reputable source? If not, address bits A1 and A2 could be preconfigured to some other values.

Also, make sure the pull-up resistors are in place and doing their job.

If all else fails, write a routine that tries writing and reading to/from every second address from $2 to $FE. The response will be slow and =$FF on every non-existent address. Hopefully, you will get a non-$FF value at the device's correct address.

I have to assume you understand the configuration details for reading and writing to the device. I have not used this one, so can't offer any real experience.

Edit: Looking at your code, you are only sending the device one byte, which I think goes to a configuration register if I'm reading the data sheet correctly. The DAC value goes in the upper 12 bits of locations 1 & 2. Pongo's sample code (refer post #2 above) is writing three bytes to the device.
 

Rick100

Senior Member
Hello sghioto,

From looking at the Adafruit and Banggood web sites that sell these modules, it looks like the ones they sell have the A1 bit set in the device address. It looks like Pongo's code pointed to by Alan should work, but I'm not sure what this line does.
'hi2cout (%00000101) 'startup and pass the value stored in the eeprom to the DAC.'
Are you using a raw chip or a module and where did you get it?


Good luck,
Rick
 
Last edited:

hippy

Technical Support
Staff member
Looking at the datasheet, it seems that fast mode can be used to send data which just requires a 12-bit value to set the DAC output $000=off, $FFF=full. Something like this should create a sawtooth output ...

Code:
HI2cSetup I2CMASTER, %11000000, I2CSLOW, I2CBYTE
Do
  For w0 = $000 To $FFF
    HI2cOut ( b1, b0 ) ; Output w0 as two bytes
    Pause 10
  Next
Loop
The EEPROM needs to be written to set the initial sate when it first powers on. That should only require a one-off programming of -

Code:
HI2cOut ( $60, $00, $00 ) ; Clear DAC and EEPROM
Pause 10
If one wants to copy a READADC input into the DAC then this should do that -

Code:
HI2cSetup I2CMASTER, %11000000, I2CSLOW, I2CBYTE
Do
  ReadAdc ADC_PIN, b0
  w0 = b0 * 16
  HI2cOut ( b1, b0 ) ; Output w0 as two bytes
Loop
Or

Code:
HI2cSetup I2CMASTER, %11000000, I2CSLOW, I2CBYTE
Do
  ReadAdc10 ADC_PIN, w0
  w0 = w0 * 4
  HI2cOut ( b1, b0 ) ; Output w0 as two bytes
Loop
 

sghioto

Senior Member
Good news it's working. The address code was incorrect, thanks inglewoodpete for the suggestion. There are only three other address possibilities and it turned out to be
%11000100. The chip is on a breakout board with the A0 pin connected to ground through a 10K resistor. Of coarse there wasn't any documentation with the unit sold by HiLetgo through Amazon. Thanks everyone for the suggestions and code examples.
Steve G

Here's the code I'm using that returns almost a rail to rail output:

Code:
[code]       [COLOR=Blue]hi2csetup i2cmaster[/COLOR][COLOR=Black], [/COLOR][COLOR=Navy]%11000100[/COLOR][COLOR=Black], [/COLOR][COLOR=Blue]i2cslow[/COLOR][COLOR=Black], [/COLOR][COLOR=Blue]i2cbyte[/COLOR]
[COLOR=Black]main:  [/COLOR][COLOR=Blue]readadc10 [/COLOR][COLOR=Navy]4[/COLOR][COLOR=Black],[/COLOR][COLOR=Purple]w0
       w0[/COLOR][COLOR=DarkCyan]=[/COLOR][COLOR=Purple]w0[/COLOR][COLOR=DarkCyan]*[/COLOR][COLOR=Navy]4
       [/COLOR][COLOR=Blue]hi2cout([/COLOR][COLOR=Purple]b1[/COLOR][COLOR=Black],[/COLOR][COLOR=Purple]b0[/COLOR][COLOR=Blue])
       goto [/COLOR][COLOR=Black]main[/COLOR]
[/CODE]
 

Attachments

Enfield8000

New member
Hi, I am using a pair of MCP4725 12 bit DACs, which are being driven from calculations in a 40x2, I am using the standard readadc10 commands, I have 3 ADC channels being used, and I am getting myself into all sorts of arithmetical mischief. It appears that the X2 ADCs read into the lowest bits of the data word leaving the top 6 binary bits as '0'. When I pass the words across to the DACs they happily work with the top data bits and drop off the lower 4 binary bits as not required - this means that I have to multiply my numbers by an extra factor of 16 to get the DAC scale range correct. Is this correct?? and does it mean that I am losing output DAC resolution against the input ADC's??

Reason? well the 40x2 is being used as an ECU!
 

hippy

Technical Support
Staff member
To convert the 10-bit lsb aligned values from the PICAXE READADC10 commands to 12-bit msb aligned values the DAC's require you can either shift left by 4 bits, multiply by 16, or shift left by 6-bits, multiply by 64 -

---- --ji hgfe dcba

--ji hgfe dcba xxxx - After multiplying by 16

jihg fedc ba-- xxxx - After multiplying by 64

If doing the first you will get an output which is a quarter of what it potentially could be. If doing the second you will get the full output it could be. You will only have 10-bit resolution in both cases, and there's no way to avoid that, except by using an average of READADC10 readings ...
Code:
ReadAdc10 pin, w1
ReadAdc10 pin, w2
ReadAdc10 pin, w3
ReadAdc10 pin, w4
w0 = w1 + w2 + w3 + w4 * 16
Then send the 'w0' value to the DAC.

Added : But looking at the MCP4725 datasheet it seems the 12-bits are sent as lsb aligned. So to make a 10-bit READADC10 value 12-bit compatible you should either send it as it is, giving a quarter of the output value it could be, or after multiplying by 4 ...
Code:
ReadAdc10 pin, w0
HI2cOut ( b1, b0 )
Code:
ReadAdc10 pin, w0
w0 = w0 * 4
HI2cOut ( b1, b0 )
Code:
ReadAdc10 pin, w1
ReadAdc10 pin, w2
ReadAdc10 pin, w3
ReadAdc10 pin, w4
w0 = w1 + w2 + w3 + w4
HI2cOut ( b1, b0 )
 
Last edited:

Enfield8000

New member
Thanks,

I was just going a bit nuts. The 40x2 is acting as the intermediate control system between a vehicle's manual controls and a 20kW 120V Traction Motor controller - There are 2 throttle pots and one brake transducer (as well as lots of other bells and whistles) - I have just spent a day trying to work out why there was no throttle or regen brake signal! - this included completely re-writing the control programme! - factors of 64 can be so frustrating! forgive me, but I need to get my abacus out again!
 
Top