Shop OBEX P1 Docs P2 Docs Learn Events
Question on the Counter Module — Parallax Forums

Question on the Counter Module

EdgarEdgar Posts: 8
edited 2008-03-24 14:04 in Propeller 1
Hi, I was reading the Counter Modules and Circuit Applications PE Lab,
on page 6, it says that between phsa~ and dira[noparse][[/noparse]17]~, there are 624 cycles.

Shouldn't that number depend on clkfreq?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-03-24 05:09
    Nope. CLKFREQ tells you how many clock cycles there are in a second. The number of cycles between two statements is fixed by the statements and the underlying Spin interpreter's code. The actual time is determined by dividing the cycle count by CLKFREQ to get the time in seconds or 1000000 * cycles / CLKFREQ to get the time in microseconds.
  • EdgarEdgar Posts: 8
    edited 2008-03-24 05:40
    I haven't read many datasheet but the Propeller manual
    is the easiest one to understand. The number 624 was
    strange to me because phsa~ and dira[noparse][[/noparse]17]~ seemed
    like simple bit operations. I would think that it would take
    less than 10 cycles to execute if done in assembly. It is
    that much slower using the interpreter?

    It also seemed that the manual was using 624 as a time
    value instead of a cycle value:

    Counter Module PE Lab Manual wrote on page 6:

    Where did 624 come from?
    The number of clock ticks between phsa~ and dira[noparse][[/noparse]17]~ was determined by replacing the 0.01 μF
    capacitor with a 100 pF capacitor and finding the lowest value before zero was returned. In the test program,
    time := phsa replaces time := (phsa – 624) #> 0, and the lowest measurable value was 625.


    The code:
    PUB Main | time
      '' Repeatedly takes and displays P17 RC decay measurements.
      repeat
        ' Charge RC circuit.
        dira[noparse][[/noparse]17] := outa[noparse][[/noparse]17] := 1 ' Set pin to output-high
        waitcnt(clkfreq/100_000 + cnt) ' Wait for circuit to charge
        ' Start RC decay measurement. It's automatic after this...
        phsa~ ' Clear the phsa register
        dira[noparse][[/noparse]17]~ ' Pin to input stops charging circuit
        ' Optional - do other things during the measurement.
        Debug.str(String(10, 10, 13, "working on other tasks"))
        repeat 10
          Debug.tx(".")
          waitcnt(clkfreq/30 + cnt)
        ' Measurement has been ready for a while. Adjust ticks between phsa~ & dira[noparse][[/noparse]17]~.
        time := (phsa - 624) #> 0
        ' Display Result
        Debug.Str(String(10, 13, "time = "))
        Debug.Dec(time)
        waitcnt(clkfreq/2 + cnt)
    
    



    My confusion is that if 624 was just the number of cycles
    of the interpreter, then the test with capacitance was
    probably irrelevant. If the capacitance test was relevant,
    then 624 corresponded to a duration, but the unit of
    624 was in clock ticks.

    Post Edited (Edgar) : 3/24/2008 5:51:16 AM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2008-03-24 13:47
    The 624 cycles figure is the time (in clock ticks) for the interpreter to fetch and interpret the several bytes of interpretive instructions for the "dira[noparse][[/noparse]17]~". If this were done in assembly, it would take one instruction for the "phsa~" and one for the "dira[noparse][[/noparse]17]~" with each instruction taking 4 cycles. Often interpreters run about 100 times slower than the best equivalent assembly and this one is no exception.

    This particular lab illustrates how you can measure the time using an RC circuit. The time is measured in clock ticks because there's a built-in timer that's incremented every clock tick. I'm sure this lab is written this way because it's designed to work with different Propeller boards and some of them come with clock crystals that produce a different system clock frequency.
  • EdgarEdgar Posts: 8
    edited 2008-03-24 14:04
    I understand now, thanks.
Sign In or Register to comment.