Hello,
I assume that you are attempting to generate a sine wave of the same frequency as the input signal. I also assume that the input frequency will be close to, but not exactly 50Hz, and would be variable over narrow limits.
I have a problem understanding how you actually intend to vary the generated frequency to achieve synchronisation, and the size of the minimum frequency increment that you require. Additionally, the sine look-up table would seem to be far too large to be practical. With 256 states in each PWM cycle, and assuming a fixed output frequency of 50 Hz, the bus frequency would need to exceed 256 * 3200 * 50 = 40.96 MHz - clearly outside the bus limit for the device you are using. A table size of 256, or maybe 512 entries would seem far more appropriate, especially as the table contains 8-bit amplitude values.
I wonder if your ISR code currently requires more than the available 250 cycles to complete, or perhaps another interrupt is causing a problem.
One method that can provide a good variable frequency resolution, with more moderate clock rate, is the Direct Digital Synthesis (DDS) method. The DDS method achieves the improved frequency resolution due to the fact that there may be small variations of step duration over each output cycle. Since these adjustments are spread over the whole cycle, there is little distortion to the output signal. Another characteristic of DDS is that, as the output frequency increases, the number of steps per cycle will gradually reduce, thus again moderating the clock frequency requirement. yet a further advantage of DDS is that the frequency adjustment parameter setting is directly proportional frequency, rather than output period, as for other synthesis methods. The MCU code for the phase update will probably need to be written in tight assembly code, and the highest bus frequency available used.
Let's set some parameters for DDS operation.
I will assume that the main DDS accumulation register is 24 bits in length, but uses only 20 active bits, and that the bus frequency is 8 MHz. Dividing this by the PWM period cycles (256) will give a maximum DDS clock of 31.25 kHz. However, the choice of a lower DDS clock frequency would allow more bus cycles for each phase update to occur. To output a constant frequency, a fixed value N needs to be added to the main register at every DDS clock interval. With this arrangement, the frequency resolution would be Fclk / (2^20) ~ 0.03 Hz. The output frequency will be given by the expression N * Fclk / (2^20). Note that the waveform at the MS bit of the main register will be a square wave at the set frequency. The upper bits of the main register would form the address for the sine look-up table, and the value from the look-up table would be applied to the PWM.
I would suggest that the PWM overflow interrupt be used to trigger each update of the PWM register. The new PWM value should then come into effect on the next overflow.
Because of the symmetry of a sinusoid, it may be possible to reduce the size of the look-up table to one-half or one-quarter of the output cycle. But this will involve further manipulations of the data, and may push over the processing cycle limit.
Regards,
Mac