Changing the sines frequency?


Closed Thread
Results 1 to 33 of 33

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default Re: Changing the sines frequency?

    Quote Originally Posted by HenrikOlsson View Post
    Hi,
    Something must be wrong with your setup. I tried the same ISR as your one, with InterruptFrequency = 10800:
    Code:
    ' ---------- Interrupt handler ------------
    TimezUp:
    PortB.3 = 1
      T1CON.0 = 0                             ' Stop TMR1
      TMRCopy.HighByte = TMR1H                ' Copy value of TMR1 registers
      TMRCopy.LowByte = TMR1L 
      TMRCopy = TMRCopy + TimerReloadValue    ' Add reload value (compensates for overhead)
      TMR1H = TMRCOPY.HighByte                ' And back to TMR1
      TMR1L = TMRCopy.LowByte
      T1CON.0 = 1                             ' Restart Timer
    PortB.3 = 0
    @ INT_RETURN
    And I get ~10500Hz even when running at 20Mhz. Here you can see that I, instead of using TOGGLE set the output high at the entry point and low at the exit which allows me to measure the execution time of the code in between. This exact code executes in ~3us on this 18F4520 running at 20MHz, if you try the same on yours it should be 1.5us - if it's not then you're not running at 40MHz.

    You say you have a 40MHz x-tal, is that really correct? To run at 40MHz with the PLL you should use a 10MHz x-tal.

    Try the above, what's the pulsewidth on the output? If it's not 1.5us you're not running at 40MHz.

    /Henrik.
    With your exact code (the same you're using) and a INTF of 10800Hz, I get 2,76ms between each pulse. Very weird, and very slow. I've disabled the PLL, the oscilator is configured as a HS oscillator.

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Changing the sines frequency?

    Hi,
    1) What's the width of the pulses? Not the time between them but the width?
    2) What's the value stamped on your x-tal?
    3) The timing calculations that I provided are based on the fact that your chip is SUPPOSED to run at 40MHz, if it's not running at 40MHz then the timing will be wrong and the interrupt frequency will wrong as well.
    4) Please don't quote the complete message you're responding to. It clutters the thread and it gets hard read because everything is quoted all the time - that's my personal view.

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: Changing the sines frequency?

    The pulses are very very thin :



    The width of a pulse is 4,4µs.

    Of course the crystal is stamped at 40MHz, I've difficulties with the setup of this timer, but I hope I'm not an idiot

    The circuit :


  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Changing the sines frequency?

    Of course the crystal is stamped at 40MHz
    Well, then that is likely the problem - or one of them... If you look at page 355 in the datasheet you'll see that the maximum x-tal frequency WITHOUT PLL is 25 MHz and with using the PLL it's 10MHz (resulting in 40Mhz). You're trying to use 40Mhz which is out of specs in both cases.

    The fact that your pulses are 4.4us wide while mine are 3us means that your chip is running way SLOWER than mine while in fact it "should" be running twice as fast.

    Get the oscillator sorted and verify that the PIC is running at the speed you think. One way to do that is a simple program like:
    Code:
    DEFINE OSC 20    'Don't forget to DEFINE the correct OSC speed.
    TRISB.0 = 0
    Main:
      PortB.0 = 1
      Pause 1
      PortB.0 = 0
      Pause 1
    Goto Main.
    If you DON't get a 500Hz signal with 1ms wide pulses the chip isn't running at the correct and/or DEFINE'd oscillator speed.

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: Changing the sines frequency?

    Okay, sincelery I'm quite new with the PICs, so I doesn't know this "problem" (in reality is not a problem).

    I'll try to find a slower crystal tomorrow.

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: Changing the sines frequency?

    Okay, so I've unsoldered an 8MHz xtal (not an 10MHz unfortunately) on a old board, and I connected this one to the PIC.
    I've enabled the PLL, so my PIC clocks at 8*4=32MHz.

    On the oscilloscope, I get now a 2µs pulse, it's a little better.

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Changing the sines frequency?

    Now it seems to run properly - finally.

    In order to make the calculations of the preload value match your new (32MHz) oscillator speed you need to change the Dummy2 value from 10000 to 8000.

    Then keep the same test code in the interrupt service routine and try with InterruptFrequency = 10800, check the scope, there should now be pulses at 10800 or likely a little bit slower.

    The reason it's not 10800 exactly is, again, due to time that the timer is actually stopped while reloading it. To tune it simply tweak the +1 in the following line:
    Code:
    TimerReload = 65535 - TimerReload + 1
    Try +12 or something and see what happens.

    Then try adding code to your interrupt, not all at once, add it in little by little and keep an eye on the scope. If you start to see jitter or "missed" pulses then you're "overloading" the PIC - ie interrupting to fast. Keep the code in the ISR as tight and short and possible. Think thru it, is there any other way to achive this or that to make this execute faster. Every cycle counts here. Verify with the scope, the pulsewidth shows the execution time of the ISR.

    Trying to make a 3-phase inverter when you're new to PICs are quite a challange - as you probably/hopefully know by now - take baby steps.

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