Shop OBEX P1 Docs P2 Docs Learn Events
toggle a LED — Parallax Forums

toggle a LED

Maria IvanovnaMaria Ivanovna Posts: 8
edited 2009-10-22 22:37 in Propeller 1
Hello Forum ,
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 smile.gif

Thank you in advance,
Masha

Post Edited (Maria Ivanovna) : 10/22/2009 7:15:37 PM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-22 18:57
    1) You probably will not be able to turn the LED on and off with 1us pulses using Spin by itself. The execution time of Spin statements is on the order of 1us. Download and read the application note on the cog counters. You should be able to easily produce 1us pulses using the counters as long as the off time is also 1us. You can find AN001 on the Propeller downloads page.

    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
  • SRLMSRLM Posts: 5,045
    edited 2009-10-22 19:00
    Please post your full code. What you have posted isn't everything (you need at least an OBJ section and a VAR section).

    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.
  • MicrocontrolledMicrocontrolled Posts: 2,461
    edited 2009-10-22 19:01
    Your line

    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




  • Maria IvanovnaMaria Ivanovna Posts: 8
    edited 2009-10-22 19:09
    Thank you Mike,
    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
  • StefanL38StefanL38 Posts: 2,292
    edited 2009-10-22 19:21
    Hello Masha,

    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
    CON
      _clkmode = xtal1 + pll16x                  ' System clock → 80 MHz
      _xinfreq = 5_000_000
    
      LED_CONTROL = 4
      X_PULSE     = 0
      
    PUB TestLED |n
    
      DIRA[noparse][[/noparse]LED_CONTROL] := 1
    
      repeat   
        repeat while ina[noparse][[/noparse]X_PULSE] > 0  
          repeat n from 0 to 200
            !outa[noparse][[/noparse]LED_CONTROL]   'toggle LED
    
    



    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
  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-22 19:32
    In assembly language, you can toggle an I/O pin with one instruction in about 50ns and the assembly WAITCNT is accurate to the nearest clock cycle (12.5ns). There are existing routines in the Propeller Object Exchange that use the counters to make pulse trains up to 125MHz.

    See obex.parallax.com/objects/47/ and obex.parallax.com/objects/467/ for examples of what's doable.
  • Maria IvanovnaMaria Ivanovna Posts: 8
    edited 2009-10-22 19:48
    Helllo,

    @ StefanL38 : yeah 1 microsecond,actualy in Assembly i don't know how to do it smile.gif .This is a LED projected to a stainless styl part in my car , i want to write some lettrs a funny things tongue.gif .

    @Mike Green: thank you , i will read them and concentrate to the part i need.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-22 20:03
    If you want to use a rotating LED to project images on a surface, you will need to look at the video drivers, particularly the VGA driver (VGA.spin) that comes with the VGA graphics demo included with the Propeller Tool. Instead of the 8 bits of video information, you would probably just have one bit (to light the LED), but the video generator would be able to do the timing for you and shift out the information from the video generator's registers.

    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
  • StefanL38StefanL38 Posts: 2,292
    edited 2009-10-22 22:37
    OK Masha

    in the attachment you find the PASM-code for bursting 200 Pulses at 500 kHz.

    in your code above

      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 
    
    



    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
Sign In or Register to comment.