• AVR Freaks

Hot!Configuration bit for PIC18F45K50 with USB

Author
Flipp3rix
Starting Member
  • Total Posts : 34
  • Reward points : 0
  • Joined: 2019/02/28 14:40:19
  • Location: 0
  • Status: offline
2019/04/06 04:13:04 (permalink)
0

Configuration bit for PIC18F45K50 with USB

I need help generating  the configuration bit for PIC18F45K50 @48Mhz whit usb
#1

12 Replies Related Threads

    katela
    Super Member
    • Total Posts : 1228
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: Configuration bit for PIC18F45K50 with USB 2019/04/06 06:07:42 (permalink)
    +1 (1)
    You can check the sample examples in Microchip Libraries for Applications folder. Are you using an external crystal oscillator or using the internal?

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
    #2
    Flipp3rix
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2019/02/28 14:40:19
    • Location: 0
    • Status: offline
    Re: Configuration bit for PIC18F45K50 with USB 2019/04/06 06:10:18 (permalink)
    0
    katela
    You can check the sample examples in Microchip Libraries for Applications folder. Are you using an external crystal oscillator or using the internal?

    I want to use internal oscillator
    #3
    mpgmike
    Super Member
    • Total Posts : 124
    • Reward points : 0
    • Joined: 2014/01/23 17:27:06
    • Location: NJ
    • Status: offline
    Re: Configuration bit for PIC18F45K50 with USB 2019/04/06 09:32:30 (permalink)
    0
    The syntax is PBP3, but here is what I used for a PIC18F25K50 project:
    #CONFIG
    CONFIG PLLSEL = PLL3X ;3x clock multiplier
    CONFIG CFGPLLEN = ON ;PLL Enabled
    CONFIG CPUDIV = NOCLKDIV ;CPU uses system clock (no divide)
    CONFIG LS48MHZ = SYS48X8 ;System clock at 48 MHz, USB clock divider is set to 8
    CONFIG FOSC = INTOSCIO ;Internal oscillator
    CONFIG PCLKEN = OFF ;Primary oscillator shutdown firmware controlled
    CONFIG FCMEN = OFF ;Fail-Safe Clock Monitor disabled
    CONFIG IESO = OFF ;Oscillator Switchover mode disabled
    ; CONFIG PWRTEN = OFF ;Power up timer disabled
    CONFIG BOREN = ON ;BOR controlled by firmware (SBOREN is enabled)
    CONFIG BORV = 190 ;BOR set to 1.9V nominal
    ; CONFIG LPBOR = OFF ;Low-Power Brown-out Reset disabled
    CONFIG WDTEN = ON ;WDT enabled in hardware (SWDTEN ignored)
    CONFIG WDTPS = 512 ;1:512
    CONFIG CCP2MX = RC1 ;CCP2 input/output is multiplexed with RC1
    CONFIG PBADEN = OFF ;PORTB<5:0> pins are configured as digital I/O on Reset
    CONFIG T3CMX = RC0 ;T3CKI function is on RC0
    CONFIG SDOMX = RB3 ;SDO function is on RB3
    CONFIG MCLRE = OFF ;MCLR pin disabled; RE3 input enabled
    CONFIG STVREN = ON ;Stack full/underflow will cause Reset
    CONFIG LVP = OFF ;Single-Supply ICSP disabled
    ; CONFIG XINST = OFF ;Instruction set extension and Indexed Addressing mode disabled
    #ENDCONFIG
    #4
    Flipp3rix
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2019/02/28 14:40:19
    • Location: 0
    • Status: offline
    Re: Configuration bit for PIC18F45K50 with USB 2019/04/06 09:44:43 (permalink)
    0
    mpgmike
    The syntax is PBP3, but here is what I used for a PIC18F25K50 project:
    #CONFIG
    CONFIG PLLSEL = PLL3X ;3x clock multiplier
    CONFIG CFGPLLEN = ON ;PLL Enabled
    CONFIG CPUDIV = NOCLKDIV ;CPU uses system clock (no divide)
    CONFIG LS48MHZ = SYS48X8 ;System clock at 48 MHz, USB clock divider is set to 8
    CONFIG FOSC = INTOSCIO ;Internal oscillator
    CONFIG PCLKEN = OFF ;Primary oscillator shutdown firmware controlled
    CONFIG FCMEN = OFF ;Fail-Safe Clock Monitor disabled
    CONFIG IESO = OFF ;Oscillator Switchover mode disabled
    ; CONFIG PWRTEN = OFF ;Power up timer disabled
    CONFIG BOREN = ON ;BOR controlled by firmware (SBOREN is enabled)
    CONFIG BORV = 190 ;BOR set to 1.9V nominal
    ; CONFIG LPBOR = OFF ;Low-Power Brown-out Reset disabled
    CONFIG WDTEN = ON ;WDT enabled in hardware (SWDTEN ignored)
    CONFIG WDTPS = 512 ;1:512
    CONFIG CCP2MX = RC1 ;CCP2 input/output is multiplexed with RC1
    CONFIG PBADEN = OFF ;PORTB<5:0> pins are configured as digital I/O on Reset
    CONFIG T3CMX = RC0 ;T3CKI function is on RC0
    CONFIG SDOMX = RB3 ;SDO function is on RB3
    CONFIG MCLRE = OFF ;MCLR pin disabled; RE3 input enabled
    CONFIG STVREN = ON ;Stack full/underflow will cause Reset
    CONFIG LVP = OFF ;Single-Supply ICSP disabled
    ; CONFIG XINST = OFF ;Instruction set extension and Indexed Addressing mode disabled
    #ENDCONFIG



    Thanks! But in my case what should be my _XTAL_FREQ?
    I want to use internal oscillator
    post edited by Flipp3rix - 2019/04/06 09:54:24
    #5
    pcbbc
    Super Member
    • Total Posts : 922
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: Configuration bit for PIC18F45K50 with USB 2019/04/06 10:01:59 (permalink)
    0
    _XTAL_FREQ doesn’t set the internal clock frequency. Other configuration bits do that.

    All that define does is tell the delay macros your chosen clock speed. That’s so they can loop for the appropriate number of machine cycles to generate the delay you require.
    #6
    Flipp3rix
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2019/02/28 14:40:19
    • Location: 0
    • Status: offline
    Re: Configuration bit for PIC18F45K50 with USB 2019/04/06 10:11:19 (permalink)
    0
    pcbbc
    _XTAL_FREQ doesn’t set the internal clock frequency. Other configuration bits do that.

    All that define does is tell the delay macros your chosen clock speed. That’s so they can loop for the appropriate number of machine cycles to generate the delay you require.

    But without _XTAL_FREQ in header file compiler gave me error
    PS. delay of 1 sec is wrong
    post edited by Flipp3rix - 2019/04/06 10:26:18
    #7
    pcbbc
    Super Member
    • Total Posts : 922
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: Configuration bit for PIC18F45K50 with USB 2019/04/06 10:41:17 (permalink)
    0
    Yes, if you try to use the delay functions. Delays will also be wrong if you have to wrong setting of _XTAL_FREQ

    For correct delays the value set for _XTAL_FREQ must match the frequency set in the configuration bits, or as programmed in your initialisation.

    What clock frequency do you think you have programmed? Post the config settings and any code that modified clock frequency in startup.
    #8
    Flipp3rix
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2019/02/28 14:40:19
    • Location: 0
    • Status: offline
    Re: Configuration bit for PIC18F45K50 with USB 2019/04/06 10:48:32 (permalink)
    0
    Main file
     
    #include <stdio.h>
    #include <stdlib.h>
    #include "k4550.h"

    #define LED1 LATDbits.LATD2
    #define LED2 LATDbits.LATD3

    void main() {
         OSCCON = 0x76;
         TRISDbits.RD2 = 0;
         TRISDbits.RD3 = 0;

         while (1) {
            LED1 = 1;
            LED2 = 0;
            __delay_ms(1000);
            LED1 = 0;
            LED2 = 1;
            __delay_ms(1000);
        }
    }

     
    Configuration beat
    #include <xc.h>

    #pragma config PLLSEL = PLL3X // PLL Selection (3x clock multiplier)
    #pragma config CFGPLLEN = ON // PLL Enable Configuration bit (PLL Enabled)
    #pragma config CPUDIV = NOCLKDIV// CPU System Clock Postscaler (CPU uses system clock (no divide))
    #pragma config LS48MHZ = SYS48X8// Low Speed USB mode with 48 MHz system clock (System clock at 48 MHz, USB clock divider is set to 8)

    // CONFIG1H
    #pragma config FOSC = INTOSCIO // Oscillator Selection (Internal oscillator)
    #pragma config PCLKEN = OFF // Primary Oscillator Shutdown (Primary oscillator shutdown firmware controlled)
    #pragma config FCMEN = OFF // Fail-Safe Clock Monitor (Fail-Safe Clock Monitor disabled)
    #pragma config IESO = OFF // Internal/External Oscillator Switchover (Oscillator Switchover mode disabled)

    // CONFIG2L
    #pragma config nPWRTEN = OFF // Power-up Timer Enable (Power up timer disabled)
    #pragma config BOREN = ON // Brown-out Reset Enable (BOR controlled by firmware (SBOREN is enabled))
    #pragma config BORV = 190 // Brown-out Reset Voltage (BOR set to 1.9V nominal)
    #pragma config nLPBOR = OFF // Low-Power Brown-out Reset (Low-Power Brown-out Reset disabled)

    // CONFIG2H
    #pragma config WDTEN = ON // Watchdog Timer Enable bits (WDT enabled in hardware (SWDTEN ignored))
    #pragma config WDTPS = 32768 // Watchdog Timer Postscaler (1:32768)

    // CONFIG3H
    #pragma config CCP2MX = RC1 // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
    #pragma config PBADEN = OFF // PORTB A/D Enable bit (PORTB<5:0> pins are configured as digital I/O on Reset)
    #pragma config T3CMX = RC0 // Timer3 Clock Input MUX bit (T3CKI function is on RC0)
    #pragma config SDOMX = RB3 // SDO Output MUX bit (SDO function is on RB3)
    #pragma config MCLRE = OFF // Master Clear Reset Pin Enable (RE3 input pin enabled; external MCLR disabled)

    // CONFIG4L
    #pragma config STVREN = ON // Stack Full/Underflow Reset (Stack full/underflow will cause Reset)
    #pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
    #pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming Port Enable (ICPORT disabled)
    #pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled)

    // CONFIG5L
    #pragma config CP0 = OFF // Block 0 Code Protect (Block 0 is not code-protected)
    #pragma config CP1 = OFF // Block 1 Code Protect (Block 1 is not code-protected)
    #pragma config CP2 = OFF // Block 2 Code Protect (Block 2 is not code-protected)
    #pragma config CP3 = OFF // Block 3 Code Protect (Block 3 is not code-protected)

    // CONFIG5H
    #pragma config CPB = OFF // Boot Block Code Protect (Boot block is not code-protected)
    #pragma config CPD = OFF // Data EEPROM Code Protect (Data EEPROM is not code-protected)

    // CONFIG6L
    #pragma config WRT0 = OFF // Block 0 Write Protect (Block 0 (0800-1FFFh) is not write-protected)
    #pragma config WRT1 = OFF // Block 1 Write Protect (Block 1 (2000-3FFFh) is not write-protected)
    #pragma config WRT2 = OFF // Block 2 Write Protect (Block 2 (04000-5FFFh) is not write-protected)
    #pragma config WRT3 = OFF // Block 3 Write Protect (Block 3 (06000-7FFFh) is not write-protected)

    // CONFIG6H
    #pragma config WRTC = OFF // Configuration Registers Write Protect (Configuration registers (300000-3000FFh) are not write-protected)
    #pragma config WRTB = OFF // Boot Block Write Protect (Boot block (0000-7FFh) is not write-protected)
    #pragma config WRTD = OFF // Data EEPROM Write Protect (Data EEPROM is not write-protected)

    // CONFIG7L
    #pragma config EBTR0 = OFF // Block 0 Table Read Protect (Block 0 is not protected from table reads executed in other blocks)
    #pragma config EBTR1 = OFF // Block 1 Table Read Protect (Block 1 is not protected from table reads executed in other blocks)
    #pragma config EBTR2 = OFF // Block 2 Table Read Protect (Block 2 is not protected from table reads executed in other blocks)
    #pragma config EBTR3 = OFF // Block 3 Table Read Protect (Block 3 is not protected from table reads executed in other blocks)

    // CONFIG7H
    #pragma config EBTRB = OFF // Boot Block Table Read Protect (Boot block is not protected from table reads executed in other blocks)
    #define _XTAL_FREQ 16000000

    #9
    pcbbc
    Super Member
    • Total Posts : 922
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: Configuration bit for PIC18F45K50 with USB 2019/04/06 11:08:34 (permalink)
    +1 (1)
    #pragma config FOSC = INTOSCIO
    OSCCON = 0x76 = 0b01110110

    Bit 7 - IDLEN: Idle Enable bit
    0 = Device enters Sleep mode on SLEEP instruction
    Bits 6-4 - IRCF<2:0>: Internal RC Oscillator Frequency Select bits
    111 = HFINTOSC – (16 MHz)
    Bit 3 - OSTS: Oscillator Start-up Time-out Status bit (Read only)
    Bit 2 - HFIOFS: HFINTOSC Frequency Stable bit (Read only)
    Bits 1-0 - SCS<1:0>: System Clock Select bit

    1x = Internal oscillator block

    So from my reading of the datasheet looks it like you have high frequency oscillator at 16MHz configured.
    #define _XTAL_FREQ 16000000

    What were you expecting?
    post edited by pcbbc - 2019/04/06 11:13:01
    #10
    Flipp3rix
    Starting Member
    • Total Posts : 34
    • Reward points : 0
    • Joined: 2019/02/28 14:40:19
    • Location: 0
    • Status: offline
    Re: Configuration bit for PIC18F45K50 with USB 2019/04/06 11:20:39 (permalink)
    0
    pcbbc
    #pragma config FOSC = INTOSCIO
    OSCCON = 0x76 = 0b01110110

    Bit 7 - IDLEN: Idle Enable bit
    0 = Device enters Sleep mode on SLEEP instruction
    Bits 6-4 - IRCF<2:0>: Internal RC Oscillator Frequency Select bits
    111 = HFINTOSC – (16 MHz)
    Bit 3 - OSTS: Oscillator Start-up Time-out Status bit (Read only)
    Bit 2 - HFIOFS: HFINTOSC Frequency Stable bit (Read only)
    Bits 1-0 - SCS<1:0>: System Clock Select bit

    1x = Internal oscillator block

    So from my reading of the datasheet looks it like you have high frequency oscillator at 16MHz configured.
    #define _XTAL_FREQ 16000000

    What were you expecting?

    So i need to change the XTAL_FREQ?
    #11
    pcbbc
    Super Member
    • Total Posts : 922
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: Configuration bit for PIC18F45K50 with USB 2019/04/06 11:45:50 (permalink)
    +1 (1)
    I don’t know, do you?
    What frequency is your LED flashy test giving you? I would think 0.5Hz?

    Unless the 3x PLL is enabled, but I think that requires you explicitly enable it in OSCCON2 - and you haven’t.
    #12
    mpgmike
    Super Member
    • Total Posts : 124
    • Reward points : 0
    • Joined: 2014/01/23 17:27:06
    • Location: NJ
    • Status: offline
    Re: Configuration bit for PIC18F45K50 with USB 2019/04/07 07:51:24 (permalink)
    0
    I see you using "#include 'k4550.h' " when you were asking about a PIC18F45K50.  You shouldn't need to #include the processor at all using "#include xc8.h".
     
    You will need (48 / 3X PLL =) 16 MHz Fosc (and of course 3X PLL).
     
    USB & OSC related SFRs:
    OSCCON = 0x72
    OSCCON2 = 0x90
    OSCTUNEbits.SPLLMULT = 1
    UCON = 0x0E
    UCFG = 0x14
     
    Hope this helps
    #13
    Jump to:
    © 2019 APG vNext Commercial Version 4.5