+ Reply to Thread
Results 1 to 13 of 13

Thread: code for setting int osc frequency

  1. #1
    tempus Newbie
    Join Date
    Sep 2009
    Posts
    23

    Default code for setting int osc frequency

    Hey all

    I'm working on code for my first PIC project. I'm using the PIC 16F737, and I'd like to use the internal oscillator at 4MHz. To do this, I need to load bits 6-4 with 110. I've worked out this as a means to do it, but I'm not sure it will work, since I'm setting the frequency with several commands and changing the contents of the w register before the frequency is set:

    movlw 0x01 ;load W with 1
    movwf ircf2 ;set int osc 110=4Mhz
    movwf ircf1
    movlw 0x00 ;load W with 0
    movwf ircf0

    Is there a more elegant way to accomplish this? Also, can you load only 3 bits of a register, or do you always have to use an 8 bit pattern?

    Thanks


  2. #2
    birdman0_o Excellent birdman0_o Excellent birdman0_o Excellent birdman0_o Excellent birdman0_o Excellent birdman0_o Excellent
    Join Date
    Feb 2009
    Location
    Montreal, Quebec
    Posts
    1,064

    Default

    As simple as setting the fuses!

    List P=16F737
    #include "P16F737.INC"
    __config _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _BODEN_ON & _LVP_OFF & _CP_OFF & _MCLRE_OFF
    Mike
    My website: www.ElectroBird.net

  3. #3
    birdman0_o Excellent birdman0_o Excellent birdman0_o Excellent birdman0_o Excellent birdman0_o Excellent birdman0_o Excellent
    Join Date
    Feb 2009
    Location
    Montreal, Quebec
    Posts
    1,064

    Default

    Put that at the start of your program
    Mike
    My website: www.ElectroBird.net

  4. #4
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,804

    Default

    As well as the config settings, change your above code to,
    Code:
    	bcf	OSCCON,IRCF0
    	bsf	OSCCON,IRCF1
    	bsf	OSCCON,IRCF2
    
    Or,
    Code:
    	movlw	0x60
    	movwf	OSCCON
    
    Note that OSCON is in bank 1.

    Mike.

  5. #5
    tempus Newbie
    Join Date
    Sep 2009
    Posts
    23

    Default

    As simple as setting the fuses!

    List P=16F737
    #include "P16F737.INC"
    __config _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _BODEN_ON & _LVP_OFF & _CP_OFF & _MCLRE_OFF
    It's a bit different for the 737, but I have this at the beginning of my program. The 737 has 8 user selectable clock frequencies. Do you know if the default is 4 MHz?

  6. #6
    tempus Newbie
    Join Date
    Sep 2009
    Posts
    23

    Default

    bcf OSCCON,IRCF0
    bsf OSCCON,IRCF1
    bsf OSCCON,IRCF2
    So this would set only the 3 bits in OSCCAN that control the frequency, leaving the remaining bits unchanged, and this

    movlw 0x60
    movwf OSCCON
    would set the bit pattern (110) and clear all other bits in OSCCAN, correct?
    This answers (or raises) another thing I was wondering about - is it OK to set the bits that you aren't using in a register to 0? I had thought about simply sending the bit pattern in your second example myself, but didn't know if it would create problems because I was changing the remaining bits in the register. Or do they just default at 0?

    Thanks

    Just thought of something else. Could also use this:

    bcf OSCCON,4
    bsf OSCCON,5
    bsf OSCCON,6

    to accomplish the same thing (bits 4, 5 and 6 correspond to the IRCF values you listed). Not much of a difference, but I was just curious.

    Thanks again
    Last edited by tempus; 1st September 2009 at 01:32 PM.

  7. #7
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,804

    Default

    Most of the bits in OSCCON are read only and so you can ignore them when you write to it.

    You can use numbers for the bits (OSCCON,4) but it is not good practice. Using the provided names makes it easier to see what the code is doing.

    Mike.

  8. #8
    pcbheaven.com Newbie
    Join Date
    May 2009
    Posts
    84

    Default

    i always have a separate file that holds all the initialization of the PIC. I just include the file with a single line code "include Init.inc" for example.

    Take a look in my tutorials for a moment.

    PIC Pages - PCB Heaven

    They are at the end of the book.
    And on the eighth day God said, "Okay, Murphy, you're in charge!"

    www.pcbheaven.com

  9. #9
    tempus Newbie
    Join Date
    Sep 2009
    Posts
    23

    Default

    OK, thanks guys.

  10. #10
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,804

    Default

    @pcbheaven,
    If you have something to add to a thread then add it. Don't just point us to your site. We already have too much of that.

    Mike.
    Last edited by Pommie; 2nd September 2009 at 02:08 PM.

  11. #11
    pcbheaven.com Newbie
    Join Date
    May 2009
    Posts
    84

    Default

    Quote Originally Posted by Pommie View Post
    @pcbheaven,
    If you have something to add to a thread then add it. Don't just point us to your site. We already have too much of that.

    Mike.
    tempus asked for a more elegant way. I told him that i have the files separated. I gave him the link to my site with the ini file that i use. What is the problem with you anyway? The links that i give are not irrelevant are they?
    And on the eighth day God said, "Okay, Murphy, you're in charge!"

    www.pcbheaven.com

  12. #12
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,804

    Default

    No, your link is not irrelevant. However, if you want to add value to this site then post an answer to the question not a link to your site. Your site may well cease to exist in a few years time and so your contribution will become worthless.

    Mike.

  13. #13
    pcbheaven.com Newbie
    Join Date
    May 2009
    Posts
    84

    Default

    The link is not irrelevant and goes along with the answer... I really don't get you after-all. I could post the whole code as well if that's what you mean.

    As long as my site is concerned, it may cease to exist indeed... it may cease NOT as well. No matter what, this is not going to happen this year, nor the next one.
    And on the eighth day God said, "Okay, Murphy, you're in charge!"

    www.pcbheaven.com

+ Reply to Thread

Similar Threads

  1. Variable Frequency Osc circuit? Whats a good one?
    By Qaiphyx in forum General Electronics Chat
    Replies: 25
    Latest: 14th September 2008, 03:41 PM
  2. External 4MHz Osc vs Internal 8MHz Osc
    By Urahara in forum Micro Controllers
    Replies: 5
    Latest: 2nd September 2008, 05:40 AM
  3. Moving from PIC18F1330 to 18F4331 Int. Osc Issue...
    By bsodmike in forum Micro Controllers
    Replies: 1
    Latest: 23rd August 2008, 11:01 PM
  4. Needed! C-code frequency measurment
    By mrrmot in forum Electronic Projects Design/Ideas/Reviews
    Replies: 9
    Latest: 12th May 2008, 05:43 PM
  5. Setting OSC bits in 16F628
    By PeterK in forum Micro Controllers
    Replies: 5
    Latest: 21st May 2004, 02:54 AM

Tags for this Thread