include/pic24_clockfreq.h File Reference

Go to the source code of this file.

Defines

#define CLOCK_CONFIG   SIM_CLOCK
#defines for CLOCK_CONFIG
#define SIM_CLOCK   0, -1, 1000000L, POSCMD_NONE, -1, 1, configClockSim, 498
#define FRCPLL_FCY16MHz   1, FNOSC_FRCPLL, 16000000L, POSCMD_NONE, -1, PIC24F_DEFINED, configClockFRCPLL_FCY16MHz, 498
#define FRC_FCY4MHz   2, FNOSC_FRC, 4000000L, POSCMD_NONE, -1, PIC24F_DEFINED, configClockFRC_FCY4MHz, 498
#define PRI_NO_PLL_7372KHzCrystal   3, FNOSC_PRI, 3686400L, POSCMD_XT, 7372800L, (PIC24F_DEFINED || PIC24H_DEFINED), configClockPRI_NO_PLL_7372KHzCrystal, 498
#define FRC_FCY3685KHz   4, FNOSC_FRC, 3685000L, POSCMD_NONE, -1, PIC24H_DEFINED, configClockFRC_FCY3685KHz, 498
#define FRCPLL_FCY40MHz   5, FNOSC_FRCPLL, 40000000L, POSCMD_NONE, -1, PIC24H_DEFINED, configClockFRCPLL_FCY40MHz, 498
#define PRIPLL_7372KHzCrystal_40MHzFCY   6, FNOSC_PRIPLL, 40000000L, POSCMD_XT, 7372800L, PIC24H_DEFINED, configClockPRIPLL_7372KHzCrystal_40MHzFCY, 498
#define PRIPLL_8MHzCrystal_40MHzFCY   7, FNOSC_PRIPLL, 40000000L, POSCMD_XT, 8000000L, PIC24H_DEFINED, configClockPRIPLL_8MHzCrystal_40MHzFCY, 498
#define PRIPLL_8MHzCrystal_16MHzFCY   8, FNOSC_PRIPLL, 16000000L, POSCMD_XT, 8000000L, PIC24F_DEFINED, configClockPRIPLL_8MHzCrystal_16MHzFCY, 498
Some of the #defines produced by CLOCK_CONFIG choice
#define CLOCK_CONFIG_INDEX   GET_CLOCK_CONFIG_INDEX(CLOCK_CONFIG)
#define FNOSC_SEL   GET_FNOSC_SEL(CLOCK_CONFIG)
#define FCY   GET_FCY(CLOCK_CONFIG)
#define POSCMD_SEL   GET_POSCMD_SEL(CLOCK_CONFIG)
#define POSC_FREQ   GET_POSC_FREQ(CLOCK_CONFIG)
#define CONFIG_DEFAULT_CLOCK()   GET_CONFIG_DEFAULT_CLOCK(CLOCK_CONFIG)()
Helper macros for clock configuration
#define IS_CLOCK_CONFIG(clockConfig)   _GET_CLOCK_CONFIG_INDEX(clockConfig) == CLOCK_CONFIG_INDEX
#define GET_OSC_SEL_BITS(bits)   _GET_OSC_SEL_BITS(bits)
#define OSC_SEL_BITS   GET_OSC_SEL_BITS(FNOSC_SEL)

Functions

static void configClock ()
void switchClock (uint8 u8_source)
configClock() functions for various clock setups
void configClockSim (void)


Detailed Description

The clocking options for the PIC24 are chosen in this file. The resulting settings are used in pic24_clockfreq.c to configure and switch the oscillator as necessary after the chip is running.

To choose a clock configuration, simply select a value for CLOCK_CONFIG from the table there. The default selection used by most of the PIC24H examples is FRCPLL_FCY40MHz (the internal FRC+ PLL configured for FCY = 40 MHz). Some of the examples in Chapter 12 expect an external 8 MHz crystal, and use the PRIPLL_8MHzCrystal_40MHzFCY option (external 8 MHz crystal + PLL confgured for FCY = 40 MHz). If CLOCK_CONFIG is not defined, the default choice for PIC24H processors is FRCPLL_FCY40MHz, while for PIC24F processors the default is FRCPLL_FCY16MHz (internal FRC+ PLL configured for FCY = 16 MHz).

