How do I check (or set) a group of consecutive bits within a byte?


Closed Thread
Results 1 to 14 of 14
  1. #1
    Join Date
    Mar 2009
    Posts
    653

    Default How do I check (or set) a group of consecutive bits within a byte?

    I know how to check/set individual bits within a register...but how do I check set a consecutive group of bits within a register.

    For example, I want to do the folloiwng...

    IF ADC channel 6 is presently selected, then set the ADC channel to ADC channel 7

    The problem with the ADC control register (ADCON0), is that there's a bit within it that's forever changing (so it's not a static register), therefore I can't do something along the lines of

    Code:
    if ADCON0 = 011001 then ADCON0 = 011101
    because bit 2 could be either a 0 or 1 (depending on when I happen to check, the way the wind blows etc), therefore I actually need to only check/set bits 2, 3 & 4 within that register

    How do I do that?!!!

    Thanks very much (an no sniggering at the back!)


    While I'm here if anyone can offer up a slick way of toggling between two ADC channels (if it helps, I'm using the special event trigger to collect ADC samples) that'd be grand. What I'm trying to achieve is extract magnitude from an arriving AC signal level....but I also want to check a pot too (which goes into another pin), so therefore need to switch ADC channels.
    Last edited by HankMcSpank; - 27th March 2012 at 18:27.

  2. #2
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: How do I check (or set) a group of consecutive bits within a byte?

    Sounds like the perfect use for logical AND. You would basically "AND" with 00011100 and it would leave any bit with zeros alone. Unfortunately "AND" does not seem to exist in my version of PBP, other than as a comparitor in an IF THEN statement?
    Huh??? This is pretty basic functionality ... not sure why it's not there?

  3. #3
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159


    Did you find this post helpful? Yes | No

    Default Re: How do I check (or set) a group of consecutive bits within a byte?

    Quote Originally Posted by Charlie View Post
    Sounds like the perfect use for logical AND. You would basically "AND" with 00011100 and it would leave any bit with zeros alone. Unfortunately "AND" does not seem to exist in my version of PBP, other than as a comparitor in an IF THEN statement?
    Huh??? This is pretty basic functionality ... not sure why it's not there?
    Check page 78 of the PBP3 manual - it talks about 'BITWISE AND' (expressed as '&') which is what I think you are looking for.

    I haven't been brave (or dumb?) enough to use it yet, but it seems to be there.

    Unless you're not using version 3, in which case, please excuse the interruption.

    Andy

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: How do I check (or set) a group of consecutive bits within a byte?

    Look in your manual under "Logical operators" AND exist. Also you can use &&

    When used as " IF(A and B ) <>= C then take Action" Is a true or false statement.

    Myvariable = %11111111 AND %0000000, this will AND the two variables into myvariable

  5. #5
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: How do I check (or set) a group of consecutive bits within a byte?

    Thanks guys, since the only difference between ADC channel6 & adc channel 7 is 1 bit, I actually I think i've figured a workaround, something like this...

    the code below assumes ADCON0 set as something like 11101 to start with (bit 2-6 set the ADC channel, so this would be ADC channel 7 selected)

    Code:
    if adcon0.2 = 1 then ' if bit 2 is set then ADC Channel must presently be CH7
    adccon.2 =0            ' clear bit 2, to set ADC channel to Channel 6
    else
    adccon0.2 = 1         ' if adcon.2 wasn't set as a '1' then ch6 must have been active therefore now set bit 2 to make AD ch7 active.
    I think that should toggle between ADC ch6 & ch7 on successive calls?
    Last edited by HankMcSpank; - 27th March 2012 at 20:57.

  6. #6
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: How do I check (or set) a group of consecutive bits within a byte?

    Quote Originally Posted by andywpg View Post
    Check page 78 of the PBP3 manual - it talks about 'BITWISE AND' (expressed as '&') which is what I think you are looking for.

    I haven't been brave (or dumb?) enough to use it yet, but it seems to be there.

    Unless you're not using version 3, in which case, please excuse the interruption.

    Andy
    I'm using 2.6 and it's not in my manual.

  7. #7
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: How do I check (or set) a group of consecutive bits within a byte?

    Mark_S wrote:
    Look in your manual under "Logical operators" AND exist. Also you can use &&

    When used as " IF(A and B ) <>= C then take Action" Is a true or false statement.

    Myvariable = %11111111 AND %0000000, this will AND the two variables into myvariable

    My 2.6 manual says the result of this test is true or false. No mention of ANDing variables into another variable. Perhaps you've hit on the first real reason to move to 3.0...

  8. #8
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default Re: How do I check (or set) a group of consecutive bits within a byte?

    Bitwise and Logical are two different things. All versions do support them.

    You will use Logical AND or &&, OR or ||, XOR or ^^, ANDNOT, ORNOT, XORNOT in TRUE/FALSE case. If value is 0 then it is treated as FALSE. Any other value is treated as TRU. So, in IF-THEN statement you can check if the values are or are not zero.

    Example:

    A=2:B=0

    IF A AND B THEN C=1

    C will be set to 1 if both A and B variables are not zero.

    But Bitwise Operators act on each and every bit of the variable using boolean maths.

    If you want to isolate bits a variables you want to use the & operand.

    If you want to set a bit then | is your operand to use.

    Example (from the Book):

    B0=B0 & %00000001 'Isolate bit 0 of B0

    B0=B0 | %00000001 'Set bit 0 of B0

    B0=B0 ^ %00000001 'Reverse state of bit 0 of B0

    So Hank, you want to use & in your case.

    ADCON0 = ADCON & %00011101

    Ioannis

  9. #9
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: How do I check (or set) a group of consecutive bits within a byte?

    Can you tell me which "Book" this example is from? I'm happy to be corrected - this is exactly what I was looking for - but It's certainly not in my 2.6 Manual.

  10. #10
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default Re: How do I check (or set) a group of consecutive bits within a byte?

    From 2.4 page 36
    From 2.6 page 42

    or

    From 3.0 pages 70 an 78

    Ioannis

  11. #11
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: How do I check (or set) a group of consecutive bits within a byte?

    You're absolutely right!

    If it's any help to anyone else, when I searched for logical AND, the first instance that came up is on page 43. The proper names for the boolean operations (AND, OR, XOR) are not used on P42.

  12. #12
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: How do I check (or set) a group of consecutive bits within a byte?

    Well, hopefully some good has come from my initial problem (which I've now sorted/worked around by other means)...thanks for your input everyone!

  13. #13
    Join Date
    Aug 2005
    Location
    Michigan, USA
    Posts
    224


    Did you find this post helpful? Yes | No

    Default Re: How do I check (or set) a group of consecutive bits within a byte?

    Hank, does PBP allow you to XOR a bit?

    Code:
    adcon0.2 = adcon0.2 XOR 1
    

    That's pretty much how I would do it BoostC ( adcon0.2 ^= 1 ).

    Cheerful regards, Mike

  14. #14
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default Re: How do I check (or set) a group of consecutive bits within a byte?

    This adcon0.2 = adcon0.2 XOR 1 does a logical XOR.

    You need bitwise XOR:

    adcon0.2 = adcon0.2 ^ 1

    Ioannis

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts