Hot!PIC16F15345 Oscillator stuck at 1MHz?

Author
steveonline84
New Member
  • Total Posts : 3
  • Reward points : 0
  • Joined: 2018/04/13 17:19:57
  • Location: 0
  • Status: offline
2018/04/13 21:03:54 (permalink)
0

PIC16F15345 Oscillator stuck at 1MHz?

Hi, i have not played around with pics for several years, and some things have changed.
 
Right now I have the PIC running, and I intended to use the internal oscillator (non timing-critical application) but the clock seems to be running at 1MHz no matter what I try, I would prefer it to run at its maximum 32MHz.
 
I have set the configuration word 1 as follows:
 
// CONFIG1
#pragma config FEXTOSC = OFF // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINTPLL // Power-up default value for COSC bits (HFINTOSC with OSCFRQ= 32 MHz and CDIV = 1:1)
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = OFF // Clock Switch Enable bit (The NOSC and NDIV bits cannot be changed by user software)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (FSCM timer disabled)

 
I believe the clock is only running at 1MHz because i only have to increment count 500 times to create an approx 1ms delay function:
 

void DELAY(int ms) //1ms delay
{
for(int r=ms; r>0; r--) //perform a 1ms delay ms number of times
{
int i = 0;
while(i<500)
{
i++;
}
}
}

 
Are there any other settings that need to be considered to change the clock frequency? 
 
Also, no matter what I set the "RSTOSC" value to, it appears to have no effect, I have tried setting it to a low frequency oscillator, high frequency, and in-between.
 
Regards,
 
Stephen
#1

6 Replies Related Threads

    katela
    Super Member
    • Total Posts : 854
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: online
    Re: PIC16F15345 Oscillator stuck at 1MHz? 2018/04/16 09:11:13 (permalink)
    0
    The easiest way to set your configuration bits is: From the main menu select Window ▶ PIC Memory Views ▶ Configuration Bits as explained in Microchip Developer Help.

    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/user/StudentCompanionSA
    #2
    steveonline84
    New Member
    • Total Posts : 3
    • Reward points : 0
    • Joined: 2018/04/13 17:19:57
    • Location: 0
    • Status: offline
    Re: PIC16F15345 Oscillator stuck at 1MHz? 2018/04/16 09:22:03 (permalink)
    0
    You are right, I seem to have been over-writing configuration bits in my code with the configuration bits in the configuration bits menu !
     
    Just removed all my internal code configuration bit settings and now seems to be working well.  (Although still not sure what was wrong with my internal config settings - but I can live with that for now!)
     
    Apologies for my rookie mistake - and thank you
     
    :)
    #3
    Sobottech
    Super Member
    • Total Posts : 233
    • Reward points : 0
    • Joined: 2015/12/02 03:32:17
    • Location: 0
    • Status: offline
    Re: PIC16F15345 Oscillator stuck at 1MHz? 2018/04/16 09:38:20 (permalink)
    0
    The datasheet tells:
     
    9.2.2.1 HFINTOSC
    The High-Frequency Internal Oscillator (HFINTOSC) is
    a precision digitally-controlled internal clock source
    that produces a stable clock up to 32 MHz. The
    HFINTOSC can be enabled through one of the
    following methods:
    • Programming the RSTOSC<2:0> bits in
    Configuration Word 1 to ‘110’ (1 MHz) or ‘001’
    (32 MHz) to set the oscillator upon device
    Power-up or Reset.
    • Write to the NOSC<2:0> bits of the OSCCON1
    register during run-time.
    The HFINTOSC frequency can be selected by setting
    the HFFRQ<2:0> bits of the OSCFRQ register. The
    MFINTOSC is an internal clock source within the
    HFINTOSC that provides two (500 kHz, 32 kHz) constant
    clock outputs. These constant clock outputs are
    available for selection to various peripherals, internally.
    The NDIV<3:0> bits of the OSCCON1 register allow for
    division of the HFINTOSC output from a range between
    1:1 and 1:512.
     
    For configurating the frequency, you need REGISTER 9-6.
     
    OSCFRQ: HFINTOSC FREQUENCY SELECTION REGISTER
    HFFRQ<2:0>(1)
    bit 7-3 Unimplemented: Read as ‘0’
    bit 2-0 HFFRQ<2:0>: HFINTOSC Frequency Selection bits
    Nominal Freq (MHz):
    111 = Reserved
    110 = 32
    101 = 16
    100 = 12
    011 = 8
    010 = 4
    001 = 2
    000 = 1
    Note 1: When RSTOSC=110 (HFINTOSC 1 MHz), the HFFRQ bits will default to ‘010’ upon Reset; when RSTOSC = 001
    (HFINTOSC 32 MHz), the HFFRQ bits will default to ‘101’ upon Reset.
     

    OSCFRQbits.HFFRQ = 0b110; //32Mhz

     
    post edited by Sobottech - 2018/04/16 09:48:12
    #4
    mbrowning
    Just a Member
    • Total Posts : 1028
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: offline
    Re: PIC16F15345 Oscillator stuck at 1MHz? 2018/04/16 09:39:59 (permalink)
    +1 (1)
    Let's see - 500 times around the while loop, with probably 16 instruction cycles per loop to increment, compare, and jump in free mode - 8MHz - so you are running at 32MHz (8MHz * 4). A much better way to make delays is to use the built-in macros __delay_us() and __delay_ms() which will make precise loops based on the _XTAL_FREQ define.
     
    A much much better way to make delays is with a timer interrupt, but maybe that's for later.

    Can't remember. I've slept since then - Mark
    #5
    steveonline84
    New Member
    • Total Posts : 3
    • Reward points : 0
    • Joined: 2018/04/13 17:19:57
    • Location: 0
    • Status: offline
    Re: PIC16F15345 Oscillator stuck at 1MHz? 2018/04/16 10:04:48 (permalink)
    0
    Thank you mbrowning.  I suspected that I made an erroneous assumption that a increment loop is not a single clock cycle - whoops!  My estimation of 1MHz was a little off I suppose ;) I definitely am/was always much closer to to 32MHz than I thought.
     
    About the configuration bits - I realize now that I do not like to use the configuration bits menu in microchip - they seem to keep defaulting to some value I do not want - I have decided to leave my configuration bit statements in the C code definitions area.  I thought using the configuration bits menu fixed the problem because I saw the LED dim (assuming it was flashing at a much faster rate now) but the truth was it had defaulted back to the external clock the second time i clicked program, I don't want to set up that menu every time I reprogram my PIC.
     
    Someone had already previously mentioned using timer interrupts to me for a 1ms clock, that'll be the next thing I will look into.
     
    Regardless, thank you all for your helpful inputs.
     
    Regards,
     
    Stephen
    #6
    Aussie Susan
    Super Member
    • Total Posts : 3251
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: PIC16F15345 Oscillator stuck at 1MHz? 2018/04/16 18:59:22 (permalink)
    +1 (1)
    Also don't let the compiler optimiser get near that delay loop code or it will remove the whole lot.
    Susan
    #7
    Jump to:
    © 2018 APG vNext Commercial Version 4.5