Hi,

I have been trying to figure out how the following equation works, i.e.

timer0 reload value = FFFFh - (Fosc/4)/ (2*(sine table entries - 1)(timer0 prescaler value/4)*frequency)

This equation can be found in AN900 and AN889 documents. It is used to update the PWM duty cycle based on the value of ADC.

The timer0 prescaler value is divided by 4 to compensate ADC multiplication factor of 4 to the frequency as the ADC is set to 10 bits. So, ADC value is treated as 8 bits (256).

The sine table entries are 19 so there are 36 samples per cycle and timer0 prescaler is 1:8. The crystal is 20 MHz (PLL is off)

when frequency is set to 50 Hz (ADC = 200),

timer0 reload value = FFFFh - 5Mhz / (36*(8/4)(200)) = 65535 - 347 = 65188

hence, timer0 overflows when it counts up to 347 and generates an interrupt.

The equation above can be written as follows:

timer reload value = FFFFh - (Frequency scale/ Frequency) = FFFFh - (69444/200) = 65188

the instruction cycle time period is 1.6 us. This is calculated from the equation 1/(5MHz/8).

so, the interrupt is generated every 555 us (1.6 us * 347) and this gives a frequency of 1.8 KHz.

This is the point which i dont understand. I thought the interrupt should be generated every 20 ms to give a frequency of 50 Hz!

The PWM frequency is fixed at 20 KHz. When I passed the PWM to a low pass filter with a cut off frequency of 100 Hz, I did get 50 Hz. So, the equation above works but how?

Thanks

## Bookmarks