PIC24 Support Libraries
Macros | Functions
pic24_clockfreq.h File Reference

Configures the system clock. More...

#include <stdint.h>
#include "pic24_chip.h"
#include "pic24_libconfig.h"

Go to the source code of this file.

Macros

#define CYCLES_PER_MS   ((uint32_t)(FCY * 0.001))
 
#define CYCLES_PER_US   ((uint32_t)(FCY * 0.000001))
 
#defines for CLOCK_CONFIG
#define SIM_CLOCK   0, -1, 1000000L, POSCMD_NONE, -1, configClockSim, 1, 498
 
#define FRCPLL_FCY16MHz   1, FNOSC_FRCPLL, 16000000L, POSCMD_NONE, -1, configClockFRCPLL_FCY16MHz, (PIC24F_DEFINED || PIC24FK_DEFINED), 498
 
#define FRC_FCY4MHz   2, FNOSC_FRC, 4000000L, POSCMD_NONE, -1, configClockFRC_FCY4MHz, (PIC24F_DEFINED || PIC24FK_DEFINED), 498
 
#define PRI_NO_PLL_7372KHzCrystal   3, FNOSC_PRI, 3686400L, POSCMD_XT, 7372800L, configClockPRI_NO_PLL_7372KHzCrystal, (PIC24F_DEFINED || PIC24FK_DEFINED || PIC24H_DEFINED || dsPIC33F_DEFINED), 498
 
#define FRC_FCY3685KHz   4, FNOSC_FRC, 3685000L, POSCMD_NONE, -1, configClockFRC_FCY3685KHz, (PIC24E_DEFINED || dsPIC33E_DEFINED || PIC24H_DEFINED || dsPIC33F_DEFINED), 498
 
#define FRCPLL_FCY40MHz   5, FNOSC_FRCPLL, 40000000L, POSCMD_NONE, -1, configClockFRCPLL_FCY40MHz, (PIC24E_DEFINED || dsPIC33E_DEFINED || PIC24H_DEFINED || dsPIC33F_DEFINED), 498
 
#define PRIPLL_7372KHzCrystal_40MHzFCY   6, FNOSC_PRIPLL, 39920833L, POSCMD_XT, 7372800L, configClockPRIPLL_7372KHzCrystal_40MHzFCY, (PIC24H_DEFINED || dsPIC33F_DEFINED), 498
 
#define PRIPLL_8MHzCrystal_40MHzFCY   7, FNOSC_PRIPLL, 40000000L, POSCMD_XT, 8000000L, configClockPRIPLL_8MHzCrystal_40MHzFCY, (PIC24H_DEFINED || dsPIC33F_DEFINED || PIC24E_DEFINED || dsPIC33E_DEFINED), 498
 
#define PRIPLL_8MHzCrystal_16MHzFCY   8, FNOSC_PRIPLL, 16000000L, POSCMD_XT, 8000000L, configClockPRIPLL_8MHzCrystal_16MHzFCY, (PIC24F_DEFINED || PIC24FK_DEFINED), 498
 
#define PRI_8MHzCrystal_4MHzFCY   9, FNOSC_PRI, 4000000L, POSCMD_XT, 8000000L, configClockPRI_8MHzCrystal_4MHzFCY, (PIC24F_DEFINED || PIC24FK_DEFINED || PIC24H_DEFINED || dsPIC33F_DEFINED || PIC24E_DEFINED || dsPIC33E_DEFINED), 498
 
#define FRCPLL_FCY60MHz   10, FNOSC_FRCPLL, 59881250L, POSCMD_NONE, -1, configClockFRCPLL_FCY60MHz, (PIC24E_DEFINED || dsPIC33E_DEFINED), 498
 
#define FRCPLL_FCY70MHz   11, FNOSC_FRCPLL, 70015000L, POSCMD_NONE, -1, configClockFRCPLL_FCY70MHz, (PIC24E_DEFINED || dsPIC33E_DEFINED), 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_t u8_source)
 

Detailed Description

Configures the system clock.

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).

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.

Macro Definition Documentation

◆ CLOCK_CONFIG_INDEX

#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

◆ CONFIG_DEFAULT_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.

Referenced by configClock().

◆ CYCLES_PER_MS

#define CYCLES_PER_MS   ((uint32_t)(FCY * 0.001))

A constant which gives the number of processor clock cycles in 1 millisecond. This value is cast to a uint32_t.

Definition at line 332 of file pic24_clockfreq.h.

◆ CYCLES_PER_US

#define CYCLES_PER_US   ((uint32_t)(FCY * 0.000001))

A constant which gives the number of processor clock cycles in 1 microsecond. This value is cast to a uint32_t.

Definition at line 338 of file pic24_clockfreq.h.

◆ FCY

#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.

Referenced by CONFIG_BAUDRATE_UART1(), configI2C1(), configUART1(), esos_pic24_configI2C1(), msToU16Ticks(), ticksToMs(), ticksToNs(), ticksToUs(), usToU16Ticks(), and usToU32Ticks().

◆ FNOSC_SEL

#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 PIC24FK PIC24H/dsPIC33F PIC24E/dsPIC33E
------------- -------------------------------------------- ------ ------- --------------- ---------------
FNOSC_FRC Fast RC oscillator X X X X
FNOSC_FRCPLL Fast RC oscillator w/ divide and PLL X X X X
FNOSC_PRI Primary oscillator (XT, HS, EC) X X X X
FNOSC_PRIPLL Primary oscillator (XT, HS, EC) w/ PLL X X X X
FNOSC_SOSC Secondary oscillator X X X X
FNOSC_LPRC Low power RC oscillator X X X X
FNOSC_FRCDIV16 Fast RC oscillator w/ divide by 16 X X
FNOSC_LPRCDIVN Low power RC oscillator w/divide by N X X
FNOSC_LPFRCDIV 500 kHz FRC Oscillator with Postscaler X
FNOSC_FRCDIV Fast RC oscillator with divide X X

◆ GET_OSC_SEL_BITS

#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 359 of file pic24_clockfreq.h.

◆ IS_CLOCK_CONFIG

#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
clockConfigClock configuration to compare the current clock configuation against.

Definition at line 353 of file pic24_clockfreq.h.

◆ OSC_SEL_BITS

#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 372 of file pic24_clockfreq.h.

◆ POSC_FREQ

#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.

◆ POSCMD_SEL

#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

◆ SIM_CLOCK

#define SIM_CLOCK   0, -1, 1000000L, POSCMD_NONE, -1, configClockSim, 1, 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 155 of file pic24_clockfreq.h.

Function Documentation

◆ configClock()

static void configClock ( )
inlinestatic

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

Definition at line 446 of file pic24_clockfreq.h.

Referenced by configBasic().

◆ switchClock()

void switchClock ( uint8_t  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_sourceThe clock source to switch to.

Definition at line 212 of file pic24_clockfreq.c.

Referenced by configClock().