Shop OBEX P1 Docs P2 Docs Learn Events
Generating a 60Hz signal from Propeller chip — Parallax Forums

Generating a 60Hz signal from Propeller chip

BonanzaManBonanzaMan Posts: 17
edited 2012-07-11 09:32 in Propeller 1
I am trying to generate two stable 60Hz signals shifted by 90 degrees to drive a clock device using the propeller chip. Using Timing.spin from OBEX, I can get close, but my clock is gaining ~5 minutes every 24 hours. Using an oscilloscope, I see my signal continuously drfts about +-1 Hz. See attached .spin sample. I am sure there must be a better, more accurate way of during this? Any suggestions welcomed.

Thanks.
980 x 600 - 311K

Comments

  • John AbshierJohn Abshier Posts: 1,116
    edited 2012-07-08 10:18
    What does it look like if you change DELAY to 414? There is some overhead in the Spin code you are executing.

    John Abshier
  • tonyp12tonyp12 Posts: 1,951
    edited 2012-07-08 10:27
    If you are using delays that are not periodic, you add the time i takes to set the delay and also the loop control adds some time too.

    http://forums.parallax.com/showthread.php?140813-clkfreq-cnt-confusion&p=1106211&viewfull=1#post1106211
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-07-08 10:33
    Tony is right. You program does not account for the timing overhead used to create the delays. Try this:
    CON
    
      _clkmode        = xtal1 + pll16x
      _xinfreq        = 6_250_000
    
      CHAN1           = 0
      CHAN2           = 1
    
    PUB TimeBase | time, delay
    
      dira[CHAN1] := 1
      dira[CHAN2] := 1
    
      outa[CHAN1] := 0
      outa[CHAN2] := 0
    
      delay := clkfreq / 240
      time := cnt
    
      repeat
        waitcnt(time += delay)
        outa[CHAN1] := 1
        waitcnt(time += delay)
        outa[CHAN2] := 1
        waitcnt(time += delay)
        outa[CHAN1] := 0
        waitcnt(time += delay)
        outa[CHAN2] := 0
    

    This still won't be 100% accurate, though, since 240 does not divide clkfreq evenly, but it should still give you results within the accuracy of your crystal. I got 60.00036 Hz with my Propeller when timed with an accurate frequency meter. That's 6ppm, or 1/2 second per day.

    BTW, if your crystal is a little off, you can fudge the value of _xinfreq to obtain better accuracy. My nominal crystal frequency is 5 MHz. But when I changed _xinfreq to 5_000_030, I got a reading of 60.00000 Hz. Even then, however, I can count on frequency drift with temperature.

    -Phil
  • T ChapT Chap Posts: 4,223
    edited 2012-07-08 10:56
    Why not set and forget 2 counters. Have two cogs launch two staggered counters at a specific time.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-07-08 10:59
    T Chap,

    On step at a time. The OP is just learning this stuff. Besides, getting two counters precisely phased with each other from Spin is no mean feat; and it's too soon to introduce PASM, methinks. :)

    -Phil
  • BonanzaManBonanzaMan Posts: 17
    edited 2012-07-08 11:45
    Thanks all -
    Phill, your code did stabilize the frequency. I suspect that my clock will be more accurate tomorrow :-) To your point about accuracy, would a different external crystal (one that is evenly dividable by 240) yield better results? I am using my default 6.250 MHz crystal for testing purposes.

    Greg
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-07-08 11:53
    BonanzaMan wrote:
    To your point about accuracy, would a different external crystal (one that is evenly dividable by 240) yield better results?
    Probably not. The crystal's tolerance over temperature would likely exceed any benefits to be gained from it. Besides, if that became the overriding source of error, there are ways to compensate in software.

    -Phil
  • jmgjmg Posts: 15,183
    edited 2012-07-08 14:27
    BonanzaMan wrote: »
    To your point about accuracy, would a different external crystal (one that is evenly dividable by 240) yield better results? I am using my default 6.250 MHz crystal for testing purposes.

    Once you have the numbers defined then yes, a different frequency source will start to matter.
    A 5ppm error is 13 seconds a month, if that is not 'close enough' you have some choices on what to do next.

    Generic crystals spec in the region of 50ppm, but can be trimmed (as Phil did above) to give a single point calibrate, but that still leaves temperature drift and even aging.

    A TXCO to 0.5ppm is possible, and that is roughly $5 (but hard to buy at 5-6MHz), or a GPS time code is perhaps the next step ?
  • frank freedmanfrank freedman Posts: 1,983
    edited 2012-07-08 19:52
    This issue is actually addressed in the Propeller Education Kit Labs book under I/O and Timing Basics Lab, p62 in the paper version. The topic is Timekeeping Applications. (The document is free for download if you don't want to shell out a hundred bucks for the Ed kit at Fry's)

    <Warning snarky comment coming, NOT pointed at the OP!!>
    Y'know, somewhere in that @#$@ poor documentation everyone was whining about in that other recent thread.
    <End snarky comment>
  • Mark_TMark_T Posts: 1,981
    edited 2012-07-10 04:19
    jmg wrote: »
    A TXCO to 0.5ppm is possible, and that is roughly $5 (but hard to buy at 5-6MHz)

    But you don't absolutely need 5 to 6MHz, you can use the Xinput setting for _clkmode, and then feed DC through 80MHz in to Xin
  • jmgjmg Posts: 15,183
    edited 2012-07-10 15:01
    Mark_T wrote: »
    But you don't absolutely need 5 to 6MHz, you can use the Xinput setting for _clkmode, and then feed DC through 80MHz in to Xin

    True, but even there, there are fish hooks...
    The Prop PLL is always x16 so wants 4..8MHz to be in spec, and TCXOs tend to be Clipped Sine out, and 12-26MHz
    (some go to 10MHz, but the smaller ones are moving to 20.000 MHz )

    So you could run in Xtal mode, and just run the Prop at 20MHz (certainly ok for 60Hz outputs!), but even here the prop data is vague on if the XIN or XOUT drives the internal clock buffer/schmitt in Xtal mode.

    With a clipped sine of ~ 0.8V adding one inverter stage in the fastest mode, would be useful amplification.

    A design could add a chip, like 74HC6323AD, to take the 20MHz clipped sine, to 5MHz square.
  • Duane C. JohnsonDuane C. Johnson Posts: 955
    edited 2012-07-10 16:31
    Hi T Chap;
    T Chap wrote: »
    Why not set and forget 2 counters. Have two cogs launch two staggered counters at a specific time.

    Actually, you wouldn't need 2 counters precisely set to output 60 Hz in quadrature.

    I presume BonanzaMan only requires an "approximately" 90 degree quadrature drive for a mechanical 60 Hz clock motor.
    He only needs to have a precision square wave from a single counter.
    The 90 degree is obtained with a simple 4.2 mS RC delay for the other output.

    Duane J
  • tonyp12tonyp12 Posts: 1,951
    edited 2012-07-11 09:32
    Long RC delays would have to be run through a Schmitt trigger as their long sloop is not a good signal, so RC delays should be avoided.
    As each cog have two counters and even the Spin cog have the two counters unused ( I think), would not need to launch any extra cogs to get this done.
    180deg would only require 1 counter (NCO differential), but 90deg would need both counters and you offset the start value of PHSA 90deg from PHSB's start value.
Sign In or Register to comment.