This file provides several useful defines as a result of the clock selection above:

To define a new clock configuation:

Definition in file pic24_clockfreq.h.


Define Documentation

#define CLOCK_CONFIG   SIM_CLOCK

Clock configuration for the PIC24 - set CLOCK_CONFIG to one of the following. Naming convention is OSCTYPE_[PRIFREQ]_FCYFREQ where OSCTYPE gives the oscillator type (see FNOSC_SEL for details), the optional PRIFREQ specifies primary oscillator frequency, and FCYFREQ determiens the processor clock (FCY) frequency. The "#defines for CLOCK_CONFIG" section gives the definition of the values below.

  Name                            uP
  ---------------                 ---
  SIM_CLOCK (simulator)           any
  FRCPLL_FCY16MHz                 24F
  FRC_FCY4MHz                     24F
  PRIPLL_8MHzCrystal_16MHzFCY     24F
  PRI_NO_PLL_7372KHzCrystal       24F, 24H
  FRC_FCY3685KHz                  24H
  FRCPLL_FCY40MHz                 24H
  PRIPLL_7372KHzCrystal_40MHzFCY  24H
  PRIPLL_8MHzCrystal_40MHzFCY     24H

Definition at line 97 of file pic24_clockfreq.h.

#define CLOCK_CONFIG_INDEX   GET_CLOCK_CONFIG_INDEX(CLOCK_CONFIG)

A unique number is assigned to each clock configuration, in order to allow comparsions between configurations. CLOCK_CONFIG_INDEX gives the number assigned to the selected clock configuration. The macro IS_CLOCK_CONFIG uses this number for its comparsisons.

See also:
SIM_CLOCK

 
#define CONFIG_DEFAULT_CLOCK (  )     GET_CONFIG_DEFAULT_CLOCK(CLOCK_CONFIG)()

Calls one of the configClock() functions defined in pic24_clockfreq.c based on the CLOCK_CONFIG chosen.

#define FCY   GET_FCY(CLOCK_CONFIG)

FCY specifies the resulting processor clock frequency after all PLL/divide operations take the inital clock and produce a processor clock. The units of this value are Hz, so the value 40000000L is 40 MHz.

NOTE: This should be defined as a long (with a trailing L after the number) to allocate enough space to correctly store the processor clock frequency.

#define FNOSC_SEL   GET_FNOSC_SEL(CLOCK_CONFIG)

Oscillator source selection for the PIC24. FNOSC_SEL is set to one of the following based on CLOCK_CONFIG :

      Name                 Description                                    PIC24F  PIC24H
      -------------        --------------------------------------------   ------  ------
      FNOSC_FRC            Fast RC oscillator                                X      X
      FNOSC_FRCPLL         Fast RC oscillator w/ divide and PLL              X      X
      FNOSC_PRI            Primary oscillator (XT, HS, EC)                   X      X
      FNOSC_PRIPLL         Primary oscillator (XT, HS, EC) w/ PLL            X      X
      FNOSC_SOSC           Secondary oscillator                              X      X
      FNOSC_LPRC           Low power RC oscillator                           X      X
      FNOSC_FRCDIV         Fast RC oscillator with divide                    X
      FNOSC_FRCDIV16       Fast RC oscillator w/ divide by 16                       X
      FNOSC_LPRCDIVN       Low power Fast RC oscillator w/divide by N               X

#define GET_OSC_SEL_BITS ( bits   )     _GET_OSC_SEL_BITS(bits)

A macro to determine the NOSC/COSC bits for the OSCCON register given bits taken from the PIC24's configuration FNOSC word.

Definition at line 332 of file pic24_clockfreq.h.

#define IS_CLOCK_CONFIG ( clockConfig   )     _GET_CLOCK_CONFIG_INDEX(clockConfig) == CLOCK_CONFIG_INDEX

