# Extracting BITs from BYTE

#### camerart

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

• 125.7 KB Views: 61

#### ericgibbs

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

• 128 KB Views: 59
Last edited:

#### ericgibbs

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

#### ericgibbs

##### Well-Known Member
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
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:

I asked about the & and AND in another thread, earlier. Oshonsoft requires the &ANDs to be done separately.

C

#### camerart

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

• 116.3 KB Views: 58
Last edited:

#### ericgibbs

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

#### Attachments

• 30.9 KB Views: 62
• 664 bytes Views: 59
Last edited:

#### sagor1

##### Member
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
'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:

#### sagor1

##### Member
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:

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)

While x < rxsize
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
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)

While x < rxsize
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
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)

While x < rxsize
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
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:

#### camerart

##### Active Member
Bit testing is either a 1 or 0 result, no need for ">" type of testing. It is either =0 or =1
Hi S,
Why did you say no need for ">" type of testing?
C.

#### ericgibbs

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

#### camerart

##### Active Member
Morning C,
The '>' symbol is a greater than test, a BIT is either 0 or 1.
E
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.

#### Pommie

##### Well-Known Member
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,

Note the code tags are just typed.

Mike.

#### camerart

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