syntax and some other simple questions about spin
![Rontopia](https://forums.parallax.com/uploads/userpics/269/nRUGWJYLHHVJT.jpg)
I bought the propeller education kit about 2 months ago. I have not had the kind of time I wanted to spend with it until now. I have been going through the labs and the manual in an attempet to understand spin. with that always comes questions. I dont know how many untill I start writing them down which is what I will do here.
I have read about the dira registors in the labs and the manual. I have done the labs but there are 2 or 3·things i dont understand about them.
1 is why do we even have them. outa and ina seem to me that they would serve the same purpous? if outa[noparse][[/noparse]pin] :=1 or 0 semply set the state of the pin to a voltage high or low state? maybe im making things to simple?
2 is the·passing·of some·operators to them.. dira[noparse][[/noparse]7]~~ sets the dir registor for pin 7 to high? or does it serve some other purpous?·it seems like I could say the same thing like dira[noparse][[/noparse]7] := 1. or am i mistaken .. im sure that i am. dira[noparse][[/noparse]7]~ seems to be the same in my mind as dira[noparse][[/noparse]7] := 0.
3 the andnot operator !.· so i set the state of the outa[noparse][[/noparse]pin] registor to the andnot of its current state with !outa[noparse][[/noparse]pin]? if i know the pin is set to 1 and I say outa[noparse][[/noparse]pin] :=0 is this not the same thing? Or is this·a way·for people who are programers to look at someones code and tell that the person that wrote outa[noparse][[/noparse]pin]:=0 is not an experanced programer? i know that seems like a silly question but im not really a programmer[noparse];)[/noparse]
can i mix and match these operorators? so could I say:
dira[noparse][[/noparse]7]~~
outa[noparse][[/noparse]7] := 0
bla bla· bla
outa[noparse][[/noparse]7] :=1 or !outa[noparse][[/noparse]7]
the next question is .. im not sure how to say it. what is the difference between := and == ? logicly i mean. i know := is supposed to be for variables but all im really doing in the registors is changing the state right? 0 and 1 are they being consitered variables here? .. maybe i just answered my own question but any light on the subject would be great. it seems that if my only state choices are 0 and 1 then I could use == to set them.
lastly
this is a syntax question about [noparse]/noparse and (). im not sure that I have figured out in what case im to use a [noparse]/noparse or ().
here·is an·example from the labs
PUB ButtonTime(pin) : delta | time1, time2
so here pin is in () not [noparse]/noparse .. also what is the variable delta here, why is it seporated from the other variables with a pipe? is delta the answer? if so then what is..
PUB ButtonBlinkTime | time, index, cog[noparse][[/noparse]6]
I guess I dont understand what the pipe is for. in this case does it mean that ButtonBlinkTime is supposed to return a value? and again the [noparse]/noparse vs () on the cog variable..
i am completely lost if you cant tell. well not completely but there is no one to ask so im asking you.. thanks in advance for your time taken to help a total noob to spin.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Muahdib
IC layout designer
Austin Texas
I have read about the dira registors in the labs and the manual. I have done the labs but there are 2 or 3·things i dont understand about them.
1 is why do we even have them. outa and ina seem to me that they would serve the same purpous? if outa[noparse][[/noparse]pin] :=1 or 0 semply set the state of the pin to a voltage high or low state? maybe im making things to simple?
2 is the·passing·of some·operators to them.. dira[noparse][[/noparse]7]~~ sets the dir registor for pin 7 to high? or does it serve some other purpous?·it seems like I could say the same thing like dira[noparse][[/noparse]7] := 1. or am i mistaken .. im sure that i am. dira[noparse][[/noparse]7]~ seems to be the same in my mind as dira[noparse][[/noparse]7] := 0.
3 the andnot operator !.· so i set the state of the outa[noparse][[/noparse]pin] registor to the andnot of its current state with !outa[noparse][[/noparse]pin]? if i know the pin is set to 1 and I say outa[noparse][[/noparse]pin] :=0 is this not the same thing? Or is this·a way·for people who are programers to look at someones code and tell that the person that wrote outa[noparse][[/noparse]pin]:=0 is not an experanced programer? i know that seems like a silly question but im not really a programmer[noparse];)[/noparse]
can i mix and match these operorators? so could I say:
dira[noparse][[/noparse]7]~~
outa[noparse][[/noparse]7] := 0
bla bla· bla
outa[noparse][[/noparse]7] :=1 or !outa[noparse][[/noparse]7]
the next question is .. im not sure how to say it. what is the difference between := and == ? logicly i mean. i know := is supposed to be for variables but all im really doing in the registors is changing the state right? 0 and 1 are they being consitered variables here? .. maybe i just answered my own question but any light on the subject would be great. it seems that if my only state choices are 0 and 1 then I could use == to set them.
lastly
this is a syntax question about [noparse]/noparse and (). im not sure that I have figured out in what case im to use a [noparse]/noparse or ().
here·is an·example from the labs
PUB ButtonTime(pin) : delta | time1, time2
so here pin is in () not [noparse]/noparse .. also what is the variable delta here, why is it seporated from the other variables with a pipe? is delta the answer? if so then what is..
PUB ButtonBlinkTime | time, index, cog[noparse][[/noparse]6]
I guess I dont understand what the pipe is for. in this case does it mean that ButtonBlinkTime is supposed to return a value? and again the [noparse]/noparse vs () on the cog variable..
i am completely lost if you cant tell. well not completely but there is no one to ask so im asking you.. thanks in advance for your time taken to help a total noob to spin.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Muahdib
IC layout designer
Austin Texas
Comments
I don't have much time, but I'll answer as much as I can.
#1 Outa is used to set the direction of a pin(or Pins). If you wish to know the state of a pin you must read it, to do that we must use the INA command. Some registers are read and write as you describe. These are not. The OUTA is writtable, where as the INA is only readable!
#2 Yes, dira[noparse][[/noparse]x] := 1 and dira[noparse][[/noparse]x]~~ do the same function, and it may sound a bit redundant, but this is a nice feature to have when typing long programs(less Key strokes), and may be easier code to read for some programmers.
#3 No, the ! operator is not a joke. If we explicitly set a bit to a one or a zero then we use dir[noparse][[/noparse]x] := 1 or dir[noparse][[/noparse]x] := 0. To do so with the not operator would mean we would have to know what the previous state of the pin was in order to get the out put we wanted. The not operator is handy when we don't really care what state the pin is, we just want it to change to the other state with minimal code. To do what the not operator does without useing it would require more code than just the one line.
#4 is similiar to question one. the := operand is used to set(or write to) a variable or bit. The == operator is used to compare(or read) the value to its right with the value on its left. It does not set anything.
I do not have a way of looking at the labs on this old win98 beast I'm useing, but I hope this helps!
Cheers
Sorry, missed one!
[noparse]/noparse is used with arrays
() is used with parameters
Post Edited (bambino) : 4/6/2007 6:21:14 PM GMT
If you will notice, there are 3 states involved for an I/O pin: driven to logic high, driven to logic low, and high impedance (input). That requires at least 2 bits, one from the direction register and one from the output register.
2) The "~~" and "~" operators do the same thing effectively as using ":= 1" and ":= 0". They have some advantages when using them in expressions like "cog~ - 1" which has the value of "cog-1", but has the side effect of zeroing cog afterwards. You can't do that with the assignment operator.
3) The "!" operator is a bit-wise not operator. If you say "outa[noparse][[/noparse]7] := !outa[noparse][[/noparse]7]" you are toggling or complementing that particular bit (setting it to its opposite state). You say "If I know that outa[noparse][[/noparse]pin] is set to 1". How do you know that? You could write this
but it wouldn't be the same as "outa[noparse][[/noparse]7] := !outa[noparse][[/noparse]7]" because the "if" statement doesn't account for the case where outa[noparse][[/noparse]7] is set to 0.
4) ":=" is the assignment operator. It changes the value of the variable on the left side to the value of the expression on the right side. "==" is the equality operator. It has the value TRUE if the left expression is equal to the right expression and the value FALSE otherwise. They're completely different.
5) Parentheses are used in parameter lists (like ButtonTime(pin)) and to group expressions within other expressions. The square brackets are used for array subscripts and things that behave like array subscripts (like bit numbers or bit group numbers ... like outa[noparse][[/noparse]7] or outa[noparse][[/noparse]4..7]).
6) The colon in a method declaration separates the function prototype from the name of the return value. The default return value name is "result" and is present for all methods whether used or not. The colon allows you to substitute a more meaningful name, that's all. The vertical bar ("pipe") separates the function prototype or return name from a list of local variables all of which are longs.
ok.. i think i get it.. let me ask this..
PUB Blink (pin, rate, reps)
······ dira[noparse][[/noparse]pin]~~
······ outa[noparse][[/noparse]pin]~
··
······ repeat reps * 2
········ waitcnt(rate /2 + cnt)
········ !outa[noparse][[/noparse]pin]
if I dont care what the begining state of outa is, do i have to set it with the ouat[noparse][[/noparse]pin]~ statment? in other words I could say
PUB Blink (pin, rate, reps)
······ dira[noparse][[/noparse]pin]~~
······ repeat reps * 2
········ waitcnt(rate /2 + cnt)
········ !outa[noparse][[/noparse]pin]
the !outa[noparse][[/noparse]pin] statment would just toggle the pin reguardless of its begining state? i do understand that this is not really a good idea, but all im trying to do here is understand the nature of the syntax and the registors
does colon = pipe here? maybe·I dont know what the proper name is? in unix we always called | a pipe operator.
also, in the statment PUB Blink (pin, rate, reps), your saying that this will return a value which is called result even if I dont ask for it? if im understanding this right this means that Blink will = result? futthermore, if I wanted a result to be named something else and I want to manage that name/value, I would say something like PUB Blink someValue | (pin, rate, rate) ?
I cant thank you guys enough for taking the time.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Muahdib
IC layout designer
Austin Texas
Post Edited (Rontopia) : 4/6/2007 7:14:48 PM GMT
Colon is the name for ":".
If you do something with the value returned by blink, you'll notice that it's zero unless set to something else within blink. The Spin interpreter always initializes the value of result to zero.
if not I'll be back
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Muahdib
IC layout designer
Austin Texas