Picking a BIT from a BYTE


Closed Thread
Results 1 to 18 of 18

Hybrid View

  1. #1
    Join Date
    Feb 2006
    Location
    Brussels, Belgium
    Posts
    104

    Default Picking a BIT from a BYTE

    I've been playing around with some old programs trying to "make them better", otherwise interpreted as "it works so now I'll break it".

    Thanks to some guidance from here, I'm now reasonably comfortable with writing to multiple pins in one command and, even better, understand why it is better than a string of multiple single pin writes.

    But I'm stuck coming the other way
    -I can read PORTB in one go, easy.
    -I can mask which pins I'm interested in, easy.
    -But I can't figure out how to easily extract say bit 3 of my PORTB byte into a single bit variable. I can & it with a mask and set the bit depending on the results but it seems there must be a better way than this:

    BUTTONS var BYTE
    Menu_1 VAR bit
    If (BUTTONS & %00001000) = 1 THEN Menu_1 = 1
    If (BUTTONS & %00001000) = 0 THEN Menu_1 = 0

    Any guidance welcome

    Thanks, Andrew

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Both these lines...

    If (BUTTONS & %00001000) = 1 THEN Menu_1 = 1
    If (BUTTONS & %00001000) = 0 THEN Menu_1 = 0

    Can be simply replaced with...

    Menu_1=BUTTONS.3

    Which means go take Bit 3 of variable BUTTONS and put it into Bit variable Menu_1

    Oh... and Yes, bit manipulation like this IS described in the PBP manual...

    You can also do things like...

    Menu_1=PORTB.3

    Which means go read PortB.3 and put that into Bit variable Menu_1

    And you can also go a stage further...

    MenuButton var PortB.3 ' Declare this at the start of your program...

    ...thereafter anytime you want you can simply ask...

    IF MenuButton=0 then Goto MenuEntry

    Which is the same as asking...

    If PortB.3=0 then Goto MenuEntry

    and so on, and so on, ad nauseum...

  3. #3
    Join Date
    Feb 2006
    Location
    Brussels, Belgium
    Posts
    104


    Did you find this post helpful? Yes | No

    Default

    Thanks Melanie, I actually tried that (Menu_1=BUTTONS.3) because it seemd the obvious solution and it didn't work so I need to go back and find out what I did wrong

    Whereabouts in the pbp manual does it describe bit manipulation because I did try to RTFM first

    Andrew

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    In manual pbpm502a.pdf or pbpm304.pdf see...

    2.6.2 Pin and Variable Names
    4.4 Aliases
    4.11 Pins

    see also in the FAQ of this forum...

    http://www.picbasic.co.uk/forum/showthread.php?t=544

    OK, Dave beat me to it!

  6. #6
    Join Date
    Feb 2006
    Location
    Brussels, Belgium
    Posts
    104


    Did you find this post helpful? Yes | No

    Default

    Thanks for your suggestions. A naive user might just go to the FAQ board, presented with three threads think "hmm, nothing here".

  7. #7
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    A naive user might... but our forum members aren't naive... they just might occasionally 'forget' to scroll to the bottom of the page and instead of simply Displaying Results FROM THE "Last Month", they would chose to display them FROM THE "Beginning".

  8. #8
    Join Date
    Feb 2006
    Location
    Brussels, Belgium
    Posts
    104


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Melanie View Post
    In manual pbpm502a.pdf or pbpm304.pdf see...

    2.6.2 Pin and Variable Names
    4.4 Aliases
    4.11 Pins

    see also in the FAQ of this forum...

    http://www.picbasic.co.uk/forum/showthread.php?t=544

    OK, Dave beat me to it!
    So the thread Dave pointed me to is a goldmine. The manual sections are virtually useless. I'm not wanting to have a go at the author (who is anonymous) but I think the quantity and type of questions that experts on this forum are good enough to answer points at the manual as being somewhat deficient.

  9. #9
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    I've heard this argument before.

    I usually answer a question with a question (girls do those things)... "At what User Level do you pitch your manual at?".

    If you write a piece of Accounting Software for example, does your User Manual have to be a complete idiots course in Accounting?

    At some point your Manual stops becomming a 'Product Manual' and starts becoming a full-blown educational course in Accounting (or in this case programming).

    I'm no different to you Andrew (other than in the obvious places!), I started with the same Compiler, same Manual, same PICs, same Datasheet, same Internet Connection, same PDF Reader, and same PC running probably the same OS. I DIDN'T study PICs or PROGRAMMING at University. I DID look at some example programs. I DID make a Blinky LED. I DID Connect an LCD so that I could figure for myself what results I got when I converted variables, or did math, or any of the other things that mysteriously happen inside the PIC. Now there's NOTHING stopping anyone else on this forum doing the same.

    If you don't know something, then FIND OUT why, what or wherefore. And there's no better way of learning than by doing.

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 07:55
  2. LCD freeze
    By harryweb in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 5th October 2009, 08:01
  3. byte compression
    By Norbert in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 16th June 2007, 18:04
  4. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  5. 16F877 RAM Question
    By Art in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 6th August 2005, 11:47

Members who have read this thread : 0

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