Microcontroller Configuration

There are certain configuration bits in the microcontroller memory that control how the device operates. To set these configuration bits, the #pragma config macro is used.

The PIC16F690 has the following options that can be configured:

CPD: Data Code Protection

Prevent the data memory from being read off the chip. Options are OFF and ON.

BOREN: Brown-out Reset Enable

This allows the microcontroller to be reset if the supply drops below the brown-out level (2.0 to 2.2V) for a period of 100us or more. Options are:

  • OFF: Brown-out reset is disabled.
  • ON: Brown-out reset is enabled.
  • NSLEEP: Enabled during operation and disabled during sleep mode.
  • SBODEN: Brown-out reset is controlled by software via the SBOREN bit of the PCON register.

IESO: Internal External Switchover

This allows the device to switch from internal and external clocks. Options are OFF and ON.

FOSC: Oscillator selection

This option is to determine how the CPU is driven. Options are:

  • EXTRCCLK: Use an external RC oscillator circuit. The RC oscillator is connected to pin RA5/OSC1/CLKIN and the clock signal is outputted via the RA4/OSC2/CLKOUT pin.
  • EXTRCIO: Use an external RC oscillator. The RC oscillator is connected to pin RA5/OSC1/CLKIN and the RA4/OSC2/CLKOUT pin is available for I/O.
  • INTRCCLK: Use the internal RC oscillator. Pin RA4/OSC2/CLKOUT operates as CLKOUT and pin RA5 is available for I/O.
  • INTRCIO: Use the internal RC oscillator. Pins RA4 and RA5 are available for I/O.
  • LP: Low power external crystal oscillator on pins OSC1 and OSC2. This mode offers low power consumption and is only designed for 32kHz watch crystals.
  • XT: External crystal/resonator on pins OSC1 and OSC2 (RA5 and RA4). This is the medium gain setting appropriate for crystals with a medium drive level specification. This is the mode you will probably use.
  • HS: External crystal/resonator on pins OSC1 and OSC2 (RA5 and RA4). This is the high gain setting appropriate for crystals with a high drive level specification.
  • EC: External clock. Use this if you already have a logic level clock signal in your circuit. The clock signal is connected to RA5/OSC1/CLKIN and RA4/OSC2/CLKOUT is available for I/O.

FCMEN: Fail-Safe Clock Monitor Enable

This setting allows the system to automatically switch over to one of the internal clock modes if an external clock source fails. Options are OFF and ON.

MCLRE: MCLR Pin Function

This selects whether the MCLR pin operates as the Master Clear (device reset) or as a digital input (RA3). Options are OFF (input) and ON (master clear). Note, if you disable Master Clear, pin RA3 can be used as an input only!

WDTE: Watchdog Timer Enable

This selects whether or not the Watchdog timer is used. The Watchdog timer is used to detect conditions where the microcontroller has frozen (perhaps gotten stuck in an endless loop). When the Watchdog timer expires, the microcontroller is reset. The user's application must therefore clear the Watchdog timer periodically to prevent this. If the software is frozen, the Watchdog timer is not cleared, and therefore forces a system reset. Options are OFF and ON.

CP: Code Protection

Prevent the program memory from being read off the chip. This is an option you may wish to select for a commercial product to prevent the firmware from being downloaded. Options are OFF and ON.

PWRTE: Power-up Timer Enable

This option selects whether the power-up timer is used. If enabled, this feature keeps the microcontroller in reset for 64ms when power is applied. This is to give the power supply time to stabilize before operation begins. Options are OFF and ON.

Using #pragma config

To set these chip options in your code, you use the #pragma config operative. This can be placed anywhere in your code since it is a pre-processor directive and not actual code. I typically place this near the top of my C file that contains the main() function.

Here is an example:

#pragma config FOSC=INTRCIO, WDTE=OFF, PWRTE=OFF, MCLRE=ON, CP=OFF, CPD=OFF, BOREN=OFF, IESO=OFF, FCMEN=OFF

In this case, I am using the internal RC oscillator with IO enabled on the RA4 and RA5 pins. The Watchdog and Power-up timers are not used. Master clear is enabled. Code protect and data protect are disabled. Brown-out reset is disabled. Clock switching is disabled. Lastly, the fail-safe clock monitor is disabled.

Note, the #pragma config directive is not code. It therefore does not require a semicolon at the end. Options are separated by a comma.

In the next section we will start taking measurements from sensors using the Analog to Digital Converter (ADC).