Shop OBEX P1 Docs P2 Docs Learn Events
Help with easy code. — Parallax Forums

Help with easy code.

BeginerBeginer Posts: 21
edited 2009-12-11 21:49 in Propeller 1
Hello, could you help me with this easy code in spin? I want to sense states at pin 7. At pin is log.1 initial. When come first log.0 at this pin, program will wait to coming second log.0. In this time run repeat cycle that increase variable COUNT. Then i want to active pin(16) for time which depends on COUNT. But I have problem. Real time to waiting next log.0 is not equal as real time that the pin7 is activate !!! I think, Repeat cycle is "too slowly". I cant understand it.
One questation yet. How highest frequency could be sensing in this code? Schould I write it in asembler? Thank you.

Zdenek (Czech)

Comments

  • TimmooreTimmoore Posts: 1,031
    edited 2009-08-22 22:07
    If I understand correctly you want to time pin 7 from 0 to the next 0?
    Try something like this, delay should contain time in clocks between 2 0's

            Pin := 7
            waitpne (|< Pin, |< Pin, 0)  'wait until 7 is not 1
    
            beg := cnt  ' get start time of pin 7 == 0
          
            waitpeq (|< Pin, |< Pin, 0)  ' wait until 7 is 1
            waitpne (|< Pin, |< Pin, 0) ' wait until 7 is not 1
            end := cnt  'get time when pin 7 ==0
            delay := end - beg
    
    
  • StefanL38StefanL38 Posts: 2,292
    edited 2009-08-23 05:21
    hello Beginner,

    can you write what you want to do IN THE END in normal words
    (no describing of code)

    anyway
    your code seems to do count the amount of time as pin7 is low
    after measuring the time set pin 16 high for the SAME time
    Count:= Count + 1 does count at a much slower speed than the systemvariable "cnt" is counting
    so it cannot be done this way

    therefore you have to use something like timmoore suggested
    and then code

    code]
    waitcnt(delay + cnt)

    [noparse][[/noparse]/code]

    if it is okay that pin 16 is high PARALLEL to pin 7 beeing low your code reduces to

      repeat
        outa[noparse][[/noparse]16] := not INA[noparse][[/noparse]7]
    
    
    



    to add some information

    you should not code

       waitcnt(cnt + 4_000))
    
    



    always as do it this way

       waitcnt(4_000 + cnt) '<== + cnt at the END
    
    



    add the actual value of cnt at the END of the expression to keep the time that will pass by between interpreting the spin-expression
    and the REAL call of the PASM-command waitcnt (done by the interpreter) as short as you can

    because of the interpreting overhead the minimum value to wait is 385

       waitcnt((385 + cnt) '<== + cnt at the END
    
    



    with values lower than 385 the counter has already passed your value

    example:
    value of counter is 1000

    interpret spin-command

       waitcnt(250 + cnt) '<== + cnt at the END
    
    



    meaning wait for cnt-value to match 1250

    AFTER interpretating the expression "(250 + cnt)" the value of cnt will be
    already 1384 and it will take 53 seconds until the counter has counted up to 2^32 and rolls over to zero and count up from zero to 1250 again


    once again:
    in your project you will do much more in the end then just that few lines of code

    Write a posting that says ALL about your project.
    In the past it has been always the same:
    as soon as the COMPLETE project is described much better solutions to detail-problems were found

    best regards

    Stefan
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-12-11 21:49
    Your duplicate posts in the BASIC Stamp Forum were removed. As a new member please read the forum guidelines at the link below. Take care.

    http://forums.parallax.com/showthread.php?p=467912

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Parallax Engineering
    Check out the new Savage Circuits TV!
    ·
Sign In or Register to comment.