Vous êtes sur la page 1sur 41

Introduction to Microprocessor

The microprocessor plays a significant role in the everyday


functioning of industrialized societies. It is the programmable
logic device that can be used to control processes or to turn
on/off devices. It can also be considered as a data processing
unit of a computer.

Like Wheels the microprocessor has created another


revolution in our life. Microprocessor is programmable integrated
device that has computing and decision-making capability similar
to that of the CPU of a computer.

Introduction to Microcontroller

A controller is used to control (makes sense!) some


process or aspect of the environment. A typical microcontroller
application is the monitoring of my house. As the temperature
rises, the controller causes the windows to open. If the
temperature goes above a certain threshold, the air conditioner
is activated. If the system detects my mother-in-law
approaching, the doors are locked and the windows barred. In
addition, upon detecting that my computer is turned on, the
stereo turns on at a deafening volume (for more on this, see the
section on development tools).

At one time, controllers were built exclusively from logic


components, and were usually large, heavy boxes (before this,
they were even bigger, more complex analog monstrosities).
Later on, microprocessors were used and the entire controller
could fit on a small circuit board. This is still common - you can
find many [good] controllers powered by one of the many
common microprocessors (including Zilog Z80, Intel 8088,
Motorola 6809, and others).

As the process of miniaturization continued, all of the


components needed for a controller were built right onto one
chip. A one chip computer, or microcontroller was born. A
microcontroller is a highly integrated chip, which includes, on
one chip, all or most of the parts needed for a controller. The
microcontroller could be called a "one-chip solution".

It typically includes:

CPU (central processing unit)

RAM (Random Access Memory)

EPROM/PROM/ROM (Erasable Programmable Read


Only Memory)

I/O (input/output) - serial and parallel

Timers

Interrupt controller

By only including the features specific to the task (control), cost


is relatively low. A typical microcontroller has bit manipulation
instructions, easy and direct access to I/O (input/output), and quick
and efficient interrupt processing. Microcontrollers are a "one-chip
solution" which drastically reduces parts count and design costs.

Basics of Microcontroller.

a. Architectural features

Von-Neuman Architecture

Microcontrollers based on the Von-Neuman architecture


have a single "data" bus that is used to fetch both instructions
and data. Program instructions and data are stored in a common
main memory. When such a controller addresses main memory,
it first fetches an instruction, and then it fetches the data to
support the instruction. The two separate fetches slows up the
controller's operation.

Harvard Architecture

Microcontrollers based on the Harvard Architecture have


separate data bus and an instruction bus. This allows execution
to occur in parallel. As an instruction is being "pre-fetched", the
current instruction is executing on the data bus. Once the
current instruction is complete, the next instruction is ready to
go. This pre-fetch theoretically allows for much faster execution
than Von-Neuman architecture, but there is some added silicon
complexity.

CISC

Almost all of today's microcontrollers are based on the


CISC (Complex Instruction Set Computer) concept. The typical
CISC microcontroller has well over 80 instructions, many of them
very powerful and very specialized for specific control tasks. It is
quite common for the instructions to all behave quite differently.
Some might only operate on certain address spaces or registers,
and others might only recognize certain addressing modes.

The advantages of the CISC architecture are that many of


the instructions are macro-like, allowing the programmer to use
one instruction in place of many simpler instructions.

RISC

The industry trend for microprocessor design is for


Reduced Instruction Set Computers (RISC) designs. This is
beginning to spill over into the microntroller market. By
implementing fewer instructions, the chip designed is able to
dedicate some of the precious silicon real estate for performance
enhancing features. The benefits of RISC design simplicity are a
smaller chip, smaller pin count, and very low power
consumption.

Among some of the typical features of a RISC processor: -


Harvard architecture (separate buses for instructions and data)
allows simultaneous access of program and data, and
overlapping of some operations for increased processing
performance - Instruction pipelining increases execution speed -
Orthogonal (symmetrical) instruction set for programming
simplicity; allows each instruction to operate on any register or
use any addressing mode; instructions have no special
combinations, exceptions, restrictions, or side effects

SISC

Actually, a microcontroller is by definition a Reduced


Instruction Set Computer (at least in my opinion). It could really
be called a Specific Instruction Set Computer (SISC). The
[original] idea behind the microcontroller was to limit the
capabilities of the CPU itself, allowing a complete computer
(memory, I/O, interrupts, etc) to fit on the available real estate.

At the expense of the more general-purpose instructions


that make the standard microprocessors (8088, 68000, 32032)
so easy to use, the instruction set was designed for the specific
purpose of control (powerful bit manipulation, easy and efficient
I/O, and so on).

Microcontrollers now come with a mind boggling array of


features that aid the control engineer - watchdog timers,
sleep/wakeup modes, power management, powerful I/O
channels, and so on. By keeping the instruction set specific (and
reduced), and thus saving valuable real estate, more and more
of these features can be added, while maintaining the economy
of the microcontroller.

b. Memory options

EEPROM - Electrically Erasable Programmable Read Only


Memory

Many microcontrollers have limited amounts of EEPROM on


the chip. EEPROM seems more suited (because of its economics)
for small amounts of memory that hold a limited number of
parameters that may have to be changed from time to time.
This type of memory is relatively slow, and the number of
erase/write cycles allowed in its lifetime is limited.

FLASH (EPROM)

Flash provides a better solution than regular EEPROM when


there is a requirement for large amounts of non-volatile program
memory. It is both faster and permits more erase/write cycles
than EEPROM.

Battery backed-up static RAM

Battery backed-up static RAM is useful when a large non-


volatile program and DATA space is required. A major advantage
of static RAM is that it is much faster than other types of non-
volatile memory so it is well suited for high performance
application. There also are no limits as to the number of times
that it may be written to so it is perfect for applications that keep
and manipulate large amounts of data locally.

Field programming/reprogramming

Using nonvolatile memory as a place to store program


memory allows the device to be reprogrammed in the field
without removing the microcontroller from the system that it
controls. One such application is in automotive engine
controllers. Reprogrammable non-volatile program memory on
the engine's microcontroller allows the engine controller program
to be modified during routine service to incorporate the latest
features or to compensate for such factors as engine aging and
changing emissions control laws (or even to fix bugs!!).
Reprogramming of the microcontroller could become a standard
part the routine engine tune-up.

Almost every application could benefit from this type of


program memory - If a modem's hardware supported it, you
could remotely upgrade your modem from Vfast to V.34, or
incorporate new features such as voice control or a digital
answering machine.

OTP - One Time Programmable

An OTP is a PROM (Programmable Read-Only-Memory)


device. Once your program is written into the device with a
standard EPROM programmer, it can not be erased or modified.
This is usually used for limited production runs before a ROM
mask is done in order to test code.

A OTP (One Time Programmable) part uses standard


EPROM, but the package has no window for erasing. Once your
program is written into the device with a standard EPROM
programmer, it cannot be erased or modified. (Well, sort of - any
bit that is a one can be changed to a zero - but a bit that is a
zero cannot be changed into a one).

As product design cycles get shorter, it is more important


for micro manufacturers to offer OTPs as an option. This was
commonly used for limited production runs before a ROM mask
in order to test code. However, one problem with Mask ROM is
that programming, setup, and engineering charges make it
economical only when the systems manufacturer purchases
large quantities of identically programmed micros. Then when
you discover THAT bug (and find it and fix your code), you have
quantities of *old buggy* micros around that you have to throw
away. Not to mention that lead-time (the time when you submit
your code to the micro manufacture, to the time you receive
your micro with your code on it) can be at least 8 weeks, and as
bad as 44 weeks.

c. I/O

UART

A UART (Universal Asynchronous Receiver Transmitter) is a


serial port adapter for asynchronous serial communications.

USART

A USART (Universal Synchronous/Asynchronous Receiver


Transmitter) is a serial port adapter for either asynchronous or
synchronous serial communications. Communications using a
USART are typically much faster (as much as 16 times) than with
a UART.

Synchronous serial port

A synchronous serial port doesn't require start/stop bits


and can operate at much higher clock rates than an
asynchronous serial port. Used to communicate with high speed
devices such as memory servers, display drivers, additional A/D
ports, etc. Can also be used to implement a simple
microcontroller network.
SPI

An SPI (serial peripheral interface) is a synchronous serial


port.

SCI

An SCI (serial communications interface) is an enhanced


UART (asynchronous serial port).

d. A/D (Analog to Digital) Converters

Converts an external analog signal (typically relative to


voltage) and converts it to a digital representation.
Microcontrollers that have this feature can be used for
instrumention, environmental data logging, or any application
that lives in an analog world.

Successive Approximation A/D converters

Single Slope A/D converters

Delta-Sigma A/Ds converters

Flash A/D

