Shop OBEX P1 Docs P2 Docs Learn Events
Duration counter — Parallax Forums

Duration counter

JkaneJkane Posts: 113
edited 2015-03-12 09:05 in Propeller 1
Hello,

i have another question

what I am doing is running a repeat for 1 second, during that one second i collect data, then break out using a repeat while, then I post a message and reset for the next second of reading

the issue that seems to be occuring is that most of the time, I get 1 second intervals, but every so off i get a burst of messages, as if the repeat while is being by passed.

I thinik it is the way I set up the one second interval. loop and the test are wrong

Here is the code
{Object_Title_and_Purpose}


CON
        _clkmode = xtal1 + pll16x                                               'Standard clock mode * crystal frequency = 80 MHz
        _xinfreq = 5_000_000

VAR
                
  long Transition_counter
  long pin_state
  long previous_pin_state
  long GPM
  long GPM_temp
  long Dt
  long record_number
  long t
 
  

OBJ
   term   : "FullDuplexSerial"
   fMath : "FloatMath"
  '  fMath : "Float32full"   
  fString : "FloatString"
  
PUB Main 

term.start(31, 30, 0, 115200)

record_number := 1

t := 0

dira[8]~  ' set to input := 0

dT  := clkfreq

repeat
  term.tx(13)
  term.str(string(" Rec Num: "))
  term.dec(record_number)
  record_number++
    
  t := clkfreq + cnt               ' get next second
  repeat while cnt < t              ' loop until second is up
     
     pin_state := ina[8]         ' check high or low
    
        if pin_state == previous_pin_state
             'do nothing
        else
            previous_pin_state := pin_state   ' make new pin state
            transition_counter++              ' and count the transision

I tried different ways, but the results is usually the same. most of the examples I have seen are using waitcnt for delays, but I want to process during the second, and not delay

regards

Jeff

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2015-03-12 08:09
    Spin comparisons are always signed. IOW your repeat while cnt < t will occasionally misbehave. In this case I'd suggest using deltas (see FullDuplexSerial, rxtime method).
  • Mike GreenMike Green Posts: 23,101
    edited 2015-03-12 08:13
    CNT is an unsigned 32-bit number while the less-than and greater-than operators work on 31-bit signed numbers, so "cnt < t" won't work as you expect. You need to do something like:
    startTime := cnt
    repeat while (cnt - startTime) < clkfreq
       'stuff
    
  • JkaneJkane Posts: 113
    edited 2015-03-12 09:05
    Thank you,

    works fine now, no runaway output

    New code
     t := cnt
       repeat while (cnt - t) < clkfreq
           pin_state := ina[8]    ' check high or low
    
    

    old code (which did not work)
    't := clkfreq + cnt               ' get next second
      'repeat while cnt < t              ' loop until second is up
    
    

    regards

    Jeff
Sign In or Register to comment.