50/60Hz Frequency conversion using PIC

Discussion in 'The Projects Forum' started by eblc1388, Jul 4, 2009.

  1. eblc1388

    Thread Starter AAC Fanatic!

    Nov 28, 2008
    For electric clock using synchronous motor designed for the other frequency vs local mains frequency, some form of frequency conversion is required.

    It is often not satisfactory to use just a crystal oscillator as one can expect timing error of about 1~4 minute per month, with the changing of ambient room temperature. An AC connected clock will do much better in long term period of months or year as the power company often undertake to correct for any timing error between an AC powered clock and an accurate time source by varying system frequency to compensate.

    In simple term, the converted frequency must be somehow related/locked to the mains frequency and the clock powered by this frequency will keep time fine as any other clock.

    There are many possibilities:

    A. using a single 8-pin PIC - By eblc1388

    1. single chip solution, works also for 60Hz into 50Hz
    2. standalone 50Hz or 60Hz standard frequency output, crystal accuracy
    3. no adjustment required
    4. software ensure exact cycles tally. e.g 600 cycle of 60Hz vs 500 cycles of 50Hz

    YouTube: 50/60Hz Frequency Conversion Using PIC

    B. Phase locked loop & 600Hz to 60Hz direct conversion - By eblc1388

    1. use x6 PLL to get 600Hz square wave from 100Hz
    2. use 5-stage ring counter to build staircase waveform(see image below)
    3. pass staircase waveform to RC filter to get 60Hz sine wave

    More information here: post#32

    Forum Post#32: frequency multiplier

    C. Harmonic method - By Rod Elliott

    1. Picking out the sixth harmonic of 100Hz (50Hz fullwave rectified) signal
    2. amplified and squaring the signal to get 600Hz square wave
    3. divide by 10 to get 60Hz square wave
    4. pass into active filter to remove all higher harmonic leaving fundamental 60Hz sine wave

    The link is here: Frequency Changer for Low Voltage Synchronous Clocks

    The bottom trace is the combined waveform output of the five stage ring counter and the top trace signal after RC filter, mentioned in option B above.

  2. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    Really nice! I've been watching the frequency conversion threads. :)

    Here is some minimalist math to add to the list, it converts 120Hz to 1200Hz, then generates 100Hz locked to the orig 120Hz. For use in clocks/timers etc that display hundredths of seconds but are still locked to 60Hz mains.

    It will also convert 100Hz->120Hz, 50->60, 60->50 etc with zero-error using the very simplest of math (a bresenham accumulator system) and just 2 variables;

    Code ( (Unknown Language)):
    2.     // C code for low-jitter generation of 100Hz from 120Hz mains
    3.     // PIC code, 4MHz xtal, TMR1 at 1:8 prescale
    4.     // uses 2 variables;
    5.     //   unsigned int bres
    6.     //   unsigned char pulse
    8.     start:
    10.     // wait here for 120Hz pulse
    11.     while(!check_120Hz()); 
    13.     // now generate 10 "fake" pulses, each is "1200Hz"
    14.     // which is 833uS. PIC 1Mhz xtal, TMR1 1:8 prescale,
    15.     // we use TMR1L period of 104 = 832uS
    16.     // note! TMR1L loop is also another zero-error system
    17.     // that we keep subtracting 104 from while retaining
    18.     // its internal error.
    20.     pulse = 10;     // make 10 "fake" pulses
    21.     TMR1L = 0;      // make first pulse immediately
    23.     while(pulse)
    24.     {
    25.         // wait here for fake 1200Hz pulse 
    26.         while(TMR1L.F7);
    28.         // now fix TMR1L and do the main bres event
    29.         TMR1L -= 104;   // subtract 1/1200th of a second
    31.         bres += 100;
    32.         if(bres >= 1200)    // if 100th sec reached!
    33.         {
    34.             bres -= 1200;
    35.             do_100th_event();   // update clock, etc
    36.         }
    38.         // subract a pulse, see if 10 done yet
    39.         pulse--;
    40.     }
    42.     // gets here after 10 "fake" 1200Hz pulses,
    43.     // need to detect a real 120Hz pulse, then
    44.     // do it all again!
    46.     goto start;
    It can be seen halfway down the page under "Advanced bresenham timing techniques" on this page;
    and there's more code on that page for generating exact 1 second periods for clock use etc.
  3. eblc1388

    Thread Starter AAC Fanatic!

    Nov 28, 2008
    Well in digital clocking one can wait until the correct moment to do things, or puts out all the required pulses within 10% of the start of the whole interval. The counter that accept the clocking pulses would not care.

    In analogue world, the AC sine waveform cannot be put on hold, and one cannot have 3/4 or 1.25 cycles of a waveform and then suddenly starts a new one.
  4. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    I agree. That is exactly the problem that the code I posted fixes. It acts as a mathematical PLL.

    That is why it is in the advanced section of the page and labeled as "low jitter".