I'm working on a project where I'd like to sync a LFO implemented as a phase accumulator oscillator to incoming MIDI clock on the UART of a microcontroller. The current setup is using an ATMega168 with a series of 256 element lookup tables of 8 byte values that contain the bandwidth-limited LFO waveforms. A 24 bit phase accumulator is used and the top byte of the accumulator is used as an index into the wavetable; at the appropriate times the accumulator is incremented and the value stored at the wavetable index is output to the ATmega's PWM analog outputs after being linear interpolated. A 4 pole Bessel filter on the output filters the sampling frequency and the 64Khz PWM frequency.
This seems to work OK, but what I'd like to do now is sync the frequency of the LFO to incoming MIDI clock. This comes in on the UART as a data byte sent by the MIDI master device 24 times per quarter note. I'm looking for ideas on what might be the best approach to synchronize the LFO to the incoming clock? The LFO frequencies would be significantly lower than the incoming clock, with a maximum period of perhaps 16 seconds down to a quarter second depending upon the tempo and user settings. One straightforward idea I considered was to extract a rough tempo from the average time between the clock bytes as a running calculation, calculate the angle increment for the accumulator based on that, start the LFO going, and then keep count of how many clock ticks it takes for the LFO to complete a cycle. Compare that to how many clock ticks there actually should be in a cycle, and then nudge the angle increment up or down accordingly.
I have a feeling, however, that this will work like crap at the slow frequencies I have in mind. The lock doesn't need to be extremely tight, though, just "good enough for rock 'n roll." Would something like a phase locked loop work better in this application, even at these frequencies? I've never tried doing a PLL in software and am frankly pessimistic about my abilities as far as getting one working - I haven't done much DSP programming. I'm willing to give it a try, however, if that's the solution that's recommended. Thanks for any advice.