e. D/A (Digital to Analog) Converters

This feature takes a Digital number and converts it to a


analog output. The number 50 would be changed to the analog
output of (50/256 * 5Volts) = .9765625V on an 8-bit / 5 Volt
system.

Pulse width modulator

Pulse accumulator

Input Capture

Comparator

f. Interrupts

Polling

Polling is not really a "feature" - it's what you have to do if


your microcontroller of choice does not have interrupts. Polling is
a software technique whereby the controller continually asks a
peripheral if it needs servicing. The peripheral sets a flag when it
has data ready for transferring to the controller, which the
controller notices on its next poll. Several such peripherals can
be polled in succession, with the controller jumping to different
software routines, depending on which flags have been set.

Interrupts

Rather than have the microcontroller continually polling –


that is, asking peripherals (timers / UARTS / A/Ds / external
components) whether they have any data available (and finding
most of the time they do not), a more efficient method is to
have the peripherals tell the controller when they have data
ready. The controller can be carrying out its normal function,
only responding to peripherals when there is data to respond to.
On receipt of an interrupt, the controller suspends its current
operation, identifies the interrupting peripheral, then jumps
(vectors) to the appropriate interrupt service routine.

The advantage of interrupts, compared with polling, is the


speed of response to external events and reduced software
overhead (of continually asking peripherals if they have any data
ready).

Maskable Interrupts

A maskable interrupt is one that you can disable or enable


(masking it out means disabling the interrupt), whereas non-
maskable interrupts you can't disable.

The benefit of maskable interrupts is that you can turn off


a particular interrupts (for example a UART) during some time
critical task. Then, those particular interrupts will be ignored thus
allowing the microcontroller to deal with the task at hand. Most
microcontrollers (as well as most microprocessors) have some
type of Global Interrupt Enable (GIE) which allows you to turn off
(or on) all of the maskable interrupts with one bit. NOTE: GIE
usually does not affect any NMI (Non-Maskable Interrupts)

Vectored Interrupts

Simple (non-vectored) interrupts is one of the simplest


interrupt schemes there is (Simple = less silicon = more software
= slower). Whenever there is an interrupt, the program counter
(PC) branches to one specific address. At this address, the
system designer needs to check the interrupts (one at a time) to
see which peripheral has caused the interrupt to occur. Code for
this may look like (on a COP8):
g. Programming Languages

i. Machine/Assembly language

Machine language is the program representation, as the


microcontroller understands it. It is not easy for humans to read
and is a common cause of migraine headaches. Assembly
language is a human-readable form of machine language, which
makes it much easier for us flesh and bone types to deal with.
Each assembly language statement corresponds to one machine
language statement (not counting macros).

An assembly/machine language program is fast and small.


This is because you are in complete charge of what goes into the
program. Of course, if you write a slow, large, stupid program,
then it will run slowly, be too big, and be stupid. Assembly
language (assembler) can’t correct stupidity - although
sometimes I wish it could ;-).

If you are starting out learning about microcontrollers, it


would be worth your while first learning assembler. By
programming in assembler, you master the underlying
architecture of the chip, which is important if you intend to do
anything significant with your microcontroller.

ii. Interpreters

An interpreter is a high-level language translator that is


closer to natural language. The interpreter itself is a program
that sits resident in the microcontroller. It executes a program
by reading each language statement one at a time and then
doing what the statement says to do. The two most popular
interpreters for microcontrollers are BASIC and FORTH.

BASIC's popularity is due to its simplicity, readability, and


of course just about everyone has at least played with BASIC at
one time or another. One common complaint about [interpreted]
BASIC is that it is slow. Often using a different technique for
performing the desired task can solve this. Other times it is just
the price paid for using an interpreter.

FORTH has a very loyal following due to its speed


(approaching that of assembler language) and its incremental
approach to building a system from reusable parts. Many FORTH
systems come with a host system, which turns your desktop
computer into a development system.

FORTH can be quite difficult to write in (if you have no


experience with it) and is probably even harder to read.
However, it is a very useful and productive language for control
systems and robotics, and can be mastered in time.

JVM - Java(TM) Virtual Machine - was added lately to the


