News:

;) This forum is the property of Proton software developers

Main Menu

Config 18F26K40

Started by GERARD, Jan 09, 2024, 06:56 PM

Previous topic - Next topic

GERARD

Hello,
Is there a simple method for the 18F26K40 configuration bits?
Thank you in advance.
I am the happy grandpa of twins

ricardourio

JohnB made a very good plugin for Proton IDE, you can download PIC Fuse Configurator here :
https://protoncompiler.com/index.php/topic,40.0.html

Ricardo Urio

GERARD

Thanks, I'll try it out.
I am the happy grandpa of twins

Stephen Moss

If you are using John's Positron Studio IDE instead of the Mechanique one that is packaged with the compiler the fuse configurator is built in, it is the Fuses tab located at the bottom right bottom of every source code file tab.

top204

#4
With the newer devices, I tend to use their internal oscillators now because they are very stable, even with naturally hot and cold temperatures. It also allows two extra pins to be available for I/O lines. i.e. RA6 and RA7. So my default program code layout for a PIC18F26K40 device is:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
'
' Written for the Positron8 compiler by Les Johnson.
'
    Device = 18F26K40                               ' Tell the compiler what device to compile for
    Declare Xtal = 64                               ' Tell the compiler what frequency the device will be operating at (in MHz)
'
' Setup USART1
'
    Declare Hserial1_Baud = 9600
    Declare HRSOut1_Pin   = PORTC.6
    Declare HRSIn1_Pin    = PORTC.7

'--------------------------------------------------------------------
' The main program starts here
'
Main:
    Setup()                                         ' Setup the program and any peripherals before the program loop starts
'
' Test the oscillator speed by viewing the "Hello World" on a serial terminal
' If it is viewed correctly, the oscillator is running at the correct frequency.
'
    Do
        HRSOutLn "Hello World"
        DelayMS 500
    Loop

'--------------------------------------------------------------------
' Setup the program and any peripherals
' Input     : None
' Output    : None
' Notes     : None
'
Proc Setup()
    Oscillator_64MHz()                              ' Setup the internal oscillator speed to 64MHz
'
' Extra setups go here...
'
EndProc

'--------------------------------------------------------------------
' Set the PIC18F26K40 device to internal 4MHz operation with a HFINTOSC_1MHZ fuse
' Input     : None
' Output    : None
' Notes     : Waits for the oscillator to become stable
'
Proc Oscillator_4MHz()
    OSCCON1 = %01100000
    OSCCON3 = %00000000
    OSCEN   = %00000000
    OSCFRQ  = %00000010
    OSCTUNE = %00000000
    Repeat: Until OSCSTATbits_HFOR = 1
EndProc

'--------------------------------------------------------------------
' Set the PIC18F26K40 device to internal 8MHz operation with an HFINTOSC_1MHZ fuse
' Input     : None
' Output    : None
' Notes     : Waits for the oscillator to become stable
'
Proc Oscillator_8MHz()
    OSCCON1 = %01100000
    OSCCON3 = %00000000
    OSCEN   = %00000000
    OSCFRQ  = %00000011
    OSCTUNE = %00000000
    Repeat: Until OSCSTATbits_HFOR = 1
EndProc

'--------------------------------------------------------------------
' Set the PIC18F26K40 device to internal 16MHz operation with an HFINTOSC_1MHZ fuse
' Input     : None
' Output    : None
' Notes     : Waits for the oscillator to become stable
'
Proc Oscillator_16MHz()
    OSCCON1 = %01100000
    OSCCON3 = %00000000
    OSCEN   = %00000000
    OSCFRQ  = %00000101
    OSCTUNE = %00000000
    Repeat: Until OSCSTATbits_HFOR = 1
EndProc

'--------------------------------------------------------------------
' Set the PIC18F26K40 device to internal 32MHz operation with an HFINTOSC_1MHZ fuse
' Input     : None
' Output    : None
' Notes     : Waits for the oscillator to become stable
'
Proc Oscillator_32MHz()
    OSCCON1 = %01100000
    OSCCON3 = %00000000
    OSCEN   = %00000000
    OSCFRQ  = %00000110
    OSCTUNE = %00000000
    Repeat : Until OSCSTATbits_HFOR = 1
