News:

Let's find out together what makes a PIC Tick!

Main Menu

coding decision bases on XTAL

Started by See_Mos, Feb 11, 2022, 12:07 PM

Previous topic - Next topic

See_Mos

I think I saw this before on the forum but now that I need it I cannot find it.

Is it possible to have the compiler make changes based on XTAL value?

At its simplest this could be
Select XTAL
Case 4
OSCCON =
Case 16
OSCCON =
etc

I use .INC files with the register symbols listed to save using copy and paste each time I start new code.  What I really want to achieve is to add the above code in the .INC file so that I can start my programs with

Declare XTAL 16
Include "12F1840.inc"


trastikata

See_Mos,

Do you mean something like this?

$if _xtal = 48
        OSCCON = $55
    $elseif _xtal = 24 
        OSCCON = $44
    $elseif _xtal = 12
        OSCCON = $33
    $else
        OSCCON = $22     
$endif 

top204

#2
Trastikata is correct. The Xtal declare is not dynamic, so its function must be chosen at compile time through the preprocessor. The timings of, virtually, all of the compiler's commands are based upon the device's oscillator frequency, and these are created with the correct assembler mnemonics at compile time.

Some of the preprocessor's defines are listed in the preprocessor section of the compiler's manual, and to see all of them, open the device's .def file and they are held as constant values at the beginning of it. For example, the start of the 12F1840.def file has:

' Device Information
$define _device _12F1840
$define _core 14
$define _ecore 1     ' Enhanced 14-bit core
$define _ram 256     ' Amount of RAM
$define _code 4096     ' Amount of Flash Memory
$define _eeprom 256     ' Amount of Eeprom Memory
$define _ports 1     ' Amount of Ports
$define _debug 1     ' Whether the device has Debug capabilities
$define _block 32     ' Size of the Code Memory Write Segment (in Words)
$define _mssp 1     ' 0 = None, 1 = Single MSSP, 2 = Dual MSSP, 3 = SSP Only
$define _flash 1     ' Whether the device has Self Modifying support. 0 = No, 1 = Read and Write, 2 = Read no Write
$define _usb 0     ' Amount of USBs
$define _uart 1     ' Amount of UARTs
$define _adcres 10     ' Resolution of ADC (in bits)
$define _adc 4     ' Amount of ADC channels
$define _bank0start 0x20
$define _bank0end 0x6F
$define _bank1start 0xA0
$define _bank1end 0xEF
$define _bank2start 0x0120
$define _bank2end 0x016F

Remember. With altering the device's oscillator, it is always advisable to wait for its stability before commensing the program, either by waiting for an SFR's stability bit to be set or a simple DelayMs after it.

See_Mos

Thanks for the explanation Les, I guess I will have to use my current method of separate INC files for each frequency, or in the case of 1840 have the most useful ones in the main code and comment out those not needed.

keytapper

It must have an option to declare the frequency, thus you may code as follow:
$if Xtal == 8
Include low_freq.inc
$elif Xtal == 16
Include mid_freq.inc
$else
Include high_freq.inc
Then you have only to set which frequency you're expecting to use, other option will come along.
NOTE
The syntax may be incorrect, but it's just for a clarification.
Ignorance comes with a cost