Shop OBEX P1 Docs P2 Docs Learn Events
How to control pasm repeat loop? — Parallax Forums

How to control pasm repeat loop?

lardomlardom Posts: 1,659
edited 2016-12-02 16:10 in Propeller 1
I just started trying to write pasm code. I wanted to see if I could get an led to blink five times. What I get is some multiple of five. Why is that happening?
CON
_clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000

VAR

  long  cog                         
  
PUB start : okay
 
okay := cog := cognew(@entry, 0) + 1   
 
DAT                         '

entry                 org                                                                 

                      mov     CountDown, #1
                      shl     CountDown, #3
                      or      dira,      CountDown
                      mov     p,         #5
                      mov     time,      cnt
                      add     time,      delay 
:loop                 or      outa,      CountDown
                      waitcnt time,      delay
                      andn    outa,      CountDown
                      waitcnt time,      delay
                      djnz    p,         #:loop
                      

' Uninitialized data

CountDown       long    0
delay           long    30000000

p                    res     1       
time                 res     1

Comments

  • AribaAriba Posts: 2,690
    You need some code behind the loop, that stops the cog or makes an infinite jump to itself:
    halt      jmp   #halt
    
    otherwise the cog just executes all the data that is in cogmemory until the end, wraps around and executes the whole code again...

    Andy
  • Or you can make the cog stop itself:
    cogid   $
    cogstop $-1
    

    Note that this reuses the register that held the cogid instruction to hold the cog id. It doesn't matter that it clobbers the cogid instruction, since the next instruction stops the cog anyway and it will just get reloaded if you restart the cog.
  • Or you can make the cog stop itself:
    cogid   $
    cogstop $-1
    

    Note that this reuses the register that held the cogid instruction to hold the cog id. It doesn't matter that it clobbers the cogid instruction, since the next instruction stops the cog anyway and it will just get reloaded if you restart the cog.
    I tested both techniques and saved them to a folder for reference. Thank you.
  • Note that if you just have the cog loop forever with a "jmp #$" instead of having it stop itself, it won't be available for use by another COGNEW and it will waste energy. You should almost always stop a cog when you're done with it instead of just having it loop forever.
  • @Electrodude, I took a screenshot of what you said and added it to my desktop folder that I call "pasm studies". Thanks.
  • PropBASIC is a great PASM-learning tool. I regard it as RAD for PASM.
    You simply write BASIC code and it spits out pure PASM which can then be grafted to your other PASM code.

    PropBASIC is the only reason I use the Prop.
  • @Mickster, thanks a million. :smile:
  • Note that if you just have the cog loop forever with a "jmp #$" instead of having it stop itself, it won't be available for use by another COGNEW and it will waste energy. You should almost always stop a cog when you're done with it instead of just having it loop forever.

    The exception to that is if you want the cog to continue to drive an output pin, because stopping
    a cog will release its control of pins - they will go high-impedance unless another cog is also
    controlling them.
  • @Mark_T, I found three situations where I would use
    cogid   $
    cogstop $-1
    
    What Electrodude made clear, at least in my case, was that if the cog was finished you want it to shut down. Jumping to a label instead of shutting it down would cause the cog to 'hang'.
Sign In or Register to comment.