How to set the internal CPU speed?


Closed Thread
Results 1 to 13 of 13
  1. #1
    Join Date
    Mar 2011
    Location
    Los Angeles, California
    Posts
    322

    Default How to set the internal CPU speed?

    Hi All!
    The fastest crystal I have seen for PIC's is 20 MHz and the spec sheets say that a PIC's internal CPU Speed can be 40 or 48, or 64 MHz! What does the code look like to change the internal CPU Speed?

    Thanks, Ed

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: How to set the internal CPU speed?

    What chip are you using?

    The register is OSCCON. And on some you can set the PLL in the configs.
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Mar 2011
    Location
    Los Angeles, California
    Posts
    322


    Did you find this post helpful? Yes | No

    Default Re: How to set the internal CPU speed?

    Thanks Dave and so I will learn, what in the spec sheet do you look for or is in in the spec sheet? Target is an 18F4685 and 18F1320.

    Best, Ed

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: How to set the internal CPU speed?

    Might be a bit of confusion here.

    The 18F1320 for example has an internal OSC that can run up to 8MHz.
    If you use an external OSC then you can set HSPLL in the configs and then the chip will run four times the external speed.

    In the data sheet look at section 2.0 Oscillator Configurations. Then to see all of the config options goto your MPASM directory and find the *.inc file for the chip. Near the EOF the possible configs are listed.

    Then look at this thread to get a handle on coding the configs.
    http://www.picbasic.co.uk/forum/showthread.php?t=543
    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    Mar 2011
    Location
    Los Angeles, California
    Posts
    322


    Did you find this post helpful? Yes | No

    Default Re: How to set the internal CPU speed?

    Hi Dave!
    You are correct it can be confusing. There are things being done that seem to make no sense such as what appears to be using XT for the crystal when I am used to using HS for a 20 MHz crystal. Some expressions are not so difficult to understand such as HSPLL (High Speed Phase Locked Loop). If is not asking too much it would be interesting and educational to know step by step what code you would use and why say for the 1320.

    Thnaks, Ed

  6. #6
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: How to set the internal CPU speed?

    First, go to the 18F1320.inc file in the PBP directory and comment the comment the config lines so it looks like this. Now the configs can be played with in code space.
    Code:
           NOLIST
        ifdef PM_USED
            LIST
            "Error: PM does not support this device.  Use MPASM."
            NOLIST
        else
            LIST
            LIST p = 18F1320, r = dec, w = -311, w = -230, f = inhx32
            INCLUDE "P18F1320.INC"  ; MPASM  Header
      ;      __CONFIG    _CONFIG1H, _XT_OSC_1H
      ;      __CONFIG    _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
      ;      __CONFIG    _CONFIG4L, _LVP_OFF_4L
            NOLIST
        endif
            LIST
    EEPROM_START	EQU	0F00000h
    BLOCK_SIZE	EQU	8
    Now go to the MPASM directory and open the P18F1320.inc and near the EOF you will find all of the possible config options and in this one there is even a sample. Copy the sample to the top of your code.
    For now change the CONFIG1H and CONFIG3H to match below. You can do other changes as needed.
    Code:
    '18F1320 INTERNAL OSC RUNNING AT 8MHZ - MCLR AS INPUT AND OSC PINS AS INPUTS
    @ __CONFIG  _CONFIG1H, _INTIO2_OSC_1H & _FSCM_OFF_1H & _RC_OSC_1H
    @ __CONFIG  _CONFIG2L, _PWRT_ON_2L & _BOR_OFF_2L & _BORV_27_2L
    @ __CONFIG  _CONFIG2H, _WDT_OFF_2H & _WDTPS_32K_2H
    @ __CONFIG  _CONFIG3H, _MCLRE_OFF_3H
    @ __CONFIG  _CONFIG4L, _DEBUG_OFF_4L & _LVP_ON_4L & _STVR_ON_4L
    @ __CONFIG  _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L
    @ __CONFIG  _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
    @ __CONFIG  _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L
    @ __CONFIG  _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H
    @ __CONFIG  _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L
    @ __CONFIG  _CONFIG7H, _EBTRB_OFF_7H
    
    OSCCON=%01110000
    DEFINE OSC 8
    Now if you want to run at 40MHz you will need an external crystal or resonator running at 10MHz.
    Changing the first line of the configs.
    Code:
    '18F1320 EXTERNAL OSC RUNNING AT 10MHZ PLL TO 40MHZ - MCLR AS INPUT AND OSC PINS AS INPUTS
    @ __CONFIG  _CONFIG1H, _HSPLL_OSC_1H & _FSCM_OFF_1H & _RC_OSC_1H
    @ __CONFIG  _CONFIG2L, _PWRT_ON_2L & _BOR_OFF_2L & _BORV_27_2L
    @ __CONFIG  _CONFIG2H, _WDT_OFF_2H & _WDTPS_32K_2H
    @ __CONFIG  _CONFIG3H, _MCLRE_OFF_3H
    @ __CONFIG  _CONFIG4L, _DEBUG_OFF_4L & _LVP_ON_4L & _STVR_ON_4L
    @ __CONFIG  _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L
    @ __CONFIG  _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
    @ __CONFIG  _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L
    @ __CONFIG  _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H
    @ __CONFIG  _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L
    @ __CONFIG  _CONFIG7H, _EBTRB_OFF_7H
    
    DEFINE OSC 40
    That should do it if I did not screw something up ....
    Dave
    Always wear safety glasses while programming.

  7. #7
    Join Date
    Mar 2011
    Location
    Los Angeles, California
    Posts
    322


    Did you find this post helpful? Yes | No

    Default Re: How to set the internal CPU speed?

    Thanks Dave!
    Now it makes a lot more sense!
    I noticed you did not use "OSCCON=%01110000"?
    Second does anything change if you are using a 20 MHz Resonator?
    Just seems like you should be putting in a multipication factor some place?

    Thanks, Ed

  8. #8
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: How to set the internal CPU speed?

    I used "OSCCON=%01110000" in the first code, the one for the internal setup. It is not needed when using the external.

    When using a 20MHz external the first config line will look like this
    @ __CONFIG _CONFIG1H, _HS_OSC_1H & _FSCM_OFF_1H & _RC_OSC_1H

    The HSPLL_OSC_1H is the multiplier. All this chip can do is multiply by four.

    Now you will want to try over-clocking. Water cooled PICs
    Dave
    Always wear safety glasses while programming.

  9. #9
    Join Date
    Mar 2011
    Location
    Los Angeles, California
    Posts
    322


    Did you find this post helpful? Yes | No

    Default Re: How to set the internal CPU speed?

    Thanks Dave!
    Very interesting! So if I understand correctly, a 20 MHz external crystal with your line of code (thank you for this) will give an internal CPU speed of 40 MHz? By the way I like your idea of a "water cooled" PIC!

    Just curious since I have been away from all this for so long, in the past when you programmed your PIC you would have to specify either XT or HS depending upon what frequency resonator you used. It appears this is now "built-in" with a default of XT? (__CONFIG _CONFIG1H, _XT_OSC_1H).

    It is correct to assume with HSPLL the multiplier is always X4?


    Best, Ed

  10. #10
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: How to set the internal CPU speed?

    When using a 20MHz external the first config line will look like this

    @ __CONFIG _CONFIG1H, _HS_OSC_1H & _FSCM_OFF_1H & _RC_OSC_1H
    And then this
    DEFINE OSC 20

    "HSPLL" would try to make the running speed 80MHz... I have my doubts if it would work. I have heard some folks tell of these running at 64MHz (and they did not use water...) but the data sheet claims 40MHz is the limit....

    It is correct to assume with HSPLL the multiplier is always X4?
    With this chip, yes.

    From the data sheet
    2. XT Crystal/Resonator
    3. HS High-Speed Crystal/Resonator
    Table 2-1 and table 2-2 are also worth looking at. 2-1 is for resonators without built in capacitors and 2-2 is for crystals. If resonators that have built in capacitors are used, even if they are 4MHz use "HS".
    Dave
    Always wear safety glasses while programming.

  11. #11
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: How to set the internal CPU speed?

    If resonators that have built in capacitors are used, even if they are 4MHz use "HS".
    Hi Dave,
    I never heard about that one, where did you ferret out that little tidbit ?
    Is it because the resonators exhibit lower impedance, and thus require more power ? I seldom use the low freq resonators, but I will put that away in my memory bank for the future.
    Thanks
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  12. #12
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: How to set the internal CPU speed?

    Well...
    I have always used the 3 pin resonators with built in capacitors and way back when I knew less than I do now.....

    Making the transition from the Stamp, I was using a 20MHz with the 887, wanted to go with a little less power so I popped in a 4MHz and did not even think about changing the config. It worked fine.

    Then stuff like this from the data sheets
    When operating below 3V VDD, or when
    using certain ceramic resonators at any
    voltage
    , it may be necessary to use the
    HS mode or switch to a crystal oscillator.
    Dave
    Always wear safety glasses while programming.

  13. #13
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: How to set the internal CPU speed?

    RTFDS,
    Still never saw that, probably never read beyond what I already knew inside the box. It makes perfect sense, esp. as it pertains to 3v operation. Anyway, thanks for pointing that out.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

Members who have read this thread : 1

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