Shop OBEX P1 Docs P2 Docs Learn Events
help with a delay in a spin code — Parallax Forums

help with a delay in a spin code

mnemonicsmnemonics Posts: 23
edited 2015-01-20 17:29 in Propeller 1
Hello everyone I need help with a spin code. I am using the 2 button shift speed code and want to add a few usec 2 to 4 delay in between pulses to make sure that the LED are completely off before the next one is turned on. You can't see it with a naked eye but i saw it in a pic I took. I am running some motors and coils in a very unique way and have to make sure that in the spin code 2 ButtonShiftSpeedfinal.spin
LED from 16 to 23 are being turned on and off with 20 to 23 completely opposite of the 16 to 19 LEDs. I am running two banks Bank one 16 to 19 bank two 20 to 23 . Bank one the LED pulse train one at a time , in Bank two when the LED of bank one are off the ones on Bank two are on, so the LED on bank two turn off when the one on bank one turns on. I used the !outa[20..23] to make that bank opposite of bank one. The code I attached works like I want it too but i want to make sure that when the LEDs on bank one, pins 16 through 19 turn on that there is a delay in between pulses of a few microseconds before each LED is turned on so that Bank two does not stay on while bank one LED starts to turn on. I took a pic of the blinking LEDs and noticed that when one pin was being turned on, the opposite bank LED should be completely off .

VAR

Byte LEDS, PULSES



PUB ShiftLedsLeft

dira[16..23] ~~ 'Pin 16 to 23 output

PULSES := 5

repeat

if LEDS == 0
LEDS := %1000 ' pulse 1 pin at a time from 16 to 19
!outa[20..23] 'Pin 20 to 23 are the opposite of 16 to 20 High
'when pins are low and low when pins are high



if ina[4] == 1 'if input on pin 4 is high increment pulses
PULSES ++
PULSES <#= 254
elseif ina[1] == 1 'if input on pin 1 is high decrement pulses
PULSES --
PULSES #>= 1

waitcnt(clkfreq/PULSES + cnt) (((( Can I add the usec after the cnt to every pulse here))))))))
outa[16..19] := LEDS
outa[20..23] := LEDS
LEDS >>= 1

Just want to make sure that the LEDs turn completely off before the next one turns on. In one of my pics I can see one LED turning on and the other opposite of it was not completely off. In the transition from on to off I need to make sure with maybe a 2 usec delay between pulses if it is possible. . Thanks.. and happy holidays

Comments

  • JonnyMacJonnyMac Posts: 9,105
    edited 2014-12-16 15:16
    You can't do delays that short in Spin. The average instruction takes about 5us.
  • kwinnkwinn Posts: 8,697
    edited 2014-12-16 15:21
    You can't get a 2 - 4 uSec delay in spin. Most if not all instructions take longer than that to execute. The simplest way to do what you want would be to set pins 16 to 23 to inputs right after the waitcnt and back to outputs after the two outa instructions. That will turn all the leds out for the length of time it takes to execute those instructions.
  • Cluso99Cluso99 Posts: 18,069
    edited 2014-12-16 15:38
    Time to take the plunge and learn PASM. You only need to master a few instructions which are similar to spin (in your usage).

    Here is a simple example...
    CON
      _CLKMODE = XTAL1 + PLL16X     'Set to ext low-speed xtal, 16x PLL
      _XINFREQ = 5_000_000          'Xtal 5MHz
    
    
    VAR
    
    
    PUB Start 
    
      cognew(@entry00, 0)                            
    
      repeat
    '---------------------------------------------------------------------------------------
    
    DAT
    '' &#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9488;
    '' &#9474; Flash_pin                                                                &#9474;
    '' &#9492;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9496;
                  org
    entry00       mov       dira,pinmask
                  mov       outa,pinmask
    loop          mov       f2,flash_2
                  xor       outa,pinmask
    loop2         djnz      f2,#loop2
                  jmp       #loop
    
    pinmask       long      1 << 24                 '%0000_0001_0000_0000_0000_0000_0000_0000 'P24
    f2            long      0
    flash_2       long      5_000_000
    
  • AribaAriba Posts: 2,690
    edited 2014-12-16 17:04
    If you want be sure that the LEDs are off before the new pattern is written, just write 0-bits to the output ports before you set the new LEDS value.
    If you set pins 23..20 in the same instruction as pins 19..16 then they should change exatly at the same time:
      ...
      OUTA[23..16] := 0                          'clear LEDs and delay some us
      OUTA[23..16] := (LEDS<<4 + LEDS) ^ $F0     'set pattern and opposite in same line
      LEDS >>= 1
    
    Andy
  • brandanbrandan Posts: 52
    edited 2014-12-18 16:47
    I saw that the problem was speed. You can use c because it's faster than spin. I converted your program to c using spin2cpp. you can the simpleIDE to upload the c code.
    http://learn.parallax.com/propellerc
    c
    c
    2K
  • mnemonicsmnemonics Posts: 23
    edited 2014-12-18 19:21
    brandan wrote: »
    I saw that the problem was speed. You can use c because it's faster than spin. I converted your program to c using spin2cpp. you can the simpleIDE to upload the c code.
    http://learn.parallax.com/propellerc

    Thanks Brandan, Interesting to see the code in C I will try all the advise everyone gave me. I appreciate it my friends Happy Holidays to everyone
  • mnemonicsmnemonics Posts: 23
    edited 2014-12-18 19:24
    Ariba wrote: »
    If you want be sure that the LEDs are off before the new pattern is written, just write 0-bits to the output ports before you set the new LEDS value.
    If you set pins 23..20 in the same instruction as pins 19..16 then they should change exatly at the same time:
      ...
      OUTA[23..16] := 0                          'clear LEDs and delay some us
      OUTA[23..16] := (LEDS<<4 + LEDS) ^ $F0     'set pattern and opposite in same line
      LEDS >>= 1
    
    Andy
    I will try you Idea first Andy it looks like the simplest solution, all I need is just s very small delay to make sure they are complete of when they have too. Thank you
  • mnemonicsmnemonics Posts: 23
    edited 2015-01-20 14:16
    Ariba wrote: »
    If you want be sure that the LEDs are off before the new pattern is written, just write 0-bits to the output ports before you set the new LEDS value.
    If you set pins 23..20 in the same instruction as pins 19..16 then they should change exatly at the same time:
      ...
      OUTA[23..16] := 0                          'clear LEDs and delay some us
      OUTA[23..16] := (LEDS<<4 + LEDS) ^ $F0     'set pattern and opposite in same line
      LEDS >>= 1
    
    Andy
    Andy your Idea seems to be working it did slow down the Pulses but still with in the range I need. Is there a way now to let Turn 1 pin on at bank one then when it shuts off turn a pin on at bank two in the pattern we have here. Your code works perfectly but i need to try turning on and off one pin at a time now. Thanks
  • AribaAriba Posts: 2,690
    edited 2015-01-20 17:29
    If you want ttry around with different pattern sequences, you can make a table that contains the bit-patterns:
      ...
      OUTA[23..16] := 0                     'clear LEDs and delay some us
      OUTA[23..16] := pattern[i]            'set pattern from table
      i := (i+1) // TABLE_ENTRIES
      ...
    
    DAT
    pattern byte  %0000_0001
            byte  %0001_0000
            byte  %0000_0010
            byte  %0010_0000
            ...
    

    Andy
Sign In or Register to comment.