Wednesday, February 4, 2009

Setting Configuration Bits and Configuring Clock Speed

Pragmas are preprocessor direrectives which are used in C18 to set configuration options for your chip. They are usually set at the top of the main source file, just under the includes.

They are declared like this:

#pragma [option] = [value]

For example, my usual options are:

#pragma config OSC = INTIO67
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = ON
#pragma config BOREN = OFF
#pragma config WDT = OFF
#pragma config MCLRE = OFF
#pragma config PBADEN = OFF
#pragma config LVP = OFF

Declare them just under your #includes.

Another essential thing to do when developing with your chip is to define a clock source and set the frequency of oscillation. In the above configuration settings, the OSC has been declared to use the internal clock source. This is advantageous because no external circuitry is required.

The OSCCON register controls the clock frequency of the inter clock source. The following code will set it to the maximum frequency of the PIC18F2620 - 8 MHz.

OSCCON = 0b01110010;

The above code should be the first line in your main() function - more on this later.

No comments:

Post a Comment