I2c library to port a MCP4725 DAC library

I want to use a MCP4725 DAC with the Core2, so I’m trying to port the adafruit library.
But how do I communicate via I2c, is there a I2c library available?

Hi
I am trying something similar (use I2C). There is a library for I2C devices http://www.i2cdevlib.com/, which supports many devices and has also a port for STM processors, but I am not sure how to use it with Core2.
Michael

Hi, hFramework already integrates I2C library: https://husarion.com/core2/api_reference/classh_framework_1_1h_i2_c.html . There are 3 x I2C interfaces: hSens1, hSens2 and hExt - https://husarion.com/core2/manuals/hardware/#hardware-hsensor .

And here is an example:

uint8_t data[] = {0xff, 0x01, 0x02, 0x03};
    
hSens1.i2c.selectI2C();
hSens1.i2c.setDataRate(100000); //bps
while(1) {
    hSens1.i2c.write(0xA0, data, 4);
    sys.delay(100);//ms
}

Thanks Dominik2 !!,

I allready thought you would have a I2C library but I couldn’t find it.
Now I have a differen problem.

I get the error: ‘hSens1’ was not declared in this scope
hSens1.i2c.selectI2C()

I do include the hI2C.h file.

Hi Ruben, I’ve just tested this code in cloud.husarion.com → Web IDE, and it works :

#include "hFramework.h"

void hMain()
{
	uint8_t data[] = {0xff, 0x01, 0x02, 0x03};

	hSens1.i2c.selectI2C();
	hSens1.i2c.setDataRate(100000); //bps
	while (1) {
		hSens1.i2c.write(0xA0, data, 4);
		sys.delay(100);//ms
	}
}

Please try to do this and let me know about results.

Hi Dominik2,

I’m changed the code in my library port, I changed the hI2C.h to hFramework.h and that seems to solve the problem.
However now I have the following error:

MCP4725.cpp:19:61: error: invalid conversion from ‘int’ to ‘uint8_t* {aka unsigned char*}’ [-fpermissive]
hSens1.i2c.write(_i2caddr, MCP4726_CMD_WRITEDACEEPROM, 1);

Hy Dominik2,

I think I have solved the problem!
Thanks for your help :slight_smile:

I thought I had it fixed, but when I only run the code provided, I don’t get any errors, but also don’t see any results when I attach a scope to the signals.

OK I figured out that my code works on the hExt I2C ports, but it won’t work in hSens1 and hSens2, is there some extra code I need to run?

Im using the following pinout:

Hi Ruben, a code for hSens1 and hSens2 should be ok. We use i2c from hSensor in this library: modules/MPU9250.cpp at master · husarion/modules · GitHub .

Maybe you have not used pull up resistors on SCL and SDA lines?

Hi Dominik,

I’m using the Sparkfun MCP4725 breakout board, with the pull-up’s enabled.
And on the hExt it works,
If I use the hSens1 and hSens2 ports, the leds go blinking mad.

“blinking mad” usually means CPU is in hard fault handler. Could you share your code with me, that results mad blinking?

Hey Dominik,

Offcourse you can see the files, here they are:
(The .c files should be without the .c extension, but otherwise the site wouldn’t let me upload them.)
(If I change hSens1 to hExt in the MCP4725.cpp file everything works fine. )

main.cpp (3.4 KB)
<a class=“attachment” href=“/uploaui.css.c (1.3 KB)
ui.html.c (1.2 KB)
ds/default/original/1X/e53f185f74709327ea7350d2e3514429ae5b97e0.h”>MCP4725.h (172 Bytes)
MCP4725.cpp (593 Bytes)MCP4725.h (172 Bytes)

Hi Ruben

Did you find a solution for this?

Michael

No I only got it working on the hExt port. So I changed my hardware design and didn’t try it anymore.

Thanks for the info.
One thing I found in your code maybe wrong.
You use hSens1.i2c.selectI2C(); as it is said in some examples. The correct call seems to be hSens1.selectI2C(); as mentioned here.
How do you use the I2C bus at hExt?

Aah ok Thanks.
That might be the problem.
I’m using:
void MCP4725::begin(uint8_t addr) {
_i2caddr = addr;
hExt.i2c.selectI2C();
hExt.i2c.setDataRate(400000);
}

And this worked on the hExt port.

Thanks for the code snippet. I will try and report the result.