list of interpreters, after the invention of the language and
concepts by Sun Microsystems. Java was adopted
enthusiastically by programmers all over the world and has
finally found its way into the embedded environment. Java
provides a new and revolutionary concept, geared towards the
use of portable software applications, which can be dynamically
downloaded over a network, rather than kept on the local disk or
in the local memory of a specific computer. This way, the client
computer does not need to keep all the applications, since they
can be dynamically downloaded from the server whenever
required. Another Java main feature is its Operating-System
independent capability. Java is also a language. The Java
language is a new object-oriented programming language, also
developed by Sun Microsystems. In its very own architecture it is
particularly suited to the development of Java's portable
application pieces of software, called applets.

The nicest thing about developing a system with an


interpreter is that you can build your program interactively. You
first write a small piece of code and then you can try it out
immediately to see how it works. When the results are
satisfactory, you can then add additional components until the
final product is achieved.

iii. Compiler

A compiler is a high-level language translator that


combines the programming ease of an interpreter with greater
speed. This is accomplished by translating the program (on a
host machine such as a desktop PC) directly into machine
language. The machine language

program is then burned onto an EPROM or downloaded


directly to the microcontroller. The microcontroller then
executes the translated program directly, without having to
interpret first.

The most popular microcontroller compilers are C and


BASIC. PL/M, from Intel, also has some popular support due to
that company's extensive use of that language. Modula-2 has
loyal following due to its efficient code and high development
productivity. Ada has many adherents among those designing on
the larger chips (16 bits and above).

Due to both its popularity and its slow speed, it was only
logical that BASIC would appear as a compiled language. A few
companies supply a BASIC compiler for several of the more
popular microcontrollers. Execution speed is drastically
increased over interpreted BASIC since the microcontroller is
freed from the task of interpreting the statements as the
program runs.

While interpreted Forth approaches (and sometimes


surpasses) the speed of many compilers, compiled Forth screams
along. Today there are many high performance optimizing
native code Forth compilers, and there are also lots of very
cheap or free public domain Froths.

Some of them like Tom Almay’s Forth CMP produces


optimized native code with less overhead and better
performance than just about anything else out there. Of course
it still has compactness and more elegant factoring of
functionality than in most languages.

C is now the language of choice for the entire universe. C


is used on computers from the tiny microcontroller up to the
largest Cray supercomputer. Although a C program can be a bit
tedious at times to read (due to the terse programming style
followed by many C programmers), it is a powerful and flexible
development tool. Although a high level language, it also gives
the developer access to the underlying machine.

There are several very good and cheap C compilers


available for the more popular microcontrollers. It is widely
used, available, supported, and produces fairly efficient code
(fast and compact).

h. Development Tools

Having a programming language is usually not enough to


develop a program for a microcontroller. Some way of
debugging your program is needed. I am only too painfully
aware of this fact.

Simulators

A simulator runs your microcontroller program on a host


machine (such as your PC). You can step through the code to
see exactly what is happening as the program runs. Contents of
registers or variables can be altered to change the way the
program runs. Eliminates (or at least delays) the
erase/burn/program EPROM cycle common in microcontroller
program development.

You can work out ideas or learn about microcontrollers by


experimenting with small code fragments and watching on the
screen what happens. A simulator can't support real interrupts
or devices, and usually runs much slower than the real device
the program is intended for.

Some manufacturers have a cross between a software


simulator and the hardware emulator - a hardware simulator.
This is a piece of equipment that plugs into your target, and the
pins will toggle and react like they should - just MUCH slower.
Cost of a device like this is only about $100. Two such boards by
National Semiconductor and Philips are detailed in section 6.2.
Resident Debuggers

A resident debugger runs your program on the


microcontroller itself, while showing the progress on your host
machine (such as a PC). Has many of the same advantages as
simulator above, with the additional benefit of seeing how the
program runs on the real target machine. A resident debugger
needs to "steal" some resources from the target machine,
including: a communications port to communicate with the host,
an interrupt to handle single stepping, and a certain amount of
memory for the resident part (on the target) of the debugger.

Emulators

If you've got the money, this is the equipment you want to


develop your system with (yeah, that's right, a preposition at the
end of a sentence!). A [usually] expensive piece of hardware
that even for the cheaper versions will run you at least $700. An
emulator is a sophisticated device that pretends that it is the
microprocessor itself, while at the same time capturing
information.

It provides full and total control over your target, while at


the same time not requiring any resources from the target. The
emulator can either be a stand alone device with its own display,
or it can be interface to a PC.

i. Applications

In addition to the above home monitoring system,


