News:

;) This forum is the property of Proton software developers

Main Menu

PIC16F18425

Started by joesaliba, Feb 02, 2025, 05:43 PM

Previous topic - Next topic

joesaliba

Needed a 14-pin PIC, and I had these PIC16F18425.

I cannot understand why it microchip do it so complicated to read and setup a `new' device, (for me), to get going.

At the moment I have the following: -

Device = 16F18425

 declare Xtal = 8

 oscfrq = %00000010

'============================================================================

Declare Reminders Off
@ CONFIG_REQ = 0          ; Override Compiler's configuration setting
asm-
__Config _Config1, 0x179A ;FEXTOSC_HS & RSTOSC_HFINTPLL & CLKOUTEN_OFF & CSWEN_OFF & FCMEN_OFF
__Config _Config2, 0x2FFE ;MCLRE_OFF & PWRTS_OFF & LPBOREN_OFF & BOREN_ON & BORV_LO & ZCDDIS_OFF & PPS1WAY_ON & STVREN_OFF
__Config _Config3, 0x3F9F ;WDTCPS_WDTCPS_31 & WDTE_OFF & WDTCWS_WDTCWS_7 & WDTCCS_SC
__Config _Config4, 0x1FFF ;BBSIZE_BB512 & BBEN_OFF & SAFEN_OFF & WRTAPP_OFF & WRTB_OFF & WRTC_OFF & WRTD_OFF & WRTSAF_OFF & LVP_OFF
__Config _Config5, 0x3FFF ;CP_OFF
endasm-
Declare Reminders On

Flashing an LED seems that I nailed the timing, but I cannot get a 4MHz frequency.

I thought that OSCFRQ = %00000010 was all it needed for 4MHz, but I was wrong.

Where should I look into the datasheet please?

Regards

Joe

top204

Microchip have a very nasty habit of changing things that do not need to be changed, or when changes or additions are made, the information for them is rather minimum now, unlike the past, where datasheets were bristling with information, and clearly laid out.

Below are two code listing templates for a PIC16F18425 device to use the internal and exteernal oscillator at 4MHz. They have not been tested on a real device, but you can see I have had to correct an anomaly in the code, because, again, microchip have changed the name of a commonly used SFR (RXPPS), and the ppi generator did not know it had changed the name so radically, so it missed it! So you know it compiles.

Below is the code listing template for an internal oscillator at 4MHz:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Internal Oscillator operating at 4MHz code listing template for a PIC16F18425 device.
' Written by Les Johnson for the Positron8 BASIC Compiler.
'
    Device = 16F18425                                               ' Tell the compiler what device to compile for
    Declare Xtal = 4                                                ' Tell the compiler what frequency the device is operating at (in MHz)
    Declare Auto_Heap_Strings  = On                                 ' Make all Strings "Heap" types, so they always get placed after standard variables
'
' Setup USART1
'
    Symbol RXPPS = $1ECB                                            ' Correction for an anomaly with this device for PPS pin for USART1 RX (SFR name changed)
    Declare Hserial_Baud = 9600                                     ' Set the Baud rate for USART1
    Declare HRSOut1_Pin  = PORTC.4                                  ' Tell the compiler to setup pin PORTC.4 for USART1 Tx
    Declare HRSIn1_Pin   = PORTC.5                                  ' Tell the compiler to setup pin PORTC.5 for USART1 Rx
   
'------------------------------------------------------------------------------------------
' The main program starts here
'
Main:
    Setup()                                                         ' Setup the program and any peripherals

    Do
   
    Loop
   
'------------------------------------------------------------------------------------------
' Setup the program and any peripherals
' Input     : None
' Outout    : None
' Notes     : None
'
Proc Setup()
    Osc_Init()                                                      ' Initialise the oscillator on a PIC16F18425 device
'
' More setups here when required
'
EndProc

'------------------------------------------------------------------------------------------
' Initialise the oscillator on a PIC16F18425 device
' Input     : None
' Outout    : None
' Notes     : For internal oscillator

Proc Osc_Init()           
    OSCCON1 = %01100010                                             ' NOSC is HFINTOSC. NDIV is 4
    OSCCON3 = %00000000
    OSCEN   = %00000000         
    OSCFRQ  = %00000010                                             ' HFFRQ is 4MHz
    OSCTUNE = %00000000
EndProc

'------------------------------------------------------------------------------------------
' Setup the config fuses for an internal oscillator on a PIC16F18425 device
'
    Config1 FEXTOSC_OFF,_                                           ' External Oscillator not enabled
            RSTOSC_HFINT1,_                                         ' Power-up default is HFINTOSC
            CLKOUTEN_OFF,_                                          ' CLKOUT function is disabled
            CSWEN_ON,_                                              ' Writing to NOSC and NDIV is allowed
            FCMEN_ON                                                ' Fail-Safe Clock Monitor  enabled

    Config2 MCLRE_ON,_                                              ' MCLR pin is Master Clear function
            PWRTS_OFF,_                                             ' Power-up Timer disabled
            LPBOREN_OFF,_                                           ' Low-Power BOR disabled
            BOREN_ON,_                                              ' Brown-out resetEnabled, SBOREN bit is ignored
            BORV_LO,_                                               ' Brown-out Reset Voltage (VBOR) set to 2.45V
            ZCDDIS_OFF,_                                            ' Zero-cross detect circuit is disabled at POR
            PPS1WAY_OFF,_                                           ' The PPSLOCK bit can be cleared and set multiple times in software
            STVREN_ON                                               ' Stack Overflow or Underflow will cause a reset

    Config3 WDTCPS_WDTCPS_31,_                                      ' WDT Period Divider ratio 1:65536. Software control of WDTPS
            WDTE_OFF,_                                              ' WDT Disabled, SWDTEN is ignored
            WDTCWS_WDTCWS_7,_                                       ' WDT window always open (100%). Software control
            WDTCCS_SC                                               ' WDT input clock is Software Control

    Config4 BBSIZE_BB512,_                                          ' Boot Block Size 512 words boot block size
            BBEN_OFF,_                                              ' Boot Block disabled
            SAFEN_OFF,_                                             ' SAF disabled
            WRTAPP_OFF,_                                            ' Application Block not write protected
            WRTB_OFF,_                                              ' Boot Block not write protected
            WRTC_OFF,_                                              ' Configuration Register not write protected
            WRTD_OFF,_                                              ' Data EEPROM not write protected
            WRTSAF_OFF,_                                            ' Storage Area Flash not write protected
            LVP_OFF                                                 ' High Voltage on MCLR/Vpp must be used for programming

    Config5 CP_OFF                                                  ' UserNVM Program memory code protection disabled

And below is a code listing template for an external oscillator:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' External Oscillator operating at 4MHz code listing template for a PIC16F18425 device.
' Written by Les Johnson for the Positron8 BASIC Compiler.
'
    Device = 16F18425                                               ' Tell the compiler what device to compile for
    Declare Xtal = 4                                                ' Tell the compiler what frequency the device is operating at (in MHz)
    Declare Auto_Heap_Strings  = On                                 ' Make all Strings "Heap" types, so they always get placed after standard variables
'
' Setup USART1
'
    Symbol RXPPS = $1ECB                                            ' Correction for an anomaly with this device for PPS pin for USART1 RX (SFR name changed)
    Declare Hserial_Baud = 9600                                     ' Set the Baud rate for USART1
    Declare HRSOut1_Pin  = PORTC.4                                  ' Tell the compiler to setup pin PORTC.4 for USART1 Tx
    Declare HRSIn1_Pin   = PORTC.5                                  ' Tell the compiler to setup pin PORTC.5 for USART1 Rx
   
'------------------------------------------------------------------------------------------
' The main program starts here
' Trnamsit text from USART1 to test the device's oscillator speed is correct
'
Main:
    Setup()                                                         ' Setup the program and any peripherals

    Do
        HRSOut1Ln "Hello World"
        DelayMS 100
    Loop
   
'------------------------------------------------------------------------------------------
' Setup the program and any peripherals
' Input     : None
' Outout    : None
' Notes     : None
'
Proc Setup()
    Osc_Init()                                                      ' Initialise the oscillator on a PIC16F18425 device
'
' More setups here when required
'
EndProc

'------------------------------------------------------------------------------------------
' Initialise the oscillator on a PIC16F18425 device
' Input     : None
' Outout    : None
' Notes     : For external oscillator
'
Proc Osc_Init()           
    OSCCON1 = %01110010                                             ' NOSC is EXTOSC. NDIV is 4
    OSCCON3 = %00000000
    OSCEN   = %00000000         
    OSCFRQ  = %00000010                                             ' HFFRQ is 4MHz
    OSCTUNE = %00000000
EndProc

'------------------------------------------------------------------------------------------
' Setup the config fuses for an external oscillator on a PIC16F18425 device
'
    Config1 FEXTOSC_HS,_                                            ' External Oscillator HS (crystal oscillator)
            RSTOSC_EXT1X,_                                          ' Power-up default is EXTOSC operating per FEXTOSC bits
            CLKOUTEN_OFF,_                                          ' CLKOUT function is disabled
            CSWEN_ON,_                                              ' Writing to NOSC and NDIV is allowed
            FCMEN_ON                                                ' Fail-Safe Clock Monitor  enabled

    Config2 MCLRE_ON,_                                              ' MCLR pin is Master Clear function
            PWRTS_OFF,_                                             ' Power-up Timer disabled
            LPBOREN_OFF,_                                           ' Low-Power BOR disabled
            BOREN_ON,_                                              ' Brown-out resetEnabled, SBOREN bit is ignored
            BORV_LO,_                                               ' Brown-out Reset Voltage (VBOR) set to 2.45V
            ZCDDIS_OFF,_                                            ' Zero-cross detect circuit is disabled at POR
            PPS1WAY_OFF,_                                           ' The PPSLOCK bit can be cleared and set multiple times in software
            STVREN_ON                                               ' Stack Overflow or Underflow will cause a reset

    Config3 WDTCPS_WDTCPS_31,_                                      ' WDT Period Divider ratio 1:65536. Software control of WDTPS
            WDTE_OFF,_                                              ' WDT Disabled, SWDTEN is ignored
            WDTCWS_WDTCWS_7,_                                       ' WDT window always open (100%). Software control
            WDTCCS_SC                                               ' WDT input clock is Software Control

    Config4 BBSIZE_BB512,_                                          ' Boot Block Size 512 words boot block size
            BBEN_OFF,_                                              ' Boot Block disabled
            SAFEN_OFF,_                                             ' SAF disabled
            WRTAPP_OFF,_                                            ' Application Block not write protected
            WRTB_OFF,_                                              ' Boot Block not write protected
            WRTC_OFF,_                                              ' Configuration Register not write protected
            WRTD_OFF,_                                              ' Data EEPROM not write protected
            WRTSAF_OFF,_                                            ' Storage Area Flash not write protected
            LVP_OFF                                                 ' High Voltage on MCLR/Vpp must be used for programming

    Config5 CP_OFF                                                  ' UserNVM Program memory code protection disabled





joesaliba

Thank you so much Les.

I did saw the RXPPS anomaly. In fact, I reported it to John for his PPS Wizard, which I thought it was not catching the name.

Kind regards

Joe

top204

It has now been corrected, so the compiler also looks for the "RX1DTPPS" SFR on enhanced 14-bit core devices.

Thanks for spotting it Joe.

joesaliba

Hi Les,

Tried your internal oscillator code and still going at 8MHz.

I changed OSCFRQ to OSCFRQ = %00000001 and it seems to be working at 4MHz now.

But I am sure that, although working, it is not the correct setting as it should be running at 2MHz.

I think there is something funny with the PLL but still cannot get my head around it.

Regards

Joe


trastikata

@joesaliba

The 2x PLL works only with 8, 12, or 16 MHz HFINTOSC. But how is that related to the 2 MHz you mentioned?

Joe, what exactly are you trying to achieve, please clarify.

@top204

Les, your code example for HFINTOSC is actually for 1 MHz clock:

Config1 RSTOSC_HFINT1 - set HFINTOSC to 1 MHZ [110 HFINTOSC (1 MHz), with OSCFRQ = '010' (4 MHz) and CDIV = '0010' (4:1)]

Proc Osc_Init()  basically does the same:
OSCFRQ  = %00000010  --> 4 MHz HFINTOSC
OSCCON1 = %01100010 --> NOSC = HFINTOSC ; NDIV = Clock Divider 4

Thus your example runs at 1 MHz



joesaliba

Trastikata,

I would like this particular PIC to run at 4MHz.

Changing the OSCFRQ to OSCFRQ = %00000001 timing is correct while flashing an LED at 500ms, but obviously, interrupts are not correct, or at least I think, as this thing is driving me crazy.

Joe

joesaliba

Forgot to say, internal oscillator.

trastikata

Quote from: joesaliba on Feb 07, 2025, 09:39 PMI would like this particular PIC to run at 4MHz.

Config1 RSTOSC_HFINT1 --> set HFINTOSC to 1 MHZ

OSCFRQ  = %00000010  --> 4 MHz HFINTOSC
OSCCON1 = %01100000 --> NOSC = HFINTOSC ; NDIV = Clock Divider 1

joesaliba

So sorry for the late reply.

No trastikata, with your settings it runs at 8Mhz.

I know that the following is not correct, but runs at 4MHz: -

OSCCON1 = %01100010                ' NOSC is HFINTOSC. NDIV is 4
 OSCCON3 = %00000000
 OSCEN   = %00000000
 OSCFRQ  = %00000001                ' HFFRQ is 4MHz
 OSCTUNE = %00000000