• AVR Freaks

Hot!#pragma problems

Author
Tuurbo46
Super Member
  • Total Posts : 226
  • Reward points : 0
  • Joined: 2004/09/12 05:05:45
  • Location: UK
  • Status: offline
2019/06/12 02:00:43 (permalink)
0

#pragma problems

Hi,
 
I am using an 18f2520 and i am trying to switch between and external high power to internal low power clock to put the device into sleep low power mode.
 
The problem i have encountered is mplab c18 v8.92 does not allow:
 

CONFIG1Hbits.FOSC3 = 0; // External RC Oscillator
CONFIG1Hbits.FOSC2 = 1;
CONFIG1Hbits.FOSC1 = 1;
CONFIG1Hbits.FOSC0 = 1;

 
Also pragma commands cannot be added into functions as these are only set once at compilation. How would i therefore add CONFIG1H commands into a function to switch between internal and external clocks?
 
Hope this makes sense.
 
Regards,
 
Tuurbo46
#1

18 Replies Related Threads

    pcbbc
    Super Member
    • Total Posts : 1325
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: #pragma problems 2019/06/12 04:31:09 (permalink)
    +1 (1)
    You can't set CONFIG bits at runtime.  They are programmed once into the chip by the programmer and define the default/statup conditions.
     
    Then once the PIC is started, for some parameters it is possible to configure different values via the standard PIC registers.
     
    Edit: For the 18f2520 you can change clock sources and frequencies via the OSCCON (Oscillator Control Register) register.  See 2.7 Clock Sources and Oscillator Switching.
    post edited by pcbbc - 2019/06/12 04:43:53
    #2
    1and0
    Access is Denied
    • Total Posts : 9763
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: online
    Re: #pragma problems 2019/06/12 05:27:40 (permalink)
    0
    pcbbc
    You can't set CONFIG bits at runtime.  

    Actually, for some bits you can program them at runtime. ;)
    #3
    pcbbc
    Super Member
    • Total Posts : 1325
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: #pragma problems 2019/06/12 05:58:35 (permalink)
    +1 (1)
    1and0
    pcbbc
    You can't set CONFIG bits at runtime.  

    Actually, for some bits you can program them at runtime. ;)

    Well yes, but not how the OP is trying to do it.
    #4
    1and0
    Access is Denied
    • Total Posts : 9763
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: online
    Re: #pragma problems 2019/06/12 06:35:45 (permalink)
    0
    pcbbc
    1and0
    pcbbc
    You can't set CONFIG bits at runtime.  

    Actually, for some bits you can program them at runtime. ;)

    Well yes, but not how the OP is trying to do it.

    Of course not, it is located in flash memory.
    #5
    Tuurbo46
    Super Member
    • Total Posts : 226
    • Reward points : 0
    • Joined: 2004/09/12 05:05:45
    • Location: UK
    • Status: offline
    Re: #pragma problems 2019/06/19 05:43:26 (permalink)
    0
    Hi all,

     

    I have read through section 2.7 clock sources and oscillator switching and i am not sure i can complete what i want to do. On page 28 the datasheet shows a diagram with a mux selecting between oscillator sources. I have tried using SCS1:SCS0 to select between sources but the function does not work. I set the oscillator bits at compile time to an internal oscillator running at 4MHz then at run time i try to switch to the external 32MHz oscillator using SCS1:SCS0 2) Run mode, but the mux does not switch between internal and external sources at run time. Does this mean this is not possible or am i setting the registers wrong?

     

    So i have 2 run modes for my project:

     

    1) Low power mode using internal 4MHz clock:

     

        OSCCONbits.IRCF2 = 1; // 4MHz
        OSCCONbits.IRCF1 = 1;
        OSCCONbits.IRCF0 = 0;

        OSCCONbits.SCS1 = 1; // internal oscillator
        OSCCONbits.SCS0 = 0;

     

    2) Run mode using external 32MHz clock:

     

        OSCCONbits.SCS1 = 0; // primary oscillator
        OSCCONbits.SCS0 = 0;

     

     
    Hi,
     
    I have set the start up oscillator in configuration bits at compile time as (Internal oscillator block, CLKO function on RA6, port function on RA7) - running at 4MHz.  At run time i am trying to switch between the internal 4MHz and external 32MHz oscillator using SCS1:SCS0.
     
    So just to confirm again, this is not possible?
     
    Thanks,
     
    Tuurbo46
     
    post edited by Tuurbo46 - 2019/06/19 07:43:30
    #6
    pcbbc
    Super Member
    • Total Posts : 1325
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: #pragma problems 2019/06/19 07:01:10 (permalink)
    +1 (1)
    What do you have configured via CONFIG1H: CONFIGURATION REGISTER 1 HIGH for your primary oscillator?
    bit 3-0 FOSC<3:0>: Oscillator Selection bits
    11xx = External RC oscillator, CLKO function on RA6
    101x = External RC oscillator, CLKO function on RA6
    1001 = Internal oscillator block, CLKO function on RA6; port function on RA7
    1000 = Internal oscillator block, port function on RA6 and RA7
    0111 = External RC oscillator, port function on RA6
    0110 = HS oscillator, PLL enabled (Clock Frequency = 4 x FOSC1)
    0101 = EC oscillator, port function on RA6
    0100 = EC oscillator, CLKO function on RA6
    0011 = External RC oscillator, CLKO function on RA6
    0010 = HS oscillator0001 = XT oscillator0000 = LP oscillator

     
    If it's one of the internal oscillator options, you are not going to have any luck switching to the external (edit: primary) oscillator via OSCCON: OSCILLATOR CONTROL REGISTER:
    bit 1-0 SCS<1:0>: System Clock Select bits
    1x = Internal oscillator block
    01 = Secondary (Timer1) oscillator
    00 = Primary oscillator

    Edit: You may switch to the external secondary (Timer 1) oscillator.
     
    See page 26:
    The primary oscillators include the External Crystal and Resonator modes, the External RC modes, the External Clock modes and the internal oscillator block. The particular mode is defined by the FOSC<3:0> Configuration bits.

     
    Basically the "primary oscillator" is defined by whatever you configure using the configuration words when you program the device.  That may or may NOT be an external clock source, but you cannot change the primary source between internal and external at runtime.
    post edited by pcbbc - 2019/06/19 07:08:01
    #7
    1and0
    Access is Denied
    • Total Posts : 9763
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: online
    Re: #pragma problems 2019/06/19 07:06:01 (permalink)
    +1 (1)
    As usual, the answer lies within what is NOT shown.  Post your config bits settings.
    #8
    pcbbc
    Super Member
    • Total Posts : 1325
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: #pragma problems 2019/06/19 07:10:00 (permalink)
    0
    1and0
    As usual, the answer lies within what is NOT shown.  Post your config bits settings.

    Actually, he does hint with...
    I set the oscillator bits at compile time to an internal oscillator running at 4MHz

    So we might guess one of the internal oscillator options.
    post edited by pcbbc - 2019/06/19 07:15:31
    #9
    1and0
    Access is Denied
    • Total Posts : 9763
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: online
    Re: #pragma problems 2019/06/19 07:24:21 (permalink)
    0
    pcbbc
     
    Actually, he does hint with...
    I set the oscillator bits at compile time to an internal oscillator running at 4MHz

    So we might guess one of the internal oscillator options.

    It is ambiguous. So, "oscillator bits" is set via CONFIG1H or SCS1:SCS0?  I don't think 4 MHz is set via CONFIG1H.
     
    #10
    Tuurbo46
    Super Member
    • Total Posts : 226
    • Reward points : 0
    • Joined: 2004/09/12 05:05:45
    • Location: UK
    • Status: offline
    Re: #pragma problems 2019/06/19 07:54:33 (permalink)
    0
    *******************************************************************************
    I have read through section 2.7 clock sources and oscillator switching and i am not sure i can complete what i want to do. On page 28 the datasheet shows a diagram with a mux selecting between oscillator sources. I have tried using SCS1:SCS0 to select between sources but the function does not work. I set the oscillator bits at compile time to an internal oscillator running at 4MHz then at run time i try to switch to the external 32MHz oscillator using SCS1:SCS0 2) Run mode, but the mux does not switch between internal and external sources at run time. Does this mean this is not possible or am i setting the registers wrong?

    So i have 2 run modes for my project: 

    1) Low power mode using internal 4MHz clock:

        OSCCONbits.IRCF2 = 1; // 4MHz
        OSCCONbits.IRCF1 = 1;
        OSCCONbits.IRCF0 = 0;

        OSCCONbits.SCS1 = 1; // internal oscillator
        OSCCONbits.SCS0 = 0;

    2) Run mode using external 32MHz clock:

        OSCCONbits.SCS1 = 0; // primary oscillator
        OSCCONbits.SCS0 = 0;
        
    *******************************************************************************
     Hi,
     
    I have set the start up oscillator in configuration bits at compile time as (Internal oscillator block, CLKO function on RA6, port function on RA7) - running at 4MHz.  At run time i am trying to switch between the internal 4MHz and external 32MHz oscillator using SCS1:SCS0.
     
    So just to confirm again, this is not possible?
     
    Thanks,
     
    Tuurbo46

     
    #11
    pcbbc
    Super Member
    • Total Posts : 1325
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: #pragma problems 2019/06/19 08:06:36 (permalink)
    +1 (1)
    Tuurbo46So just to confirm again, this is not possible?

    No.

    You must configure an external oscillator option, as that controls what is defined as the primary oscillator.
    Then, at runtime, you can switch back and forth between primary (external) and the internal oscillator using OSCCON SCS.
     
    The only way to start on the internal oscillator, and be able to switch to an external source at runtime, would be to use the Timer 1 external oscillator.
     
    Is there some reason you cannot start with the external oscillator selected? You can switch to the internal one as the first thing your application does.
     
    Edit: And you're not starting at 4MHz.  1MHz is the default start up frequency for the internal oscillator.  See page 30 OSCON note 3:
    bit 6-4IRCF<2:0>: Internal Oscillator Frequency Select bits
    100 = 1 MHz(3)
    Note: (3) Default output frequency of INTOSC on Reset.

    post edited by pcbbc - 2019/06/19 08:11:17
    #12
    1and0
    Access is Denied
    • Total Posts : 9763
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: online
    Re: #pragma problems 2019/06/19 09:11:46 (permalink)
    +1 (1)
    For the second time, post your config bits settings.  You are switching between the internal oscillator and the "primary oscillator" selected by the config bits; so you must setup the config bits to the external 32 MHz oscillator.
    #13
    1and0
    Access is Denied
    • Total Posts : 9763
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: online
    Re: #pragma problems 2019/06/19 09:11:59 (permalink)
    0
    ... stupid forum, double post.
    #14
    1and0
    Access is Denied
    • Total Posts : 9763
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: online
    Re: #pragma problems 2019/06/19 09:16:08 (permalink)
    +1 (1)
    Tuurbo46
    I have set the start up oscillator in configuration bits at compile time as (Internal oscillator block, CLKO function on RA6, port function on RA7) - running at 4MHz.  At run time i am trying to switch between the internal 4MHz and external 32MHz oscillator using SCS1:SCS0.

    Where do you think the 32 MHz oscillator is coming from?
     
    #15
    Tuurbo46
    Super Member
    • Total Posts : 226
    • Reward points : 0
    • Joined: 2004/09/12 05:05:45
    • Location: UK
    • Status: offline
    Re: #pragma problems 2019/06/19 11:13:50 (permalink)
    0
    Hi 1and0,

    Thanks for your help.

    I will post all my configuration settings and more details tomorrow. I have set these in the drop down toolbar in mplab v8.92, but i will include all individual settings.

    I think i know what the problem is. If i use the external crystal to first power the micro up, the external crystal has an enable pin controlled by an i/o pin. I/o pin high running, i/o pin low stopped. This i/o pin is set high at run time, therefore im guessing the external crystal never gets turned on. If this is the problem, could this i/o pin be initially set at compile time to power the crystal up sooner?

    Thanks,

    Tuurbo46
    #16
    NKurzman
    A Guy on the Net
    • Total Posts : 17846
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: #pragma problems 2019/06/19 11:43:41 (permalink)
    +1 (1)
    The Crystal is Setup in the Config bits.  Once you do the Pins I/O functions are disabled.
    #17
    pcbbc
    Super Member
    • Total Posts : 1325
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: #pragma problems 2019/06/19 11:46:43 (permalink)
    +1 (1)
    You can’t configure IO pins at startup. If they are not assigned a special function (clock out for example) they all start as inputs = high impedance.
    Pull the pin high with a pull-up resistor for startup. The PIC can then drive the pin LOW to turn off the oscillator later.
    #18
    1and0
    Access is Denied
    • Total Posts : 9763
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: online
    Re: #pragma problems 2019/06/19 11:49:08 (permalink)
    +1 (1)
    Tuurbo46
    I will post all my configuration settings and more details tomorrow. I have set these in the drop down toolbar in mplab v8.92, but i will include all individual settings.

    Is the "Configuration Bits set in code" checkbox unchecked?  Anyway, it's best to include ALL the config bits settings IN your source code.
     

    I think i know what the problem is. If i use the external crystal to first power the micro up, the external crystal has an enable pin controlled by an i/o pin. I/o pin high running, i/o pin low stopped. This i/o pin is set high at run time, therefore im guessing the external crystal never gets turned on. If this is the problem, could this i/o pin be initially set at compile time to power the crystal up sooner?

    A "crystal" controlled by an I/O pin?  Post your schematic.  If you meant it is an oscillator, then use a pull-up resistor; and if that is not fast enough add a parallel capacitor.
     
    post edited by 1and0 - 2019/06/19 11:51:06
    #19
    Jump to:
    © 2019 APG vNext Commercial Version 4.5