• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Adding HEX bits in a byte.

camerart

Active Member
Thread starter #1
Hi,
I need to change REGISTERS with different settings, see attachment.

What is the easiest way of 'say' adding the MODE 0x01 with the Lowfrequencymodeon 0x01? I know it's not 0x01 + 0x01 = 0x2, and should be = 0x09.

Camerart.
 

Attachments

ericgibbs

Well-Known Member
Most Helpful Member
#2
Afternoon C,
I am not quite sure what you are asking.
Is it for example: a 8 bit Byte Named Mode, 00100010 .? and you want to make say bit 2 =1, you could use Mode.2=1

E
 

JimB

Super Moderator
Most Helpful Member
#3
To me there is something very confusing about that table, but I think what you want is:

To set Standby mode and to set Lowfrequencymodeon

Standby mode is bit 0 of the byte ie 0x01

Lowfrequencymodeon is bit 3 of the byte ie 0x08

To set both of these bits you need to do a logical AND operation.

Lowfrequencymodeon AND Standby mode
0x08 AND 0x01 = 0x09

JimB
 

camerart

Active Member
Thread starter #4
Afternoon E and J,
I am able to work it out, but I am trying to find the best way, especially as there are many registers, with various combinations of bits in each byte and variable number of bits in each section.

In J's reply he correctly shows that bit 3 is 0x08, but it it shown as 0x01. I can usually see what the lower bits should be, but 'say' bits 6-5 need one bit changing, how would I do that?

I am able to enter each bit into the calculator as binary and then output a HEX number, is this how it's done? This is a bit long winded though

C.
 

camerart

Active Member
Thread starter #6
Hi I,
There maybe more than 1 bit to change in a byte.

I'm asking what method you use? Do you use a calculator or memory?
C.


When you have a single register and each bit is on or off and you need to turn one on You don't "add" you "or"

If you need to change a bit in a byte you XOR it..
 

camerart

Active Member
Thread starter #7
Hi,
I'll try to clarify the question! (Watch out for my mistakes)

There are over 100 pages of DATA sheet pages x number of SETTINGS on a page, but not all need to be set.

The example I've posted RegOpMode is REG 0x01. LORA bit 7 = 1. Low frequency is Bit 3 = 1 TX is bits 0 and 1 = 3 Therefore (RegOpMode) 0x01 = 0xab

To change to RX, then (RegOpMode) 0x01 = 0x8d

What actual method do you all use?

EDIT: Just added another REGISTER image, how would you set this one, it doesn't show each bit?
 

Attachments

Last edited:

camerart

Active Member
Thread starter #9
Define the bits ..

PA = 128
MaxPower = 64
OutputPower = 7;

then set what you need..

RegPaConfig = PA + MaxPower + OutputPower
Hi I,
I think you're misunderstanding what I'm asking. When you looked at each example, how did you find the 3 settings. Are you so practiced that you know them, or did you use some equipment, i,e, a calculator or reference sheet?

I find it difficult, and it it takes me a lot of time, and I hope to find a better way than the way I use, which is convert each section into binary and enter each bit as I go along into a calculator which outputs the HEX, which I enter into the code.

C.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
#10
Ive been using hex now for soooo long I know the bit weights of by heart... If you don't care for bit weights... Just try and use hex or binary... Most systems have data sheets with all the settings in... You just need to create the definitions in a file.

PA = b10000000
MaxPower = b01000000
OutputPower = b00000111

Just the same....
 

camerart

Active Member
Thread starter #11
Ive been using hex now for soooo long I know the bit weights of by heart... If you don't care for bit weights... Just try and use hex or binary... Most systems have data sheets with all the settings in... You just need to create the definitions in a file.

PA = b10000000
MaxPower = b01000000
OutputPower = b00000111

Just the same....
Hi I,
What are bit weights? Will they be able to help me add the HEX's together as they are shown in the examples?
C.
 
Last edited:

Dr_Doggy

Well-Known Member
#12
going back to initial post if you do them separate you command it separate:
MODE = 0x01 ;
Lowfrequencymodeon = 0x01;

if you wrap it together then you need more like as below,
but then you can't call them by the bit names like MODE and Lowfrequencymodeon:
regopmode = (Lowfrequencymodeon<<3) + (MODE);
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
#13
What are bit weights?
We work with computers.... Micro's are essentially the same... Binary is the language of the computer..

you have seen the bit weights..... 128, 64, 32, 16, 8, 4, 2 and 1... Each bit carries a decimal "weight" these are pretty well know, Hexadecimal is binary shorthand.... If you can work well with Hex and Bin, your computer programming will be 100% better..
 

camerart

Active Member
Thread starter #14
We work with computers.... Micro's are essentially the same... Binary is the language of the computer..

you have seen the bit weights..... 128, 64, 32, 16, 8, 4, 2 and 1... Each bit carries a decimal "weight" these are pretty well know, Hexadecimal is binary shorthand.... If you can work well with Hex and Bin, your computer programming will be 100% better..
Hi I,
I am familiar with the bit weight list you post. I have a list of equivalent BIN, HEX and DEC, which I find very useful.

