Picking a BIT from a BYTE


Closed Thread
Results 1 to 18 of 18
  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
    Feb 2006
    Location
    Brussels, Belgium
    Posts
    104


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Melanie View Post
    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".
    .. ahem

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


    Did you find this post helpful? Yes | No

    Default

    But seriously - have any of you "experts" who are patient enough to answer the same q's time and time again ever thought of grouping your answers into a Wiki type dynamic on line manual ?

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


    Did you find this post helpful? Yes | No

    Default

    ... and to wrap this up this is the bit of code I was (re)inventing to look for any button press and then transfer out which button.

    Button_read:
    BUTTON_PRESSED = 0 ' set no button pressed to start
    WHILE BUTTON_PRESSED = 0
    BUTTONS = PORTB & BUTTON_MASK 'READ PORTB and pick out the bits I want
    IF BUTTONS <> BUTTON_MASK then BUTTON_PRESSED = 1 'if any selected pin has been pulled to 0 then button pressed
    PAUSE 10
    WEND
    Menu_1 = BUTTONS.3
    Menu_2 = BUTTONS.4
    RETURN

    It works just fine now, thanks for the pointers. I've no idea why the bit extraction did not work the first time, sods law.

  12. #12
    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.

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


    Did you find this post helpful? Yes | No

    Default

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

    Make sure you have a fire extinguisher handy and safety glasses on while programming.

    I DO
    Dave
    Always wear safety glasses while programming.

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    I will add to what Melanie said.

    Make sure you have a fire extinguisher handy and safety glasses on while programming.

    I DO
    Wow ! All of a sudden my humble attempts at programming seem so inadequate

  15. #15
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by AndrewC View Post
    But seriously - have any of you "experts" who are patient enough to answer the same q's time and time again ever thought of grouping your answers into a Wiki type dynamic on line manual ?
    What's the fun in that?
    Everyone gets all their answers from a wiki, and we've got nobody to talk to.

    We are your "Living Wiki". And can answer questions that haven't even been asked yet. Then once answered, it becomes part of our own little PICWIKI (otherwise known as the FORUM). &nbsp; It may not be organized as well ... but it is DYNAMIC.
    <br>
    DT

  16. #16
    Join Date
    May 2008
    Location
    Florida
    Posts
    64


    Did you find this post helpful? Yes | No

    Wink

    Now if someone could just fix that darn search .....

  17. #17
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    <!-- Search Google --><left><FORM method=GET action=http://www.google.com/custom><TABLE bgcolor=#FFFFFF cellspacing=0 border=0><tr valign=top><td><A HREF=http://www.google.com/search><IMG SRC=http://www.google.com/logos/Logo_40wht.gif border=0 ALT=Google align=middle></A></td><td><INPUT TYPE=text name=q size=31 maxlength=255 value=""><INPUT type=submit name=sa VALUE="Google Search"><INPUT type=hidden name=cof VALUE="S:http://www.picbasic.co.uk/forum;AH:left;LH:37;L:http://www.crownhill.co.uk/logo.gif;LW:174;AWFID:f08c2ab0d3333e22;"><input type=hidden name=domains value="picbasic.co.uk"><br><input type=radio name=sitesearch value=""> Search WWW <input type=radio name=sitesearch value="picbasic.co.uk" checked> Search picbasic.co.uk </td></tr></TABLE></FORM></left><!-- Search Google --><script type="text/javascript">var adminuser = document.getElementById("inlinemodform");if (adminuser == null) {document.write(" ");} else {document.write("<br><br><b>This form won't work for Admin Users.<br>The inlinemod form interferes with the search form</b>");}</script>
    Last edited by Darrel Taylor; - 3rd August 2008 at 20:08.
    DT

  18. #18
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    I will add to what Melanie said.

    Make sure you have a fire extinguisher handy and safety glasses on while programming.

    I DO
    That's because you get up too early on Sunday, Fire extinguisher indeed, where is your spirit of adventure? YA need marshmellows and a stick !
    Quote Originally Posted by Melanie
    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 becoming a 'Product Manual' and starts becoming a full-blown educational course in Accounting (or in this case programming).
    Hi Melanie,
    Does such a thing exist? I entered the "Computer Age" with "DOS For DUMMIES" and I really would not be offended by a course for "complete idiots", as long as it worked.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

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