embedded processors and microcontrollers are frequently found
in: appliances (microwave oven, refrigerators, television and
VCRs, stereos), computers and computer equipment (laser
printers, modems, disk drives), automobiles (engine control,
diagnostics, climate control), environmental control (greenhouse,
factory, home), instrumentation, aerospace, and thousands of
other uses. In many items, more than one processor can be
found.

Microcontrollers are typically used where processing power


isn't so important. Although some of you out there might find a
microwave oven controlled by a Unix system an attractive idea,
controlling a microwave oven is easily accomplished with the
smallest of microcontrollers. On the other hand, if you're putting
together a cruise missile to solve the problem of your neighbor's
dog barking at 3 in the morning, you'll probably need to use
processors with a bit more computing power.
Embedded processors and microcontrollers are used
extensively in robotics. In this application, many specific tasks
might be distributed among a large number of controllers in one
system.

Communications between each controller and a central,


possibly more powerful controller (or micro/mini/mainframe)
would enable information to be processed by the central
computer, or to be passed around to other controllers in the
system.
A special application that microcontrollers are well suited
for is data logging. Stick one of these chips out in the middle of
a cornfield or up in a ballon, and monitor and record
environmental parameters (temperature, humidity, rain, etc).
Small size, low power consumption, and flexibility make these
devices ideal for unattended data monitoring and recording
PIC MICROCONTROLLER 16F877
Fig 3.6 PIC pin diagram

Microcontroller Core Features:


• High performance RISC CPU

• Only 35 single word instructions to learn


• All single cycle instructions except for program branches
which
and two cycle
• Operating speed:
DC - 20 MHz clock input
DC - 200 ns instruction cycle
• Up to 8K x 14 words of FLASH Program Memory,
Up to 368 x 8 bytes of Data Memory (RAM)
Up to 256 x 8 bytes of EEPROM Data Memory
• Pin out compatible to the PIC16C73B/74B/76/77
• Interrupt capability (up to 14 sources)
• Eight level deep hardware stack
• Direct, indirect and relative addressing modes
• Power-on Reset (POR)
• Power-up Timer (PWRT) and Oscillator Start-up Timer
(OST)
• Watchdog Timer (WDT) with its own on-chip RC oscillator
for reliable operation
• Programmable code protection
• Power saving SLEEP mode
• Selectable oscillator options
• Low power, high speed CMOS FLASH/EEPROM
technology
• Fully static design
Single 5V In-Circuit Serial Programming capability
• In-Circuit Debugging via two pins
• Processor read/write access to program memory
• Wide operating voltage range: 2.0V to 5.5V
• High Sink/Source Current: 25 mA
• Commercial, Industrial and Extended temperature ranges
• Low-power consumption:
- < 0.6 mA typical @ 3V, 4 MHz
- 20 μA typical @ 3V, 32 kHz
- < 1 μA typical standby current

Peripheral Features:

• Timer0: 8-bit timer/counter with 8-bit prescaler


• Timer1: 16-bit timer/counter with prescaler, can be
incremented during SLEEP via external crystal/clock
• Timer2: 8-bit timer/counter with 8-bit period register,
prescaler and postscaler
• Two Capture, Compare, PWM modules
- Capture is 16-bit, max. Resolution is 12.5 ns
- Compare is 16-bit, max. Resolution is 200 ns
- PWM max. Resolution is 10-bit
• 10-bit multi-channel Analog-to-Digital converter
• Synchronous Serial Port (SSP) with SPI (Master mode) and
I2C (Master/Slave)
3.5.2 16f877 architecture
Fig 3.7 16f877 architecture
3.5.2.1 Memory organization
There are two memory blocks in each of thePIC16F87X MCUs
namely

Program memory organization

Data memory organization

Fig 3.8 memory map and stack


Program memory organization

The PIC16F87X devices have a 13-bit program counter capable of


addressing an 8K x 14 program memory space. The PIC16F877/876 devices
have 8K x 14words of FLASH program memory, and thePIC16F873/874
devices have 4K x 14.

Accessing allocation above the physically implemented address


will cause wraparound. The RESET vector is at 0000h and the interrupt
vector is at 000h.

Fig 3.9 program memory and stack


Data memory organization

The data memory is partitioned into multiple banks which contain


the General Purpose Registers and the Special Function Registers. Bits RP1
(STATUS<6>) and RP0 (STATUS<5>) are the bank select bits. Each bank
extends up to 7Fh (128 bytes). The lower locations of each bank are reserved
for the Special Function Registers. Above the Special Function Registers are
General Purpose Registers, implemented as static RAM. All implemented
banks contain Special Function Registers. Some frequently used Special
Function Registers from one bank may be mirrored in another bank for code
reduction and quicker access.

