toggle a LED
Maria Ivanovna
Posts: 8
Hello Forum ,
I wrote a·code in spin , i would like to ask you about it·because i am not sure·. The code is :
·I want from this code to do : turn the LED ON 200 times after·2 seconds from the start of the program and the time between the·OFF and the ON is·1 micro second while ina[noparse][[/noparse]X_PULSE]>0. I don't know if i explain it good
Thank you in advance,
Masha
Post Edited (Maria Ivanovna) : 10/22/2009 7:15:37 PM GMT
I wrote a·code in spin , i would like to ask you about it·because i am not sure·. The code is :
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 OneSecond = 80_000_000 MicroSecond = OneSecond/1_000_000 X_PULSE = 0 LED_CONTROL = 1 ' LED turn on, active high
PUB start|n dira[noparse][[/noparse]LED_CONTROL]~~ 'set LED control port to output x1.start y1.start WaitCnt(OneSecond*2 + Cnt) repeat repeat while ina[noparse][[/noparse]X_PULSE]>0 repeat n from 0 to 200 outa[noparse][[/noparse]LED_CONTROL]~~ ' turn LED on WaitCnt(MicroSecond+ Cnt) ' wait 1 microsecond outa[noparse][[/noparse]LED_CONTROL]~ ' turn LED off
·I want from this code to do : turn the LED ON 200 times after·2 seconds from the start of the program and the time between the·OFF and the ON is·1 micro second while ina[noparse][[/noparse]X_PULSE]>0. I don't know if i explain it good
Thank you in advance,
Masha
Post Edited (Maria Ivanovna) : 10/22/2009 7:15:37 PM GMT
Comments
2) Your repeats are not indented and the outa/waitcnt/outa is not indented. Also, what's x1 and y1? When something doesn't work the way you expect it, the problem or part of the problem may lie in what you're not showing.
Look here: www.parallax.com/tabid/442/Default.aspx
A quick look over what you have so far indicates a few potential problems. First, you have two repeat repeat loops with nothing in the body, so they effectively only delay. Second, you don't have microseconds defined anywhere.
I believe that there is a toggle method in the Spin Tutorial in the Propeller Manual.
repeat while ina[noparse][[/noparse]X_PULSE]>0
dosn't repeat anything because, SPIN being space oriented, there it nothing to repeat under it. It just wastes time.
Also, there is no direction command. for the LED to light there needs to be a
dira[noparse][[/noparse]LED_CONTROL]~~
Stateing it as an output.
Another thing: SPIN is not like BASIC. The "outa" command has to be constantly repeated otherwise the LED will only light for as long as it takes the instruction to execte. Put it in the repeat loop above it and it will light. Hope this helps.
Micro
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Computers are microcontrolled.
Robots are microcontrolled.
I am microcontrolled.
But you·can·call me micro.
Want to·experiment with the SX or just put together a cool project?
SX Spinning light display·
Want cheap wholesale electronic parts?
Transistor parts wholesale
x1 and y1 are 2 objects to control another LED , x1 and y1 works well , the problem can't be from this part.
And with propeller assembly i can be able to turn the LED on and off with 1us ?
I edited my post , thanks for your reply
Post Edited (Maria Ivanovna) : 10/22/2009 7:17:23 PM GMT
the SPIN-command WaitCnt can wait minimum 385 Clock-ticks which is
385 / 80_000_000 = 4.8125 microseconds.
The reason is that SPIN is a interpreted language. It needs some time to fetch the SPIN-byte-token
to evaluate the expression etc. until the SPIN-interpreter finally executes the assembler-command waitcnt
This process takes minimum 385 clock-ticks.
Are you sure you want the LED turned on ony 1/Million seconds ?
Did you mean Milliseconds ? 1/1000 seconds ?
If you really need microseconds this is only possible in PASM (Propeller-Assembler)
or second idea woud be using the counters. I'm not a specialist about the counters (yet)
maybe the counterfreakss can chime in:
would it be possible to do 200 pulses that fast and then stop ?
maybe by using two counters with IO-Pins connected to each other ?
one counter somehow "pre-programmed" to count up 200 pulses and then make the other counter stop ?
I tested the following code with the oscilloscope
and measured a frequency of 26,475 kHz which is 18.885 microseconds
Masha I'm very curious for what kind of project you need such a fast blinking LED ?
Maybe 1 microsecond is even faster than the LED needs time to turn on ??
best regards
Stefan
See obex.parallax.com/objects/47/ and obex.parallax.com/objects/467/ for examples of what's doable.
@ StefanL38 : yeah 1 microsecond,actualy in Assembly i don't know how to do it .This is a LED projected to a stainless styl part in my car , i want to write some lettrs a funny things .
@Mike Green: thank you , i will read them and concentrate to the part i need.
If I understand your project correctly, you have a lot of learning to do. It's a doable, but difficult project. Take small steps. It should be fun (and frustrating too at times).
Post Edited (Mike Green) : 10/22/2009 8:08:26 PM GMT
in the attachment you find the PASM-code for bursting 200 Pulses at 500 kHz.
in your code above
there is only ONE waitcnt
with this code you switch on the pin wait
switch off and with NO waiting the pin is switched on again
This means after each wait it is switched off much shorter than switched on
Do you want it that way ? I guess not.
So my PASM-code toggles the IO-Pin with the command xor
and switches off at the end
this means the PIN is 1 microsecond high then 1 microsecond low, 1 microsecond high then 1 microsecond low ....
= 500 kHz
you have to adapt the IO-pinnumbers to your configuration (just change the number)
best regards
Stefan
Post Edited (StefanL38) : 10/22/2009 10:49:37 PM GMT