I wondered if the HEXs in each section could be ANDed or ADDED using a kind of 'weight' or similar by using the BIT number.

I think the following post looks like a promising way to go.
C.
 

camerart

Active Member
Thread starter #15
going back to initial post if you do them separate you command it separate:
MODE = 0x01 ;
Lowfrequencymodeon = 0x01;

if you wrap it together then you need more like as below,
but then you can't call them by the bit names like MODE and Lowfrequencymodeon:
regopmode = (Lowfrequencymodeon<<3) + (MODE);
Hi Doc,
I am using existing programs from other sources written in languages, I can barely follow, but I see lines written in the way you suggest. They have a list separated by commas, with the list shown in the REG e,g, SX1276SetOpMode(OpMode_ModeLoRa|OpMode_LowFrequencyModeOn|OpMode_ModeSleep), (I know that |=OR)

I'll have to ask if this can be used in the Oshonsoft section.
C.
 

ericgibbs

Well-Known Member
Most Helpful Member
#16
hi C,
The '|' is not recognised in OSH, in your eg: list replace the '|' with OR
OSH is very limited so it may burp.
e,g, SX1276SetOpMode(OpMode_ModeLoRa OR OpMode_LowFrequencyModeOn OR OpMode_ModeSleep)

E
 

camerart

Active Member
Thread starter #17
hi C,
The '|' is not recognised in OSH, in your eg: list replace the '|' with OR
OSH is very limited so it may burp.
e,g, SX1276SetOpMode(OpMode_ModeLoRa OR OpMode_LowFrequencyModeOn OR OpMode_ModeSleep)

E
Morning Eric,
I was just about to post in Oshonsoft.

I assume that I use sections like OpMode_ModeSleep....OpMode_ModeTX, then change the list accordingly through the program as I need to change the settings?

Am I correct that he OR will join the different section HEXs as my question?

I'm not keen on 'Burping' but if it looks as though it's working, would you say it will be reliable?
C.
 

camerart

Active Member
Thread starter #18
Hi,
Just tried this in Oshonsoft:

Const lrm = 0x00
Const mt = 0x00
Const res = 0x0
Const lfmo = 0x01
Const modestdby = 0x01

Dim regopmode As Byte
regopmode = lrm Or mt Or res (Here it said too many arguments) I tried the full list and the same answer :(

With only two it looked ok.
EDIT: In the RegOpMode list it shows Reserved as 0x0 is this correct? What happens if I put 0x00?
C
 

ericgibbs

Well-Known Member
Most Helpful Member
#19
hi C,
Perhaps a simple OR example will make clearer.

You know that a BCD '1' is 00000001 0r 0x01 , we cannot display this using a LCD, which requires ASCII characters.

So to turn this into a ASCII number '1' we OR the 0x01 with 0x30 which makes it 0x31 which is a ASCII '1' character, which we can display.

The 0x01 in BIN is 00000001 and the 0x30 is 00110000 and when they a ORed together give 00110001 .

Do that make sense to you.?

E

Have you tried
Dim regopmode As Byte
regopmode = lrm Or mt
regopmode= regopmode OR res1 :::: you cannot use the 'res', its part of the compiler language, choose say res1
 

camerart

Active Member
Thread starter #20
hi C,
Perhaps a simple OR example will make clearer.

You know that a BCD '1' is 00000001 0r 0x01 , we cannot display this using a LCD, which requires ASCII characters.

So to turn this into a ASCII number '1' we OR the 0x01 with 0x30 which makes it 0x31 which is a ASCII '1' character, which we can display.

The 0x01 in BIN is 00000001 and the 0x30 is 00110000 and when they a ORed together give 00110001 .

Do that make sense to you.?

E

Have you tried
Dim regopmode As Byte
regopmode = lrm Or mt
regopmode= regopmode OR res1 :::: you cannot use the 'res', its part of the compiler language, choose say res1
Hi Eric,
You may recall that I've been using OR 80 in my SX1278 programs to switch the WRITE bit on when writing the registers, so I am kind of familiar with ORs

I have just tried your suggestion, which works, and verifies with a calculator however the answer has puzzled me.

Using the settings in RegOpMode I posted.

Dim regopmode As Byte
Const longrangemode = 0x01
Const modulationtype = 0x00
Const reserved1 = 0x0
Const lowfrequencymodeon = 0x01
Const modestdby = 0x01

regopmode = longrangemode Or modulationtype
regopmode = regopmode Or reserved1
regopmode = regopmode Or lowfrequencymodeon
regopmode = regopmode Or modestdby

If the longrangemode bit is changed to 0x01 the result is the same as if it is 0x00 Result 0x01. So the RegOpMode Register has not been changed.

C
 

Latest threads

EE World Online Articles

Loading

 
Top