--- Wagner Lipnharski wrote: > You can use a timer interrupt, calibrated to > generate one interrupt each > one millisecond (this is your granularity, or > resolution, isn't?). > > You need to specify 3 x 16 bits reload counters, > each one would contain > exactly half the value from each one of the 3 > frequencies (double > frequency), necessary to flip the output pin twice > per complete > frequency cycle. Then, the reload counters are > loaded into 3 decrement > counters (16 bits each). > > Each loop through the interrupt routine will > decrement each one of the > decrement counters, when any reaches zero its > correspondent port pin > output should flip level, then this decrement > counter is load with its > correspondent reload counter value. > > If your instrument only job is generate those 3 > frequencies, *and > nothing else*, this can be done with a one > millisecond timer routine, > but in this case you should have the same number of > instruction cycles > executed with or without the "flip output and reload > timers" steps, so > the routine would run steady without any jitter. > > Remember that in all cirscunstances involving > software, you will never > have the 3 wave forms happening at exactly the same > time, if your > specify the same frequency for all of them. They > will always have some > microseconds between edges. I'm sure that Wagner, like always, has good intentions, but don't let his absolutisms fool you. The subject you wish to learn more about is: phase accumulators. Do some home work and search about this on the web. Others have commented (quite succinctly) on this list in the past. I remember posts from Paul (not sure which one) and Mike Keitz that were both very good. So perhaps the archives would be a good place to begin. A phase accumulator in C would go something like this unsigned delta_phase,new_phase; delta_phase = ----; do { new_phase = phase + delta_phase; if(new_phase < phase) output ^= 1; phase = new_phase; } The trick is in the roll-over arithmetic. Each time you go through the loop, you add delta_phase to the phase. If this rolls over, then you toggle the output. The output frequency is half of the roll over frequency. If the update rate is too slow, the output frequency will have a lot of jitter. The 'average' output frequency would be correct, but most people want the instantaneous frequency to be correct too. So to mitigate this problem, the phase is updated as fast as possible. Perhaps an example would clarify the concept: Suppose you wanted to generate 1000Hz and were updating the phase accumulator every 50uS. Then exactly 20 (1/1000/50uS)iterations in the loop above (with the appropriate delta_phase) would generate the 1000Hz square wave. However, if you wanted to generate a 900Hz square wave you'd need to iterate through the loop 22.2222 (1/900/50uS) times! This of course isn't possible. Consequently, the phase accumulator will constrict you to either 909.0909Hz (1/22/50uS) or 869.565Hz (1/23/50uS) exact square waves. To obtain an average frequency of 900Hz, you'll have to squirt out a combination of 909 and 869 Hz square waves. The phase accumulator algorithm will automatically take care of this for you, but you'll have a bit of jitter... As far as generating 3 square waves simultaneously, well you simply have three different phase accumulators. If you want them to change at the same time (i.e. no phase shift if they're perfectly sunchronized) then you'd store the toggle info in a register and then write that register out to the I/O port. .lo ===== __________________________________________________ Do You Yahoo!? Bid and sell for free at http://auctions.yahoo.com