Programming Techniques:Special Features

Special Features

These include options such as oscillator type, internal timers to make the chip operation more reliable, code protection and internal hardware to support in-circuit programming and debugging. Most of these options are selected via the chip configuration word.

Clock Oscillator Type

The PIC MCU can be driven by an external RC network, a crystal oscillator or an internally generated clock signal. An external system clock can also be applied to synchronize its operation with other system components. The clock type is selected in the chip configuration word, which is programmed into a special location at the same time as downloading the user code. The configuration options can be set via a dialogue in MPLAB or at the top of the program using the CONFIG directive (see Section 6.6 below).

The default clock option is normally the internal oscillator, if available. It reduces the number of external components required, and provides a default clock rate of 4 MHz in standard chips and a maximum clock rate of 32 MHz in the more recent 16F1xxx series chips. The internal oscillator is factory calibrated, but can be tuned to a more precise figure using a set of bits in the control register OSCTUNE.

For applications where the precise timing of the program is not important, and an internal oscillator is not available, an inexpensive RC low-frequency clock circuit can be used. This requires only a resistor and capacitor connected to the CLKIN pin of the chip. If a variable resistor is used, as in the BIN hardware, the clock rate can be adjusted, within limits, and therefore all output signal frequencies can be changed simultaneously. This can be very useful, but the clock will not be very precise or stable.

The external quartz crystal oscillator option is slightly more expensive, but far more precise.

The crystal is connected across the OSC1 and OSC2 pins, with a capacitor (15e22 pF) to ground from each pin, and an internal amplifier completing the circuit. The crystal resonates at a precise frequency, with an accuracy of around 50 ppm (parts per million), or 0.005%. This allows the hardware timer to measure exact intervals, and to generate accurate output signals. The overall execution time of the program blocks can also be predicted more precisely.

Three types of external crystal can be used: low power (LS), standard (XT) or high speed (HS). LS mode should be selected for low-speed crystals up to 32.768 kHz, which provides a frequency that is conveniently divisible by two. XT mode should be selected for clock speeds up to 4 MHz (1 ms instruction period), and HS used up to 20 MHz; these select a higher gain in the clock oscillator. Note that the overall power consumption is broadly proportional to the clock speed. The full supply voltage (5.0 V) is generally needed to run at high frequency, so a battery supply may not be suitable in this case.

Refer to the data sheet for specific devices for more information, and various application circuits for the external component connections.

Power-up Timer

When a power supply is switched on, the voltage and current initially rise in an unpredictable way, depending on the design of the supply and the load connected to it. If the processor program tries to start immediately, before the supply has settled down, it may malfunction. The PIC has a power-up timer (PWRT) built into the chip to overcome this problem. This timer is also invoked if the chip is reset at some later stage or the power supply dips temporarily (brownout).

When the PIC is powered up, it waits until the minimum operating voltage has been reached (typically 2.0 V), then generates an internal reset, which starts the PWRT. This times out after approximately 64 ms and the program starts executing. As a precaution, PWRT should normally be enabled when programming the chip, as the resulting delay on start-up will usually be insignificant.

The !MCLR (master clear) input (active low) can be used to restart the program at any time. This is useful as the power does not need to be switched off to restart, but particularly so when debugging, since the processor may hang for no obvious reason. If no reset input is required, this pin must be tied high to enable the processor to run; it is recommended that the !MCLR input is decoupled with

a 1k0 resistor (minimum value) and a 100 nF capacitor to protect against power supply transients causing a random reset, and electrostatic discharge, which can damage the input.

Watchdog Timer

The watchdog timer (WDT) is an internal independent timer that automatically forces the PIC to restart after a selectable period. The purpose is to allow the processor to escape from an endless loop or other error condition, without having to be reset manually. This option will be used by more advanced programs, so our main concern here is to prevent watchdog timeout occurring when not required, because it will disrupt the normal operation of our demo programs. WDT will therefore normally be disabled by selecting the appropriate configuration setting during program downloading, or specifying it in the configuration word in the source code.

If the watchdog is to be enabled, the WDT must be regularly reset within the program loop using the instruction CLRWDT. If this happens at least every, say, 1 ms (1000 instructions at 4 MHz), the WDT auto-reset can be prevented. If a program misbehaves in the simulator, check that WDT is disabled. If the WDT option is enabled, an interrupt is generated, so a suitable service routine to restart the processor must be set up at address 004, the ISR vector address.

Sleep Mode

The instruction SLEEP causes normal operation to be suspended and the clock oscillator is switched off. Power consumption is minimized in this state, which is useful for battery- powered applications. The PIC is woken up by a reset or interrupt; for example, when a key connected to port B is pressed. The SLEEP instruction is also used to terminate the program if it is not required to loop continuously (see Program 6.3). This prevents program execution running on into unused locations, where the program memory bits default high. This code (all 1s) is in fact a valid instruction in the PIC 16 instruction set, ADDLW FF, which will be repeated throughout the unused locations. If the program is not terminated, these meaningless instructions will be executed up to the end of program memory. The program counter will then roll over and the program will be restarted at address zero, so the program will restart by default.

Code Protection

In commercial applications, the PIC firmware may need to be protected from piracy. The code protect fuses, selected during programming, will prevent unauthorized copying of the code. The chip can also be given a unique identification code during programming, if required. In the demo programs, the code protection is not enabled, as the program could not then be read back for verification.

Configuration Word

The oscillator selection bits, watchdog timer, power-up timer, code protection and other options are selected by setting the bits of a configuration word, located at a special address that is only accessible when the chip is being programmed. These bits can be set via the programming dialogue in MPLAB. Alternatively, the configuration options can be set by including an assembler directive in the source code (see CONFIG directive, Section 6.6, below). The default settings suggested here are:

• Clock source as required

• Watchdog timer disabled

• Power-up timer enabled

• Master clear enabled

• Code protection disabled

• All other features disabled.

Leave a comment

Your email address will not be published. Required fields are marked *