04. Configurations

Configure What?

All 16 bit microcontrollers from Microchip contain a series of configurable parameters that are arranged outside of programming. These are more or less special directives built into the program instructions that tells the microcontroller what to do on the most basic level. These may include the parameters for selecting which programming pins to use, alternate pin configurations, watch dog timers, JTAG programming configurations, oscillator selection and many more, depending on the chip. This section will give a general overview of how to use and set the correct configuration for your chip.

For this demonstration, I will be using the PIC24HJ32GP204; it’s got a few more configurations than the PIC24FJ series since it is more performance oriented. However, generally I like using the FJ series because they have more features. It’s really up to the application of the project. All the PICs usually work in the same manner so it’s not that much work going from one set of hardware to another.

Here, I have a project set up as per my previous tutorial. This is an old project, but it’s a good example of how to use configurations.

config-functions.jpg

Every chip in the PIC24 series has a few special registers called the “Configuration Bits”. In the datasheets available from Microchip, their functions are usually listed under the section “Special Features”. The datasheets also list the addresses where these registers can be accessed. However, those address are not of any real importance since the header files provided by MPLABs have macros already built in for the programmer to configure the device. In the above example the, section highlighted in red display the functions used to set the configuration bits.

config-datasheet.jpg

Through the Code

So how do you know what functions to use and what options there exist for a particular device? You will need to first look at the datasheets for your particular device. You will need to do this step because the datasheets usually have a detailed explanation of every option available on the device. Once you know which options are needed for the project, it’s time to configure.

The macros are stored in the header file that you have added for your project on a particular device. In this case, my project uses a PIC24HJ32GP204. If you look in the header file for the device, there is a section of the code entitled “Macros for setting device configuration registers.” These are the macros used to set the configuration bits. Below the macro declarations there is the list of options. These options will correspond to the options in the datasheets.

config-macros.jpg

Example

Here is a common example. Let’s say I want to use an external clock for my PIC24HJ32GP204. First I find the option in the datasheet. I now know which options are available, and which options I would like to select. Next I go to the file “p24HJ32GP204.h”, the header file corresponding to my device. The relevant section defines the macro as follows:

(in the file “p24HJ32GP204.h”)


#define _FOSCSEL(x) 
__attribute__((section("__FOSCSEL.sec,code"))) int _FOSCSEL = (x);

/*
** Only one invocation of FOSCSEL should appear in a project,
** at the top of a C source file (outside of any function).
**
** The following constants can be used to set FOSCSEL.
** Multiple options may be combined, as shown:
**
** _FOSCSEL( OPT1_ON & OPT2_OFF & OPT3_PLL )
**
**   Oscillator Source Selection:
**     FNOSC_FRC            Fast RC oscillator
**     FNOSC_FRCPLL         Fast RC oscillator w/ divide and PLL
**     FNOSC_PRI            Primary oscillator (XT, HS, EC)
**     FNOSC_PRIPLL         Primary oscillator (XT, HS, EC) w/ PLL
**     FNOSC_SOSC           Secondary oscillator
**     FNOSC_LPRC           Low power RC oscillator
**     FNOSC_FRCDIV16       Fast RC oscillator w/ divide by 16
**     FNOSC_LPRCDIVN        Low power Fast RC oscillator w/divide by N
**
**   Two-speed Oscillator Startup :
**     IESO_OFF             Disabled
**     IESO_ON              Enabled
**
*/

#define FNOSC_FRC            0xFFF8
#define FNOSC_FRCPLL         0xFFF9
#define FNOSC_PRI            0xFFFA
#define FNOSC_PRIPLL         0xFFFB
#define FNOSC_SOSC           0xFFFC
#define FNOSC_LPRC           0xFFFD
#define FNOSC_FRCDIV16       0xFFFE
#define FNOSC_LPRCDIVN       0xFFFF

#define IESO_OFF             0xFF7F
#define IESO_ON              0xFFFF

The options I’m interested in are “FNOSC_PRI” (because “EC” stands for external clock), and I want to disable the two speed start-up, option “IESO_OFF”. Now to set the configurations, in my main program body, after the compiler directives, but before the main function, I call the _FOSCSEL macro:

(in the file “main.c”)


//compiler directives
#include “../h/system.h”
#include “timer.h”
#include “pic.h”

