Pages: [1]
Author Topic: Setting Bits of a Byte  (Read 5711 times)
phila_dot
Hero Member
*****

Karma: +172/-11
Offline Offline

Posts: 1709


« 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
« Last Edit: September 28, 2011, 02:48:30 PM by phila_dot » Logged
nyet
Administrator
Hero Member
*****

Karma: +604/-166
Offline Offline

Posts: 12234


WWW
« Reply #1 on: September 28, 2011, 02:41:10 PM »

Can we do this in hex Smiley

The pattern is a bit more obvious ...

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

ME7.1 tuning guide (READ FIRST)
ECUx Plot
ME7Sum checksum checker/corrrector for ME7.x

Please do not ask me for tunes. I'm here to help people make their own.

Do not PM me technical questions! Please, ask all questions on the forums! Doing so will ensure the next person with the same issue gets the opportunity to learn from your experience.
phila_dot
Hero Member
*****

Karma: +172/-11
Offline Offline

Posts: 1709


« Reply #2 on: September 28, 2011, 03:00:44 PM »

Can we do this in hex Smiley

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.
Logged
nyet
Administrator
Hero Member
*****

Karma: +604/-166
Offline Offline

Posts: 12234


WWW
« Reply #3 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)
« Last Edit: September 29, 2011, 07:36:06 PM by nyet » Logged

ME7.1 tuning guide (READ FIRST)
ECUx Plot
ME7Sum checksum checker/corrrector for ME7.x

Please do not ask me for tunes. I'm here to help people make their own.

Do not PM me technical questions! Please, ask all questions on the forums! Doing so will ensure the next person with the same issue gets the opportunity to learn from your experience.
phila_dot
Hero Member
*****

Karma: +172/-11
Offline Offline

Posts: 1709


« Reply #4 on: September 29, 2011, 07:21:31 PM »

Ahh...I see.

Binary to hex to decimal is still a little abstract to me.
Logged
nyet
Administrator
Hero Member
*****

Karma: +604/-166
Offline Offline

Posts: 12234


WWW
« Reply #5 on: September 29, 2011, 07:36:32 PM »

You'll get it Smiley

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

ME7.1 tuning guide (READ FIRST)
ECUx Plot
ME7Sum checksum checker/corrrector for ME7.x

Please do not ask me for tunes. I'm here to help people make their own.

Do not PM me technical questions! Please, ask all questions on the forums! Doing so will ensure the next person with the same issue gets the opportunity to learn from your experience.
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Page created in 0.016 seconds with 16 queries. (Pretty URLs adds 0s, 0q)