A helper macro to check if the given clock configuration was selected. Example usage is given in pic24_clockfreq.c. A shorter example:

  #if IS_CLOCK_CONFIG(FRCPLL_FCY40MHz)
  ... code only included if CLOCK_CONFIG is FRCPLL_FCY40MHz ...
  #endif
Parameters:
clockConfig Clock configuration to compare the current clock configuation against.

Definition at line 326 of file pic24_clockfreq.h.

#define OSC_SEL_BITS   GET_OSC_SEL_BITS(FNOSC_SEL)

A macro giving the NOSC/COSC bits for the OSCCON register which correspond to FNOSC_SEL, the configuration word which contains the FNOSC bits.

Definition at line 345 of file pic24_clockfreq.h.

#define POSC_FREQ   GET_POSC_FREQ(CLOCK_CONFIG)

This values gives the primary osciallator frequency (if used). The units of this value are Hz, so the value 40000000L is 40 MHz.

NOTE: This should be defined as a long (with a trailing L after the number) to allocate enough space to correctly store the processor clock frequency.

#define POSCMD_SEL   GET_POSCMD_SEL(CLOCK_CONFIG)

Specifies the primary oscillator type. Valid values:

      POSCMD_EC            External clock
      POSCMD_XT            XT oscillator (for crystals from 3.5 MHz to 10 MHz)
      POSCMD_HS            HS oscillator (for crystals from 10 MHz to 32 MHz)
      POSCMD_NONE          Primary oscillator disabled

#define SIM_CLOCK   0, -1, 1000000L, POSCMD_NONE, -1, 1, configClockSim, 498

Create a table of vales for CLOCK_CONFIG. When adding a new entry, you must also write C code to configure the clock in pic24_clockfreq.c. See that file for examples and the detailed description section at the top of this page for more information. Note that FCY and POSC_FREQ should be defined as a long (with a trailing L) to retain enough accuracy to store these values.

Table entry notes: (see actual code, not the documentation, for the table)

  • Use -1 for don't-care values
  • The "unique index" just means each clock configuration should be assigned a different number. Giving two configurations the same index will cause problems!
    See also:
    CLOCK_CONFIG_INDEX.
  • The processor field should evaluate to a Boolean value which is true if the processor under which this file is compiled is supported. Later in this file, the values PIC24F_DEFINED and PIC24H_DEFINED are set to true (1) or false (0) based on which processor this file is compiled with. Use these values in the Boolean expression. For example:
    • 1 (works on any processor)
    • PIC24H_DEFINED (works only on the PIC24H)
    • PIC24F_DEFINED || PIC24H_DEFINED (works on PIC24F and PIC24H).
  • Valid entries for FNOSC_SEL are defined documentation for that variable.

Definition at line 135 of file pic24_clockfreq.h.


Function Documentation

static void configClock (  )  [inline, static]

Configure the default clock by calling the CONFIG_DEFAULT_CLOCK() macro.

Definition at line 407 of file pic24_clockfreq.h.

void configClockSim ( void   ) 

Nothing to do! Simulator clock not set by OSCCON, but instead in the Debugger | Settings, Osc/Trace dialog box. It should be 2 MHz there, since that's Fosc = 2*Fcy. At 1 MHz, simulation speed seems fast enough to execute a reasonable amount of code. Also, at a 64K line trace buffer, we can see 64 ms of execution, which is plenty to see 20 ms servo pulses.

void switchClock ( uint8  u8_source  ) 

Switch the clock to the source specified. The source given will be assigned to NOSC in the OSCCON register, the a switch clock performed.

Note: DO NOT use FNOSC_SEL as the source; instead, use OSC_SEL_BITS. FNOSC_SEL is for configuration bits while switchClock expects a NOSC value to write to the OSCCON register.

Parameters:
u8_source The clock source to switch to.

Definition at line 170 of file pic24_clockfreq.c.


Generated on Sun Mar 15 11:14:22 2009 for PIC24 Support Libraries by  doxygen 1.5.8