2021-06-28 10:53 AM
So I am trying to send an ADC value that I receiving on the board over I2C to another device.
I have adc_Value as a 32 bit one since the ADC read function on HAL is 32 bits.
I want to send this over i2c.
Currently I have this.
HAL_ADC_Start(&hadc1); // starts the adc
HAL_ADC_PollForConversion(&hadc1, 1); //
// HAL_Delay(2000);
ADC_Value = HAL_ADC_GetValue(&hadc1);
HAL_I2C_Master_Transmit(&hi2c4, slave_addr, (uint8_t*)ADC_Value, 4, 50); // issue with (uint8_t *)
so I was confused about the casting here, I know that they want to send 8 bits but I was wondering since you can select the size as 4 bytes if it would send all 4.
I was thinking then of splitting my adc value into 4 bytes then just sending them one at a time.
Am i able to just send 32bits over I2c?
2021-06-28 11:49 AM
> HAL_I2C_Master_Transmit(&hi2c4, slave_addr, (uint8_t*)ADC_Value, 4, 50); // issue with (uint8_t *)
Almost. Assuming ADC_Value is a uint32_t, you want to pass the address of it, not its actual value.
HAL_I2C_Master_Transmit(&hi2c4, slave_addr, (uint8_t*) &ADC_Value, 4, 50);
The casting is needed because the parameter expects a pointer to uint8_t and not uint32_t, and pointers cannot be implicitly converted between those two.
Edit: Also, the ADC value is probably packed into the lower 2 bytes (or possibly only one byte), and data is stored in little endian format, so only sending the first 1 or 2 bytes instead of all 4 will suffice. So just make ADC_Value a uint16_t.
2021-06-28 12:26 PM
Oh ok, yeah most of that makes sense, I chose ADC_Value to be 32 because HAL_read adc returned a 32 bit. So the 4 in the parameters will send 4 bytes from the pointers, so in my cause I will only need to do 2 after i change adc_value to 16 bit. So then I just need to make sure that my receiving program will read 2 bytes then. Also if I am not wrong I belive ( uint8_t * ) takes care of the pointer and another alternative is (uint8_t) &.( this is something I need to double check).
2021-06-28 12:36 PM
> Also if I am not wrong I belive ( uint8_t * ) takes care of the pointer and another alternative is (uint8_t) &.( this is something I need to double check).
Neither of those will do what you want. The former will compile without errors, but that doesn't mean it'll work.
2021-06-28 12:43 PM
you might probably be right.