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

birdman0_o

Active Member
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

birdman0_o

Active Member
Put that at the start of your program

Pommie

Well-Known Member
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.

tempus

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

tempus

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

Pommie

Well-Known Member
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.

pcbheaven.com

New Member
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.

They are at the end of the book.

OK, thanks guys.

@pcbheaven,

Mike.

Last edited:

pcbheaven.com

New Member
@pcbheaven,

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?

Pommie

Well-Known Member
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.

pcbheaven.com

New Member
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.

Replies
5
Views
2K
Replies
3
Views
1K
Replies
0
Views
901
Replies
4
Views
1K
Replies
3
Views
3K