EndProc

'--------------------------------------------------------------------
' Set the PIC18F26K40 device to internal 64MHz operation with an HFINTOSC_1MHZ fuse
' Input     : None
' Output    : None
' Notes     : Waits for the oscillator to become stable
'
Proc Oscillator_64MHz()
    OSCCON1 = %01100000
    OSCCON3 = %00000000
    OSCEN   = %00000000
    OSCFRQ  = %00001000
    OSCTUNE = %00000000
    Repeat: Until OSCSTATbits_HFOR = 1
EndProc

'-----------------------------------------------------------------------------------------
' Setup the fuses to use the internal oscillator on a PIC18F26K40. With RA6 and RA7 as I/O lines
'
Config_Start
    RSTOSC = HFINTOSC_1MHZ          ' With HFFRQ = 4MHz and CDIV = 4:1
    FEXTOSC = Off                   ' External Oscillator not enabled
    WDTE = Off                      ' WDT disabled
    CLKOUTEN = Off                  ' CLKOUT function is disabled
    CSWEN = On                      ' Writing to NOSC and NDIV is allowed
    FCMEN = Off                     ' Fail-Safe Clock Monitor disabled
    MCLRE = EXTMCLR                 ' If LVP = 0, MCLR pin is MCLR. If LVP = 1, RE3 pin function is MCLR
    PWRTE = On                      ' Power up timer enabled
    LPBOREN = off                   ' ULPBOR disabled
    BOREN = On                      ' Brown-out turned on
    BORV = VBOR_245                 ' Brown-out Reset Voltage (VBOR) set to 2.45V
    ZCD = Off                       ' ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON
    PPS1WAY = Off                   ' PPSLOCK bit can be set and cleared repeatedly (subject to the unlock sequence)
    STVREN = Off                    ' Stack full/underflow will not cause Reset
    Debug = Off                     ' Background debugger disabled
    XINST = Off                     ' Extended Instruction Set and Indexed Addressing Mode disabled
    SCANE = Off                     ' Scanner module is Not available for use. SCANMD bit is ignored
    LVP = Off                       ' Low Voltage Programming disabled
    WDTCPS = WDTCPS_15              ' Watchdog Divider ratio 1:1048576 (32 seconds)
    WDTCWS = WDTCWS_7               ' Window always open (100%). Software control. Keyed access not required
    WDTCCS = LFINTOSC               ' WDT input clock selector->WDT reference clock is the 31.2kHz HFINTOSC output
    WRT0 = Off                      ' Block 0 (000800-001FFF) not write-protected
    WRT1 = Off                      ' Block 1 (002000-003FFF) not write-protected
    WRTC = Off                      ' Configuration registers (300000-30000B) not write-protected
    WRTB = Off                      ' Boot Block (000000-0007FF) write-protected
    WRTD = Off                      ' Data EEPROM not write-protected
    Cp = Off                        ' UserNVM code protection disabled
    CPD = Off                       ' DataNVM code protection disabled
    EBTR0 = Off                     ' Block 0 (000800-001FFF) not protected from table reads executed in other blocks
    EBTR1 = Off                     ' Block 1 (002000-003FFF) not protected from table reads executed in other blocks
    EBTRB = Off                     ' Boot Block (000000-0007FF) not protected from table reads executed in other blocks
Config_End

As can be seen, there are a few procedures that allow the device to operate at different frequencies, but I virtually always run a device at 64MHz, unless lower currents are required, then the oscillator can be slowed down or speeded up dynamically. So the oscillator setting procedures that are not needed can be removed to make the code listing less cluttered.

It's just a shame the internal oscillator cannot be over-clocked because the 18F devices are capable of running at 100MHz without any problems, but an external 25MHz crystal is required for that, with 4xPLL enabled.

When initially testing the program, the simple loop with the serial output text is a good method of testing the device's layout. If "Hello World" is viewed on a serial terminal set to 9600 Baud, the microcontroller is operating at the correct speed for its setups.

GERARD

Thank you all very much.
This is what I wanted.
I am the happy grandpa of twins