Just assembled my board, trying to get my head wrapped around SPIN
turbosupra
Posts: 1,088
Ok, I know my coding is wrong, and I'm trying to understand the SPIN coding language. Can anyone tell me what I've done wrong?
I was able to get the green led on pin 3 to flash at the clock speed I programmed, but now I'm trying to get the yellow led on pin 6 to flash at a different clock speed, how should my code be different so that I can have all the inputs and outputs function independently?
Thanks for reading
My modified PushButtonLedTest.spin code
Original PushButtonLedTest.spin code
I was able to get the green led on pin 3 to flash at the clock speed I programmed, but now I'm trying to get the yellow led on pin 6 to flash at a different clock speed, how should my code be different so that I can have all the inputs and outputs function independently?
Thanks for reading
My modified PushButtonLedTest.spin code
LEDs_Green = 3 ' Start of I/O pin group for on/off signals LEDs_Yellow = 6 ' End of I/O pin group for on/off signals PUSHBUTTON = 18 ' Pushbutton Input Pin PUB ButtonBlinkSpeed ' Main method '' Sends on/off (3.3 V / 0 V) signals at approximatley 2Hz. dira[noparse][[/noparse]LEDs_Green]~~ ' Set entire pin group to output repeat ' Endless loop ! outa[noparse][[/noparse]LEDs_Green] ' Change the state of pin group if ina[noparse][[/noparse]PUSHBUTTON] == 1 ' If pushbutton pressed waitcnt(clkfreq / 20 + cnt) ' Wait 1/4 second -> 2 Hz else waitcnt(clkfreq / 4 + cnt) ' Wait 1/4 second -> 2 Hz PUB ButtonBlinkSpeed2 dira[noparse][[/noparse]LEDs_Yellow]~~ ' Endless loop repeat ! outa[noparse][[/noparse]LEDs_Yellow] if ina[noparse][[/noparse]PUSHBUTTON] == 1 waitcnt(clkfreq / 20 + cnt) else waitcnt(clkfreq / 4 + cnt)
Original PushButtonLedTest.spin code
'' File: PushbuttonLedTest.spin '' Test program for the Propeller Education Lab "Pe Platform Setup" CON _clkmode = xtal1 + pll16x ' Feedback and PLL multiplier _xinfreq = 5_000_000 ' External oscillator = 5 MHz LEDs_START = 0 ' Start of I/O pin group for on/off signals LEDs_END = 15 ' End of I/O pin group for on/off signals PUSHBUTTON = 18 ' Pushbutton Input Pin PUB ButtonBlinkSpeed ' Main method '' Sends on/off (3.3 V / 0 V) signals at approximatley 2Hz. dira[noparse][[/noparse]LEDs_START..LEDs_END]~~ ' Set entire pin group to output repeat ' Endless loop ! outa[noparse][[/noparse]LEDs_START..LEDs_END] ' Change the state of pin group if ina[noparse][[/noparse]PUSHBUTTON] == 1 ' If pushbutton pressed waitcnt(clkfreq / 4 + cnt) ' Wait 1/4 second -> 2 Hz else ' If pushbutton not pressed waitcnt(clkfreq / 20 + cnt) ' Wait 1/20 second -> 10 Hz
Comments
Turbo,
The main problem I see here....
The code is only going to get to the first repeat cycle.....and will continue in this loop forever.
"PUB ButtonBlinkSpeed2" Will never be called.
The organization of your code is the main problem here.....your are thinking linearly....rather than by methods.
My first question, Do you want to do this at any cost? Or do you specifically want to blink these two LED's with one COG?
This task is pretty easy with the use of two cogs.....but with one, it is slightly harder.
I will assume you are just trying to get two leds to blink independently at any cost.
James L
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
James L
Partner/Designer
Lil Brother SMT Assembly Services
Please note: Due to economic conditions the light at the end of the tunnel will be turned off until further notice. Thanks for your understanding.
At this point I don't mind how many cogs I have to use, I'm just trying to familiarize myself with the code and how it interacts with the chip
How should I alter my code, in the easiest manner, so that I can get the two lights to blink independent of each other?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Links to other interesting threads:
· Home of the MultiBladeProps: TriBlade,·RamBlade, RetroBlade,·TwinBlade,·SixBlade, website
· Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
· Prop Tools under Development or Completed (Index)
· Emulators: Micros eg Altair, and Terminals eg VT100 (Index) ZiCog (Z80) , MoCog (6809)
· Search the Propeller forums·(uses advanced Google search)
My cruising website is: ·www.bluemagic.biz·· MultiBladeProp is: www.bluemagic.biz/cluso.htm
I couldn't test the button part here but the blinking works.
You could then increase or decrease that value if a button is being pressed.· This would all run in the first cog (internal processor)
Option two is you use the cognew command and launch the second function into another processor which then completely independently runs that loop ie cognew(ButtonBlinkSpeed2, @Stack).· Stack is a long array that the launched cog will use to store it's stack.· Look for the PE Labs document in the tacked threads up the top of the forum.· It's a great intro to Spin and goes into detail on how to launch cogs to run processes in parallel.
Post Edited (photomankc) : 10/2/2009 2:04:25 AM GMT
So here is a cheat::
I didn't try this....but it will compile.
James L
EDIT: the bottom method timing should be changed...or the led's will blink the same when the button is pushed........if you change the timing....they will blink at different rates.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
James L
Partner/Designer
Lil Brother SMT Assembly Services
Please note: Due to economic conditions the light at the end of the tunnel will be turned off until further notice. Thanks for your understanding.
Post Edited (James Long) : 10/2/2009 2:09:11 AM GMT
If I understand this correctly,
In the first section you have a description
In the second section you have the crystal frequency value and variables defined to pins 24 and 26. What does CON mean? Constants? And what's with the function _Clkmode? Is that something that is in every circuit that needs to count?
In the 3rd section it appears you define more variables, I am not sure as to why just yet
In the 4th section you have PUB start, which I think defines where the instructions start? And then variable = and a function where it grabs a new cog
And the rest I'm lost. I can make it work, but I'm trying to figure out what each part of the code does in making it work
CON is constants -- symbols that are fixed values in the program
_Clkmode is a constant used to control the system clock speed.
_XINFREQ = is a constant that tells the prop what the crystal input freq is.
if you don't define these then the prop uses it's internal clock at RCFAST which is 12Mhz give or take.... a lot.
VAR section is where you define global variables for the spin object (file) these can be accessed by any function in the spin object (file)
PUB Start is the first function in the object and so it is the start-up function. From there all other code that runs must be executed from here or launched into another cog (processor) using Cognew. In this case it launched two new cogs in seperate processors and then it quits leaving the other two processors running. Each one is running the code in Toggle completely seperately.
Can you tell me the math you used to calculate the toggles and cycles per second, and is runCt an object that is already built into spin?
How come you had to add the Variable stack? And how come/where was LEDs_Yellow changed to LEDs_color?
A millisecond is 1/1000th of a second so we take the number of clock cycles in one second and divide that by 1000 or in this case 1100 so that we give the code some time to execute since it will obviously take some time to run. This tells waitcnt how many system clock ticks we want to it to wait before it goes on. So knowing the code will loop about 1000 times a second then if we want the light to blink on and off every 1/4 of a second we need it to turn on AND off every 250 loops (1000/4). Since we need it turn on and off we need the toggle to happen twice in that period of time so we need it to execute every 125 loops (250/2). The // operator divides runCt by 125 and reports the remainder.... if runCt is a multiple of 125 on any particular loop iteration the remainder will be zero and then we toggle the pins output.
runCt is a local variable declared by ButtonBlinkSpeed | runCt. Local variables exist only in the function in which they are defined. Just before the loop starts we set that variable to zero to initialize it and after that the loop increments it (runCt++) each time it loops. It will just keep incrementing forever until it overflows and returns to zero.
You really need to pick up the "PE Labs" manual and start from the beginning. Otherwise your going to be stumbling around in the dark like this for a long time. I was in your shoes at first and it took me about a week of working through the examples in the lab before I was writing programs to actually do something useful.
Is that the book you are talking about? If so, I guess I will read some more before I ask any more questions
CON and VAR have been explained by others and there is more info in the manual.
The PUB Start is a public routine and since it is the first, it is where execution begins.
This just starts a new cog with the program (routine) called Toggle. The rest are parameters which are passed to this routine. cognew returns the cog number (0..7) that it starts, so we add +1 to this to give a value 1-8. If no cog can be started -1 is returned, so we have just converted this to 0. This is quite a common practice and cog (or cog2) is can be used later to stop the cog. You need to supply a seperate stack space for each cog running spin.
Obviously, the next line does exactly the same thing, but in another cog, using a different time, led pin and stack space.
The routine should be self explanatory. If you don't understand the hyroglyphics such as ~~, !, look them up in the manual. Unfortunately, these are shorthand methods and are not readily obvious.
The last line cog~ is irrelevant and also incorrect as it does not take into account the 2 cogs (cog and cog2). Each cog will stop after this instruction. However, this will not happen as I called the routines with 0 count so the repeat loop will run indefinately.
Postedit: Sorry I was interrupted during my replay and others have responded. But I notice you are trying to set a group of pins. I suggest you look at the manual in the help section while you have proptool open, and look up DIRA. There are numerous ways to do this.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Links to other interesting threads:
· Home of the MultiBladeProps: TriBlade,·RamBlade, RetroBlade,·TwinBlade,·SixBlade, website
· Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
· Prop Tools under Development or Completed (Index)
· Emulators: Micros eg Altair, and Terminals eg VT100 (Index) ZiCog (Z80) , MoCog (6809)
· Search the Propeller forums·(uses advanced Google search)
My cruising website is: ·www.bluemagic.biz·· MultiBladeProp is: www.bluemagic.biz/cluso.htm
Post Edited (Cluso99) : 10/2/2009 3:58:49 AM GMT
I'll do some more reading this weekend and hopefully be able to have a better understanding of some of the basic things by Monday
Turbo,
The stack is for run time work space (stack space). This is for use of variables and such.......in case you want to change the LED you are blinking. This is on Pg. 107 of the Propeller manual.
Because the nomenclature on spin...the names can not be the same. The cognew command uses the constants that you have named, but the PUB method must have a different name for those items. This can be a confusing area of spin........
The PUB method just uses an alias (something you make up to keep things straight). This is my way of thinking........sort of having a nickname for the same thing.
James L
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
James L
Partner/Designer
Lil Brother SMT Assembly Services
Please note: Due to economic conditions the light at the end of the tunnel will be turned off until further notice. Thanks for your understanding.
-Stephanie Lindsay
Editor, Parallax Inc.
Yes, the PropTool download 1.2.6 comes with lots of good info and documents. Greatly improved. The only think missing IIRC, the web doesn't state all this great info is present in the download.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Links to other interesting threads:
· Home of the MultiBladeProps: TriBlade,·RamBlade, RetroBlade,·TwinBlade,·SixBlade, website
· Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
· Prop Tools under Development or Completed (Index)
· Emulators: Micros eg Altair, and Terminals eg VT100 (Index) ZiCog (Z80) , MoCog (6809)
· Search the Propeller forums·(uses advanced Google search)
My cruising website is: ·www.bluemagic.biz·· MultiBladeProp is: www.bluemagic.biz/cluso.htm
I'm making a little progress [noparse]:)[/noparse]