NefMoto

Technical => Tuning => Topic started by: phila_dot on September 28, 2011, 02:37:14 PM



Title: Setting Bits of a Byte
Post by: phila_dot on September 28, 2011, 02:37:14 PM
I apologize in advance if my description is not completely correct, but the method is correct.

Generally speaking, a byte is made up of 8 bits. The decimal value of a byte is the sum of its bits (in decimal). A bit only has two possible values in binary, 0 and 1.
In order to set the individual bits of a byte you will need to determine the decimal value of the bit that you want to set.

BV = binary value (0 or 1)

Bit       7              6              5               4              
Byte = BV*2^7 + BV*2^6 + BV*2^5 + BV*2^4 +         
           BV*128 + BV*64    + BV*32   + BV*16   +

Bit       3              2              1               0
Byte = BV*2^3 + BV*2^2 + BV*2^1 + BV*2^0  
           BV*8     + BV*4     + BV*2     + BV*1


Example: CWKONLS

            bit 7 6 5 4 3 2 1 0
               +---------------+
CWKONLS |x x x x x x x x|
               +---------------+
               ^ ^ ^ ^ ^ ^ ^ ^
                |  |  |  |  |  |  | +-- B lsv
                |  |  |  |  |  | +---- B lsh
                |  |  |  |  | +------ B ls3
                |  |  |  | +-------- B ls4
                |  |  |  +---------- B lsv2
                |  |  +------------ B lsh2
                |  +-------------- B ls32
                +---------------- B ls42


For front O2 sensors you need to set bit 0 (B_lsv) and bit 4 (B_lsv2):

bit 0 = 1*2^0 = 1*1  = 1
bit 4 = 1*2^4 = 1*16 = 16

CWKONLS = 17

For rear O2 sensors you need to set bit 1 (B_lsh) and bit 5 (B_lsh2):

bit 1 = 1*2^1 = 1*2  = 2
bit 5 = 1*2^5 = 1*32 = 32

CWKONLS = 34

For all four sensors:

CWKONLS = 51


Title: Re: Setting Bits of a Byte
Post by: nyet on September 28, 2011, 02:41:10 PM
Can we do this in hex :)

The pattern is a bit more obvious ...

17 = 0x11
34 = 0x22
51 = 0x33 = (0x11 | 0x22)


Title: Re: Setting Bits of a Byte
Post by: phila_dot on September 28, 2011, 03:00:44 PM
Can we do this in hex :)

The pattern is a bit more obvious ...

17 = 0x11
34 = 0x22
51 = 0x33 = (0x11 | 0x22)

Can you explain further?

How can you calculate the on/off value of a bit of a byte in hex?

This is something that was unclear to me for a while so I thought it may help out others.


Title: Re: Setting Bits of a Byte
Post by: nyet on September 28, 2011, 03:15:39 PM
2^n in "programmer" is 1<<n (1 upshifted by n bits)

1<<0 = 0000.0001b = 0x01
1<<1 = 0000.0010b = 0x02
1<<2 = 0000.0100b = 0x04
1<<3 = 0000.1000b = 0x08
1<<4 = 0001.0000b = 0x10 = 16
1<<5 = 0010.0000b = 0x20 = 32
1<<6 = 0100.0000b = 0x40 = 64
1<<7 = 1000.0000b = 0x80 = 128

0x11 = 1*16 + 1*1 = 17
0x22 = 2*16 + 2*1 = 32 + 2 = 34
0x33 = 3*16 + 3*3 = 48 + 9 = 57

"|" is arithmetic or

0x01 | 0x02 = 0000.0001b | 0000.0010b = 0000.0011b = 3
0x10 | 0x20 = 0001.0000b | 0010.0000b = 0011.0000b = 0x30
0x11 | 0x22 = 0001.0001b | 0010.0010b = 0011.0011b = 0x33

ETA: separated every 4 bits with a "." to show "nibble" boundaries. A "nibble" is 4 bits, or a single hex digit. It has a value from 0-16 (0x0-0xf)


Title: Re: Setting Bits of a Byte
Post by: phila_dot on September 29, 2011, 07:21:31 PM
Ahh...I see.

Binary to hex to decimal is still a little abstract to me.


Title: Re: Setting Bits of a Byte
Post by: nyet on September 29, 2011, 07:36:32 PM
You'll get it :)

Just stare at the above post until you see the regular patterns.