Shop OBEX P1 Docs P2 Docs Learn Events
Recording data at a given clock frequency — Parallax Forums

Recording data at a given clock frequency

RussMRussM Posts: 27
edited 2013-04-01 06:15 in Propeller 1
Hey everyone. I have a program that constantly records data from two rotary quadrature encoders and I am trying to adjust it to only record data every 60 Hz. I am unsure as how to actually do this. My first thought was to use a simple if statement to check if the clock frequency was a multiple of 60 using the mod operator, something like:
if ( (clkfreq // 60) == 0)
  'record data

My second idea was to manually change the frequency of the system clock using the following declarations (I think this is correct):
_CLKMODE = XTAL2
_CLKFREQ = 60_000_000

I don't have my propeller demo board with me at the moment so I won't be able to test these out but I was hoping to gain some insight on the issue beforehand. Any suggestions are greatly appreciated. Thank you!

Comments

  • jmgjmg Posts: 15,173
    edited 2013-03-31 19:28
    RussM wrote: »
    ... I am trying to adjust it to only record data every 60 Hz.

    Search the docs/ manuals for waitcnt/waitpeq/waitpne .
    The simplest is usually waitcnt, but a small detail is that will not quite hit your 60Hz, what you will actually get is
    80e6/round(80e6/60) = 60.00001500000375 Hz, but that is less than 1ppm of error, so is likely to be tolerable.
  • JonnyMacJonnyMac Posts: 9,108
    edited 2013-03-31 21:17
    You can use a synchronized loop -- lauch something like this into its own cog when you want to record.
    pub record(p_data) | t
    
      t := cnt
      repeat
        long[p_data++] := new_reading
        waitcnt(t += (clkfreq / 60))
    
  • RussMRussM Posts: 27
    edited 2013-04-01 05:16
    jmg wrote: »
    Search the docs/ manuals for waitcnt/waitpeq/waitpne .
    The simplest is usually waitcnt, but a small detail is that will not quite hit your 60Hz, what you will actually get is
    80e6/round(80e6/60) = 60.00001500000375 Hz, but that is less than 1ppm of error, so is likely to be tolerable.

    How would this code look? I've only ever used waitcnt to pause for a time delay.
  • rjo__rjo__ Posts: 2,114
    edited 2013-04-01 06:15
    Howdy...

    The good thing about not using a waitcnt... is that your whole loop of code won't be waiting.



    Problems with your approach:

    1) It isn't clear how much accuracy you want.

    2) _Clkfreq is a constant... so is clkfreq... so, clkfreq/60 should always be 1x10^6 and clkfreq//60 should always be zero.

    and you are using spin... which isn't going to see the system counter in real time, anyway.

    I have used a clock object to obtain timed events using spin. Don't remember which one or why:) Take a look in the OBEX.
    problem....
    The problem with using a clock type approach...the more often you check the clock the more accurate your result... but the slower everything else in your loop will execute.
    And there will always be some kind of error... which will generally be a "normal" error... affecting all your data and can be corrected.

    I like Jeff Martin's encoder... which gives you the facility to see change in the values since last polled... so if you poll on a regular basis you get nice comparable values.

    ... sorry no link right now... but it is in the obex.

    Rich
Sign In or Register to comment.