PDA

View Full Version : Conditional statement question.



JMLStamp2p
01-12-2008, 04:22 AM
Hello all,
If I wanted to check multiple Pins on my Prop what is the most efficient way to Code ? PUB Check_Pins method is Highlighted below.
Any advise would be appreciated.

.................................................. .............................................

CON
·_CLKMODE = XTAL1 + PLL1X····· 'External crystal 5MHZ times "1"
·_CLKFREQ = 5_000_000·········· ·'5 Million cycles

OBJ
·DELAY: "Clock"
·DATA: "FullDuplexSerial"

VAR
·long Stack [9]
·byte Byte_Recieved
·long Input_2
·long Input_3

PUB MAIN
·dira[0]~~
·dira[1]~
·dira[2]~
·dira[3]~

·data.start(1,0,0,9600)
·Run_Program

PUB Run_Program
·cognew((Check_Pins), @Stack)

...............................
PUB Check_Pins

·Input_2 := INA[2]
· If Input_2 == 0
·· Increment_Display
· ELSE·······················
·· Check_Pins
...............................
··
PUB Increment_Display
· waitcnt(625_000 + cnt)
· data.tx($01)
· waitcnt(625_000 + cnt)
· data.tx($00)
· Check_Pins

PUB Decrement_Display
· waitcnt(625_000 + cnt)
· data.tx($02)
· Check_Pins
·
PUB Recieve
·data.start(21,20,%0000,9600)
· REPEAT
·· case data.rx
··· 1: Pixels
··· 2: TestPlay


·

Paul Baker
01-12-2008, 04:32 AM
Before we get to your question, we need to deal with something first. The way you have coded your program is fundamentally flawed, you have Check_Pins doing two possible branches, Increment_Display or recursively calling Check_Pins again, and Increment_Display calls Check_Pins repeating the loop. Nowhere in this program flow is a return from a call ever taken. You will keep growing the stack until you run out of memory and your Propeller stops working.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker (mailto:pbaker@parallax.com)
Propeller Applications Engineer
[/url][url=http://www.parallax.com] (http://www.parallax.com)
Parallax, Inc. (http://www.parallax.com)

Mike Green
01-12-2008, 04:38 AM
You will run into several problems with this:
1) Your Check_Pins routine calls itself and calls Increment_Display which calls Check_Pins. That will cause a stack overflow.

2) Check_Pins runs in its own cog and calls the serial I/O routines. Depending on what your main program is doing, you
may have several cogs calling the serial I/O routines which could cause problems. If this cog is the only one calling the
transmit routines, you may be ok.

I don't know what you're doing with Recieve. It doesn't seem to be called and it reinitializes the serial I/O object.

Run_Program just exits after starting Check_Pins and, after returning to your main routine, "falls off the end" which causes the
main cog to stop.

How about starting with a brief description of what you want to accomplish rather than posting relatively long fragments of code that
can't do anything useful? Then we can point you to possible examples of similar problems that you can start with and modify.

JMLStamp2p
01-14-2008, 05:31 AM
Sorry Guys, I guess I should have explained. I am just learning Spin and was trying several code fragments just to see if I was coding right and if they would compile. My question· was in referance to the highlighted part of the code as follows:

....................................
PUB Check_Pins

· Input_2 := INA[2]

· If Input_2 == 0
··· Increment_Display

· ELSE·······················
·· ·Check_Pins
....................................

I want to have·8 push buttons connected to 8 seperate IO lines on the Prop. I would like to have a dedicated processor check the status of these inputs constantly and jump to a specific sub-routine for that paticular button press. What would be the most efficient way for the dedicated processor to run in a loop checking the status of all 8 buttons and call methods to handle each one ?

Thanks for the help,
JMLStamp2p

Paul Baker
01-14-2008, 06:26 AM
Ok ignoring the errors in the program flow of your code, the most efficient means for checking the states of multiple pins is waitpne, you establish the mask as the 8 pins you are checking and set the state to thier current value, so whenever the state of any pin deviates from from it's current value waitpne exits and you compare the current state with the old state to determine which pins have changed thier value.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker (mailto:pbaker@parallax.com)
Propeller Applications Engineer
[/url][url=http://www.parallax.com] (http://www.parallax.com)
Parallax, Inc. (http://www.parallax.com)

deSilva
01-14-2008, 06:29 AM
JMLStamp:
What Paul and Mike didn't say so openly is that your code is a total mess! Of course we will help, but it would help you much more if you were working yourself through the tutorials, especialy chapter 3 of the manual - from the beginning to the end!

You want to read the status of an input pin; you do this by INA[nr_of_pin]; so the simplest way (there are three or four more possibilities) is to check them one ofter the other:



REPEAT
IF INA[pin1]
pin1routine
IF INA[pin2]
pin2routine

JMLStamp2p
01-14-2008, 08:35 PM
Thank You DeSilva,
The code as a whole was never intended to run a specific task, my question in general was the highlighted code as you have answered.
Thanks,
JMLStamp2p

PS: I will read chapter 3