You, or MCC may define _XTAL_FREQ to be any value you need it to be.
MCC will calculate _XTAL_FREQ according to what Oscillator settings have been made in the GUI,
both Configuration bit settings, and oscillator control registers.
_XTAL_FREQ 1000000 // As calculated by MCC is correct:
If you use MCC, and do No Change or Selection of clock settings in 'System Module'
then 16 MHz
Internal Oscillator Divided by 16
Strictly, _XTAL_FREQ is a misleading term, since what is actually meant, is the frequency of the system clock signal, after PLL frequency multiplier and whatever frequency divider and clock signal selector that may be used.
If you program oscillator control registers manually,
or change oscillator control registers from what is used when MCC generated code,
then you should recalculate the _XTAL_FREQ value, taking all clock register settings into account.
If you select different settings in MCC user interface, and Generate updated code, _XTAL_FREQ will be recalculated and updated.
Note, that you may change oscillator control register settings by program when the MCU is runing,
but _XTAL_FREQ is a macro that cannot be changed after program have been compiled, and device have been programmed.
So if you do Clock Switching in runtime, you may need to recalculate delays and peripheral settings.
Also, __delay_ms(x), and siblings, are Macros that are calculated by the Compiler,
they are Not functions, so cannot be changed in the running program.
If you need a variable delay, then you will need to make a loop, with a variable loop counter, around one of the fixed delay macros.
Or use a Timer with a variable register setting.
Be aware that _XTAL_FREQ == System clock frequency, is different from Instruction frequency.
All 8-bit PIC microcontrollers, PIC10..., PIC12..., PIC16... and PIC18..., use 4 clock periods for each instruction.
post edited by Mysil - 2019/05/05 21:17:58