Sie sind auf Seite 1von 8

CLOCK OSCILLATOR

The internal clock oscillator may be used (most common), as is done in this book, or an external
clock oscillator may be used. The details of various external oscillator circuits and components
as well as internal clock oscillator use options are given in the Microchip data sheet. At pro­
gramming time, the part must be told via configuration bits which clock oscillator option will be
used. This will be explained as we go along.

For experimentation, we will use the internal clock oscillator as follows:

• Default frequency (31.25 KFIz) for most applications.


• 4 MHz for applications using a time delay (built-in function or timer 0).

RESET

The PIC16F818 has built-in power-on reset which works as long as the power supply voltage
comes up quickly. Commonly the MCLR pin is merely tied to the power supply using a pull-up
resistor. A switch may be used to regain control if things run away.

For our experiments, we will use pin 4 as MCLR which stands for Master Clear (reset). It will
be pulled up to +5volts via a 47 K resistor to keep the device out of reset unless the MCLR pin is
pulled low by some external device.

If you choose to use one of the ICD-type in-circuit serial programmers, the programmer will use
pin 4 for the programming threshold voltage, Vpp, which puts the device in programming mode.
After programming is completed, you may bring the device out of reset to test your program
using the ICD control interface running on the PC. This makes programming and testing your
codc fast and easy.

9
PORTS

Port A, as we will be using it, has 7 bits/lines. Port B is 8 bits/lines wide or byte-wide. Each
port line may be individually programmed as an input line or output line. This is done using a
special function which matches a bit pattern with the port lines in registers callcd "tristate" or
"tris" registers. A "0" associated with a port line makes it an output, a "1" makes it an input.
Examples follow.

Pins which may have analog functions in use are analog functions when the microcontroller
comes out of reset. For the PIC16F818, the CCS compiler will generate an instruction which
changes those pins to digital I/O as part of the initialization process unless the compiler encoun­
ters a call to setup_adc_ports ( ) ; . This is a default and it is very important to keep in mind.

The Port B lines have weak pullup resistors on them which may be enabled or disabled under
software control. All 8 resistors are enabled/disabled as a group via a built-in function in the
compiler (not used in this book). The pullup resistor on an individual port line is automatically
turned off when that line is configured as an output. The pullups are disabled on power-on reset.

Port A, bit 4 is shared with the external timer/counter input called TOCKI. As a digital input
line, the input is Schmitt trigger. As a digital output line, it is open drain, so a pullup resistor is
required. The output cannot source current, it can only sink current.

For experimenting, all unused port lines should be tied to the power supply via 10 K pullup
resistors (CMOS rule - all inputs must go somewhere). On reset, all port lines are inputs.

SPECIAL FEATURES

Watchdog Timer

The watchdog timer is useful in some control applications where a runaway program could
cause a safety problem. We will not deal with it exccpt to say that it is important to select
"watchdog timer off' when programming the configuration bits.

Power-up Timer (PWRT)

The power7up timer holds the device in reset for a time which allows the power to come up to a
level wjiich will provide reliable operation of the device at which time it is allowed to come out
of resetV The power-up timer should be selected "enabled" when programming the configuration
bits.

Brown-out Reset (BOR)

If Vdd falls below approximately 4 volts for about 100 microseconds, the device will be reset.
The brown-out reset feature should be selected "enabled" when programming the configuration
bits.

Sleep Mode

The feature of the "sleep mode" is drastically reduced power consumption achieved by turning
off the main clock oscillator.

10
In-Circuit Debugging

The PIC16F818 is designed so that an in-circuit debugger may be connected to it (advanced


topic).

Low-Voltage ICSP Programming

Low voltage ICSP will not be used.

Peripherals

Peripherals such as timer/counters and A/D converters will be discussed in chapters devoted to
the subjects.

Special Feature Selection

Details follow.

PIC MICROCONTROLLER ARCHITECTURE

PIC microcontrollers have two separate blocks of memory, program memory and data memory.

Program Memory

The PIC16F818 program memory is 14 bits wide and 1K words long. Program memory is
flash which means it can be erased electrically. Program memory is read-only at run time for the
purposes of this book. PIC microcontrollers can only execute code contained in program
memory.

Pointed To By ■
Reset Vector

Pointed To By
Interrupt Vector

0 x 3 F F _______________________

A limited amount of data may be stored in program memory (see Writing Programs chapter).

11
Weird Hex Notation

The "Ox" means hexadecimal. The Ox notation comes from the C programming language. The
main thing is, when you see OxOF, it means hexadecimal OF. 0x004 means hexadecimal 004.

