Announcement

Collapse
No announcement yet.

Confusion between oscillator speed and instruction clock speed

Collapse
X
 
  • Time
  • Show
Clear All
new posts

  • Confusion between oscillator speed and instruction clock speed

    Confusion between oscillator speed and instruction clock speed

    Using the ME Labs Trainer w/PBP with a PIC 16F1937, I am confused between the speed of the oscillator and the speed of the instruction clock.

    I am monitoring the output of portA.0 with an oscilloscope. My program just says Low PortA.0 then High PortA.0 a number of times. With the osc set to 32 Mhz I see a pulse of 0.5 Mhz on a oscilloscope. With the osc set to 16 Mhz I see a pulse of 1.0 Mhz.

    What is the relationship between the clock (osc) speed and the instruction clock speed?

    Thanks

    Larry Cardo

  • #2
    Low PortA.0
    translates to something like

    Code:
    BANKSEL trisa 
    bcf   trisa ,0
    BANKSEL porta
    bcf   porta ,0

    High PortA.0
    Code:
    BANKSEL trisa 
    bcf   trisa ,0
    BANKSEL porta
    bsf   porta ,0
    both of which are 4 x machine code instructions , each machine code instruction takes 4 clock {osc} cycles

    using high / low is not the quickest way to toggle pins

    Comment


    • #3
      Hi,
      With the osc set to 32 Mhz I see a pulse of 0.5 Mhz on a oscilloscope. With the osc set to 16 Mhz I see a pulse of 1.0 Mhz.
      That seems opposite to what you'd actually get. Are you sure you have the numbers correct or do you actually mean that you see a pulse of 0.5us at 32MHz and 1us at 16MHz (that would make much more sense)?

      Actually, a program consisting of nothing other than
      Code:
      Main:
        High PortA.0
        Low PortA.0
      Goto Main
      Looks like this when compiled
      Code:
      _Main
         bsf     PORTA,  000h
         movlb   (((PORTA) + 80h)) >> 7
         bcf     ((PORTA) + 80h), 000h
         movlb   (PORTA) >> 7
         bcf     PORTA,  000h
         movlb   (((PORTA) + 80h)) >> 7
         bcf     ((PORTA) + 80h), 000h
         movlb   0
         bra     _Main
      Each of the above instructions takes one instruction cycle, except the BRAnch which takes two, and each instruction cycle is four clock cycles. So the above program would take 10 instruction cycles, ie 40 clock cycles. At 32MHz it'll procduce a freqency of 800kHz.

      Now, if we do this instead:
      Code:
      TRISA.0 = 0
      Main:
        PortA.0 = 1
        PortA.0 = 0
      Goto Main
      The compiled code is smaller and faster, it looks like this:
      Code:
      movlb   (TRISA) >> 7
      bcf     TRISA,  000h
      movlb   0
      _Main
        bsf     PORTA,  000h
        bcf     PORTA,  000h
        bra     _Main
      Now the loop is only 2+2 instructions instead of 8+2 before. What used to be 800kHz output at 32MHz now becomes 2MHz. The dutycycle will be different too of course.

      Larry, this example is EXACTLY what I've tried to explain in your previous threads. Ie that the high level commands are convenient but not always very effecient. Since HIGH/LOW "automatically" makes the pin an output it has to clear the TRIS bit right? So it does and it does it EVERY time you use HIGH/LOW because it doesn't know if something has changed the TRIS-bit.

      (When you're doing things like this you tend to get into RMW-issues but that's another topic).

      If you're not shooting for as much performance as possible you don't have to worry about but you should know about it, this illustrates the point perfectly.

      But again, if what you wrote earlier is correct and you're getting double the output frequency when you're reducing the clock frequency somehting else is wrong.

      /Henrik.

      Comment


      • #4
        Henrik,
        Yes, I had the numbers backwards. I meant us not Mhz. Sorry. Thank you once again for the timing overview. It is all starting to fit together--slowly. The application I am planning is not especially time critical, but I will have a lot going on. I will have 10 Analog inputs and 10 servo outputs. Along with about a half dozen other inputs that need to be processed. The servo alone would take up the full time, except I am hoping to use interrupts to stop the pulses at the appropriate time, as the servos have to be refreshed every 20 MS.

        Richard, Thank you for clarifying the timing of instructions.

        Larry

        Comment

        Working...
        X
        😀
        🥰
        🤢
        😎
        😡
        👍
        👎