cancel
Showing results for 
Search instead for 
Did you mean: 

i cant send data over i2c

Samuel1
Associate III

I wanted to use the MCP4725 12 bit DAC chip so I can generate analog voltage range of 0-5 V. Now I have 2 problems. My approach was as follows: first I checked communication with this showen below function.

if(HAL_I2C_IsDeviceReady(&hi2c1,0xC4, 2,10)==HAL_OK){ //0b11010000 slave adress

HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_13);

HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_14);

HAL_Delay(50);

}

The led has also blinked. Then I have defined a variable whose value is 0-4954 (12bit DAC). At the moment it is not so important to me how the variable can be changed. Then, according to the data sheet of the MCP4725, 3 bytes (in the normal mode) must be sent over 12c.

The first problem is when I write the date in bit format, the code can be compiled but not uploaded. But if I write this in hex format, it works. e.g. :

if (HAL_I2C_IsDeviceReady (& hi2c1,0b11000100, 2,10) == HAL_OK) { // 0xC4 slave adress in HEX

HAL_GPIO_TogglePin (GPIOB, GPIO_PIN_13);

HAL_GPIO_TogglePin (GPIOB, GPIO_PIN_14);

HAL_Delay (50);

}

2.Problem is that the code below does not work. I have just shifted MSB and LSB of data accordinng to the val:

i2c_send_data [0] = 0x40; // address of register in the to be written

// DAC 12 bit register

i2c_send_data [1] = 0x00 >> val; // bits 7-0

i2c_send_data [2] = 0x00 << val; // bits 7-4

// slave adress second para.,

HAL_I2C_Master_Transmit (hi2c1,0xC4, i2c_send_data, 3.10);

  }

and of course the variables defined above:

uint8_t i2c_send_data [3];

uint16_t val = 2000;

Can someone help please?

this is the whole code

6 REPLIES 6
Samuel1
Associate III

this is the code

Mon2
Senior III

Please review the I2C address for this MCP4725 slave. Believe it may be 0x62 or 0x63 rather than 0xc4 (varies with the strapping of A0 on this chip).

Also, be sure that you have the required 2k-10k pull-up resistors on the I2C SCL & SDL lines since the bus is open drain style of interface.

See here:

https://learn.adafruit.com/mcp4725-12-bit-dac-tutorial/wiring

  • so depending on your strapping of A0 of this IC, I2C slave address could be 0x62 OR 0x63

https://www.microchip.com/forums/m874535.aspx

and this code example:

https://www.stm32duino.com/viewtopic.php?t=1048

https://community.st.com/s/question/0D50X00009XkW8USAV/stm32f103rb-i2c-with-arduino-ide

S.Ma
Principal

Get an oscilloscope and look at the I2C lines to find out what's going on.

For debug, use the forum shared I2C Master code using SW with GPIO to check HW and device functionality.

sorry but 0xC4 is already correct. The address is indeed

0b0110 0001 == 0x61,But the HAL function is includedalso R / W bit and this has to be shifted to the left. and then 0 Bit for master write so : 0b1100 0100 (0 Master Write) = 0xC4 and that's has been successfully tested with this function:

if (HAL_I2C_IsDeviceReady (& hi2c1,0b11000100, 2,10) == HAL_OK) { // 0xC4 slave adress in HEX

HAL_GPIO_TogglePin (GPIOB, GPIO_PIN_13);

HAL_GPIO_TogglePin (GPIOB, GPIO_PIN_14);

HAL_Delay (50);

}

but I think that my mistake is the bitmasking here:

i2c_send_data[0]=0x40; // Adresse von Register in den zu schreibende

// DAC 12 bit Register

i2c_send_data[1]=0x00>>val; // bits 7-0

i2c_send_data[2]=0x00<<val; // bits 7-4

HAL_I2C_Master_Transmit(&hi2c1,0xC4,i2c_send_data,3,10);

thanks

@Samuel​ ,

1) if 0xC4 is working with the HAL call then your I2C address of the IC is

0b0110 0010 == 0x62.

This is because 0b0110 0010 == 0x62 left shifted by 1 bit = 0b1100 0100 = 0xC4.

2) Your hunch on the error is in the listed region of the code.

i2c_send_data[0]=0x40; // Adresse von Register in den zu schreibende
 
// DAC 12 bit Register
 
i2c_send_data[1]=0x00>>val; // bits 7-0 - BUT val = 2000
 
i2c_send_data[2]=0x00<<val; // bits 7-4 BUT val = 2000

So, send_data[1] = 0x00 = 0b0000 0000 to start with.

Now take this value 0x00 and RIGHT shift this value 2,000 times. What is the result?

It should be 0x00.

Repeat with send_data[2] = 0x00. Same end result.

Something is wrong with this logic which you will have to study.

In the meantime, try again with:

i2c_send_data[0]=0x40; // Adresse von Register in den zu schreibende
 
// DAC 12 bit Register
 
i2c_send_data[1]=0xff;
i2c_send_data[2]=0xf0;

As I do not know this IC controller you are using, you may or may not require the permanent loop of the while(1) construct. Test it and post your results.

>> This is because 0b0110 0010 == 0x62 left shifted by 1 bit = 0b1100 0100 = 0xC4.

that's right. 0x62 is the right address. and it works as expected. Thanks