[time-nuts] Recording mains frequency/phase [WAS: NoGPSsatellites]

Tom Van Baak tvb at LeapSecond.com
Mon Mar 2 03:55:26 EST 2015


> Using a zero-crossing detector with the picPET and logging the timing of 
> each zero-crossing, how do you toss out the other 59 samples each second?

Hi Ben,

1) The PC program that reads the serial port can toss lines it doesn't want.

2) Or you can prune the log file with a C program like this:

    // filter to pass one of every N input lines
    #include <stdio.h>
    #include <stdlib.h>
    int main (int argc, char *argv[])
    {
        long count = 0;
        char line[1000];
        long nth = (argc > 1) ? atol(argv[1]) : 60;
        while (fgets(line, sizeof line, stdin) != NULL) {
            if ((count++ % nth) == 0)
                fputs(line, stdout);
        }
        return 0;
    }

3) The other solution that I often use is a version of the picPET (pP19) that deliberately takes 990 ms to output the timestamp (events are counted in h/w). That way it still counts all cycles but only outputs one timestamp per second. This is sort of like putting a prescaler in front of the picPET, but better. Better because no h/w is needed and because it is more immune to false triggers than a pre-scaler. It also works without changes for both 50 Hz or 60 Hz grids.

> Doing this after-the-fact seems do-able with some software.  I seem to 
> recall doing something like this with FORTRAN or BASIC.  (open log file, 
> open output file, read log file line by line, tossing out 59 samples and 
> writing the 60th to the output file, reach EOF, close both log and 
> output file)

Yes, see code above. Same logic in any language, but probably one line of code in awk or perl or python.
 
> But...is there a clever way to do it up front?  Time to do more reading. 
>  I've seen some folks doing frequency / phase monitoring using an 
> Arduino, but nothing (yet) that uses a high-precision reference such as 
> 10 MHz from a GPSDO.

If you do it up-front, like a pre-scaler, you have to worry about signal quality to avoid off-by-one glitches. You'll get lots of suggestions here on the mailing list and on the web about how to best detect zero-crossings. The circuits get pretty complex. It may surprise you that I don't do any of that.

The beauty of timestamping (instead of traditional counting) is that signal conditioning is much less important. I put raw 5 VAC into the PIC pin via a 10k resistor. That's it. Any "conditioning" can be done in software. About once a month I see a stray timestamp or an extra pulse. You just delete it. So you can recover from glitches. With counters you can't - which is why signal conditioning is critical in those designs.

/tvb


More information about the time-nuts mailing list