PLL enabled HFINTOSC PIC18F

Discussion in 'Embedded Systems and Microcontrollers' started by peter_morley, Sep 17, 2011.

  1. peter_morley

    Thread Starter Member

    Mar 12, 2011
    179
    0
    I am calculating that I am only getting 16MHz signal because when I implement a 4 instruction loop I get a frequency on PORTA of about 1MHz. I know each instruction takes 4 cycles so 4*4*1Mhz comes out to 16MHz. Yeah I can do math! Well I am trying to enable the PLL bit so I can multiple that signal by 4, creating a 64MHz monster signal.:D I have enabled the PLL bit by this instruction bsf OSCTUNE,6. I am still not getting the 64MHz signal though.
    Code ( (Unknown Language)):
    1. CONFIG FOSC = INTIO7, FCMEN = OFF, IESO = ON, PWRT = OFF, BOREN = OFF
    2.      CONFIG BORV = 18, WDTEN = OFF, WDTPS = 1, MCLRE = ON, HFOFST = ON
    3.      CONFIG LPT1OSC = OFF, PBADEN = OFF, CCP2MX = PORTC, STVREN = OFF
    4.      CONFIG LVP = OFF,  XINST = OFF, CP0 = OFF, CP1 = OFF, CP2 = OFF
    5.      CONFIG CP3 = OFF, CPB = OFF, CPD = OFF, WRT0 = OFF, WRT1 = OFF
    6.      CONFIG WRT2 = OFF, WRT3 = OFF, WRTB = OFF, WRTC = OFF, WRTD = OFF
    7.      CONFIG EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
    8.      CONFIG EBTRB = OFF
    9.  
    10.  
    11. START
    12.         movlw   0x09
    13.         movwf   PLCounter
    14.  
    15.         movlw   0x06
    16.         movwf   ExtraCounter
    17.        
    18.         movlb   B'00001111'
    19.         clrf    PORTA
    20.         comf    PORTA
    21.         movlw   0xE0                    ; Configure I/O
    22.         movwf   ANSEL                   ; for digital inputs
    23.         movlw   0xC0                    ; Value used to
    24.         movwf   TRISA                   ; Set RA<5:0> as outputs
    25.        
    26.  
    27.         clrf    OSCCON
    28.         bsf     OSCCON,1
    29.         bsf     OSCCON,4
    30.         bsf     OSCCON,5
    31.         bsf     OSCCON,6
    32.         bsf     OSCTUNE,6
    33.        
    34.         movlw   0x10                    ; start loading pixels at RAM location x10 
    35.         movwf   FSR0L,1
    36.         movlw   0x50                    ; start loading pixels at RAM location x10
    37.         movwf   FSR0H,1
    38. Freq
    39.         comf    PORTA
    40.         comf    PORTA
    41.         goto    Freq
    42. END    
     
  2. CraigHB

    Member

    Aug 12, 2011
    127
    15
    With the internal 8MHz RC oscillator without PLL, you'll get an instruction rate of 2MIPS. Since it takes 2 instruction cycles to oscillate a port pin, the highest frequency you could get out of a single port pin would be 1MHz, but also, it takes 2 instruction cycles for the program counter to jump. The output would not be perfectly square and the overall frequency would be lower.

    If you enable PLL, you can increase instruction execution speed to 8MIPS, an increase by a factor of 4, but even so, ouptut is going to be impacted by anything else the code is doing. To generate a signal while doing other code tasks, you'd have to use an IRQ which would greatly reduce maximum frequency output. Typically, you'd use the PWM module or OCM module to avoid code execution dependancies, albeit at a lower frequency.

    For an 18F, you can enable PLL with the following assembly instruction;

    bsf OSCTUNE, PLLEN

    Don't know if it matters, but keep in mind you'll increase MCU power consumption proportionally as you increase instruction speed.

    I believe it's possible to output oscillator frequency on one of the pins through the FOSC config setting. In that case, you'd be able to get a higher code independant frequency output on one of the pins.

    Not sure about the limit on the 18F series, I think some of the newer ones are higher, but you can get much higher instruction execution rates with the 24F series since the instruction clock for those is Fosc/2 instead of Fosc/4.
     
    Last edited: Sep 18, 2011
  3. peter_morley

    Thread Starter Member

    Mar 12, 2011
    179
    0
    Thanks Craig,

    I already had the PLLEN bit set but its still not functioning properly. To oscillate PORTA i need actually 4 instruction cycles due to the goto command. So I actually have an internal frequency of 16MHz and each period takes 16 cycles = 4 instruction cycles. Do you know if my config words are set up correctly?
     
  4. peter_morley

    Thread Starter Member

    Mar 12, 2011
    179
    0
    With this code I have an oscillator frequency of 32MHz because I am getting porta oscillations of 2Mhz. I don't know how I got it from 16Hz to 32Hz but its making me mad that I can't do this knowing what I'm doing. blah help please!:mad:

    Code ( (Unknown Language)):
    1.  
    2.      CONFIG FOSC = INTIO7, IESO = OFF, FCMEN = OFF, PWRT = OFF, BOREN = OFF
    3.      CONFIG BORV = 18, WDTEN = OFF, WDTPS = 1, MCLRE = ON, HFOFST = ON
    4.      CONFIG LPT1OSC = OFF, PBADEN = OFF, CCP2MX = PORTC, STVREN = OFF
    5.      CONFIG LVP = OFF,  XINST = OFF, CP0 = OFF, CP1 = OFF, CP2 = OFF
    6.      CONFIG CP3 = OFF, CPB = OFF, CPD = OFF, WRT0 = OFF, WRT1 = OFF
    7.      CONFIG WRT2 = OFF, WRT3 = OFF, WRTB = OFF, WRTC = OFF, WRTD = OFF
    8.      CONFIG EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
    9.      CONFIG EBTRB = OFF
    10.    
    11.  
    12. ;------------------------------------------------------------------------------
    13. ;
    14. ; VARIABLE DEFINITIONS
    15. ;
    16. ;------------------------------------------------------------------------------    
    17.     CBLOCK 0x000 ; Sample GPR variable register allocations
    18.         PLCounter
    19.         ExtraCounter
    20.     ENDC
    21.      
    22. START
    23.         movlw   0x09
    24.         movwf   PLCounter
    25.  
    26.         movlw   0x06
    27.         movwf   ExtraCounter
    28.        
    29.         movlb   B'00001111'
    30.         clrf    PORTA
    31.         comf    PORTA
    32.         movlw   0xE0                    ; Configure I/O
    33.         movwf   ANSEL                   ; for digital inputs
    34.         movlw   0xC0                    ; Value used to
    35.         movwf   TRISA                   ; Set RA<5:0> as outputs
    36.  
    37.         movlw   0x70
    38.         movwf   OSCCON
    39.         movlw   0x40
    40.         movwf   OSCTUNE
    41.        
    42.         movlw   0x10                    ; start loading pixels at RAM location x10 
    43.         movwf   FSR0L,1
    44.         movlw   0x50                    ; start loading pixels at RAM location x10
    45.         movwf   FSR0H,1
    46. Freq
    47.         comf    PORTA
    48.         comf    PORTA
    49.         goto    Freq
    50. END    
     
  5. CraigHB

    Member

    Aug 12, 2011
    127
    15
    I would suggest using the MPLab SIM debugger. There's a stopwatch you can display. I use it a lot myself for code timing. It's identified as "StopWatch" in the Debugger drop down menu after you've selected the MPLab SIM debugger. Go to Debugger->Settings then select the Osc/Trace tap to set the clock speed. Then step through your program with the debugger to observe instruction timings. If I were to load your code and see what's happening, that is what I would do.

    Hope that helps.
     
  6. peter_morley

    Thread Starter Member

    Mar 12, 2011
    179
    0
    Thanks again Craig.

    I used the stop watch setting and everything is working as I expected. Still have a 32MHz signal. I have been looking on the internet and I see people have had similar problems. Can't find a good answer to my question.
     
  7. peter_morley

    Thread Starter Member

    Mar 12, 2011
    179
    0
    With this code I get a 16MHz clock...

    Code ( (Unknown Language)):
    1.  
    2. movlw   0x70
    3. movwf   OSCCON
    4.  
    If I add this part I get 32MHz...

    Code ( (Unknown Language)):
    1.  
    2. bsf     OSCTUNE,6
    3.  
    So according to mathematics! I should be getting 4*16MHz = 64MHz...well I get 32MHz. The PLL bit should multiply the signal by 4...well its not.
     
  8. nickelflipper

    Active Member

    Jun 2, 2010
    280
    35
    With the config setting you should have internal clockout available on RA6. Can you measure that with an oscilloscope?, a DVM could load the output down too much.

    An alternate method to check the clock is using the CCPx module and a PWM output. Here is some short code that accomplishes 16Mips (ie. 64Mhz clock) as shown with a 18f26k22 here. Notice how you can use ACCESS to move around in different memory banks, no need to use bsr values?

    Also a good practice to write to the ports using the LATx registers, try that. Could be critical at very high clock rates.
     
  9. peter_morley

    Thread Starter Member

    Mar 12, 2011
    179
    0
    I just got it working by only changing one part of the config setting which is config FOSC = INTIO7. What a pain for something so small but at least I am getting somewhere. My problem now is changing the FSRL and FSRH registers. My FSRL register can be changed but I can't change the value in FSRH. One question I have is when FSRL gets to xFF does the carry roll over to FSRH when increasing FSRL by one. The 16 bit word would look like this after 0x0100?

    Code ( (Unknown Language)):
    1.      CONFIG FOSC = INTIO7
    2.      ;CONFIG FCMEN = OFF, IESO = OFF,PWRT = OFF, BOREN = OFF, BORV = 30
    3.      ;CONFIG WDTEN = OFF, WDTPS = 32768,MCLRE = ON, LPT1OSC = OFF, PBADEN = OFF, CCP2MX = PORTC,STVREN = ON, LVP = OFF
    4.      ;CONFIG XINST = OFF,CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF,CPB = OFF, CPD = OFF  
    5.      ;CONFIG WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF,WRTB = OFF, WRTC = OFF, WRTD = OFF          
    6.      ;CONFIG EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF, EBTRB = OFF      
    7.  
    8. #define     WHITE       B'00110111'
    9. #define     BLACK       B'00110000'
    10. #define     HLOW        B'00100000'
    11. #define     VLOW        B'00000000'
    12.  
    13. RES_VECT  ORG     0x0000            ; processor reset vector
    14.           GOTO    START  
    15. ;------------------------------------------------------------------------------
    16. ;
    17. ; VARIABLE DEFINITIONS
    18. ;
    19. ;------------------------------------------------------------------------------    
    20.     CBLOCK 0x000 ; Sample GPR variable register allocations
    21.         PLCounter
    22.         ExtraCounter
    23.     ENDC
    24.      
    25.  
    26. ;------------------------------------------------------------------------------
    27. ; MAIN PROGRAM
    28. ;------------------------------------------------------------------------------
    29.  
    30. START
    31.         movlw   0x29
    32.         movwf   PLCounter
    33.  
    34.         movlw   0x22
    35.         movwf   ExtraCounter
    36.  
    37.         movlb   B'00001111'             ; Bank Register Location
    38.         movlw   0x70                    ; load OSCCON value to make
    39.         movwf   OSCCON                  ; oscillator 16MHz
    40.         movlw   0x40                    ; enable PLL bit so
    41.         movwf   OSCTUNE                 ; oscillator is 4*16MHz = 64MHz!
    42.        
    43.         clrf    PORTA                   ; initialize PORTA to 0
    44.         movlw   0xE0                    ; Configure I/O
    45.         movwf   ANSEL                   ; for digital inputs
    46.         movlw   0xC0                    ; Value used to
    47.         movwf   TRISA                   ; Set RA Pins 5,4,3,2,1 as outputs
    48.    
    49.  
    50.         movlw   0x10                    ; start loading pixels at RAM location x10 
    51.         movwf   FSR0L,1
    52.             movlw   0x50                    ; start loading pixels at RAM location x10
    53.         movwf   FSR0H,1
     
  10. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    Instead of incrementing FSR0L and FSR0H separately, use the POSTINC0 register (instead of INDF0) which will automatically increment the 16 bit FSR0 register pair...
     
    peter_morley likes this.
  11. peter_morley

    Thread Starter Member

    Mar 12, 2011
    179
    0
    Oh wow that makes programming even easier. Great call on post incrementing.
     
Loading...