News:

Let's find out together what makes a PIC Tick!

Main Menu

Any example code to write to a MCP4725?

Started by TimB, Mar 25, 2023, 07:16 PM

Previous topic - Next topic

TimB

Hi All

I'm trying to talk to a MCP4725

I tried using my software i2c code and no luck and I tried to use bus out etc but no luck

I have the MCPAddress as $60 and A0 is tied to Grnd

        Bstart
        Busout cMCP4725_Address
        Busout $40
        Busout pADCValue.byte1
        Busout pADCValue.byte0
        BStop


Anybody had any success using them? If so do you have the code to share?

Thanks

Tim

trastikata

#1
Quote from: TimB on Mar 25, 2023, 07:16 PMI have the MCPAddress as $60 and A0 is tied to Grnd

Hello TimB, the I2C address seems wrong.

0x60 = %01100000

The Address Byte consists of Device Code (1100) + Address Bits(A2+A1+A0)  + Read/Write bit

Thus the I2C address can not be 0x60 because the device code does not correspond. Make sure the Address Bits A2+A1 are correct since they are manufacturer set and can vary from lot to lot.

MCP.jpg

TimB


Hi and thanks

The A2 and A1 are programmed to '00' (default), if not requested by customer......

I took my code from the sparkfun board code


MCP4725.begin(0x60); // Default I2C Address of MCP4725 breakout board (sparkfun)   If not try 0x61 or 0x62


Adafuit says:-
A0 allow you to change the I2C address. By default (nothing attached to A0) the address is hex 0x62. If A0 is connected to VDD the address is 0x63.

All does not compute


trastikata

0x60 is the slave address, add the read/write bit, the address byte becomes 0xC1 and 0xC0.

Symbol MCP4725_rc = 0xC1  'Read control
Symbol MCP4725_wc = 0xC0  'Writecontrol

Bstart
 Busout MCP4725_wc
....
BStop

Pepe

I agree with trastikata further if A0=1 then would be
$C3 to read and $C2 to write

TimB

#5
Well I fully agree with the $C0 Address etc

I'm getting conflicting information on the rest though

This from the data sheet

MCP4725 data 1.png


This is from a 2009 MCP4725 PICtail Plus Daughter Board User's Guide

MCP4725 data 2.png

Either way nothing works. I will have to get the scope out to check the slew rates etc

To be fair the data sheet does actually show the fast mode the same way as the PICtail data sheet. It was when I was jumping to it in the PDF it took me to the wrong image 

trastikata

Quote from: TimB on Mar 26, 2023, 08:17 AMWell I fully agree with the $C0 Address etc
I'm getting conflicting information on the rest though

Hello TimB,

The datasheet says the maximum I2C clock is 100 kHz in Standard Mode, so maybe in software I2C this could be an issue if the MCU runs too fast?

Another thought - pADCValue.byte1 and pADCValue.byte0 in your original post should be 12b value aligned to the left, did you shift the data?

In your OP, the code shows:

Bstart
        Busout cMCP4725_Address
        Busout $40
        Busout pADCValue.byte1
        Busout pADCValue.byte0
BStop

0x40 means 01000000 - which is C2 = 0, C1 = 1, C0 = 0 - this means Fast Mode - however Fast Mode requires 3 bytes before the stop condition. Thus this should be, if Standard Mode is used, in my opinion:

Bstart
        Busout $C0
        Busout $00
        Busout pADCValue.byte1
        Busout pADCValue.byte0
BStop

Where  pADCValue.byte1 and pADCValue.byte0 is 12b value aligned to the left.


TimB


Thanks

After reading the data sheet a lot

The address is $C0 with the A0 at grnd

The example I got was not using fast mode

I have since changed my code to

        Bstart
        Busout cMCP4725_Address
        Busout pADCValue.byte1
        Busout pADCValue.byte0
        BStop

I have also tried
        I2Cout portB.1, portB.2, $C0, [ WGPTemp1.byte1, WGPTemp1.byte0 ]

And
        I2C_Start()                                                             ' / Bus Start
        I2C_WriteByte(cMCP4725_Address)                                         ' \ Slave address
        delayus 10
        I2C_WriteByte(pADCValue.byte1)                                          ' \ Write the high DAC Data
        delayus 10
        I2C_WriteByte(pADCValue.byte0)                                          ' / Write the low DAC Data
        delayus 10
        I2C_Stop()   

I will now slow everything down even more

Thanks


TimB

Ok finally working

The issue was the address. It seems Mouser ordered a version with A2 and A1 set

I only found out when I was looking at the part under macro photo and was checking I actually had the right part.

I had tried many address's but I bet I had some other fault so needed to get everything stable and 100% all the code was working in a VSM

To help others here is what to look for

MCP4725 chip.png

package marking.png 



And here is the code I used


    Symbol cMCP4725Address  = $CC
    Symbol cMCP4725ReadAddress  = $CD  ; Note: READ address is slave address + 1

    Proc MCP4725WriteDac(pDACVal as Word)

        BStart
        BusOut cMCP4725Address    ' Must match address of slave pic
        Busout pDACVal.Byte1
        Busout pDACVal.Byte0
        BStop
  EndProc

keytapper

I published a i2c scanner. That was the simplest way to find what devices are on the bus.
Ignorance comes with a cost