Table2: pin ports

3.5.2.2 SPECIAL FUNCTION REGISTERS

The Special Function Registers are registers used by the CPU and
peripheral modules for controlling the desired operation of the device. These
registers are implemented as static RAM. The Special Function Registers
can be classified into two sets: core (CPU) and peripheral. Those registers
associated with the core functions are described in detail in this section.
Those related to the operation of the peripheral features are described in
detail in the peripheral features section.

Status register

The STATUS register contains the arithmetic status of the ALU, the
RESET status and the bank select bits for data memory. The STATUS
register can be the destination for any instruction, as with any other register.
If the STATUS register is the destination for an instruction that affects the Z,
DC or C bits, then the write to these three bits is disabled. These bits are set
or cleared according to the device logic. Furthermore, the TO and PD bits
are not writable, therefore, the result of an instruction with the STATUS
register as destination may be different than intended.

Table 3: status registers (address 03h, 83h, 103h, and 183h)

Bit 7 IRP: Register Bank Select bit (used for indirect addressing)
1 = Bank 2, 3 (100h - 1FFh)
0 = Bank 0, 1 (00h - FFh)

Bit 6-5 RP1:RP0: Register Bank Select bits (used for direct addressing)
11 = Bank 3 (180h - 1FFh)
10 = Bank 2 (100h - 17Fh)
01 = Bank 1 (80h - FFh)
00 = Bank 0 (00h - 7Fh)
Each bank is 128 bytes
Bit 4 TO: Time-out bit
1 = after power-up, CLRWDT instruction, or SLEEP instruction
0 = A WDT time-out occurred
Bit 3 PD: Power-down bit
1 = after power-up or by the CLRWDT instruction
0 = by execution of the SLEEP instruction

Bit 2 Z: Zero bit


1 = the result of an arithmetic or logic operation is zero
0 = the result of an arithmetic or logic operation is not zero

Bit 1 DC: Digit carry/borrow bit (ADDWF, ADDLW, SUBLW, SUBWF


instructions) (for borrow, the polarity is reverse)
1 = A carry-out from the 4th low order bit of the result occurred
0 = No carry-out from the 4th low order bit of the result

Bit 0 C: Carry/borrow bit (ADDWF, ADDLW, SUBLW, SUBWF


instructions)
1 = A carry-out from the Most Significant bit of the result occurred
0 = No carry-out from the Most Significant bit of the result occurred

3.5.2.3 I/O PORTS

Some pins for these I/O ports are multiplexed with an alternate function
for the peripheral features on the device. In general, when a peripheral is
enabled, that pin may not be used as a general purpose I/O pin. Additional
information on I/O ports may be found in the PIC micro™ Mid-Range
Reference Manual, (DS33023).

PORT A and the TRIS A Register

PORTA is a 6-bit wide, bi-directional port. The corresponding data


direction register is TRISA.
Setting a TRISA bit (= 1) will make the corresponding PORTA pin an
input (i.e., put the corresponding output driver in a Hi-Impedance mode).
Clearing a TRISA bit (= 0) will make the corresponding PORTA pin
an output (i.e., put the contents of the output latch on the selected pin).
Reading the PORTA register reads the status of the pins, whereas
writing to it will write to the port latch.

Fig 3.10 PORT A

All write operations are read-modify-write operation. Therefore, a


write to a port implies that the port pins are read; the value is modified and
then written to the port data latch. Pin RA4 is multiplexed with the Timer0
module clock input to become the RA4/T0CKI pin. The RA4/T0CKIpin is a
Schmitt Trigger input and an open drain output.
All other PORTA pins have TTL input levels and full CMOS output
drivers. Other PORTA pins are multiplexed with analog inputs and analog
VREF input. The operation of each pin is selected by clearing/setting the
control bits in theADCON1 registers (A/D Control Register1).The TRISA
register controls the direction of the RApins, even when they are being used
as analog inputs. The user must ensure the bits in the TRISA register are
maintained set when using them as analog inputs.

PORT B and the TRIS B Register

PORTB is an 8-bit wide, bi-directional port. The corresponding data


