You should look at the DDS synthesizers by Micro Linear. They are self contained 8 pin devices programmed through the serial port. These will go up to a few 100khz. You could square up the signal if you needed a variable frequency timing signal. Norman norman@pcseng.com > -----Original Message----- > From: pic microcontroller discussion list > [mailto:PICLIST@MITVMA.MIT.EDU]On Behalf Of N Steenkamp > Sent: Tuesday, August 04, 1998 5:43 AM > To: PICLIST@MITVMA.MIT.EDU > Subject: Re: PIC as NCO (numerically controlled oscillator) > > > Hi, > > > > *Much* more elegant. Use DDS (Direct Digital Synthesis). Your > > >isosynchronous loop runs at 5kHz (200 instructions at 4MHz crystal > > >frequency; should allow a fair amount of "thinking"). > > > > > > It adds the "frequency" value, expressed in tenths of Hz (i.e., > > >1498.5 Hz is the value 14,985 or $3A89) to the two-byte "phase > > >accumulator", modulo 50,000 and compares the modulus to half that to > > >determine the output state. Actually, where all phase increments are > > >less than half the modulus, it is easier to make the MSB of the phase > > >accumulator be the output - it will be a little asymmetrical. > > > > > > Even simpler, as you are differentiating this "frequency" > value anyway > > >to derive "step" pulses, you may take the compound carry from the > > >addition (mod 50,000) as the trigger to perform a step. The whole > > >procedure becomes a dozen or so instructions as long as the whole loop > > >is isosynchronous at 200 instructions. > > > > > > Alternately, if you find a "binary" crystal such as > 4194.304 kHz, the > > >addition can be performed modulo 65536 which is to say a pure two-byte > > >add and ignore the remainder, with an isosynchronous loop of > 160 cycles. > > > > > > Good enough? > > > > Great! > > I tried a couple of examples on paper, and see how it works. > > > > I figure (and tell me if I'm making any mistakes) > > With a 4.096 mhz x-tal, and pa=phase accumulator > > > > start: > > pa=pa+hz*2 > > output '8192' bit (bit 13) of pa > > repeat the above 4096/times sec. (250 cycles thru loop) > > > > This would give me max. freq of 2048 Hz, with resolution of .5 hz. > > To me it looks like pa=pa+hz*4. > You need to accumelate a phase of 16384 for one period of the output and > at 4096 iterations per second, you need to add 4 to pa each interation if > you want a 1Hz output. > > I would be a bit careful stating a resolution. On AVERAGE, yes, you would > achieve it, but in reality the DDS technique > does exactly what you mentioned as a *kludgy* technique: It varies the > instantaneous period of the output between two multiples of its "loop > execution time" so that the average output frequency is within the desired > of the resolution. This granularity becomes worse as your output > frequency increases, for example: > Suppose you want a 7Hz output. The period (in loops) of the output > waveform would be: > Low High > Period Nr. > 1 293 293 > 2 292 293 > 3 292 293 > 4 292 293 > 5 293 292 > 6 293 292 > 7 293 292 > 8 293 293 > 9 292 293 > ... > The first period is thus 250*(293+293)/(4096000/4) = 0.143066. This is > 0.14% too long. > The second period is 250*(293+292)/(4096000/4) = 0.142822. This is 0.02% > too short. The next 6 periods are all the same (0.02% too short), which > cancels out the 0.14% too long first period. Then it all repeats. The > frequency, based on the first period is 6.9898Hz and on the second period > it is 7.0017Hz. > > Take now an output frequency of 777Hz. The periods are (In loop > iterations, once again): > Low High > Priod Nr. > 1 3 3 > 2 2 3 > 3 3 2 > 4 3 3 > 5 2 3 > 6 2 3 > 7 3 2 > ... > > The first period is 0.0014648, 13.82% too long. > the second period is 0.0012207, 5.15% too short. > the frequency, based on the first period is 682.67Hz and the frequency > based on the second period is 819.2 Hz. > Taken over 7 periods, the AVERAGE frequency is 774.92Hz. > > So, as you move closer to your maximum output frequency, the > instantaneous frequency can be further and further away from the desired > frequency. If you look at your output wavform in the frequency domain, > then you will see that you get stronger spurious frequency components at > higher output frequencies. > > Niki >