Changing the sines frequency?


Results 1 to 33 of 33

Threaded View

  1. #4


    Did you find this post helpful? Yes | No

    Default Re: Changing the sines frequency?

    Quote Originally Posted by HenrikOlsson View Post
    Hi,
    Running the PIC at 40Mhz and having a prescaler on TMR1 of 1:1 (no prescaler) makes it "tick" at 10MHz. Your sine-table, if I understand correctly contains 72 "steps" so you need 72 interrupts per second per Hz output frequency.

    At 120 Hz output frequency you need 120*72= 8640Hz or an interrupt interval of 115.7us. At 10Mhz one "tick" is 0.1us so you need 1116 ticks between interrupts and therefor you should (theoretically) reload the timer with: 65536-1116 = 64420.

    At 10Hz you need 720 interrupts per second or an interrupt interval of 1388.9us. That's 13889 ticks so you need to reload the timer with 65536-13889 = 51647.


    Now, when the timer overflows and trips the interrupt a lot of things has to happend. The DT-Ints interrupt "engine" has to save all the PBP system varibles and this takes time. By the time the code actually gets to reloading TMR1 with your calculated reload value several hundred cycles have passed. If you then reload it with the "original", calculated value you are effectively "turning back the time" or reclaiming time that has already passed.

    What you usually do is stop the timer, make a copy of its content and then ADD the reload value to the value of the copy. Then you put the value back in the timer and restart it. Obviously this too takes a couple of cycles so to be really accurate you need to tweak the reload values to account for it. Basically
    Code:
    TimerShadow VAR WORD
     
    T1CON.0 = 0  'Stop TMR1
    TimerShadow.HighByte = TMR1H
    TimerShadow.LowByte = TMR1L
    TimerShadow = TimerShadow + TimerReload
    TMR1H = TimerShadow.HighByte
    TMR1L = TimerShadow.LowByte
    T1CON.0 = 1  'Restart TMR1
    I see that you have 16bit read/write of TMR1 enabled, I don't know how that affects the above but I'd probably turn that off to begin with.

    In the INT_List for the low priority interrupt you have the Reset_Flag set to NO for TMR0 - why is that? You don't seem to reset it "manually" in the ISR.

    Finally, in your TMR0 interupt, don't do the LCDOUT etc in there. Set a flag and get out of there, then in your main routine you check the flag, if set do whatever and reset the flag.
    Excuse me for my misunderstanding...
    I've made some experiments with your routine, I can change the frequency, but even not proportionnaly, how do you integrate your routine into my program?
    Last edited by pxidr84; - 4th March 2011 at 17:54.

Members who have read this thread : 0

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

Posting Permissions

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