MATLAB: How to select bits form 1 byte values for sensor

selecting values for sensor

The parameter to the Measurement command is 1 byte in length but consists of
two, 4-bit values: Concentration Unit (bits 7:4) and Mode (bits 3:0).
The response of sensor is '1100001'
The “concentration unit” (bits 7:4 of the measurement byte) can be either %LEL or
%VOL. By specifying the desired unit,
Unit Name Description
0x0 PERCENT_LEL Concentration reported as %LEL.
0x1 PERCENT_VOL Concentration reported as %VOL.
The measurement mode (bits 3:0 of the measurement byte) consists of the
following possible values:
MODE Name Description
0x2 MPS_CONT MPS operates in an autonomous, continuous mode
0x3 MPS_STOP Stop measurement – no measurements are being taken

Best Answer

Example:
>> a=uint8(132);
>> bitget(a,8:-1:5)
ans =
1×4 uint8 row vector
1 0 0 0
>> bitget(a,4:-1:1)
ans =
1×4 uint8 row vector
0 1 0 0
That is, the left-most of the 8 bits is bit number 8, and the right-most (least significant) is bit number 1. For your purposes you might want to code something like (7:-1:4)+1 to match the 7:0 numbering of your documentation.