Hello everyone
I have the following problem and I can't figure out how to do it
to program a chip I have to change the settings of its registers for example: its basic setting is given by the number 0x 006f0ced and I have to change it to 0x006f0cec, since the values to be changed are from bit 6 to bit 15 I had thought of transforming the number into many nibbles, changing the necessary bits and then recomposing it into hex, is it possible or is there a better method?
convert hex to nibble and vice versa
-
- Posts: 113
- http://meble-kuchenne.info.pl
- Joined: Thu Dec 10, 2020 5:40 pm
- Been thanked: 10 times
-
- Valued Contributor
- Posts: 1685
- Joined: Thu Dec 03, 2020 10:57 am
- Has thanked: 374 times
- Been thanked: 583 times
Re: convert hex to nibble and vice versa
Hi
Hexadecimal is just a way to make binary a bit easier to read for us humans, but as far as the MCU is concerned it's all binary.
0x 006f0ced = 0000 0000 0110 1111 0000 1100 1110 1101 (note I spaced every four bits and coloured bits 6-15 for clarity).
If you read your register then performed an AND with 0xFFFF801F you would get a value with bits 6-15 all zero with the remaining bits unchanged. You could then OR this value with your new bit 6-15 values to give a new register value
0000 0000 1101 1110 0101 1100 0010 0010 (0xDE5C22) = Original register value
AND
1111 1111 1111 1111 1000 0000 0001 1111 (0xFFFF801F) This would effectively set bits 6 - 15 to 0 whilst leaving the rest alone
0000 0000 1101 1110 0000 0000 0000 0010 (0xDE0002) = Original register with bits 6-15 now zero
OR
0000 0000 0000 0000 0110 1010 1110 0000 (0x6AE0) = New bits 6-15 value
0000 0000 1101 1110 0110 1010 1110 0010 (0xDE6AE2) = New register value
Hope this helps
Edit...
Re-reading your post you may have meant the bits representing the values of 6 - 15 (bits 1-4). If so principle is the same but this time AND with 0xFFFFFFF0 which will set the first four bits to zero leaving the rest untouched, then OR with whatever your new value is (bits 1-4).
reg = original value
x = the value to AND with
y = the new "bits"
reg = reg AND x // (0xfffffff0) clearing first four bits
reg = reg OR y // sets the new first four bit value
Hexadecimal is just a way to make binary a bit easier to read for us humans, but as far as the MCU is concerned it's all binary.
0x 006f0ced = 0000 0000 0110 1111 0000 1100 1110 1101 (note I spaced every four bits and coloured bits 6-15 for clarity).
If you read your register then performed an AND with 0xFFFF801F you would get a value with bits 6-15 all zero with the remaining bits unchanged. You could then OR this value with your new bit 6-15 values to give a new register value
0000 0000 1101 1110 0101 1100 0010 0010 (0xDE5C22) = Original register value
AND
1111 1111 1111 1111 1000 0000 0001 1111 (0xFFFF801F) This would effectively set bits 6 - 15 to 0 whilst leaving the rest alone
0000 0000 1101 1110 0000 0000 0000 0010 (0xDE0002) = Original register with bits 6-15 now zero
OR
0000 0000 0000 0000 0110 1010 1110 0000 (0x6AE0) = New bits 6-15 value
0000 0000 1101 1110 0110 1010 1110 0010 (0xDE6AE2) = New register value
Hope this helps
Edit...
Re-reading your post you may have meant the bits representing the values of 6 - 15 (bits 1-4). If so principle is the same but this time AND with 0xFFFFFFF0 which will set the first four bits to zero leaving the rest untouched, then OR with whatever your new value is (bits 1-4).
reg = original value
x = the value to AND with
y = the new "bits"
reg = reg AND x // (0xfffffff0) clearing first four bits
reg = reg OR y // sets the new first four bit value
Re: convert hex to nibble and vice versa
Ok thanks chipfryer 27
the first example you gave is my case, I tried your example with the calculator and the result is correct, so 0xFFFF801F is a fixed value to be able to reset the bits from 6 to 15, correct?
thank you very much
the first example you gave is my case, I tried your example with the calculator and the result is correct, so 0xFFFF801F is a fixed value to be able to reset the bits from 6 to 15, correct?
thank you very much
-
- Valued Contributor
- Posts: 1685
- Joined: Thu Dec 03, 2020 10:57 am
- Has thanked: 374 times
- Been thanked: 583 times