1 Bit variables in Propeller
jnogues
Posts: 25
Hi everybody. I'm a new user of this forum and the Propeller. I have experience programming BS2 and Pic's in PBP, but
I decided go to Propeller. Now I'm starting whith spin, PropBasic and 12Blocks, but I have a little problem:
I need use 1 bit boolean functions like:
P16= (P0 & P1) | (M1 & M2)
where PX are pins and MX are 1 bit variable.
In BS2 is very simple, I do :
M1 var bit
but what could I do in Propeller?
Regards,
Jaume Nogues
Barcelona
Spain
I decided go to Propeller. Now I'm starting whith spin, PropBasic and 12Blocks, but I have a little problem:
I need use 1 bit boolean functions like:
P16= (P0 & P1) | (M1 & M2)
where PX are pins and MX are 1 bit variable.
In BS2 is very simple, I do :
M1 var bit
but what could I do in Propeller?
Regards,
Jaume Nogues
Barcelona
Spain
Comments
You could also use the DIRB register, which is just an IO register that isn't mapped to any IO lines (yet). However, in future versions of the propeller it could be, so I would use the bit manipulation object.
I have had this problem before, and Mike Green has always jumped in to answer the question. Since I know the answer I'll post it here.
Direct bit manipulation is not straight forward like on the stamp, but is pretty easy once you know the method.
The instructions to set a bit are like the following:
byteVar &= !Mask ' To set a bit off
byteVar |= Mask ' To set a bit on
byteVar ^= Mask ' To toggle a bit
if byteVar & Mask ' To test if a bit is on
where
CON Mask = %10000000 ' or something similar <
this being the bit you set.
it should be noted that the commands are exact (Spin). If you leave out the | or any other text it will be a problem. I can never remember the key stroke for that symbol, but it can be found under help in the Propeller Tool.
If you do not understand this, you may elect to use the object from the exchange.
Since the operations are a little different, you may want to play with this some. I just went ahead and programmed my project only to find out, I didn't have a clue how it worked. I then had to go back and figure it out. I wrote a simple program to play with setting individual bits, but lost it recently. Well I didn't loose it, I just haven't retrieved it from a raid system yet.
It was nice to have the program to refresh from time to time. Any time I put this code in a program, I put some notation beside it saying what it does. That will keep you straight on the operation. Good notation will save a huge amount of headaches.
James L
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
James L
Partner/Designer
Lil Brother SMT Assembly Services
Are you addicted to technology or Micro-controllers..... then checkout the forums at Savage Circuits. Learn to build your own Gizmos!
Spin does let you manipulate bits with those...
I just tried:
outb:=5 'sets the whole value
and then
outb[noparse][[/noparse]3]~~ 'set just bit#3
Since outb isn't really used, you can do what you like with it...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
My Prop Info&Apps: ·http://www.rayslogic.com/propeller/propeller.htm
Post Edited (Rayman) : 1/2/2010 3:15:03 PM GMT
Ray,
That is a good catch, but.........I'm not sure I would do that. If you get familiar with doing the bits that way, when moving to Propeller II, you may not be allowed to do that (it may have a outb).
James L
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
James L
Partner/Designer
Lil Brother SMT Assembly Services
Are you addicted to technology or Micro-controllers..... then checkout the forums at Savage Circuits. Learn to build your own Gizmos!
There is an operator in Spin that can help you very easily obtain the state of a SINGLE bit from a variable no matter which one it is.
·
The operator is |<· ….. What this operator does is return a number with only ONE BIT set to 1
·
Use this formula
···· BitStatus := AnyVariable & (|< RequiredBitNumber)
·
Lets say you have a variable X and you want the 9th bit· (ie bit 8) you do this
·· BitStatus := X & (|< 8)
·
This way you can get at any bit you want from a number just by using the formula above.
·
This is in many ways equivalent to saying in BS2
··· BitStatus = AnyVariable.BitN···· ‘where N ist the bit number you need
·
I hope this helps….if you need to read about the |< operator it is on page 160 of the Propeller manual.
Oh and by the way the & operator is a bit wise anding ... page 164
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Samuel
www.RobotBASIC.com
Post Edited (SamMishal) : 1/3/2010 2:43:27 AM GMT
·
···· AnyVariable·|= |< RequiredBitNumber····· ''''' or more readable AnyVariable := AnyVariable·| !(|<RequiredBitNumber)
·
oops.....above line is wrong.....here is the correct one.... the ! should not have been there....thanks Kuroneko
····
·
To reset (0) a bit do
·
To Toggle (0->1 or 1->0)· then do this
·
····· AnyVariable ^= (|< RequiredBitNumber)··· ·''''' or more readable AnyVariable := AnyVariable·^ !(|<RequiredBitNumber)
·
oops.....above line is wrong too .....here is the correct one.... the ! should not have been there....thanks Kuroneko
careless copy and paste ...sorry...
·
·
| is bit wise OR···· ^ is bit wise XOR··· and ! is bit wise Not
·
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Samuel
www.RobotBASIC.com
Post Edited (SamMishal) : 1/3/2010 4:07:11 AM GMT
But I tink that all the solutions are more dificult using a Propeller than a BS2. Is there
another compiler diferent to spin with 1 bit variables?
I need reasons for change difinitely from BS2 to Propeller!!
Jaume Nogues
Barcelona
Spain
Yes....the Propeller is more difficult than the BS2....and if you are not an experienced programmer
and you cannot understand or easily use BitWise operators using Anding/Oring/Xoring/Noting then
MAYBE you need to stick to the Bs2 for a little while longer.
HOWEVER, everything worth while does not come easy. Like everything in life Hay que APRENDERLO....no?
Whenever you advance to more versatile or powerful things you have to learn how to use/do them.
Driving a car is easy....but driving an airplane is hard but it gives you more possibilities than a car does.
So you need to LEARN it to harness the power...
It is not really much harder to say
········· Bit_State := Variable & (|< 5)
than it is to say
········· Bit_State = Variable.Bit5
Just a slight bit of syntax difference but you can soon get used to that ..... no?
But you are right....if this is too difficult....then the other stuff which is more complicated might just be
a little too difficult for you at this stage of your development and you are better off to stick with the
BS2 for a little while longer.
After all....the BS2 is a GREAT microcontroller and can do MANY MANY things and a lot easier than the
Propeller.
If you find yourself doing a project where you need TWO or THREE or more BS2s to do what you need....THEN....you are
going to appreciate the need for the Propeller.
If you do projects where you find yourself wishing the BS2 could do two or more things AT THE SAME TIME......THEN....you are
ready for the Propeller.
If you do projects where you find yourself needing more I/O·PINS and more SPEED and·more MEMORY .....THEN.....you are
in need for the Propeller.
So if you have not come across these issues or do not even know what they mean then you are WELL SERVED by the BS2
and you DO NOT need to move.
But when you do.....the Propeller is there to help you and you will find that SPIN is not really harder than PBasic and with
just a little bit of studying you will be able to HARNESS the extra power and VERSATILITY that the Propeller can give you.
Also soon there is going to be a GREAT PBasic like language for the Propeller called PropBAsic which will be similar but not
too similar to PBasic. It will still require some learning to use it. But probably not much.
If you still want to learn the Propeller and you are SERIOUS about it ..... I cannot recommend anything better than
the Propeller Education Kit and its manual......get your self one·of these and hunker down for a few Weekends or
a few weeks and just DO IT.
The PEK is the BEST way to learn the Propeller in a METHODICAL and EFFECTIVE way.....just do not take short cuts and
do not JUMP THE GUN.....take it step by step and DO the exercises.....do not progress on to the next level until you are
100% able to do the stuff you have just learnt........ you just HAVE TO DO IT.....there is no easy QUICK way......
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Samuel
www.RobotBASIC.com
·
Thanks for your post. After reading it I decided go to Propeller, first whith PropBasic
and after with spin. Now I go to download propBasic!!
Regards,
Jaume
·
Dinos cómo te fue al final.
·
¡buena suerte!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Samuel
www.RobotBASIC.com
·
The method will deliver the bit value but it is still in the 2^n position.
In this case n=5 will set var to a decimal value of 32.
If you need your decimal value to be only 1 or 0 based on the nth bit you need to bit shift right by n. This will give you a 1 or 0 provided n's range == [0, 31]
Hope this helps
Sweet, I appreciate the update