1.2. PIC16 MCU Configuration

• Clock oscillator types

• Watchdog, power-up, brown-out timers

• Low-voltage programming

• Code protection

• In-circuit debug mode

When programming the PIC microcontroller, certain operational modes must be set prior to the main program download. These are controlled by individual bits in a special configuration register separated from the main memory block. The main options are as follows.

Clock Options

The ‘877 chip has two main clock modes, CR and XT. The CR mode needs a simple capacitor and resistor circuit attached to CLKIN, whose time constant (C × R) determines the clock period. R should be between 3 k and 100 k, and C greater than 20 pF.For example, if R=10 kΩ and C=10 nF, the clock period will be around 2×C×R=200 μs (calculated from the CR rise/fall time) and the frequency about 5 kHz.This option is acceptable when the program timing is not critical.

The XT mode is the one most commonly used, since the extra component cost is small compared with the cost of the chip itself and accurate timing is often a necessity. An external crystal and two capacitors are fitted to CLKIN and CLKOUT pins. The crystal frequency in this mode can be from 200 kHz to 4 MHz and is typically accurate to better than 50 ppm (parts per million) or 0.005%. A convenient value is 4 Mz, as this is the maximum frequency possible with a standard crystal and gives an instruction execution time of 1.000 μs (1 million instructions per second, or 1 Mip).

A low-speed crystal can be used to reduce power consumption, which is proportional to clock speed in CMOS devices. The LP (low-power) mode supports the clock frequency range 32–200 kHz.To achieve the maximum clock speed of 20 MHz, a high-speed (HS) crystal is needed, with a corresponding increase in power consumption.

The MCU configuration fuses must be set to the required clock mode when the chip is programmed. Many PIC chips now have an internal oscillator, which needs no external components. It is more accurate than the RC clock but less accurate than a crystal. It typically runs at 8 MHz and can be calibrated in the chip configuration phase to provide a more accurate timing source.

Configuration Options

Apart from the clock options, several other hardware options must be selected.

Watchdog Timer

When enabled, the watchdog timer (WDT) automatically resets the processor after a given period (default 18 ms). This allows, for example, an application to escape from an endless loop caused by a program bug or run-time condition not anticipated by the software designer. To maintain normal operation, the WDT must be disabled or reset within the program loop before the set time-out period has expired. It is therefore important to set the MCU configuration bits to disable the WDT if it is not intended to use this feature. Otherwise, the program is liable to misbehave, due to random resetting of the MCU.

Power-up Timer

The power-up timer (PuT) provides a nominal 72 ms delay between the power supply voltage reaching the operating value and the start of program execution. This ensures that the supply voltage is stable before the clock starts up. It is recommended that it be enabled as a precaution, as there is no adverse effect on normal program execution.

Oscillator Start-up Timer

After the power-up timer has expired, a further delay allows the clock to stabilize before program execution begins. When one of the crystal clock modes is selected, the CPU waits 1024 cycles before the CPU is enabled.

Brown-out Reset (BoR)

It is possible for a transitory supply voltage drop, or brown-out, to disrupt the MCU program execution. When enabled, the brown-out detection circuit holds the MCU in reset while the supply voltage is below a given threshold and releases it when the supply has recovered. In CCS C, a low-voltage detect function triggers an interrupt that allows the program to be restarted in an orderly way.

Code Protection (CP)

The chip can be configured during programming to prevent the machine code being read back from the chip to protect commercially valuable or secure code. Optionally, only selected portions of the program code may be write protected (see WRTX% later).

In-Circuit Programming and Debugging

Most PIC chips now support in-circuit programming and debugging (ICPD), which allows the program code to be downloaded and tested in the target hardware, under the control of the host system. This provides a final test stage after software simulation has been used to eliminate most of the program bugs. MPLAB allows the same interface to be used for debugging in both the simulation and in-circuit modes. The slight disadvantage of this option is that care must be taken that any application circuit connected to the programming/ICPD pins does not interfere with the operation of these features. It is preferable to leave these pins for the exclusive use of the ICPD system. In addition, a small section of program memory is required to run the debugging code.

Low-Voltage Programming Mode

The low-voltage programming mode can be selected during programming so that the customary high (12–V) programming voltage is not needed, and the chip can be programmed at Vdd (+5 V). The downside is that the programming pin cannot then be used for digital I/O. In any case, it is recommended here that the programming pins not be used for I/O by the inexperienced designer, as hardware contention could occur.

Electrically Erasable Programmable Read Only Memory

Many PIC MCUs have a block of nonvolatile user memory where data can be stored during power-down. These data could, for example, be the secure code for an electronic lock or smart card reader. The electrically erasable programmable read only memory (EEPROM) can be rewritten by individual location, unlike flash program ROM. The ‘877 has a block of 256 bytes, which is a fairly typical value. There is a special read/write sequence to prevent accidental overwriting of the data.

Configuration in C

The preprocessor directive #fuses is used to set the configuration fuses in C programs for PICs. A typical statement is

#fuses XT,PUT,NOWDT,NOPROTECT,NOBROWNOUT

The options defined in the standard CCS C 16F877 header file are

Clock Type SelectLP, XT, HS, RC
Watchdog Timer EnableWDT, NOWDT
Power Up Timer EnablePUT, NOPUT
Program Code ProtectPROTECT, NOPROTECT
In Circuit Debugging EnableDEBUG, NODEBUG
Brownout Reset EnableBROWNOUT, NOBROWNOUT
Low Voltage Program EnableLVP, NOLVP
EEPROM Write ProtectCPD, NOCPD
Program Memory Write ProtectWRT_50%, WRT_25%,
(with percentage protected)WRT_5%, NOWRT

The default condition for the fuses if no such directive is included is equivalent to

#fuses RC,WDT,NOPUT,BROWNOUT,LVP,NOCPD,NOWRT

This corresponds to all the bits of configuration register being default high.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.63.184