1. 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.
    Dismiss Notice

Adding HEX bits in a byte.

Discussion in 'Mathematics and Physics' started by camerart, Apr 17, 2017.

  1. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,368
    Likes:
    11
    Location:
    Dorset UK.
    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.
     

    Attached Files:

  2. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,220
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    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
     
  3. JimB

    JimB Super Moderator Most Helpful Member

    Joined:
    Sep 11, 2004
    Messages:
    6,356
    Likes:
    586
    Location:
    Peterhead, Scotland
    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
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,368
    Likes:
    11
    Location:
    Dorset UK.

    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.
     
  6. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,251
    Likes:
    911
    Location:
    Rochdale UK
    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..
     
  7. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,368
    Likes:
    11
    Location:
    Dorset UK.
    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.


     
  8. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,368
    Likes:
    11
    Location:
    Dorset UK.
    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?
     

    Attached Files:

    Last edited: Apr 24, 2017
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,251
    Likes:
    911
    Location:
    Rochdale UK
    Define the bits ..

    PA = 128
    MaxPower = 64
    OutputPower = 7;

    then set what you need..

    RegPaConfig = PA + MaxPower + OutputPower
     
  10. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,368
    Likes:
    11
    Location:
    Dorset UK.
    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.
     
  11. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,251
    Likes:
    911
    Location:
    Rochdale UK
    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....
     
  12. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,368
    Likes:
    11
    Location:
    Dorset UK.
    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: Apr 17, 2017
  13. Dr_Doggy

    Dr_Doggy Well-Known Member

    Joined:
    Aug 11, 2007
    Messages:
    1,718
    Likes:
    37
    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);
     
  14. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,251
    Likes:
    911
    Location:
    Rochdale UK
    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..
     
  15. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,368
    Likes:
    11
    Location:
    Dorset UK.
    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.
     
  16. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,368
    Likes:
    11
    Location:
    Dorset UK.
    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.
     
  17. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,220
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    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
     
  18. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,368
    Likes:
    11
    Location:
    Dorset UK.
    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.
     
  19. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,368
    Likes:
    11
    Location:
    Dorset UK.
    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
     
  20. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,220
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    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
     
  21. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,368
    Likes:
    11
    Location:
    Dorset UK.
    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
     

Share This Page