Log in

View Full Version : I2C code to write the DAC MCP4725



acjacques
- 11th November 2023, 21:20
I am trying to control the DAC MCP4725 but my code seems not work . My code:

sda var portb.4
scl var portb.5

cont var byte
cont = %1100
addr var byte
addr = $60


'=========================================
writedac4725:

I2CWRITE sda,scl,cont,addr,[4095] '12 bit value
PAUSE 10 ' Wait 10ms for write to complete

return
'=========================================​


The address of my board was confirmed $60 by reading the board with I2CScanner Arduino sketch
The device control %1100 was from datasheet :


https://ww1.microchip.com/downloads/en/devicedoc/22039d.pdf


I would appreciate any help

Thanks
ACJacques

Ioannis
- 11th November 2023, 22:21
The address depends on the A2, A1 that are hardcoded into the chip at manufacturing time and also on the A0 that the user controls.

I guess you have double checked them.

Also the control byte I think should be a constant and not a variable.

Ioannis

acjacques
- 11th November 2023, 22:41
Richard from Support Melabs give me the solution:

addr var byte
addr = $C0 ;$60 <<1

'==========================================
writedac4725:
I2CWRITE sda,scl,addr,[4000] '12 bit value
PAUSE 10 ' Wait 10ms for write to complete
return
'=========================================

richard
- 12th November 2023, 04:12
Richard from Support Melabs give me the solution:

I should point out that I'm just an enthusiastic user on that forum and this one . I do not represent Melabs in any way shape or form

Ioannis
- 12th November 2023, 21:14
I2CWRITE sda,scl,addr,[4000] '12 bit value


And that does work now?

Ioannis

tumbleweed
- 12th November 2023, 22:46
addr var byte
addr = $C0 ;$60 <<1

writedac4725:
I2CWRITE sda,scl,addr,[4000] '12 bit value
PAUSE 10 ' Wait 10ms for write to complete

That'll do a fast-mode write to the DAC register as long as the "12-bit value" is <4095, so you could remove the 'PAUSE 10'.
If the upper control bits are '01x' then it'll possibly write to the DAC + EEPROM, depending on the actual value.

Ioannis
- 13th November 2023, 13:29
Isn't necessary the control byte?

Ioannis

tumbleweed
- 13th November 2023, 16:41
In fast mode (with the upper two bits 00) you only need two bytes.
If you want to use the EEPROM function you need all three bytes.

Of course, both modes need the address byte too.

acjacques
- 14th November 2023, 01:16
Yes. It is working in the Fast Mode.

valuedac= 4095

I2CWRITE sda,scl,%11000010,[valuedac.highbyte,valuedac.lowbyte]


The above control byte in binary is from data sheet 1100 = device code
0010 is the board with address code set to A0 = 1 . Most default boards are factory 0000. I have tied the user A0 bit to VCC in the board.
Control and Address are in the same first byte.

Data sheet says that second and third bytes may be repeated (valuedac may be repeated ) like
I2CWRITE sda,scl,%11000010,[valuedac.highbyte,valuedac.lowbyte,valuedac.highby te,valuedac.lowbyte]
This also works but seems be not necessary.

I will try next days to write a code for other modes.

Data Sheet link: https://ww1.microchip.com/downloads/en/devicedoc/22039d.pdf