Shop OBEX P1 Docs P2 Docs Learn Events
keeping time but not waiting — Parallax Forums

keeping time but not waiting

Digital JunkieDigital Junkie Posts: 11
edited 2009-09-24 18:46 in Propeller 1
Hello,
I just got my propeller and I was wondering if there is a way to keep time but not waiting for it to pass. I want to send data to the computer every 2-60 seconds (I want to be able to change this value from 2secs to 60secs) but want to continue running many other things in the code. Is this possible using the cnt someway.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-09-24 14:01
    CNT increments continuously, so it can be used to keep track of time up to about 50 seconds (32-bits).

    Beyond 50 seconds, you have several choices. You can devote a cog to timekeeping or you can use a pair of cog counters and a spare I/O pin. One of the counters is set up to produce a 1Hz pulse on one I/O pin and the 2nd counter simply counts the 1Hz pulses. The I/O pin itself is not connected to anything.

    See the application note on the counters (AN001) for details of the counter modes and the use of the counters.

    Since you want the Propeller to do something when a time interval is up, you might want to use a cog to take care of the whole thing. It can use CNT to produce a 1 second down counter, then wait for the counter to go to zero, send the data, and start up the counter again ... all separate from the rest of your program and what it's doing. You may need a lock / semaphore so that this timer driven routine doesn't try to send the data while the rest of your program is changing it.

    Post Edited (Mike Green) : 9/24/2009 2:07:39 PM GMT
  • Digital JunkieDigital Junkie Posts: 11
    edited 2009-09-24 14:16
    Thanks for the advice

    I am not into using many cogs and other pins to keep a time value. How about this

     repeat
        IF (SecClock(Time) == 3) '< the 3 can be changed up to ?       
                                    Time  := cnt
                                    'do something here
        'more stuff here 
    
    



    PUB SecClock(Past): Out | Present , tick
    
      Present := Cnt   
             
      IF (Present < Past)                         
          Tick := (Present + Past)
                       
      IF (Present => Past)      
          Tick := (Present - Past)
    
      Out := Tick / (clkfreq / 1) 
    
    
    



    So far its working but tends to wander after a few minutes jumpin.gif
  • Mike GreenMike Green Posts: 23,101
    edited 2009-09-24 14:27
    Remember that CNT is a 32 bit register containing an unsigned number and that Spin does all arithmetic in signed 32 bit arithmetic. Your technique will not work very well as CNT goes from looking like it's positive to looking like it's negative and back again over a period of less than a minute.

    Generally it's best to compare time intervals like:

    if (CNT - startTime) > delayPeriod

    where startTime was set to CNT and delayPeriod is a delay time in clock ticks (12.5ns normally).

    This limits your timing to about 25 seconds maximum. Beyond that, you have to use the cog counters or a separate cog to keep accurate time.

    Post Edited (Mike Green) : 9/24/2009 2:36:22 PM GMT
  • Digital JunkieDigital Junkie Posts: 11
    edited 2009-09-24 14:38
    What is a cog counter?
  • Mike GreenMike Green Posts: 23,101
    edited 2009-09-24 14:45
  • StefanL38StefanL38 Posts: 2,292
    edited 2009-09-24 14:51
    the propellerchip has 8 cores called "cog"

    you can see them as 8 separate microcontrollers inside one housing each having his own RAM (cog-RAM)
    and shared HUB-RAM

    Each cog has 2 32-bit-counters which can be configured different modes

    best regards

    Stefan
  • photomankcphotomankc Posts: 943
    edited 2009-09-24 15:26
    I'd second the motion of going ahead and learning to work with cogs to get some time-keeping. Having a background timer to add simple time-keeping to apps has been quite helpful in several of my projects. I have a simple run-timer I can add to my code to get the HH:MM:SS that the timer has been running as well as a straight seconds count.
  • Digital JunkieDigital Junkie Posts: 11
    edited 2009-09-24 18:46
    Thank you everyone.
Sign In or Register to comment.