direction register is TRISB. Setting a TRISB bit (= 1) will make the
corresponding PORTB pin an input (i.e., put the corresponding output driver
in a Hi-Impedance mode). Clearing a TRISB bit (= 0) will make the
corresponding PORTB pin an output (i.e., put the contents of the output
latch on the selected pin).Three pins of PORTB are multiplexed with the
Low Voltage Programming function: RB3/PGM, RB6/PGC and RB7/PGD.
The alternate functions of these pins are described in the Special Features
Section.
Each of the PORTB pins has a weak internal pull-up. Single control
bit can turn on all the pull-ups. This is performed by clearing bit RBPU
(OPTION_REG<7>).The weak pull-up is automatically turned off when the

port pin is configured as an output. The pull-ups are disabled on a Power-on


Reset.
This interrupt can wake the device from SLEEP. The user, in the
Interrupt Service Routine, can clear the interrupt in the following manner: a)
any read or write of PORTB.

Fig 3.11 PORT B

This will end the mismatch conditions) Clear flag bit RBIF.A
mismatch condition will continue to set flag bit Reading PORTB will end
the mismatch condition and allow flag bit RBIF to be cleared.

The interrupt-on-change feature is recommended for wake-up on key


depression operation and operations where PORTB is only used for the
interrupt-on-change feature.

Polling of PORTB is not recommended while using the interrupt-on-


change features this interrupt-on-mismatch feature, together with software
configurable pull-ups on these four pins, allow easy interface to a keypad
and make it possible for wake-up on key depression.

PORT C and the TRIS C Register

PORTC is an 8-bit wide, bi-directional port. The corresponding data


direction register is TRISC. Setting a TRISC bit= 1 will make the
corresponding PORTC pin an input (i.e., put the corresponding output driver
in a Hi-Impedance mode). Clearing a TRISC bit= 0 will make the
corresponding PORTC pin an output (i.e., put the contents of the output
latch on the selected pin).PORTC is multiplexed with several peripheral
functions. PORTC pins have Schmitt Trigger input buffers. When the I2C
module is enabled, the PORTC<4:3>pins can be configured with normal I2C
levels, or with SMB levels by using the CKE bit (SSPSTAT<6>).
Fig 3.12 PORT C

When enabling peripheral functions, care should be taken in defining


TRIS bits for each PORTC pin. Some peripherals override the TRIS bit to
make a pin an output, while other peripherals override the TRIS bit to make
a pin an input. Since the TRIS bit override is in effect while the peripheral is
enabled, read-modify write instructions (BSF, BCF, and XORWF) with
TRISC as destination, should be avoided. The user should refer to the
corresponding peripheral section for the correct TRIS bit settings.

PORTD and TRISD Registers


PORTD and TRISD are not implemented on thePIC16F873 or
PIC16F876.PORTD is an 8-bit port with Schmitt Trigger input buffers. Each
pin is individually configurable as an input or output. PORTD can be
configured as an 8-bit wide microprocessor port (parallel slave port) by
setting control bit PSPMODE (TRISE<4>). In this mode, the input buffers
are TTL.

Fig 3.13 PORT D

PORTE and TRISE Register

PORTE and TRISE are not implemented on thePIC16F873 or


PIC16F876.PORTE has three pins (RE0/RD/AN5, RE1/WR/AN6, and
RE2/CS/AN7) which are individually configurable as inputs or outputs.
These pins have Schmitt Trigger input buffers. The PORTE pins become the
I/O control inputs for the microprocessor port when bit PSPMODE
(TRISE<4>) Is set. In this mode, the user must make certain that the
TRISE<2:0> bits are set, and that the pins are configured as digital inputs.
Also ensure that ADCON1 is configured for digital I/O. In this mode, the
input buffers are TTL.
Fig 3.14 PORT E

3.5.3 ANALOG-TO-DIGITAL CONVERTER (A/D)


MODULE

The Analog-to-Digital (A/D) Converter module has five inputs for the
28-pin devices and eight for the other devices. The analog input charges a
sample and holds capacitor. The output of the sample and hold capacitor is
the input into the converter. The converter then generates a digital result of
this analog level via successive approximation. The A/D conversion of the
analog input signal results in a corresponding 10-bit digital number. The
A/D module has high and low voltage reference input that is software
selectable to some combination of VDD, VSS, RA2, or RA3.The A/D
converter has a unique feature of being able to operate while the device is in
SLEEP mode. To operate in SLEEP, the A/D clock must be derived from the
A/D’s internal RC oscillator

The A/D module has four registers. These registers are:

• A/D Result High Register (ADRESH)


