Why is this not working ??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-10-08 4:43 AM
Hi guys
I have a real strange issue
trying to move two 8bits values in to one 16bit variable,
can someone please tell me why this does not work
It should OR the lsb to the msb
Sensor_Flow = I2C1_DATA[0] ;
Sensor_Flow = Sensor_Flow << 8;
Sensor_Flow = Sensor_Flow || I2C1_DATA[1];
thanks in addvance
Solved! Go to Solution.
- Labels:
-
I2C
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-10-08 7:25 AM
Simpler, less prone to be wrong..
Sensor_Flow = ((uint16_t)I2C1_DATA[0] << 8) + (uint16_t)I2C1_DATA[1];
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-10-08 4:55 AM
Hi,
you mixed up || (logical OR) and | (binary OR), please try :
Sensor_Flow = Sensor_Flow | I2C1_DATA[1];
assuming I2C1_DATA[ ] is defined as bytes array, otherwise need to mask to get only LSByte (i.e. I2C1_DATA[x] & 0xFF) .
Regards,
In order to give better visibility on the answered topics, please click on 'Select as Best' on the reply which solved your issue or answered your question. See also 'Best Answers'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-10-08 4:56 AM
if sensor_flow is uint16_t and I2C_DATA is a uint8_t or char array....
you could also try
memcpy(&Sensor_Flow, &I2C_DATA[0],2);//2=2bytes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-10-08 5:51 AM
Thanks guys
Just coming back to C after a long break,
Doh back to the drawing board :(
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-10-08 6:10 AM
Hi guys
don't think mem copy will work as my first byte of data [0] is the msb and byte 2 [1] is lsb
It create the 16bit data variable with the byte order wrong
though I like the simplistic approach
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-10-08 7:25 AM
Simpler, less prone to be wrong..
Sensor_Flow = ((uint16_t)I2C1_DATA[0] << 8) + (uint16_t)I2C1_DATA[1];
Up vote any posts that you find helpful, it shows what's working..
