PDA

View Full Version : Picking a BIT from a BYTE



AndrewC
- 3rd August 2008, 12:01
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

Melanie
- 3rd August 2008, 12:38
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...

AndrewC
- 3rd August 2008, 12:54
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

mackrackit
- 3rd August 2008, 13:14
This is a very good read.
http://www.picbasic.co.uk/forum/showthread.php?t=544

Melanie
- 3rd August 2008, 13:20
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!

AndrewC
- 3rd August 2008, 13:27
Thanks for your suggestions. A naive user might just go to the FAQ board, presented with three threads think "hmm, nothing here".

Melanie
- 3rd August 2008, 13:30
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".

AndrewC
- 3rd August 2008, 13:33
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.

AndrewC
- 3rd August 2008, 13:36
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 :(

AndrewC
- 3rd August 2008, 13:37
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 ?

AndrewC
- 3rd August 2008, 13:45
... 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.

Melanie
- 3rd August 2008, 13:47
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.

mackrackit
- 3rd August 2008, 13:53
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 :D

AndrewC
- 3rd August 2008, 14:01
I will add to what Melanie said.

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

I DO :D

Wow ! All of a sudden my humble attempts at programming seem so inadequate :)

Darrel Taylor
- 3rd August 2008, 18:31
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>

manwolf
- 3rd August 2008, 19:40
Now if someone could just fix that darn search .....

Darrel Taylor
- 3rd August 2008, 19:51
<!-- 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>

Archangel
- 3rd August 2008, 20:00
I will add to what Melanie said.

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

I DO :D
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 ! :)


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.