Continue to Site

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.

  • 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.

code for setting int osc frequency

Status
Not open for further replies.

tempus

Member
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
 
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
 
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.
 
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?
 
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:
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.
 
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.

**broken link removed**

They are at the end of the book.
 
@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:
@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?
 
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.
 
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.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top