• 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.

Extracting BITs from BYTE

camerart

Active Member
Thread starter #1
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.
 

Attachments

ericgibbs

Well-Known Member
Most Helpful Member
#2
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
 

camerart

Active Member
Thread starter #3
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
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.
 

Attachments

Last edited:

ericgibbs

Well-Known Member
Most Helpful Member
#4
hi C,
You are making the coding too verbose, trim it down a bit.
E
Image1.png
 

ericgibbs

Well-Known Member
Most Helpful Member
#5
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
 

camerart

Active Member
Thread starter #6
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
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
 

camerart

Active Member
Thread starter #7
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
 

Attachments

Last edited:

ericgibbs

Well-Known Member
Most Helpful Member
#8
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
 

camerart

Active Member
Thread starter #9
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
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:
 

Attachments

Last edited:
#10
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:
#11
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:

Pommie

Well-Known Member
Most Helpful Member
#12
If you use code tags and indent your code it becomes much easier to read (and debug).
I.E.
Code:
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.
 

camerart

Active Member
Thread starter #13
If you use code tags and indent your code it becomes much easier to read (and debug).
I.E.
Code:
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.
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.
 

camerart

Active Member
Thread starter #14
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:

ericgibbs

Well-Known Member
Most Helpful Member
#16
Morning C,
The '>' symbol is a greater than test, a BIT is either 0 or 1.
E
 

Pommie

Well-Known Member
Most Helpful Member
#18
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.
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.
 

camerart

Active Member
Thread starter #19
You don't need to upload files to include formatted code. This is what I typed above to include the code,
View attachment 106686

Note the code tags are just typed.

Mike.
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.
 

ericgibbs

Well-Known Member
Most Helpful Member
#20
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
 

Latest threads

EE World Online Articles

Loading

 
Top