• A/D Result Low Register (ADRESL)
• A/D Control Register0 (ADCON0)
• A/D Control Register1 (ADCON1)

The ADCON0 register, shown in Register 11-1, controls the operation


of the A/D module. The ADCON register, shown in Register 11-2,
configures the functions of the port pins.

The port pins can be configured as analog inputs (RA3 can also be the
voltage reference),or as digital I/O. Additional information on using the A/D
module can be found in the PIC micro™ Mid-Range MCU Family
Reference Manual (DS33023).These steps should be followed for doing an
A/D conversion:

1. Configure the A/D module:


• Configure analog pins/voltage reference and
Digital I/O (ADCON1)
• Select A/D input channel (ADCON0)
• Select A/D conversion clock (ADCON0)
• Turn on A/D module (ADCON0)
2. Configure A/D interrupts (if desired):
• Clear ADIF bit
• Set ADIE bit
• Set PEIE bit
• Set GIE bit

3. Wait the required acquisition time.


4. Start conversion:
• Set GO/DONE bit (ADCON0)
5. Wait for A/D conversion to complete, by either:
• Polling for the GO/DONE bit to be cleared
(With interrupts enabled); OR
• Waiting for the A/D interrupt
6. Read A/D result register pair
(ADRESH: ADRESL), clear bit ADIF if required.
7. For the next conversion, go to step 1 or step 2,
As required. The A/D conversion time per bit is
Defined as TAD. A minimum wait of 2TAD is
Required before the next acquisition starts
BLOCK DIAGRAM-A/D CONVERTER

Fig 3.15 A/D Converter


3.5 .4 SPECIAL FEATURES OF THE CPU

All PIC16F87X devices have a host of features intended to maximize


system reliability, minimize cost through elimination of external
components, provide power saving operating modes and offer code
protection. These are:

• Oscillator Selection
• RESET
- Power-on Reset (POR)
- Power-up Timer (PWRT)
- Oscillator Start-up Timer (OST)
- Brown-out Reset (BOR)
• Interrupts
• Watchdog Timer (WDT)
• SLEEP
• Code Protection
• ID Locations
• In-Circuit Serial Programming
• Low Voltage In-Circuit Serial Programming
• In-Circuit Debugger
PIC16F87X devices have a Watchdog Timer, which can be shut-
off only through configuration bits. It runs off its own RC oscillator for
added reliability. There are two timers that offer necessary delays on power-
up. One is the Oscillator Start-up Timer (OST), intended to keep the chip in
RESET until the crystal oscillator is stable. The other is the Power-up Timer
(PWRT), which provides a fixed delay of 72 ms (nominal) on power-up
only. It is designed to keep the part in RESET while the power supply
stabilizes.

41 With these two timers on-chip, most applications need no external


RESET circuitry SLEEP mode is designed to offer a very low current
Power-down mode. The user can wake-up from SLEEP through external
RESET, Watchdog Timer Wake-up, or through an interrupt. Several
oscillator options are also made available to allow the part to fit the
application.

The RC oscillator option saves system cost while the LP crystal


option saves power. A set of configuration bits is used to select various
options.

Additional information on special features is available in the PIC


micro™ Mid-Range Reference Manual,(DS33023).

3.5.5 INSTRUCTION SET SUMMARY

The instruction set is highly orthogonal and is grouped into three basic
categories:

• Byte-oriented operations
• Bit-oriented operations
• Literal and control operations
For byte-oriented instructions, ’f’ represents a file register designator
and’d’ represents a destination designator. The file register designator
specifies which file register is to be used by the instruction.The destination
designator specifies where the result of the operation is to be placed. If’d’ is
zero, the result is placed in the W register. If’d’ is one, the result is placed in
the file register specified in the instruction.

For bit-oriented instructions, ’b’ represents a bit field designator


which selects the number of the bit affected by the operation, while ’f’
represents the address of the file in which the bit is located.

For literal and control operations, ’k’ represents an eight or eleven bit
constant or literal value. All instructions are executed within one single
instruction cycle, unless a conditional test is true or the program counter is
changed as a result of an instruction. In this case, the execution takes two
instruction cycles with the second cycle executed as a NOP. One instruction
cycle consists of four oscillator periods. Thus, for an oscillator frequency of
4 MHz, the normal instruction execution time is 1 μs. If a conditional test is
true, or the program counter is changed as a result of an instruction, the
instruction execution time is 2 μs.

Vous aimerez peut-être aussi