Timer0 Reload based on ADC result


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2010
    Posts
    10

    Default Timer0 Reload based on ADC result

    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

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3
    Join Date
    Jan 2010
    Posts
    10


    Did you find this post helpful? Yes | No

    Default

    All right. Thank you very much.

  4. #4
    Join Date
    Jan 2010
    Posts
    10


    Did you find this post helpful? Yes | No

    Default

    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

  5. #5
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    What do you get with a timer reload value of 65,362?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  6. #6
    Join Date
    Jan 2010
    Posts
    10


    Did you find this post helpful? Yes | No

    Default

    I get 37.5 Hz

  7. #7
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Is your ADC=200?

    How many entries do you have in the new table?
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  8. #8
    Join Date
    Jan 2010
    Posts
    10


    Did you find this post helpful? Yes | No

    Default

    My ADC value is 200 (50 Hz) and my new table has 72 entries from 0 to 350 in steps of 5.

  9. #9
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  10. #10
    Join Date
    Jan 2010
    Posts
    10


    Did you find this post helpful? Yes | No

    Default

    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

  11. #11
    Join Date
    Feb 2011
    Posts
    2


    Did you find this post helpful? Yes | No

    Default Re: Timer0 Reload based on ADC result

    Quote Originally Posted by Cyborg View Post
    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

Similar Threads

  1. Stable Adc Reading Routine
    By gebillpap in forum General
    Replies: 27
    Last Post: - 13th May 2015, 03:18
  2. Strugling without floating point
    By pjsmith in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 27th March 2011, 07:29
  3. ADC problem with PIC16F917
    By eetech in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 7th March 2007, 22:22
  4. ADC value with 2 decimals on an LCD
    By Squibcakes in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 2nd December 2005, 16:54
  5. PBP 16-bit ADC result math
    By sonic in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 13th March 2005, 15:21

Members who have read this thread : 1

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts