How to set the internal CPU speed?


Closed Thread
Results 1 to 13 of 13

Hybrid View

  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.

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