Positron8 - Setting up a PIC18F45K80 device to use its internal oscillator

Started by top204, Apr 08, 2024, 02:55 PM

Previous topic - Next topic

top204

There are several combinations of config fuse settings and SFR (Special Function Register) loading that can give particular oscillator speeds on the later PIC devices, but I always prefer the software switching methods, when possible, because a simple call to a particular procedure will alter the speed of the device, and the config fuse settings can stay they same. However, always make sure the Declare Xtal value matches the device's actual operating speed because this is what tells the compiler what frequency the device is operating at, so it can create the correct timings code at compile time.

An example of a standard Positron8 BASIC code layout for a PIC18F45K80 device that can run at 16MHz or 32MHz or 64MHz using its internal oscillator is listed below. All the information required is in the device's datasheet and the available fuse settings are in a devices's compiler '.PPI' file:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Setup a PIC18F45K80 device to operate at 16MHz or 32MHz or 64MHz using its internal oscillator and 4xPLL software enabled.
'
' Written for the Positron8 compiler by Les Johnson.
'
    Device = 18F45K80                           ' Tell the compiler what device to compile for
    Declare Xtal = 32                           ' 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
'
' Create any global variables and constants here
'

'----------------------------------------------------------------------
' The main program starts here
' Transmit the text "Hello World" at 9600 Baud to make sure the device is running at the speed required by the Xtal declare.
' If the text is not seen on the serial terminal, then the device is not operating at the correct speed for the Xtal Declare.
'
Main:
    Setup()                                     ' Setup the program and any peripherals

    Do                                          ' Create a loop
        HRsout1Ln "Hello World"                 ' Transmit text to a serial terminal
        DelayMs 500                             ' Delay between transmissions
    Loop                                        ' Do it forever

'----------------------------------------------------------------------
' Setup the program and any peripherals
' Input     : None
' Output    : None
' Notes     : None
'
Proc Setup()
    'Int_Osc16MHz()                              ' Use to set the internal oscillator to operate at 16MHz on a PIC18F45K80 device
    Int_Osc32MHz()                              ' Use to set the internal oscillator to operate at 32MHz on a PIC18F45K80 device
    'Int_Osc64MHz()                              ' Use to set the internal oscillator to operate at 64MHz on a PIC18F45K80 device
'
' Any other setup code goes here, after the device's oscillator has been setup
'
EndProc

'----------------------------------------------------------------------
' Set the internal oscillator to operate at 16MHz on a PIC18F45K80 device
' Input     : None
' Output    : None
' Notes     : None
'
Proc Int_Osc16MHz()
    OSCCON  = %01010000                         ' 4MHz
    OSCCON2 = $00
    OSCTUNE = %10000000                         ' Use INTRC with 4xPLL enabled
    REFOCON = $00
    DelayMS 100                                 ' Wait for stability
EndProc

'----------------------------------------------------------------------
' Set the internal oscillator to operate at 32MHz on a PIC18F45K80 device
' Input     : None
' Output    : None
' Notes     : None
'
Proc Int_Osc32MHz()
    OSCCON  = %01100000                         ' 8MHz
    OSCCON2 = $00
    OSCTUNE = %10000000                         ' Use INTRC with 4xPLL enabled
    REFOCON = $00
    DelayMS 100                                 ' Wait for stability
EndProc

'----------------------------------------------------------------------
' Set the internal oscillator to operate at 64MHz on a PIC18F45K80 device
' Input     : None
' Output    : None
' Notes     : None
'
Proc Int_Osc64MHz()
    OSCCON  = %01110000                         ' 16MHz
    OSCCON2 = $00
    OSCTUNE = %10000000                         ' Use INTRC with 4xPLL enabled
    REFOCON = $00
    DelayMS 100                                 ' Wait for stability
EndProc

'----------------------------------------------------------------------
' Setup the fuses for internal oscillator on a PIC18F45K80
' The OSC pins are set as genral purpose I/O pins
'
Config_Start
    FOSC = INTIO2                   ' Internal RC oscillator
    PLLCFG = Off                    ' 4xPLL disabled
    RETEN = Off                     ' Ultra low-power regulator is disabled (Controlled by REGSLP bit)
    INTOSCSEL = HIGH                ' LF-INTOSC in High power mode during Sleep
    SOSCSEL = DIG                   ' SOSC Power Selection and mode is digital (SCLKI)
    FCMEN = Off                     ' Fail-Safe Clock Monitor disabled
    IESO = Off                      ' Internal External Oscillator Switch Over Mode disabled
    PWRTEN = Off                    ' Power Up Timer disabled
    BOREN = SBORDIS                 ' Brown Out Detect Enabled in hardware. SBOREN disabled
    BORV = 3                        ' Brown-out Reset Voltage is 1.8V
    BORPWR = ZPBORMV                ' ZPBORMV instead of BORMV is selected
    WDTEN = Off                     ' WDT disabled in hardware. SWDTEN bit disabled
    WDTPS = 128                     ' Watchdog Postscaler is 1:128
    CANMX = PORTB                   ' ECAN TX and RX pins are located on RB2 and RB3
    MSSPMSK = MSK7                  ' MSSP uses a 7 Bit address masking
    MCLRE = On                      ' MCLR Enabled. RE3 disabled
    STVREN = Off                    ' Stack Overflow Reset disabled
    XINST = Off                     ' Extended Instruction Set disabled
    BBSIZ = BB1K                    ' 1K word Boot Block size
    CP0 = Off                       ' Code Protect 00800-01FFF disabled
    CP1 = Off                       ' Code Protect 02000-03FFF disabled
    CP2 = Off                       ' Code Protect 04000-05FFF disabled
    CP3 = Off                       ' Code Protect 06000-07FFF disabled
    CPB = Off                       ' Code Protect Boot disabled
    CPD = Off                       ' EEPROM Read Protect disabled
    WRT0 = Off                      ' Table Write Protect 00800-01FFF disabled
    WRT1 = Off                      ' Table Write Protect 02000-03FFF disabled
    WRT2 = Off                      ' Table Write Protect 04000-05FFF disabled
    WRT3 = Off                      ' Table Write Protect 06000-07FFF disabled
    WRTC = Off                      ' Config Write Protect disabled
    WRTB = Off                      ' Table Write Protect Boot disabled
    WRTD = Off                      ' EEPROM Write Protect disabled
    EBTR0 = Off                     ' Table Read Protect 00800-01FFF disabled
    EBTR1 = Off                     ' Table Read Protect 02000-03FFF disabled
    EBTR2 = Off                     ' Table Read Protect 04000-05FFF disabled
    EBTR3 = Off                     ' Table Read Protect 06000-07FFF disabled
    EBTRB = Off                     ' Table Read Protect Boot disabled
Config_End