Pic 16f84a Manual
Pic 16f84a Manual
Pic 16f84a Manual
The microcontroller because of its versatility, ease of use and cost will
change the way electronic circuits are designed and will now enable projects to
be designed which previously were too complex. Additional components such as
versatile interface adapters (VIA), RAM, ROM, EPROM and address decoders
are no longer required.
One of the most difficult hurdles to overcome when using any new
technology is the first one – getting started! It was the aim of this guide is to
explain as simply as possible how to program and use the PIC microcontrollers.
PIC MICROCONTROLLERS
When you buy a PIC microcontroller, you get a useless lump of silicon
with amazing potential. It will do nothing without – but almost anything with – the
program that you write. Under your guidance, almost any number or combination
of normal logic chips can be squeezed into one PIC program and thus in turn,
into one PIC microcontroller. Figure shows the steps in developing a PIC
program.
Figure 1. The blank PIC microcontroller does nothing; 2. Write a program on a
computer; 3. Pretend to program the PIC microcontroller on a computer; 4. Test
the program on a computer; 5. Program a real PIC microcontroller; 6. Test the
PIC microcontroller in a real circuit.
RAM
RAM means Random Access Memory. It is a general-purpose memory
which usually stores the user data used in a program. RAM is volatile, i.e. data is
lost after the removal of power. Most microcontrollers have some amount of
internal RAM. 256 bytes is a common amount, although some microcontrollers
have more, some less. In general it is possible to extend the memory by adding
external memory chips.
ROM
ROM is Read Only Memory. This type of memory usually holds program
or fixed user data. ROM memories are programmed at factory during the
manufacturing process and their contents cannot be changed by the user. ROM
memories are only useful if you have developed a program and wishto order
several thousand copies of it.
EEPROM
EEPROM is Electrically Erasable Programmable Read Only Memory,
which is a non-volatile memory. These memories can be erased and also be
programmed under program control. EEPROMs are used to save configuration
information, maximum and minimum values, identification data, etc.
Some microcontrollers have built-in EEPROM memories (e.g. PIC16F84
contains a 64-byte EEPROM memory where each byte can be programmed and
erased directly by software). EEPROM memories are usually very slow.
EPROM
EPROM is erasable Programmable Read Only Memory. This is similar to
ROM, but the EPROM can be programmed using a suitable programming device.
EPROM memories have a small clear glass window on top of the chip where the
data can be erased under UV light. Many development versions of
microcontrollers are manufactured with EPROM memories where the user
program can be stored. These memories are erased and re-programmed until
the user is satisfied with the program. Some versions of EPROMs, known as
OTP (One Time Programmable), can be programmed using a suitable
programmer device but these memories cannot be erased. OTP memories cost
much less than the EPROMs. OTP is useful after a project has been developed
completely and it is required to make many copies of the program memory.
Flash EEPROM
This is another version of EEPROM-type memory. This memory has
become popular in microcontroller applications and is used to store the user
program. Flash EEPROM is non-volatile and is usually very fast. The data is
erased and then re-programmed using a programming device. The entire
contents of the memory should be erased and then re-programmed.
MICROCONTROLLER FEATURES
Supply voltage
The clock
Timers
Interrupts
Power-on reset
MICROCONTROLLER ARCHITECTURES
MICROCONTROLLER OF CHOICE
PIC16F84: This has been one of the most popular PIC microcontrollers for a very
long time. This is an 18-pin device and it offers 1024 _ 14 flash program memory,
36 bytes of data RAM, 64 bytes of non-volatile EEPROM data memory, 13 I/O
pins, a timer, a watchdog, and internal and external interrupt sources. The timer
is 8-bits wide but can be programmed to generate internal interrupts for timing
purposes. PIC16F84 can be operated from a crystal or a resonator for accurate
timing. A resistor-capacitor can also be used as a timing device for applications
where accurate timing is not required.
Inside a PIC16f84a microcontroller
The data memory is used to store all of your program variables. This is a
RAM which means that all the data is lost when power is removed. The width of
the data memory is 8-bits wide and this is why the PIC microcontrollers are called
8-bit microcontrollers.
The data memory in a PIC microcontroller consists of banks where some
models may have only 2 banks, some models 4 banks, and so on. A required
bank of the data memory can be selected under program control.
● OPTION register
● I/O registers
● Timer registers
● INTCON register
● STATUS register
OPTION register
These registers are used for the I/O control. Every I/O port in the PIC
microcontroller has two registers:
Port data register has the same name as the port it controls. For example,
PIC16F84 microcontroller has two port data registers PORTA and PORTB. A
PIC16F877 microcontroller has 5 port data registers PORTA, PORTB, PORTC,
PORTD, and PORTE. An 8-bit data can be sent to any port, or an 8-bit data can
be read from the ports. It is also possible to read or write to individual port pins.
For example, any bit of a given port can be set or cleared, or data can be read
from one or more port pins at the same time.
Timer registers
Depending on the model used, some PIC microcontrollers have only one
timer, and some may have up to 3 timers. In this section we shall look at the
PIC16F84 microcontroller which has only one timer. The extension to several
timers is similar and we shall see in the projects section how to use more than
one timer.
Prescaler rates vary from 1:2 to 1:256. For example, when using a 4 MHz clock,
the basic instruction cycle is 1_s (the clock is internally divided by four). If we
select a prescaler rate of 1:16, the counter will be incremented at every 16_s.
INTCON register
1XX1XXXX
The STATUS register contains the arithmetic status of the ALU, the
RESET status and the bank select bit for data memory. As with any register, the
STATUS register can be the destination for any instruction. 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 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. For example,
CLRF STATUS will clear the upper-three bits and set the Z bit. This leaves the
STATUS register as 000u u1uu (where u = unchanged).
Oscillator circuits
PIC microcontrollers have built-in oscillator circuits and this oscillator can be
operated in one of five modes.
● LP – Low-power crystal
● XT – Crystal/resonator
● HS – High-speed crystal/resonator
● RC resistor – capacitor
● No external components (only on some PIC microcontrollers).
In LP, XT, or HS modes, an external oscillator can be connected to the OSC1
input as shown in Figure. This can be a crystal-based oscillator, or simple logic
gates can be used to design an oscillator circuit.
Crystal operation
Resonator operation
Resonators are available from 4 to about 8 MHz. They are not as accurate
as crystal-based oscillators.
Resonators are usually 3-pin devices and the two pins at either sides are
connected to OSC1 and OSC2 inputs of the microcontroller. The middle pin is
connected to the ground. Figure shows how a resonator can be used in a PIC
microcontroller circuit.
RC oscillator
Reset circuit
Reset is used to put the microcontroller into a known state. Normally when
a PIC microcontroller is reset execution starts from address 0 of the program
memory. This is where the first executable user program resides. The reset
action also initialises various SFR registers inside the microcontroller.
PIC microcontrollers can be reset when one of the following conditions occur:
● Reset during power on (POR – Power On Reset)
● Reset by lowering MCLR input to logic 0
● Reset when the watchdog overflows.
As shown in Figure, a PIC microcontroller is normally reset when power is
applied to the chip and when the MCLR input is tied to the supply voltage through
a 4.7 K resistor.
I/O interface
LED interface
LEDs come in many different sizes, shapes, and colours. The brightness
of an LED depends on the current through the device. Some small LEDs operate
with only a few milliamperes of current, while standard size LEDs consume about
10 mA of current for normal brightness. Some very bright LEDs consume 15–20
mA of current. The voltage drop across an LED is about 2V, but the voltage at
the output of a microcontroller port is about 5 V when the port is at logic 1 level.
As a result of this it is not possible to connect an LED directly to a microcontroller
output port.
Button input
● A desktop or a laptop PC
● PIC microcontroller programmer device
● A solderless breadboard or a similar circuit development board
● PIC microcontroller chip(s) and support components
● Power supply
● A text editor
● MPASMWIN.EXE compiler
● PIC programmer device software
Numerical Systems
Introduction
It was always difficult for people to accept the fact that some things differ
from them or their way of thinking. That is probably one of the reasons why
numerical systems which differ from a decimal are still hard to understand. Still,
whether we want it or not, reality is different. Decimal numerical system that
people use in everyday life is so far behind the binary system used by millions of
computers around the world.
Each numerical system are based on some basis. With a decimal numerical
system, that basis is 10, with binary 2, and with a hexadecimal system 16. The
value of each decimal is determined by its position in relation to the whole
number represented in the given numerical system. The sum of values of each
decimal gives the value of the whole number. Binary and hexadecimal numerical
systems are especially interesting for the subject of this book. Beside these, we
will also discuss a decimal system, in order to compare it with the other two.
Even though a decimal numerical system is a subject we are well acquainted
with, we will discuss it here because of its relatedness to other numerical
systems.
Example:
Binary numerical system differs in many aspects from the decimal system
we are used to in our everyday lives. Its numerical basis is 2, and each number
can have only two values, '1' or '0'. Binary numerical system is used in computers
and microcontrollers because it is far more suitable for processing than a decimal
system. Usually, binary number consists of binary digits 8, 16 or 32, and it is not
important in view of the contents of our book to discuss why. It will be enough for
now to adopt this information.
Example:
What remains is for us to get acquainted with logic that is used with binary
numerical system, or how to get a numerical value from a series of zeros and
ones in a way we can understand, of course. This procedure is called conversion
from a binary to a decimal number.
Example:
Example:
Example:
Subtraction, like addition is done on the same principle. The result of subtraction
between two zeros, or two ones remains a zero. When subtracting one from
zero, we have to borrow one from binary digit which has a higher value in the
binary number.
Example:
Example:
Example:
Example:
Example:
By checking this result, we get values 11590 for the first number and 5970
for the second, where their difference is 5620, which corresponds to a number
$15F4 after a transfer into a decimal numerical system.
Digital Logic, Arithmetic, and Conversions
CPU Flags
All microcontrollers are equipped with a special register that reflects the
current processing status. This register, sometimes called the status register or
the flags register, contains individual bits, usually called flags, that are
meaningful during the execution of logic and arithmetic operations.
1. The zero flag. This flag is set if a previous operation produces a value of zero.
2. The carry/overflow flag. This flag is set if there has been a carry or a borrow-
out of the high-order bit of the operand.
3. The half-carry or digit-carry flag. This flag is set if there has been a carry or a
borrow-out of the low-order nibble of the operand.
Not all instructions affect all the flags. For example, loading a zero
constant into a register may be said to produce a zero value; however, such an
instruction may or may not affect the zero flag, according to the implementation
on each particular device. More powerful and sophisticated microcontrollers
sometimes implement other flags, such as flags to indicate a negative operand,
an arithmetic overflow, or an interrupt.
Word Size
In the context of digital arithmetic and logic the data word-size determines
the processing capabilities of each device. For example, a machine with an 8-bit
word-size can perform unsigned addition of operands whose sum does not
exceed the decimal value 255, since 255 is the largest unsigned integer that can
be stored in eight bits. However, a machine with 16-bit words can perform
unsigned additions up to a sum of 65,535 since it is the largest number that can
be stored in 16 bits.
Logical Instructions
The logical instructions include the Boolean operators, AND, OR, NOT,
and XOR, as well as instructions to shift and rotate individual bits.
The logical instructions operate on a bit-by-bit basis; therefore, in the AND, OR,
NOT, and XOR there is no interaction between bits. The action performed by the
logical instructions is as follows:
1. AND, OR, and XOR logically combine each bit in the source operand with the
corresponding bit in the destination operand. The result does not affect the
neighbouring bits.
2. The NOT operator inverts all bits in the destination operand.
These actions explain the term bitwise operation sometimes used to describe the
instructions.
Logical AND
The AND instruction performs a bitwise logical AND of two operands. This
determines that a bit in the result is set if and only if the corresponding bits are
set in both operands. A frequent use of the AND operation is to clear one or more
bits without affecting the remaining ones. This action is possible because ANDing
with a 0 bit always clears the result bit and ANDing with a 1 bit preserves the
original value of the first operand.
For example, if we have the binary coded decimal number 34 packed into a
single byte, we can isolate the four low-order bits as follows:
The second operand, in this case 0FH, is called a mask. The AND operation
preserves the 1-bits in the mask and clears the bits that are 0. Consequently, the
mask 00000001B clears the seven high-order bits and preserves the original
value of the low-order bit.
Logical OR
The OR operation sets the bits that are 1 in the mask and preserves the bits that
are masked 0.
Logical XOR
The XOR operator performs the bitwise logical exclusive OR of the two
operands. Therefore, a bit in the result is set if the corresponding bits in the
operands have opposite values. For this reason, XORing a value with itself
always generates a zero result since all bits necessarily have the same value. On
the other hand, XORing with a 1-bit inverts the value of the other operand, since
0 XOR 1 is 1 and 1 XOR 1 is 0.
For example:
In the previous example, the XOR operation inverts the bits that are 1 in the
mask and preserves the bits that are masked 0. Consequently, the XOR mask
11110000B inverts the four high-order bits.
Logical NOT
In contrast with the other logical operators which require two operands,
the NOT instruction acts on a single value. Its action is consistent with a Boolean
NOT function, which converts all 1-bits to 0 and all 0-bits to 1. Arithmetically, the
result is the one’s complement of the original value. This instruction can be useful
in obtaining the two’s complement representation by performing the logical NOT
and then adding one to the results.
Microcontroller Arithmetic
The question that arises is: in a device that performs two’s complement
addition, must we always assume that the operands are in two’s complement
form? The answer is: no. Signed addition of two’s complement operands and the
unsigned addition of integer operands can be performed with identical processing
and by the same electronic circuitry. It is the software that must take into account
the encoding of the operands in order to interpret the results. For example, in the
4-binary digit device previously considered, the two’s complement addition of the
values 6 and 7 produce an overflow, which can be detected by observing the
change in the high-order bit (the sign bit) of the result. Therefore, in this case, the
result of the addition operation is invalid. However, if the same decimal values
represent unsigned operands, then the addition of 7 plus 6 produce the valid
result 13. In either case the binary values of the operands, as well as the result,
are the same.
In the previous examples the results are valid because the sum of each digit
does not exceed the range of the BCD format. However, the following additions
do not produce valid BCD results:
In the case of the first operation the valid BCD result would be: 33 + 27 = 60, in
the second one 31 + 59 = 90, and in the third one 56 + 27 = 83. A simple
adjustment corrects the error, as follows:
In all three cases adding 6 to the previous sum produces the expected
result. The logic for deciding when the value 6 must be added is simple: if the
sum of the low-order digit is greater than 9 or if the sum produced a carry out of
the low-order nibble, then add 6 to the sum to perform the decimal adjustment.
Some high-end microcontrollers contain a primitive instruction that executes the
decimal adjustment automatically, that is, without having to test the sum.
However, this instruction is not available in low- and mid-range devices.
Also note that the largest number that can be encoded in packed BCD
format is the decimal 99. When adding two BCD digits the high-order digit of the
sum cannot be greater than 9. If so, then the capacity of the format has been
exceeded and the result cannot be adjusted by the simple addition of 6. Here
again, a multi-byte processing routine can be developed in order to
accommodate the result of BCD addition when the sum exceeds a single byte.
The rotate operation differs from the shift in that in the rotate the low-order
bit is either a copy of the high-order bit or of the carry flag. In the first case the
operation is a pure rotate, in the second case the rotate is referred to as rotate-
through-carry. Figure shows the action of a left-rotate-through-carry flag.
Note that in the figure contents of the carry flag are first copied to the low-
order bit of the destination operand, then the individual bits of the source (in gray
in the illustration) are shifted left and moved to the destination. Finally the high-
order bit of the source is copied to the carry flag.
There are several possible variations of the rotate operation. The Intel
microprocessors distinguish between arithmetic and logic rotates. In the
arithmetic rotation the high-order bit is preserved in order to maintain the sign of
the operand. The rotate shown in the figure is the one most common in
microcontroller hardware.
Clearing the carry flag before the rotate takes place makes the operation
identical to a shift.
Comparison Operations
1. If the result of a subtraction is zero, then both operands were of the same size.
2. If the result of a subtraction is a positive number, then the subtrahend was
smaller than the minuend.
3. If the result of a subtraction is a negative number, the subtrahend was larger
than the minuend.
The one objection to the use of subtraction in comparing the size of two
operands is that the process will change one of them. To use subtraction in
comparison operations the programmer has to find some way of preserving the
minuend. Alternatively, some devices contain a comparison operator that sets
the flags as if a subtraction had taken place but without changing the operands.
High-end microcontrollers are equipped with dedicated comparison operators,
but the middle- and low-range devices usually are not.
Other Support Operations
Introduction
Data transfer
First three instructions (look at the following table) provide for a constant being
written in W register (MOVLW is short for MOVe Literal to W), and for data to be
copied from W register onto RAM and data from RAM to be copied onto W
register (or on the same RAM location, at which point only the status of Z flag
changes). Instruction CLRF writes constant 0 in 'f ' register, and CLRW writes
constant 0 in register W. SWAPF instruction exchanges places of the 4-bit
nibbles field inside a register.
Bit operations
Instructions GOTO, CALL and RETURN are executed the same way as
on all other microcontrollers, only stack is independent of internal RAM and
limited to eight levels. 'RETLW k' instruction is identical with RETURN instruction,
except that before coming back from a subprogram a constant defined by
instruction operand is written in W register. This instruction enables us to design
easily the Look-up tables (lists). Mostly we use them by determining data position
on our table adding it to the address at which the table begins, and then we read
data from that location (which is usually found in program memory). Table can be
formed as a subprogram which consists of a series of 'RETLW k' instructions,
where 'k' constants are members of the table.
We write the position of a member of our table in W register, and using CALL
instruction we call a subprogram which creates the table. First subprogram line
ADDWF PCL, f adds the position of a W register member to the starting address
of our table, found in PCL register, and so we get the real data address in
program memory. When returning from a subprogram we will have in W register
the contents of an addressed table member. In a previous example, constant 'k2'
will be in W register following a return from a subprogram.
Conditional jumps are synthesized into two instructions: BTFSC and BTFSS.
Depending on a bit status in 'f' register that is being tested, instructions skip or
don't skip over the next program instruction.
*1 If I/O port is source operand, status on microcontroller pins is read
*2 If this instruction is executed on TMR register and if d=1, prescaler assigned to
that timer will automatically be cleared
*3 If PC was modified, or test result =1, instruction was executed in two cycles.
Instruction Execution Period
All instructions are executed in one cycle except for conditional branch
instructions if condition was true, or if the contents of program counter was
changed by some instruction. In that case, execution requires two instruction
cycles, and the second cycle is executed as NOP (No Operation). Four oscillator
clocks make up one instruction cycle. If we are using an oscillator with 4MHz
frequency, the normal time for executing an instruction is 1 µs, and in case of
conditional branching, execution period is 2 µs.
Word list
The lists contains all instructions presented separately with examples for their
use. Syntax, description and its effects on status bits are given for each
instruction.
A.1 MOVLW
A.2 MOVWF
A.3 MOVF
A.4 CLRW
A.5 CLRF
A.6 SWAPF
A.7 ADDLW
A.8 ADDWF
A.9 SUBLW
A.10 SUBWF
A.11 ANDLW
A.12 ANDWF
A.13 IORLW
A.14 IORWF
A.15 XORLW
A.16 XORWF
A.17 INCF
A.18 DECF
A.19 RLF
A.20 RRF
A.21 COMF
A.22 BCF
A.23 BSF
A.24 BTFSC
A.25 BTFSS
A.26 INCFSZ
A.27 DECFSZ
A.28 GOTO
A.29 CALL
A.30 RETURN
A.31 RETLW
A.32 RETFIE
A.33 NOP
A.34 CLRWDT
A.35 SLEEP
A.1 MOVLW Write constant in W register