Some of the newer Microchip literature uses "h" to denote hexadecimal numbers. 3FFh means
3FF hexadecimal. h'3FF' has the same meaning.

Data Memory

Data memory consists of register files containing registers of two types:

• General purpose registers which hold your data.


• Special Function Registers (SFRs).

The register files are 8 bits wide (with the exception of the PCLATH register which is 5 bits
wide). The P1C16F818 has a 512 register file address space (0x000 - Ox IFF) divided into four
banks, but not all addresses are used.

Register File

0x00 Indirect Address Indirect Address Pointer *


01 TMR0 Timer/Counter
02 PCL Program Counter Low Order 8 Bits
03 Status Status Register - Flags
04 File Select Indirect Pointer
05 Port A Data Port A
06 Port B Data Port B
07
08
09
0A PCLATH Program Counter Latch High Order 5 Bits
0B INTCON Interrupt Control
0C

IF
20

General Purpose Registers


Think. Of This Area As RAM (Data Memory)
0x7F
Bank 0
Not Physically Implemented

Note: Bank Switching And


Banks 2 And 3 Ignored

64 file registers have specific dedicated purposes and are called Special Function Registers
(SFRs). For the most part, the C compiler knows what to do with the SFRs and the address of
each. Wc will discuss the very few situations in which the compiler needs help finding address­
es as the need arises. 128 registers arc there for the C compiler to use and may be thought of as
RAM or data memory for storing data during program execution.

12
Data EEPROM Memory

Data EEPROM memory is not directly mapped into the data memory register file address space
described earlier. Instead, it is addressed using six special function registers and some built-in C
functions. This topic is beyond the scope of this book.

Oh yes, I neglected to spell out what the acronym means. It stands for memory that can be read,
written to, or even erased electronically. It is usually used to store data acquired during program
execution (think data logger).

CODE AND DATA PROTECTION

The code protection bits in the configuration register may be set so as to protect the code in pro­
gram memory, the data in data memory, and the data in EEPROM memory from examination by
the outside world (so your code can't be ripped off). Your program can still access and change
the contents of data EEPROM memory with the code protection bits set.

CONFIGURATION BITS

The configuration bits are loeated in flash memory outside the main part of flash memory used
for program storage. They are used to determine things like clock oscillator type, functions of
some of the pins, etc. There is a chapter devoted to this subject. The device programmer
accesses these bits during the device programming procedure. By doing this, the device will be
in the correct configuration when it comes out of reset.

13
CIRCUIT FOR EXPERIMENTS

One simple circuit may be used for all but one of the experiments in this book. The exception
utilizes a 7-segment LED digital display.

TOCKI ANO INT


RA4 RA1 RAO RBO

14
Looking at what is included may give you some ideas about how you would like to construct it.
My suggestions on how to proceed follow.

A simple circuit board may be assembled which includes a socket for a PIC16F818, power
supply terminal block, power supply decoupling capacitors, three 3-pin headers and shorting
blocks for use in selecting functions for pins RAO, RA1 and RA4, screw terminal blocks as a
means of connecting RAO/ANO, RA I, RA4/T0CKI, and RBO/INT to the off-board components
used in the experiments, and DIP switches for pins RAO, 1, 2, 3. A modular jack is included for
easy connection to any of the three ICD/programmers described in the book.

If you decide to use a device programmer rather than use an ICD as a programmer, I would defi­
nitely use a ZIF socket for the PIC microcontroller to avoid bending or damaging the pins.
18-pin ZIF sockets are becoming expensive and difficult to find. The once common part made
by the TEXTOOL division of 3M is still available from Digi-Key. It is the gold plated (literally)
version (3M part number 218-3341-00-0602J) and the cost is around $18.

A 24-pin Aries socket is available from JAMECO, Digi-Key and others. The Aries part number
is 24-6554-10 (tin plated contacts) and the price will be in the $8 range. Simply ignore the extra
6 pins.

Pullup resistors are used in the experiments primarily for the purpose of preventing unused
inputs from floating. There are pullups on port B built into the PIC16F818. I decided not to use
them because they just add confusion to the program examples and detract from explanation of
the applications themselves. So........... as a beginner, when you use the circuit module, remember
there are 10 K pullup resistors on all unused port lines. You can save refinements for later.

15
A modular phone jack is used to connect to the ICD via cable. A printed circuit board style jack
is shown. One manufacturer is tyco Electronics AMP. The part number is 5204703.
The Digi-Key part number is A9049-ND.

The modular phone jack is connected as shown:

Modular Jack
6-Conductor

tyco Electronics / AMP

Mfgr. P/N 520470-3
Digi-Key P/N A9049-ND

16

Das könnte Ihnen auch gefallen