i cant send data over i2c
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
