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

Extracting BITs from BYTE

Discussion in 'Oshonsoft' started by camerart, Jun 20, 2017.

  1. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,366
    Likes:
    11
    Location:
    Dorset UK.
    Hi,
    I'm programming a PIC that will control an SX1278 radio module.
    I've come to a section where I read a BYTE then compare the BITs, see attached.
    How do I do this please?
    Camerart.
     

    Attached Files:

    • blob.jpg
      blob.jpg
      File size:
      125.7 KB
      Views:
      35
  2. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,218
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi C,
    You use the Bits location in a WORD or BYTE.
    eg:
    Dim Cam as Byte
    say Cam Byte = 40h, 01000000 [ B7 thru B0, Bit7 is the Most Significant Bit]]
    The '1' is at B6
    so to test use Cam.6

    If Cam.6 = 1 then
    'do something
    else
    'do some thing else
    endif

    A WORD has Bits 15 thru B0.

    Ok.??
    Eric

    EDIT:

    You can also SET a Bit in a Byte or Word.

    Cam.1 =1
    or
    Cam.7=0
     
    • Like Like x 1
  3. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,366
    Likes:
    11
    Location:
    Dorset UK.
    Hi E,
    Does this look ok to you.
    There are comments, I haven't got to yet, so only above the ??????????????????????????????????????
    EDIT: read with image at #1
    C.
     

    Attached Files:

    Last edited: Jun 21, 2017
  4. dave

    Dave New Member

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


     
  5. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,218
    Likes:
    644
    Location:
    Ex Yorks' Hants UK

    hi C,
    You are making the coding too verbose, trim it down a bit.
    E
    Image1.png
     
  6. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,218
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi,

    res1 =irqflags And irgflags.4

    Note that res1 after the AND, res1 will '00010000' is this what you really want.?
    Do you need an OR function ????
    E
     
  7. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,366
    Likes:
    11
    Location:
    Dorset UK.
    Hi E,
    I'm copying from notes I've been given, here's a section:

    if ((irqFlags & IrqFlags_ValidHeader) AND (NOT(irqFlags & IrqFlags_PayloadCrcError))) {
    sizerx = SPIRead(RxNbBytes())
    I asked about the & and AND in another thread, earlier. Oshonsoft requires the &ANDs to be done separately.

    C
     
  8. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,366
    Likes:
    11
    Location:
    Dorset UK.
    Hi,
    The program won't compile with this section:
    EDIT:I'm guessing, from reading the code, that the IRQFLAGS byte is READ, and if there is a VALIDHEADER BIT AND there is NOT a PAYLOADCRCERROR BIT then OK. (I'm not certain though!)
    C
     

    Attached Files:

    • blob.jpg
      blob.jpg
      File size:
      116.3 KB
      Views:
      31
    Last edited: Jun 21, 2017
  9. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,218
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi C,
    I will try it.
    E

    Hi C,
    Please post the code clips as text, I don't have time to keep translating from an image.:(

    E
     
  10. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,366
    Likes:
    11
    Location:
    Dorset UK.
    Hi E,
    I have been unable to post TXT files, this is why I posted images.
    Here is a test using Firefox
    Here's a test using Microsoft edge.
    Found the answer, I must use edge, sorry for the inconvenience:oops:
     

    Attached Files:

    Last edited: Jun 21, 2017
  11. sagor1

    sagor1 Member

    Joined:
    Dec 11, 2014
    Messages:
    52
    Likes:
    2
    C, the way I read the other code, is that you check for valid header = 1, and check that the CRC flag is = 0
    In simple terms, I would simply make a compound if/then/else. Like: (this example compiles ok). If any "IF" condition fails, it falls through to the "ENDIF" and "LOOP:"

    Dim irqflags As Byte
    'Read irqflags, but I set to zero for example
    irqflags = 0

    main:

    If irqflags.4 = 1 Then
    If irqflags.5 = 0 Then
    'do the reading code
    'rxsize = ....
    'goto loop
    Endif
    Endif

    loop:
    Goto main
    End

    You could check other flags as well, if necessary. Just keep building another "IF" in the chain until all necessary conditions are met, then read the data. I would check the RXDone (bit 6) as well, to be sure. I don't see why one would need extra variables and lots of "OR" or "AND" commands, simply check the condition of the result byte bit by bit.
    Bit testing is either a 1 or 0 result, no need for ">" type of testing. It is either =0 or =1
     
    Last edited: Jun 21, 2017
  12. sagor1

    sagor1 Member

    Joined:
    Dec 11, 2014
    Messages:
    52
    Likes:
    2
    To use your code, I would write it like this:
    (NOTE: The two variables for header and CRC must be defined as "Bit" type for this to work). Typical way to do this, is by using the "Symbol" declaration instead of a variable as a "Bit"
    IE:
    Symbol regirqflags_validheader = irqflags.4
    Symbol regirqflags_payloadcrcerror = irqflags.5

    and skip your two lines with 'Mask in them...

    If dio0 = 1 Then 'DIO0 goes high from SX1278 when data has been received and ready.
    irqflags = spi_read_reg_func(r_regirqflags, irqflags) 'CHECK SYNTAX (SUBROUTINE)
    regirqflags_validheader = irqflags.4 'Mask
    regirqflags_payloadcrcerror = irqflags.5 'mask

    If regirqflags_validheader = 1 Then
    If regirqflags_payloadcrcerror = 0 Then
    rxsize = spi_read_reg_func(r_regrxnbbytes, fiforxbytesnb)
    While x < rxsize
    read_reg_spi = spi_read_reg_func(r_regfiforxbaseaddr, fiforxbaseaddr)
    x = x + 1
    Wend 'IS THIS in the correct place?????????????????????
    Endif
    Endif
    Endif

    I'm assuming I have the right value of the bits. That is, 1 = valid header and 0 = no CRC error detected. I'm not looking at the rest of the logic you set up, not familiar with how your device actually works.
     
    Last edited: Jun 21, 2017
    • Agree Agree x 1
  13. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,080
    Likes:
    326
    Location:
    Brisbane Australia
    If you use code tags and indent your code it becomes much easier to read (and debug).
    I.E.
    Code (text):

    If dio0 = 1 Then 'DIO0 goes high from SX1278 when data has been received and ready.
      irqflags = spi_read_reg_func(r_regirqflags, irqflags) 'CHECK SYNTAX (SUBROUTINE)
      regirqflags_validheader = irqflags.4 'Mask
      regirqflags_payloadcrcerror = irqflags.5 'mask

      If regirqflags_validheader = 1 Then
        If regirqflags_payloadcrcerror = 0 Then
          rxsize = spi_read_reg_func(r_regrxnbbytes, fiforxbytesnb)
          While x < rxsize
            read_reg_spi = spi_read_reg_func(r_regfiforxbaseaddr, fiforxbaseaddr)
            x = x + 1
          Wend 'IS THIS in the correct place?????????????????????
        Endif
      Endif
    Endif
     
    As you can see from the above indenting, the Wend is in the correct place.

    BTW, I don't see where x is initialised.

    Mike.
     
  14. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,366
    Likes:
    11
    Location:
    Dorset UK.
    Hi P,
    If you look at port#9 you will see that I have had difficulty uploading files. I have managed to do what you say in the past, but they files were blocked by 'protection' I'll keep trying.
    C.
     
  15. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,366
    Likes:
    11
    Location:
    Dorset UK.
    Hi All,
    Thanks for your replies, all points noted, I will test them.
    My question here is regarding BITs from BYTES, and I will need to verify the results. I can only verify the results with a full program, that compiles, and programmed onto a PIC connected to the Radio module, so that the module can report the result on 'say' and LCD. I think it is best for me to post the complete program on the main thread here: http://www.electro-tech-online.com/...ion-receiving-using-scr-radio-modules.149198/ I haven't quite finished my version of the program, but I'll post it anyway, so that you can get a better idea.

    Thanks C.
     
    Last edited: Jun 22, 2017
  16. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,366
    Likes:
    11
    Location:
    Dorset UK.
    Hi S,
    Why did you say no need for ">" type of testing?
    C.
     
  17. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,218
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    Morning C,
    The '>' symbol is a greater than test, a BIT is either 0 or 1.
    E
     
  18. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,366
    Likes:
    11
    Location:
    Dorset UK.
    Morning E,
    I understand that the > sign is greater than, but why did S mention it in this context? Unless he was mixing up the < in that section?
    C.
     
  19. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,080
    Likes:
    326
    Location:
    Brisbane Australia
    You don't need to upload files to include formatted code. This is what I typed above to include the code,
    screen.png

    Note the code tags are just typed.

    Mike.
     
  20. camerart

    camerart Active Member

    Joined:
    Jun 12, 2008
    Messages:
    1,366
    Likes:
    11
    Location:
    Dorset UK.
    Hi M,
    Thanks, I've used my usual method, but with EDGE. follow the link in POST#14 and you'll see the full code including the answer to your comment " BTW, I don't see where x is initialised." and your WEND answer too.
    C.
     
  21. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,218
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi C,
    If want to avoid initialising 'simple' variables to zero within your program, tick the menu option.
    I always prefer code initialising.
    E
    A01.gif
     

Share This Page