On 14/07/2011 11:30, RussellMc wrote: > Determine period of input waveform. > > 1. You can gross-synchronise by just starting your sine wave on its > rising edge at the negative to positive zero crossing. I think you > already know that. > > 2. If you have a synthesised sinewave already being output and you > want to bring it into synchronisation with an external waveform of the > same frequency then you MUST slip frequency either + or - in order to > change phase. eg if you are say zero crossing 30 degrees too late you > need to progressively advance each of your zero crossings by a > suitably small amount until you catch up. eg you could decide that an > OK rate was 1 degree per cycle. By effectively speeding up to 360/359 > x 50 Hz =3D 19,944 uS/cycle instead of 20,000 uS you will catch up in 30 > cycles +` 0.6 seconds. How fast is acceptable is up to you. > > 3. Synchronising with phase offset is just a matter [tm] of > determining where the desired phase point is wrt the input zero > crossings and steering for that as above. > > 4. To maintain running synchronisation you can monitor input zero > crossings wrt output zero crossings and make decisions about changing > output sampling rate as above. Simplest is just add or subtract an > extra delta count to compensate for observed error. Nicer is to > introduce correction which takes several cycles to correct. You could > end up at something like full PID and also introduce input frequency > filtering in a particularly obnoxious environment but this is unlikely > to be needed. > > > Russell McMahon Excellent stuff. Better than I can say it. And you only need 1/4 of a sinewave table (90 degrees) for 360 degrees,=20 and any frequency and phase, even a number output at once is generated=20 from the one table. The PIC16F887 is fine to start will. If using JAL or C to program, you=20 can move trivially to cheaper and better 18F4550 which has option of USB=20 slave port and is up to 48MHz internal clock from a wide range of=20 external crystals, instead of clock =3D crystal on 16F887 and max of 20MHz. Unless you are (a) already conversant with C, or (b) absolutely want to=20 use C, try JAL. It's specially designed for 10F/12F/16F/18F family and=20 embedded Programming. --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .