PDA

View Full Version : Timer0 Reload based on ADC result



Cyborg
- 9th January 2010, 13:40
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

Bruce
- 9th January 2010, 15:48
With 36 samples per cycle (of your 50Hz sine wave) this sounds about right.

The app continuously adjusts PWM duty cycle to create the sine waves, so
1.8kHz/36 (samples per cycle)=50Hz.

Cyborg
- 9th January 2010, 16:30
All right. Thank you very much.

Cyborg
- 15th February 2010, 12:45
Hi again,

I changed my sine entries to 72 samples from 0 to 350 in steps of 5. According to calculations above, TMR0 interrupt is generated every 141us (88*1.6us) so for 142 samples the frequency is 50 Hz (1/0.02 s). However, I am getting at the output 40 Hz sine wave! What I am doing wrong ? I used 72 samples instead of 142 also as it is one complete sine wave but I still get 40 Hz!

In the first post, the sine table had 19 entries from 270 - 90 in steps 10 and I was getting 50 Hz sine wave.

I am simulating my program on Proteus 7.5.

Thanks

Bruce
- 15th February 2010, 14:34
What do you get with a timer reload value of 65,362?

Cyborg
- 15th February 2010, 18:33
I get 37.5 Hz

Bruce
- 15th February 2010, 18:40
Is your ADC=200?

How many entries do you have in the new table?

Cyborg
- 15th February 2010, 18:49
My ADC value is 200 (50 Hz) and my new table has 72 entries from 0 to 350 in steps of 5.

Bruce
- 15th February 2010, 19:04
timer0 reload value = FFFFh - 5Mhz / (36*(8/4)(200)) = 65535 - 347 = 65188
If you haven't changed the prescaler it should work according to your equation.

5MHz/(72*(8/4)*(200)) = 5MHz/28,800=173.6

65,535-173=65,362!

If you go by equation 3 in AN900 it would be around 65,447 for a reload value.

Cyborg
- 16th February 2010, 10:12
I will test the software on my development board and see if I get a sine wave of 50Hz or not. Maybe Proteus is not simulating the program at the right clock speed.

Thanks

xtrem7
- 22nd February 2011, 07:11
when frequency is set to 50 Hz (ADC = 200),
timer0 reload value = FFFFh - 5Mhz / (36*(8/4)(200)) = 65535 - 347 = 65188


hi,
i am referring to application note AN843 for my project, and i am having some difficulties calculating some parameters
i have been through your post, and it helped me quite a lot, but i have some things to clarify with you
how do you set the frequency of the sine wave from the potentiometer
i mean since it is a 10bit ADC, reference setting from 0-5V gives me 0-1023,my question is how does your input vary with the frequency(5V--->200ADC-->(200/4=50Hz)
and secondly what does the value of timer0 reload value correspond to (65188)
else my value of prescalar is 16, and as per your equations i do get the value of 50Hz when i use the ADC=200 value, but i do not understand how you obtain that value
thanks in anticipation