Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to OSCCON register in PIC16f887 using mplab XC8

Status
Not open for further replies.

ComputerAngel

Junior Member level 2
Joined
May 28, 2017
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
202
Hello, I am new to PIC. I was able to write a simple program that turned an LED on and OFF but am finding it difficult to to make it blink. the internal clock is just not working. I have tried all I can but still not working. Can anyone please help me out? How can I configure PIC16F887 internal oscillator (OSCCON register) to use 8MHz internal oscillator? the only thing I know is defining it in the header file #define _XTAL_FREQ = 8000000
 

Look at Figure 4-1 on the data sheet. It demonstrates the bit settings to configure the oscillator. You will see the IRCF bits have to be set to 111 and also you have to set the SCS bit to 1 to use the internal oscillator.

Brian.
 

Thanks a lot.
Do I have to set HTS, LTS, OST? Or is it just the IRF and SCS?
 

The #define that you mention does NOT do anything to configure the oscillator - all it does is to tell the various "__delay" functions what the oscillator *should* be so they can determine the correct delay values.
Have a look at figure 4-a of the data sheet and you will see how the system clock can be generated. In your case you want to use the internal oscillator so that means the FOSC (one of the configuration word fields) and the SCS bits in the OSCCON register need to be set correctly. Next you will see that the HFINTOSC generates an 8MHz signal and therefore the MUX needs to be set to 1:1 which is done using the IRCF bits of the OSCCON register.
Now that you know where to look, you need to read up on those fields in the data sheet. As mentioned above the FOSC bits are in the configuration word and so you need to look at section 14.1, and in particular Register 14-1.
To properly understand how to read the register definitions in the data sheet, you need to understand how Microchip lay them out. However the config registers are a little different in that the default (i.e. after a complete erase of the device before programming) is for all bits to be '1'. (This is mentioned in first sentence of Section 14.1.) Therefore, if you do nothing to control these bits, the FOSC bits will be 0b111 which is the RC oscillator (which also requires you to have a resistor and capacitor on the oscillator pins.) If you want to use the internal oscillator, you need to set the FOSC to INTOSC or INTOSCIO, depending on how you want to use the specified pins.
You set the FOSC value using the 'CONFIG' pragma statements in your source code - see the XC8 manual for how to do this.
Actually configuring the CONFIG settings is a very important part of getting any PIC MCU to work correctly so spending a bit of time understanding how to d this is time very well spent.
The other bit fields that you need to set are in the OSCCON register that is detailed in Register 4-1. This register definition is more representative of how Microchip lay these things out. If you look along the top you will see (as explained in the legend below) how each bit is used - unused, read only, write only or read/write. Along side most of the bit locations is a '-0' or '-1' which tells you the default setting for that bit after a reset (normally after power on or 'POR'). Look at the SCS bit definition and you will see that it is "R/W-0
which means that you can read and write the bit and that it will default to '0' which means that it will use the FOSC bit settings (which we dealt with above). This is what you want and so you can, if you like, leave that bit at its default setting.
The IRCF bit field has a POR default of 0b110 which defaults to a 4MHz clock. You want an 8MHz clock and so this bit field needs to beset to 0b111 and you will need to write code to do that.
You can now answer the question about the OSTS, HTS and LTS bits for yourself. (Hint: the "R" means they are 'read only')
Section 4.5 shows all of this is detail, including a link to the section that talks about clock switching. Unfortunately (or perhaps fortunately) the Microchip documentation does tell you everything you need to know about each detail of the MCU but it can be a bit hard to understand when you are starting out. However going through this 'learning curve' is an essential thing to do.
Also I suggest that you look at some of the PIC tutorials that are on the Internet. While they might talk about different processors (and different processor families) the principles are normally the same across all Microchip MCUs and you can apply them to your device with the aid of the data sheet.
Susan
 

Thanks a lot. I will go over the datasheet again. Thanks a lot.
@Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top