//variable declaration
unsigned int state = 0;
unsigned char temp1;
char flag1 = 0;

//Configs, EC clock, No protect, Watchdog Off
_FBS	(BWRP_WRPROTECT_OFF & BSS_NO_FLASH);
_FGS	(GSS_OFF & GCP_OFF & GWRP_OFF);
_FOSCSEL(FNOSC_PRI & IESO_OFF);
_FOSC	(FCKSM_CSDCMD & IOL1WAY_OFF & OSCIOFNC_OFF & POSCMD_EC);
_FWDT	(FWDTEN_OFF);
_FPOR	(FPWRT_PWR1 & ALTI2C_ON);

int main(void)
{
   OSCCON = 0x2200;	 //Use primary, no divide, FCY = 10Mhz/2 = 5Mhz
   … (rest of main function)

Similarly, the other configuration bits need to be declared. There are default values, but I often find the need to change them.

Through the GUI

There is an easier to way to change these configuration bits. There is a built in program in MPLAB with drop down menus associated with every device from which you can select the options you want. However, I like to have my configuration bits embedded into the code. This decreases the chance that you’ll make a mistake when you copy a set of code over to another project. In addition, embedding the configuration in the code increases the readability of your program. I always know how a project is configured by just looking at the code, instead of having to go into MPLAB’s bit configuration program. However, if you still prefer a graphical user interface, you can do the following. Note that you only need to set the configurations with one method or the other. If you do both, one will take precedent over the other. I believe the macros in the code defer to the configuration from the program, but I am not 100% sure (since assembly programmers don’t do stupid things like that… duh).

First go to “Configure”, then “Configuration Bits”.

config-select.jpg

Next uncheck the box that says “Configuration bits set in code”.

config-config-box.jpg

Now it is just a matter of selecting the option in the drop down. It is very tempting to do it this way since the task is significantly easier. However I strongly urge you to configure it in your code. One of the main reasons is that, you will probably have to share code in the future (with others, or with yourself, in future projects). If the configurations are not in the code, the task becomes that much more complicated, and is more prone to error.

That’s all there is to it; as Carlin use to say, “Easy as pie”.

Table of Contents
Previous – First Program
Next – Inputs and Outputs

7 Responses to “04. Configurations”
  1. Jose V says:

    Hi,

    Very good tutorial, but I have a question, on previous section you mentioned you will explain this:

    //config
    _CONFIG2(0xF9FC);
    _CONFIG1(0×377F);

    on the next section ( this one) .

    However you showed a complete different stuff; I browsed the internet and there is little or no information on these _CONFIG 1 and 2 . The best is to read in the PIC24 header file, but still kind of criptic;

    _CONFIG1( OPT1_ON & OPT2_OFF & OPT3_PLL )

    But there are a lot of options; which options did you used to reach 0xF9FC and 0x377F and why ???

    Thanks in advance and thanks for this very good tutorial !

    Jose

  2. jliu83 says:

    Hi Jose,
    Well if you look in the datasheets, you’ll find a section called “Configuration Bits”. The _ConfigX(int) are macros to configure those special registers. You can use the macro and use the variables set up for you, or you can just calculate the integer value yourself. For example on the PIC24FJ64GA004, the values loaded in config2() was 0xF9FC. Looking at the datasheet for the PIC device we can see that:

    0xF9FC = 0b1111 1001 1111 1100
    -IESO enable
    -use FRCPLL
    -Clock switch/fail-safe disable
    -OSCO pin as CLKO
    -IOLOCK on
    -use default I2C pins
    -external oscillator

    Check it out, it’s all on the datasheets. The PIC24H’s might have more Configuration registers, but the datasheet is usually your best friend.

    -J

  3. Saya says:

    ok! good explanation. Thanks again. :-)

  4. Shehmas says:

    Fantastic tutorial, can’t thank you enough for taking the time out to write this!

  5. sascha says:

    Top. This tutorial is great. I have to use the PIC24FJ64GB004 for a Project.
    There are not too many examples on the web. So this gives me a very good startup on the PIC24

    Thanks a lot.

  6. Jeff says:

    I know I’m new but where are the files noted in the example above.

    #include “../h/system.h”
    #include “timer.h”

  7. jliu83 says:

    Read the other sections, they will have examples/explanations about the other headers.

    -J

  8.  
Leave a Reply