1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

What "Define Clock_Frequency" to use for 16F690 with Internal OSC

Discussion in 'Oshonsoft' started by sagor1, Mar 5, 2015.

  1. sagor1

    sagor1 Member

    Joined:
    Dec 11, 2014
    Messages:
    52
    Likes:
    2
    Reading the datasheet for the 16F690, it states that the default internal frequency on startup is 4Mhz. This device can run at 8Mhz using internal OSC. The register OSCCON can set the internal clock speed via IRCF<2:0> to a full 8Mhz.
    So, the question is, what does one set in the Basic compiler as Clock_Frequency? If I set it to 8Mhz, but do not set OSCCON, all the timing loops (WaitMS) will assume 8Mhz, but power on default will be 4Mhz.
    If I set Clock_Frequency to 4Mhz, but set OSCCON to run at 8Mhz, everything will be too short in timing.

    Should the compiler not set OSCCON bits as part of the initialization routine when checking the config bits (INTOSCIO) and the speed is set in the Basic code? Is this a "wish list" item? What is the point of having Define Clock_Frequency if it does not actually try to set the OSCCON register?

    I realize I can fix all this by defining Clock_Frequency = 8 and setting OSCCON to 0x71 at the first line of code. Or, am I mis-interpreting the 16F690 datasheet somehow?
    Or, am I not understanding the purpose of Clock_Frequency definition in the compiler? (I know it sets the timing loop counters)
    Any input would be appreciated... Early learning user...
     
  2. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    6,058
    Likes:
    519
    Location:
    Cleveland, OH, USA
    ONLINE
    I suspect that you are referring to debugging and simulation of the program. Compiling and programming would just set the OSCCON register to whatever you chose. Those settings do not affect the process of compiling and programming.

    I use MPLAB and specifically an ICD3 or PK3 for simulation. There is also MPLAB SIM that does not require a physical programmer to be attached. See: Debugger>Select tool. I suspect other compilers are similar in the way in which the oscillator frequency is set: MPLAB > Debugger > Settings > OSC/Trace
    The exact tree and location may differ, but remember that you are asking about a simulation function, not a compiling or programming function.

    John
     
  3. moty22

    moty22 Member

    Joined:
    Jan 31, 2015
    Messages:
    171
    Likes:
    18
    The part of your post that I quoted you did right.
    You also have to set in your CONFIG part to INTOSCIO or INTOSC.

    When you power the pic it starts as the CONFIG bits are; internal osc. Since internal osc set by default to 4MHz the pic starts doing his job and reading the code at 4MHz. When the code reach the instructions of OSCCON it selects the frequency of 8MHz and from that moment the pic works at 8MHz and process the next code at 8MHz.
    The purpose of this osc is; if in any part of your code you want to change the frequency you put instruction for OSCCON for the new frequency, it is normally done for saving power.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. sagor1

    sagor1 Member

    Joined:
    Dec 11, 2014
    Messages:
    52
    Likes:
    2

    You are correct (both answers). It is just if one defines Clock_Frequecy in the Basic compiler, it then defines the timing loops based on that declaration only. I guess one has to read every data sheet of any PIC before compiling, to see what the default is if using internal OSC. Of course, none of this matters if using HS or XT, since that sets the actual speed externally. Anyway, thanks for the confirmation that I have to just watch carefully what I define and what I set with some of these PICs when I use internal OSC.

    I think what I wanted was a "wish list", if INTOSC is selected, and Clock_Frequency is set to 8Mhz by a compiler declaration, the compiler should include code at the start to set OSCCON to the proper value, if possible, else give some warning about timing loops (ie: WaitMS)
     
    Last edited: Mar 6, 2015
  6. moty22

    moty22 Member

    Joined:
    Jan 31, 2015
    Messages:
    171
    Likes:
    18
    Some of the IDEs or compilers have this "wish list" or automation; MikroC, Arduino. I guess you will come across them in the future. MPLAB is for those engineers who want to learn the hard way.
     

Share This Page