Hi Everyone,

I'm doing a little CAN display project. As part of it, the equipment I'm wanting to get information out of uses a couple of bytes in a frame for the status of some features on the equipment.

For example, on frame 14, bytes 6-7 so 16bits total.
Bits 15,14,13 show a status of the AntiLag. Bits 12,11 for Launch Control etc.

Reason for up to 4 bits is there can be up to 7 states a feature is in.

Now what I'm having issues with is grouping the bits and getting them to a decimal output so I can then output a return text to the screen depending on the decimal of those bits.

At the moment, I'm using bitread() fine to display a single bit, but I need to group together multiple bits into a data type where I can then use it.

I've tried word() but that isn't workable with bitread, I'm just wondering if there is something simple I'm missing.

Here is the code I'm currently using

``````word STATUS1 = 0;
int STATUS2 = 0;

void loop() {
unsigned int canId = CAN0.getCanId();                         // Check CAN ID

if ((canId == 0x03E8) && (rxBuf == 13)) {
STATUS1 = word(rxBuf, rxBuf);
}
display.print(STATUS1, BIN);   // returns full 16bit binary ok
display.print(STATUS2);   // returns 0
``````

I've tested printing the return of a bitRead() by itself, and I do get the correct bit.
I know word isn't correct here but what can I use to combine these 3 bits and give me the decimal?

Bits 15,14,13 show a status of the AntiLag. ...there can be up to 7 states a feature is in.
what can I use to combine these 3 bits and give me the decimal?

STATUS2 will be a byte and you can construct it like this

Using the power of 2 multiplication equivalent to the bitshifts the combined value can also be written

The compiler treats them the same, so you can use whatever you understand the best.

Thanks cattledog, the multiplication way did what I wanted.

3bits combined = 010
returned 2 decimal.

a simple bit mask and shift?
shift to where you need your bits.

``STATUS2 = (STATUS1 & 0b1110000000000000) >> 13;``

Welcome,

You can shift N bits to the right, and mask with the bits that you want to keep

For example if you have a byte and want bits 3 to 6, you right-shift by 3 and mask with 0b1111

``````   0b11001010
>> 3
= 0b00011001
& 0b00001111
= 0b00001001
``````

Another example with a 16-bits value from which you want only bits 11 and 12.
Right-shift by 11 and mask with 0b11 :

``````   0b1101011110000111
>> 11
= 0b0000000000011010
& 0b0000000000000011
= 0b0000000000000010
``````

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.